Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Zzvms-0008Gs-Ng for pgsql-bugs@arkaria.postgresql.org; Sat, 21 Nov 2015 00:12:18 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1Zzvmr-0001zc-Iu for pgsql-bugs@arkaria.postgresql.org; Sat, 21 Nov 2015 00:12:17 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1ZzvmE-0001Iu-2d for pgsql-bugs@postgresql.org; Sat, 21 Nov 2015 00:11:38 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1ZzvmB-0000p0-6S for pgsql-bugs@postgresql.org; Sat, 21 Nov 2015 00:11:36 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id tAL0BNrH006802; Fri, 20 Nov 2015 19:11:23 -0500 From: Tom Lane To: Alvaro Herrera cc: Guillaume Lelarge , Michael Paquier , PostgreSQL Bugs List , Josh Berkus Subject: Re: Confusing error message with too-large file in pg_basebackup In-reply-to: <27295.1448050812@sss.pgh.pa.us> References: <564E6B2F.0@agliodbs.com> <11737.1447994494@sss.pgh.pa.us> <20151120123359.GO614468@alvherre.pgsql> <27295.1448050812@sss.pgh.pa.us> Comments: In-reply-to Tom Lane message dated "Fri, 20 Nov 2015 15:20:12 -0500" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <6721.1448064513.0@sss.pgh.pa.us> Date: Fri, 20 Nov 2015 19:11:23 -0500 Message-ID: <6801.1448064683@sss.pgh.pa.us> X-Pg-Spam-Score: -2.5 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <6721.1448064513.1@sss.pgh.pa.us> I wrote: > Actually ... why don't we get rid of the limit? wikipedia's entry on > tar format says > ... only 11 octal digits can be stored. This gives a maximum file size > of 8 gigabytes on archived files. To overcome this limitation, star in > 2001 introduced a base-256 coding that is indicated by setting the > high-order bit of the leftmost byte of a numeric field. GNU-tar and > BSD-tar followed this idea. > If that extension is as widespread as this suggests, then following it > when we have a file > 8GB seems like a better answer than failing > entirely. If you try to read the dump with an old tar program, old > pg_restore, etc, it might fail ... but are you really worse off than > if you couldn't make the dump at all? I looked into the GNU tar sources and confirmed that gtar supports this concept. (It looks from the GNU sources like they've supported it for a *really long time*, like since the 90's, in which case wikipedia's credit to "star" for the idea is full of it.) Hence, I propose something like the attached (WIP, has no doc corrections). I've done simple testing on the pg_dump/pg_restore code path, but not on basebackup --- anyone want to test that? I'm not sure whether we should treat this as a back-patchable bug fix or a new feature for HEAD only. If we don't back-patch it, there are in any case several bugs here that we must fix. In particular, the existing coding in ReceiveTarFile: size_t filesz = 0; ... sscanf(&tarhdr[124], "%11o", (unsigned int *) &filesz); is utterly, absolutely, completely broken; it'll fail grossly on any 64-bit big-endian hardware. There are other places with misplaced faith that "unsigned long" is at least as wide as size_t. Comments? regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="remove-tar-size-limit.patch"; charset="us-ascii" Content-ID: <6721.1448064513.2@sss.pgh.pa.us> Content-Description: remove-tar-size-limit.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/replication/basebackup.c b/src/backend/replication= /basebackup.c index 1af011e..6120c8f 100644 *** a/src/backend/replication/basebackup.c --- b/src/backend/replication/basebackup.c *************** sendDir(char *path, int basepathlen, boo *** 1132,1144 **** =20=20 =20=20 /* - * Maximum file size for a tar member: The limit inherent in the - * format is 2^33-1 bytes (nearly 8 GB). But we don't want to exceed - * what we can represent in pgoff_t. - */ - #define MAX_TAR_MEMBER_FILELEN (((int64) 1 << Min(33, sizeof(pgoff_t)*8 -= 1)) - 1) -=20 - /* * Given the member, write the TAR header & send the file. * * If 'missing_ok' is true, will not throw an error if the file is not fo= und. --- 1132,1137 ---- *************** sendFile(char *readfilename, char *tarfi *** 1166,1180 **** errmsg("could not open file \"%s\": %m", readfilename))); } =20=20 - /* - * Some compilers will throw a warning knowing this test can never be tr= ue - * because pgoff_t can't exceed the compared maximum on their platform. - */ - if (statbuf->st_size > MAX_TAR_MEMBER_FILELEN) - ereport(ERROR, - (errmsg("archive member \"%s\" too large for tar format", - tarfilename))); -=20 _tarWriteHeader(tarfilename, NULL, statbuf); =20=20 while ((cnt =3D fread(buf, 1, Min(sizeof(buf), statbuf->st_size - len), = fp)) > 0) --- 1159,1164 ---- diff --git a/src/bin/pg_basebackup/pg_basebackup.c b/src/bin/pg_basebackup/= pg_basebackup.c index 80de882..8c4dffe 100644 *** a/src/bin/pg_basebackup/pg_basebackup.c --- b/src/bin/pg_basebackup/pg_basebackup.c *************** ReceiveTarFile(PGconn *conn, PGresult *r *** 781,787 **** bool in_tarhdr =3D true; bool skip_file =3D false; size_t tarhdrsz =3D 0; ! size_t filesz =3D 0; =20=20 #ifdef HAVE_LIBZ gzFile ztarfile =3D NULL; --- 781,787 ---- bool in_tarhdr =3D true; bool skip_file =3D false; size_t tarhdrsz =3D 0; ! pgoff_t filesz =3D 0; =20=20 #ifdef HAVE_LIBZ gzFile ztarfile =3D NULL; *************** ReceiveTarFile(PGconn *conn, PGresult *r *** 1046,1052 **** =20=20 skip_file =3D (strcmp(&tarhdr[0], "recovery.conf") =3D=3D 0); =20=20 ! sscanf(&tarhdr[124], "%11o", (unsigned int *) &filesz); =20=20 padding =3D ((filesz + 511) & ~511) - filesz; filesz +=3D padding; --- 1046,1052 ---- =20=20 skip_file =3D (strcmp(&tarhdr[0], "recovery.conf") =3D=3D 0); =20=20 ! filesz =3D read_tar_number(&tarhdr[124], 12); =20=20 padding =3D ((filesz + 511) & ~511) - filesz; filesz +=3D padding; *************** ReceiveAndUnpackTarFile(PGconn *conn, PG *** 1139,1145 **** char current_path[MAXPGPATH]; char filename[MAXPGPATH]; const char *mapped_tblspc_path; ! int current_len_left; int current_padding =3D 0; bool basetablespace; char *copybuf =3D NULL; --- 1139,1145 ---- char current_path[MAXPGPATH]; char filename[MAXPGPATH]; const char *mapped_tblspc_path; ! pgoff_t current_len_left =3D 0; int current_padding =3D 0; bool basetablespace; char *copybuf =3D NULL; *************** ReceiveAndUnpackTarFile(PGconn *conn, PG *** 1208,1227 **** } totaldone +=3D 512; =20=20 ! if (sscanf(copybuf + 124, "%11o", ¤t_len_left) !=3D 1) ! { ! fprintf(stderr, _("%s: could not parse file size\n"), ! progname); ! disconnect_and_exit(1); ! } =20=20 /* Set permissions on the file */ ! if (sscanf(©buf[100], "%07o ", &filemode) !=3D 1) ! { ! fprintf(stderr, _("%s: could not parse file mode\n"), ! progname); ! disconnect_and_exit(1); ! } =20=20 /* * All files are padded up to 512 bytes --- 1208,1217 ---- } totaldone +=3D 512; =20=20 ! current_len_left =3D read_tar_number(©buf[124], 12); =20=20 /* Set permissions on the file */ ! filemode =3D read_tar_number(©buf[100], 8); =20=20 /* * All files are padded up to 512 bytes diff --git a/src/bin/pg_dump/pg_backup_tar.c b/src/bin/pg_dump/pg_backup_ta= r.c index 532eacc..5fff355 100644 *** a/src/bin/pg_dump/pg_backup_tar.c --- b/src/bin/pg_dump/pg_backup_tar.c *************** typedef struct *** 78,90 **** ArchiveHandle *AH; } TAR_MEMBER; =20=20 - /* - * Maximum file size for a tar member: The limit inherent in the - * format is 2^33-1 bytes (nearly 8 GB). But we don't want to exceed - * what we can represent in pgoff_t. - */ - #define MAX_TAR_MEMBER_FILELEN (((int64) 1 << Min(33, sizeof(pgoff_t)*8 -= 1)) - 1) -=20 typedef struct { int hasSeek; --- 78,83 ---- *************** isValidTarHeader(char *header) *** 1049,1055 **** int sum; int chk =3D tarChecksum(header); =20=20 ! sscanf(&header[148], "%8o", &sum); =20=20 if (sum !=3D chk) return false; --- 1042,1048 ---- int sum; int chk =3D tarChecksum(header); =20=20 ! sum =3D read_tar_number(&header[148], 8); =20=20 if (sum !=3D chk) return false; *************** _tarAddFile(ArchiveHandle *AH, TAR_MEMBE *** 1091,1103 **** strerror(errno)); fseeko(tmp, 0, SEEK_SET); =20=20 - /* - * Some compilers will throw a warning knowing this test can never be tr= ue - * because pgoff_t can't exceed the compared maximum on their platform. - */ - if (th->fileLen > MAX_TAR_MEMBER_FILELEN) - exit_horribly(modulename, "archive member too large for tar format\n"); -=20 _tarWriteHeader(th); =20=20 while ((cnt =3D fread(buf, 1, sizeof(buf), tmp)) > 0) --- 1084,1089 ---- *************** _tarGetHeader(ArchiveHandle *AH, TAR_MEM *** 1225,1232 **** char tag[100]; int sum, chk; ! size_t len; ! unsigned long ullen; pgoff_t hPos; bool gotBlock =3D false; =20=20 --- 1211,1217 ---- char tag[100]; int sum, chk; ! pgoff_t len; pgoff_t hPos; bool gotBlock =3D false; =20=20 *************** _tarGetHeader(ArchiveHandle *AH, TAR_MEM *** 1249,1255 **** =20=20 /* Calc checksum */ chk =3D tarChecksum(h); ! sscanf(&h[148], "%8o", &sum); =20=20 /* * If the checksum failed, see if it is a null block. If so, silently --- 1234,1240 ---- =20=20 /* Calc checksum */ chk =3D tarChecksum(h); ! sum =3D read_tar_number(&h[148], 8); =20=20 /* * If the checksum failed, see if it is a null block. If so, silently *************** _tarGetHeader(ArchiveHandle *AH, TAR_MEM *** 1273,1280 **** } =20=20 sscanf(&h[0], "%99s", tag); ! sscanf(&h[124], "%12lo", &ullen); ! len =3D (size_t) ullen; =20=20 { char buf[100]; --- 1258,1264 ---- } =20=20 sscanf(&h[0], "%99s", tag); ! len =3D read_tar_number(&h[124], 12); =20=20 { char buf[100]; *************** _tarWriteHeader(TAR_MEMBER *th) *** 1307,1313 **** { char h[512]; =20=20 ! tarCreateHeader(h, th->targetFile, NULL, th->fileLen, 0600, 04000, 02000= , time(NULL)); =20=20 /* Now write the completed header. */ if (fwrite(h, 1, 512, th->tarFH) !=3D 512) --- 1291,1298 ---- { char h[512]; =20=20 ! tarCreateHeader(h, th->targetFile, NULL, th->fileLen, ! 0600, 04000, 02000, time(NULL)); =20=20 /* Now write the completed header. */ if (fwrite(h, 1, 512, th->tarFH) !=3D 512) diff --git a/src/include/pgtar.h b/src/include/pgtar.h index 906db7c..9c94a58 100644 *** a/src/include/pgtar.h --- b/src/include/pgtar.h *************** enum tarError *** 19,23 **** TAR_SYMLINK_TOO_LONG }; =20=20 ! extern enum tarError tarCreateHeader(char *h, const char *filename, const= char *linktarget, size_t size, mode_t mode, uid_t uid, gid_t gid, time_t m= time); extern int tarChecksum(char *header); --- 19,25 ---- TAR_SYMLINK_TOO_LONG }; =20=20 ! extern enum tarError tarCreateHeader(char *h, const char *filename, const= char *linktarget, ! pgoff_t size, mode_t mode, uid_t uid, gid_t gid, time_t mtime); ! extern uint64 read_tar_number(const char *s, int len); extern int tarChecksum(char *header); diff --git a/src/port/tar.c b/src/port/tar.c index 72fd4e1..631ff4d 100644 *** a/src/port/tar.c --- b/src/port/tar.c *************** *** 3,23 **** #include =20=20 /* ! * Utility routine to print possibly larger than 32 bit integers in a ! * portable fashion. Filled with zeros. */ static void ! print_val(char *s, uint64 val, unsigned int base, size_t len) { ! int i; !=20 ! for (i =3D len; i > 0; i--) { ! int digit =3D val % base; =20=20 ! s[i - 1] =3D '0' + digit; ! val =3D val / base; } } =20=20 =20=20 --- 3,82 ---- #include =20=20 /* ! * Print a numeric field in a tar header. The field starts at *s and is = of ! * length len; val is the value to be written. ! * ! * Per POSIX, the way to write a number is in octal with leading zeroes a= nd ! * one trailing space (or NUL, but we use space) at the end of the specif= ied ! * field width. ! * ! * However, the given value may not fit in the available space in octal f= orm. ! * If that's true, we use the GNU extension of writing \200 followed by t= he ! * number in base-256 form (ie, stored in binary MSB-first). (Note: here= we ! * support only non-negative numbers, so we don't worry about the GNU rul= es ! * for handling negative numbers.) */ static void ! print_tar_number(char *s, int len, uint64 val) { ! if (val < (((uint64) 1) << ((len - 1) * 3))) { ! /* Use octal with trailing space */ ! s[--len] =3D ' '; ! while (len) ! { ! s[--len] =3D (val & 7) + '0'; ! val >>=3D 3; ! } ! } ! else ! { ! /* Use base-256 with leading \200 */ ! s[0] =3D '\200'; ! while (len > 1) ! { ! s[--len] =3D (val & 255); ! val >>=3D 8; ! } ! } ! } =20=20 !=20 ! /* ! * Read a numeric field in a tar header. The field starts at *s and is of ! * length len. ! * ! * The POSIX-approved format for a number is octal, ending with a space or ! * NUL. However, for values that don't fit, we recognize the GNU extensi= on ! * of \200 followed by the number in base-256 form (ie, stored in binary ! * MSB-first). (Note: here we support only non-negative numbers, so we d= on't ! * worry about the GNU rules for handling negative numbers.) ! */ ! uint64 ! read_tar_number(const char *s, int len) ! { ! uint64 result =3D 0; !=20 ! if (*s =3D=3D '\200') ! { ! /* base-256 */ ! while (--len) ! { ! result <<=3D 8; ! result |=3D (unsigned char) (*++s); ! } } + else + { + /* octal */ + while (len-- && *s >=3D '0' && *s <=3D '7') + { + result <<=3D 3; + result |=3D (*s - '0'); + s++; + } + } + return result; } =20=20 =20=20 *************** tarChecksum(char *header) *** 46,57 **** =20=20 /* * Fill in the buffer pointed to by h with a tar format header. This buff= er ! * must always have space for 512 characters, which is a requirement by * the tar format. */ enum tarError tarCreateHeader(char *h, const char *filename, const char *linktarget, ! size_t size, mode_t mode, uid_t uid, gid_t gid, time_t mtime) { if (strlen(filename) > 99) return TAR_NAME_TOO_LONG; --- 105,116 ---- =20=20 /* * Fill in the buffer pointed to by h with a tar format header. This buff= er ! * must always have space for 512 characters, which is a requirement of * the tar format. */ enum tarError tarCreateHeader(char *h, const char *filename, const char *linktarget, ! pgoff_t size, mode_t mode, uid_t uid, gid_t gid, time_t mtime) { if (strlen(filename) > 99) return TAR_NAME_TOO_LONG; *************** tarCreateHeader(char *h, const char *fil *** 84,107 **** } =20=20 /* Mode 8 - this doesn't include the file type bits (S_IFMT) */ ! sprintf(&h[100], "%07o ", (int) (mode & 07777)); =20=20 /* User ID 8 */ ! sprintf(&h[108], "%07o ", (int) uid); =20=20 /* Group 8 */ ! sprintf(&h[116], "%07o ", (int) gid); =20=20 ! /* File size 12 - 11 digits, 1 space; use print_val for 64 bit support */ if (linktarget !=3D NULL || S_ISDIR(mode)) /* Symbolic link or directory has size zero */ ! print_val(&h[124], 0, 8, 11); else ! print_val(&h[124], size, 8, 11); ! sprintf(&h[135], " "); =20=20 /* Mod Time 12 */ ! sprintf(&h[136], "%011o ", (int) mtime); =20=20 /* Checksum 8 cannot be calculated until we've filled all other fields */ =20=20 --- 143,165 ---- } =20=20 /* Mode 8 - this doesn't include the file type bits (S_IFMT) */ ! print_tar_number(&h[100], 8, (mode & 07777)); =20=20 /* User ID 8 */ ! print_tar_number(&h[108], 8, uid); =20=20 /* Group 8 */ ! print_tar_number(&h[116], 8, gid); =20=20 ! /* File size 12 */ if (linktarget !=3D NULL || S_ISDIR(mode)) /* Symbolic link or directory has size zero */ ! print_tar_number(&h[124], 12, 0); else ! print_tar_number(&h[124], 12, size); =20=20 /* Mod Time 12 */ ! print_tar_number(&h[136], 12, mtime); =20=20 /* Checksum 8 cannot be calculated until we've filled all other fields */ =20=20 *************** tarCreateHeader(char *h, const char *fil *** 134,152 **** strlcpy(&h[297], "postgres", 32); =20=20 /* Major Dev 8 */ ! sprintf(&h[329], "%07o ", 0); =20=20 /* Minor Dev 8 */ ! sprintf(&h[337], "%07o ", 0); =20=20 /* Prefix 155 - not used, leave as nulls */ =20=20 ! /* ! * We mustn't overwrite the next field while inserting the checksum. ! * Fortunately, the checksum can't exceed 6 octal digits, so we just wri= te ! * 6 digits, a space, and a null, which is legal per POSIX. ! */ ! sprintf(&h[148], "%06o ", tarChecksum(h)); =20=20 return TAR_OK; } --- 192,206 ---- strlcpy(&h[297], "postgres", 32); =20=20 /* Major Dev 8 */ ! print_tar_number(&h[329], 8, 0); =20=20 /* Minor Dev 8 */ ! print_tar_number(&h[337], 8, 0); =20=20 /* Prefix 155 - not used, leave as nulls */ =20=20 ! /* Finally, compute and insert the checksum */ ! print_tar_number(&h[148], 8, tarChecksum(h)); =20=20 return TAR_OK; } ------- =_aaaaaaaaaa0 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs ------- =_aaaaaaaaaa0--