From 8513495ab5cfb63eb7c4c933fdf0b78c6196cd27 Mon Sep 17 00:00:00 2001
From: Sergey Poznyakoff <gray@gnu.org>
Date: Fri, 28 Apr 2023 15:23:46 +0300
Subject: [PATCH 4/4] Fix appending to archives bigger than 2G

* src/extern.h (last_header_start): Change type to off_t.
* src/global.c: Likewise.
* src/util.c (prepare_append): Use off_t for file offsets.

Upstream-Status: Backport [0987d63384f0419b4b14aecdc6a61729b75ce86a]
Signed-off-by: Marek Vasut <marex@denx.de>
---
 src/extern.h | 11 ++++-----
 src/global.c |  2 +-
 src/util.c   | 66 ++++++++++++++++++++++++++--------------------------
 3 files changed, 39 insertions(+), 40 deletions(-)

diff --git a/src/extern.h b/src/extern.h
index 11ac6bf..12f14a9 100644
--- a/src/extern.h
+++ b/src/extern.h
@@ -67,7 +67,7 @@ extern int ignore_devno_option;
 
 extern bool to_stdout_option;
 
-extern int last_header_start;
+extern off_t last_header_start;
 extern int copy_matching_files;
 extern int numeric_uid;
 extern char *pattern_file_name;
@@ -123,7 +123,7 @@ void field_width_error (const char *filename, const char *fieldname,
 
 /* copypass.c */
 void process_copy_pass (void);
-int link_to_maj_min_ino (char *file_name, int st_dev_maj, 
+int link_to_maj_min_ino (char *file_name, int st_dev_maj,
 			 int st_dev_min, ino_t st_ino);
 int link_to_name (char const *link_name, char const *link_target);
 
@@ -171,7 +171,7 @@ void copy_files_tape_to_disk (int in_des, int out_des, off_t num_bytes);
 void copy_files_disk_to_tape (int in_des, int out_des, off_t num_bytes, char *filename);
 void copy_files_disk_to_disk (int in_des, int out_des, off_t num_bytes, char *filename);
 void warn_if_file_changed (char *file_name, off_t old_file_size,
-                           time_t old_file_mtime);
+			   time_t old_file_mtime);
 void create_all_directories (char const *name);
 void prepare_append (int out_file_des);
 char *find_inode_file (ino_t node_num,
@@ -185,7 +185,7 @@ void set_new_media_message (char *message);
 #ifdef HPUX_CDF
 char *add_cdf_double_slashes (char *filename);
 #endif
-void write_nuls_to_file (off_t num_bytes, int out_des, 
+void write_nuls_to_file (off_t num_bytes, int out_des,
 			 void (*writer) (char *in_buf,
 					 int out_des, off_t num_bytes));
 #define DISK_IO_BLOCK_SIZE	512
@@ -229,6 +229,5 @@ void delay_set_stat (char const *file_name, struct stat *st,
 		     mode_t invert_permissions);
 int repair_delayed_set_stat (struct cpio_file_stat *file_hdr);
 void apply_delayed_set_stat (void);
-     
-int arf_stores_inode_p (enum archive_format arf);
 
+int arf_stores_inode_p (enum archive_format arf);
diff --git a/src/global.c b/src/global.c
index fb3abe9..5c9fc05 100644
--- a/src/global.c
+++ b/src/global.c
@@ -114,7 +114,7 @@ int debug_flag = false;
 
 /* File position of last header read.  Only used during -A to determine
    where the old TRAILER!!! record started.  */
-int last_header_start = 0;
+off_t last_header_start = 0;
 
 /* With -i; if true, copy only files that match any of the given patterns;
    if false, copy only files that do not match any of the patterns. (-f) */
diff --git a/src/util.c b/src/util.c
index 4421b20..3be89a4 100644
--- a/src/util.c
+++ b/src/util.c
@@ -60,8 +60,8 @@ tape_empty_output_buffer (int out_des)
   static long output_bytes_before_lseek = 0;
 
   /* Some tape drivers seem to have a signed internal seek pointer and
-     they lose if it overflows and becomes negative (e.g. when writing 
-     tapes > 2Gb).  Doing an lseek (des, 0, SEEK_SET) seems to reset the 
+     they lose if it overflows and becomes negative (e.g. when writing
+     tapes > 2Gb).  Doing an lseek (des, 0, SEEK_SET) seems to reset the
      seek pointer and prevent it from overflowing.  */
   if (output_is_special
      && ( (output_bytes_before_lseek += output_size) >= 1073741824L) )
@@ -106,7 +106,7 @@ static ssize_t sparse_write (int fildes, char *buf, size_t nbyte, bool flush);
    descriptor OUT_DES and reset `output_size' and `out_buff'.
    If `swapping_halfwords' or `swapping_bytes' is set,
    do the appropriate swapping first.  Our callers have
-   to make sure to only set these flags if `output_size' 
+   to make sure to only set these flags if `output_size'
    is appropriate (a multiple of 4 for `swapping_halfwords',
    2 for `swapping_bytes').  The fact that DISK_IO_BLOCK_SIZE
    must always be a multiple of 4 helps us (and our callers)
@@ -188,8 +188,8 @@ tape_fill_input_buffer (int in_des, int num_bytes)
 {
 #ifdef BROKEN_LONG_TAPE_DRIVER
   /* Some tape drivers seem to have a signed internal seek pointer and
-     they lose if it overflows and becomes negative (e.g. when writing 
-     tapes > 4Gb).  Doing an lseek (des, 0, SEEK_SET) seems to reset the 
+     they lose if it overflows and becomes negative (e.g. when writing
+     tapes > 4Gb).  Doing an lseek (des, 0, SEEK_SET) seems to reset the
      seek pointer and prevent it from overflowing.  */
   if (input_is_special
       && ( (input_bytes_before_lseek += num_bytes) >= 1073741824L) )
@@ -332,8 +332,8 @@ tape_buffered_peek (char *peek_buf, int in_des, int num_bytes)
 
 #ifdef BROKEN_LONG_TAPE_DRIVER
   /* Some tape drivers seem to have a signed internal seek pointer and
-     they lose if it overflows and becomes negative (e.g. when writing 
-     tapes > 4Gb).  Doing an lseek (des, 0, SEEK_SET) seems to reset the 
+     they lose if it overflows and becomes negative (e.g. when writing
+     tapes > 4Gb).  Doing an lseek (des, 0, SEEK_SET) seems to reset the
      seek pointer and prevent it from overflowing.  */
   if (input_is_special
       && ( (input_bytes_before_lseek += num_bytes) >= 1073741824L) )
@@ -404,7 +404,7 @@ tape_toss_input (int in_des, off_t num_bytes)
 
       if (crc_i_flag && only_verify_crc_flag)
 	{
- 	  int k;
+	  int k;
 	  for (k = 0; k < space_left; ++k)
 	    crc += in_buff[k] & 0xff;
 	}
@@ -416,14 +416,14 @@ tape_toss_input (int in_des, off_t num_bytes)
 }
 
 void
-write_nuls_to_file (off_t num_bytes, int out_des, 
-                    void (*writer) (char *in_buf, int out_des, off_t num_bytes))
+write_nuls_to_file (off_t num_bytes, int out_des,
+		    void (*writer) (char *in_buf, int out_des, off_t num_bytes))
 {
   off_t	blocks;
   off_t	extra_bytes;
   off_t	i;
   static char zeros_512[512];
-  
+
   blocks = num_bytes / sizeof zeros_512;
   extra_bytes = num_bytes % sizeof zeros_512;
   for (i = 0; i < blocks; ++i)
@@ -603,7 +603,7 @@ create_all_directories (char const *name)
   char *dir;
 
   dir = dir_name (name);
-  
+
   if (dir == NULL)
     error (PAXEXIT_FAILURE, 0, _("virtual memory exhausted"));
 
@@ -637,9 +637,9 @@ create_all_directories (char const *name)
 void
 prepare_append (int out_file_des)
 {
-  int start_of_header;
-  int start_of_block;
-  int useful_bytes_in_block;
+  off_t start_of_header;
+  off_t start_of_block;
+  size_t useful_bytes_in_block;
   char *tmp_buf;
 
   start_of_header = last_header_start;
@@ -697,8 +697,8 @@ inode_val_compare (const void *val1, const void *val2)
   const struct inode_val *ival1 = val1;
   const struct inode_val *ival2 = val2;
   return ival1->inode == ival2->inode
-         && ival1->major_num == ival2->major_num
-         && ival1->minor_num == ival2->minor_num;
+	 && ival1->major_num == ival2->major_num
+	 && ival1->minor_num == ival2->minor_num;
 }
 
 static struct inode_val *
@@ -706,10 +706,10 @@ find_inode_val (ino_t node_num, unsigned long major_num,
 		 unsigned long minor_num)
 {
   struct inode_val sample;
-  
+
   if (!hash_table)
     return NULL;
-  
+
   sample.inode = node_num;
   sample.major_num = major_num;
   sample.minor_num = minor_num;
@@ -734,7 +734,7 @@ add_inode (ino_t node_num, char *file_name, unsigned long major_num,
 {
   struct inode_val *temp;
   struct inode_val *e = NULL;
-  
+
   /* Create new inode record.  */
   temp = (struct inode_val *) xmalloc (sizeof (struct inode_val));
   temp->inode = node_num;
@@ -1007,7 +1007,7 @@ buf_all_zeros (char *buf, int bufsize)
 
 /* Write NBYTE bytes from BUF to file descriptor FILDES, trying to
    create holes instead of writing blockfuls of zeros.
-   
+
    Return the number of bytes written (including bytes in zero
    regions) on success, -1 on error.
 
@@ -1027,7 +1027,7 @@ sparse_write (int fildes, char *buf, size_t nbytes, bool flush)
 
   enum { begin, in_zeros, not_in_zeros } state =
 			   delayed_seek_count ? in_zeros : begin;
-  
+
   while (nbytes)
     {
       size_t rest = nbytes;
@@ -1042,7 +1042,7 @@ sparse_write (int fildes, char *buf, size_t nbytes, bool flush)
 	      if (state == not_in_zeros)
 		{
 		  ssize_t bytes = buf - start_ptr + rest;
-		  
+
 		  n = write (fildes, start_ptr, bytes);
 		  if (n == -1)
 		    return -1;
@@ -1091,8 +1091,8 @@ sparse_write (int fildes, char *buf, size_t nbytes, bool flush)
       if (n != 1)
 	return n;
       delayed_seek_count = 0;
-    }      
-  
+    }
+
   return nwritten + seek_count;
 }
 
@@ -1222,7 +1222,7 @@ set_perms (int fd, struct cpio_file_stat *header)
   if (!no_chown_flag)
     {
       uid_t uid = CPIO_UID (header->c_uid);
-      gid_t gid = CPIO_GID (header->c_gid); 
+      gid_t gid = CPIO_GID (header->c_gid);
       if ((fchown_or_chown (fd, header->c_name, uid, gid) < 0)
 	  && errno != EPERM)
 	chown_error_details (header->c_name, uid, gid);
@@ -1239,13 +1239,13 @@ set_file_times (int fd,
 		const char *name, unsigned long atime, unsigned long mtime)
 {
   struct timespec ts[2];
-  
+
   memset (&ts, 0, sizeof ts);
 
   ts[0].tv_sec = atime;
   ts[1].tv_sec = mtime;
 
-  /* Silently ignore EROFS because reading the file won't have upset its 
+  /* Silently ignore EROFS because reading the file won't have upset its
      timestamp if it's on a read-only filesystem. */
   if (fdutimens (fd, name, ts) < 0 && errno != EROFS)
     utime_error (name);
@@ -1297,7 +1297,7 @@ cpio_safer_name_suffix (char *name, bool link_target, bool absolute_names,
 
 /* This is a simplified form of delayed set_stat used by GNU tar.
    With the time, both forms will merge and pass to paxutils
-   
+
    List of directories whose statuses we need to extract after we've
    finished extracting their subsidiary files.  If you consider each
    contiguous subsequence of elements of the form [D]?[^D]*, where [D]
@@ -1415,7 +1415,7 @@ cpio_mkdir (struct cpio_file_stat *file_hdr, int *setstat_delayed)
 {
   int rc;
   mode_t mode = file_hdr->c_mode;
-  
+
   if (!(file_hdr->c_mode & S_IWUSR))
     {
       rc = mkdir (file_hdr->c_name, mode | S_IWUSR);
@@ -1438,10 +1438,10 @@ cpio_create_dir (struct cpio_file_stat *file_hdr, int existing_dir)
 {
   int res;			/* Result of various function calls.  */
   int setstat_delayed = 0;
-  
+
   if (to_stdout_option)
     return 0;
-  
+
   /* Strip any trailing `/'s off the filename; tar puts
      them on.  We might as well do it here in case anybody
      else does too, since they cause strange things to happen.  */
@@ -1530,7 +1530,7 @@ arf_stores_inode_p (enum archive_format arf)
     }
   return 1;
 }
-  
+
 void
 cpio_file_stat_init (struct cpio_file_stat *file_hdr)
 {
-- 
2.39.2

