7893ae
From 90f446f9b04ab820a99b9408e68c01dc6b57056c Mon Sep 17 00:00:00 2001
7893ae
From: rpm-build <rpm-build>
7893ae
Date: Mon, 28 Jul 2014 08:10:10 +0200
7893ae
Subject: [PATCH 2/9] vfat-like FS & sparse (still downstream)
7893ae
7893ae
Fix extracting sparse files to a file system like vfat, when
7893ae
ftruncate may fail to grow the size of a file.  Still downstram,
7893ae
(do we need this now? ftruncate & vfat works is now OK).
7893ae
7893ae
Resolves: #179507
7893ae
7893ae
Upstream bugreport:
7893ae
http://lists.gnu.org/archive/html/bug-tar/2006-02/msg00000.html
7893ae
7893ae
---
7893ae
 src/system.c | 19 ++++++++++++++++++-
7893ae
 1 file changed, 18 insertions(+), 1 deletion(-)
7893ae
7893ae
diff --git a/src/system.c b/src/system.c
7893ae
index 9414233..37e9a3e 100644
7893ae
--- a/src/system.c
7893ae
+++ b/src/system.c
7893ae
@@ -243,8 +243,25 @@ sys_compare_links (struct stat *link_data, struct stat *stat_data)
7893ae
 int
7893ae
 sys_truncate (int fd)
7893ae
 {
7893ae
+  struct stat st;
7893ae
   off_t pos = lseek (fd, (off_t) 0, SEEK_CUR);
7893ae
-  return pos < 0 ? -1 : ftruncate (fd, pos);
7893ae
+
7893ae
+  if ( pos < 0) 
7893ae
+    return -1;
7893ae
+
7893ae
+  if ( ftruncate(fd, pos) && errno == EPERM ) {
7893ae
+    /* wrapper around ftruncate:
7893ae
+     * ftruncate may fail to grow the size of a file with some OS and filesystem
7893ae
+     * combinations. Linux and vfat/fat is one example. If this is the case do
7893ae
+     * a write to grow the file to the desired length.
7893ae
+     */
7893ae
+    if( (fstat( fd, &st ) == -1) || 
7893ae
+        (st.st_size >= pos)  ||
7893ae
+        (lseek( fd, pos - 1, SEEK_SET) == (off_t)-1) ||
7893ae
+        (write( fd, "\0", 1) == -1) )
7893ae
+	return -1;
7893ae
+  }
7893ae
+  return 0;
7893ae
 }
7893ae
 
7893ae
 /* Return nonzero if NAME is the name of a regular file, or if the file
7893ae
-- 
7893ae
1.9.3
7893ae