+static bool
+sparse_scan_file_wholesparse (struct tar_sparse_file *file)
+{
+ struct tar_stat_info *st = file->stat_info;
+ struct sp_array sp = {0, 0};
+
+ /* Note that this function is called only for truly sparse files of size >= 1
+ block size (checked via ST_IS_SPARSE before). See the thread
+ http://www.mail-archive.com/bug-tar@gnu.org/msg04209.html for more info */
+ if (ST_NBLOCKS (st->stat) == 0)
+ {
+ st->archive_file_size = 0;
+ sp.offset = st->stat.st_size;
+ sparse_add_map (st, &sp);
+ return true;
+ }
+
+ return false;
+}
+
+#ifdef SEEK_HOLE
+/* Try to engage SEEK_HOLE/SEEK_DATA feature. */
+static bool
+sparse_scan_file_seek (struct tar_sparse_file *file)
+{
+ struct tar_stat_info *st = file->stat_info;
+ int fd = file->fd;
+ struct sp_array sp = {0, 0};
+ off_t offset = 0;
+ off_t data_offset;
+ off_t hole_offset;
+
+ st->archive_file_size = 0;
+
+ for (;;)
+ {
+ /* locate first chunk of data */
+ data_offset = lseek (fd, offset, SEEK_DATA);
+
+ if (data_offset == (off_t)-1)
+ /* ENXIO == EOF; error otherwise */
+ {
+ if (errno == ENXIO)
+ {
+ /* file ends with hole, add one more empty chunk of data */
+ sp.numbytes = 0;
+ sp.offset = st->stat.st_size;
+ sparse_add_map (st, &sp);
+ return true;
+ }
+ return false;
+ }
+
+ hole_offset = lseek (fd, data_offset, SEEK_HOLE);
+
+ /* according to specs, if FS does not fully support
+ SEEK_DATA/SEEK_HOLE it may just implement kind of "wrapper" around
+ classic lseek() call. We must detect it here and try to use other
+ hole-detection methods. */
+ if (offset == 0 /* first loop */
+ && data_offset == 0
+ && hole_offset == st->stat.st_size)
+ {
+ lseek (fd, 0, SEEK_SET);
+ return false;
+ }
+
+ sp.offset = data_offset;
+ sp.numbytes = hole_offset - data_offset;
+ sparse_add_map (st, &sp);
+
+ st->archive_file_size += sp.numbytes;
+ offset = hole_offset;
+ }
+
+ return true;
+}
+#endif
+
+static bool
+sparse_scan_file (struct tar_sparse_file *file)
+{
+ /* always check for completely sparse files */
+ if (sparse_scan_file_wholesparse (file))
+ return true;
+
+ switch (hole_detection)
+ {
+ case HOLE_DETECTION_DEFAULT:
+ case HOLE_DETECTION_SEEK:
+#ifdef SEEK_HOLE
+ if (sparse_scan_file_seek (file))
+ return true;
+#else
+ if (hole_detection == HOLE_DETECTION_SEEK)
+ WARN((0, 0,
+ _("\"seek\" hole detection is not supported, using \"raw\".")));
+ /* fall back to "raw" for this and all other files */
+ hole_detection = HOLE_DETECTION_RAW;
+#endif
+ case HOLE_DETECTION_RAW:
+ if (sparse_scan_file_raw (file))
+ return true;
+ }
+
+ return false;
+}
+