- upperbound = SPARSES_IN_OLDGNU_HEADER - 1;
-
- sizeleft = current_stat.st_size;
-
- /* Don't bother opening empty, world readable files. Also do not open
- files when archive is meant for /dev/null. */
-
- if (dev_null_output
- || (sizeleft == 0
- && MODE_R == (MODE_R & current_stat.st_mode)))
- f = -1;
- else
- {
- f = open (p, O_RDONLY | O_BINARY);
- if (f < 0)
- {
- WARN ((0, errno, _("Cannot add file %s"), p));
- if (!ignore_failed_read_option)
- exit_status = TAREXIT_FAILURE;
- return;
- }
- }
-
- /* If the file is sparse, we've already taken care of this. */
-
- if (!header_moved)
- header = start_header (p, ¤t_stat);
-
- /* Mark contiguous files, if we support them. */
-
- if (archive_format != V7_FORMAT && S_ISCTG (current_stat.st_mode))
- header->header.typeflag = CONTTYPE;
-
- isextended = header->oldgnu_header.isextended;
- save_typeflag = header->header.typeflag;
- finish_header (header);
- if (isextended)
- {
- int counter;
- /* static */ int index_offset = SPARSES_IN_OLDGNU_HEADER;
-
- extend:
- exhdr = find_next_block ();
- memset (exhdr->buffer, 0, BLOCKSIZE);
- for (counter = 0; counter < SPARSES_IN_SPARSE_HEADER; counter++)
- {
- if (counter + index_offset > upperbound)
- break;
-
- SIZE_TO_CHARS (sparsearray[counter + index_offset].numbytes,
- exhdr->sparse_header.sp[counter].numbytes);
- OFF_TO_CHARS (sparsearray[counter + index_offset].offset,
- exhdr->sparse_header.sp[counter].offset);
- }
- set_next_block_after (exhdr);
- if (index_offset + counter <= upperbound)
- {
- index_offset += counter;
- exhdr->sparse_header.isextended = 1;
- goto extend;
- }
-
- }
- if (save_typeflag == GNUTYPE_SPARSE)
- {
- if (f < 0
- || finish_sparse_file (f, &sizeleft, current_stat.st_size, p))
- goto padit;
- }
- else
- while (sizeleft > 0)
- {
- if (multi_volume_option)
- {
- assign_string (&save_name, p);
- save_sizeleft = sizeleft;
- save_totsize = current_stat.st_size;
- }
- start = find_next_block ();
-
- bufsize = available_space_after (start);
-
- if (sizeleft < bufsize)
- {
- /* Last read -- zero out area beyond. */
-
- bufsize = sizeleft;
- count = bufsize % BLOCKSIZE;
- if (count)
- memset (start->buffer + sizeleft, 0, BLOCKSIZE - count);
- }
- if (f < 0)
- count = bufsize;
- else
- count = safe_read (f, start->buffer, bufsize);
- if (count < 0)
- {
- char buf[UINTMAX_STRSIZE_BOUND];
- ERROR ((0, errno,
- _("Read error at byte %s, reading %lu bytes, in file %s"),
- STRINGIFY_BIGINT (current_stat.st_size - sizeleft,
- buf),
- (unsigned long) bufsize, p));
- goto padit;
- }
- sizeleft -= count;
-
- /* This is nonportable (the type of set_next_block_after's arg). */
-
- set_next_block_after (start + (count - 1) / BLOCKSIZE);
-
- if (count == bufsize)
- continue;
- else
- {
- char buf[UINTMAX_STRSIZE_BOUND];
- ERROR ((0, 0,
- _("File %s shrunk by %s bytes, padding with zeros"),
- p, STRINGIFY_BIGINT (sizeleft, buf)));
- goto padit; /* short read */
- }
- }
-
- if (multi_volume_option)
- assign_string (&save_name, 0);
-
- if (f >= 0)
- {
- struct stat final_stat;
- if (fstat (f, &final_stat) != 0)
- ERROR ((0, errno, "%s: fstat", p));
- else if (final_stat.st_mtime != restore_times.modtime
- || final_stat.st_size != restore_size)
- ERROR ((0, errno, _("%s: file changed as we read it"), p));
- if (close (f) != 0)
- ERROR ((0, errno, _("%s: close"), p));
- if (atime_preserve_option)
- utime (p, &restore_times);
- }
- if (remove_files_option)
- {
- if (unlink (p) == -1)
- ERROR ((0, errno, _("Cannot remove %s"), p));
- }
- return;
-
- /* File shrunk or gave error, pad out tape to match the size we
- specified in the header. */
-
- padit:
- while (sizeleft > 0)