- hstat.st_size = 0; /* Force 0 size on symlink */
- header = start_header(p, &hstat);
- if (header == NULL)
- {
- critical_error = 1;
- goto badfile;
- }
- size = readlink(p, header->header.linkname, NAMSIZ);
- if (size < 0) goto badperror;
- if (size == NAMSIZ) {
- char *buf = ck_malloc(PATH_MAX);
-
- readlink(p,buf,PATH_MAX);
- /* next_mangle(header->header.linkname); */
- add_symlink_mangle(buf,p,header->header.linkname);
- msg("symbolic link %s too long: mangling to %s",p, header->header.linkname);
- /* size=strlen(header->header.linkname); */
- free(buf);
- } else
- header->header.linkname[size] = '\0';
- header->header.linkflag = LF_SYMLINK;
- finish_header(header); /* Nothing more to do to it */
- if (f_remove_files)
- {
- if (unlink (p) == -1)
- msg_perror ("cannot remove %s", p);
- }
- return;
+ case USTAR_FORMAT:
+ case STAR_FORMAT:
+ return write_ustar_long_name (st->file_name);
+
+ case OLDGNU_FORMAT:
+ case GNU_FORMAT:
+ write_gnu_long_link (st, st->file_name, GNUTYPE_LONGNAME);
+ break;
+
+ default:
+ abort(); /*FIXME*/
+ }
+ return write_short_name (st);
+}
+
+union block *
+write_extended (bool global, struct tar_stat_info *st, union block *old_header)
+{
+ union block *header, hp;
+ char *p;
+ int type;
+ time_t t;
+
+ if (st->xhdr.buffer || st->xhdr.stk == NULL)
+ return old_header;
+
+ xheader_finish (&st->xhdr);
+ memcpy (hp.buffer, old_header, sizeof (hp));
+ if (global)
+ {
+ type = XGLTYPE;
+ p = xheader_ghdr_name ();
+ time (&t);
+ }
+ else
+ {
+ type = XHDTYPE;
+ p = xheader_xhdr_name (st);
+ t = st->stat.st_mtime;
+ }
+ xheader_write (type, p, t, &st->xhdr);
+ free (p);
+ header = find_next_block ();
+ memcpy (header, &hp.buffer, sizeof (hp.buffer));
+ return header;
+}
+
+static union block *
+write_header_name (struct tar_stat_info *st)
+{
+ if (archive_format == POSIX_FORMAT && !string_ascii_p (st->file_name))
+ {
+ xheader_store ("path", st, NULL);
+ return write_short_name (st);
+ }
+ else if (NAME_FIELD_SIZE - (archive_format == OLDGNU_FORMAT)
+ < strlen (st->file_name))
+ return write_long_name (st);
+ else
+ return write_short_name (st);
+}
+
+\f
+/* Header handling. */
+
+/* Make a header block for the file whose stat info is st,
+ and return its address. */
+
+union block *
+start_header (struct tar_stat_info *st)
+{
+ union block *header;
+
+ header = write_header_name (st);
+ if (!header)
+ return NULL;
+
+ /* Override some stat fields, if requested to do so. */
+
+ if (owner_option != (uid_t) -1)
+ st->stat.st_uid = owner_option;
+ if (group_option != (gid_t) -1)
+ st->stat.st_gid = group_option;
+ if (mode_option)
+ st->stat.st_mode =
+ ((st->stat.st_mode & ~MODE_ALL)
+ | mode_adjust (st->stat.st_mode, S_ISDIR (st->stat.st_mode) != 0,
+ initial_umask, mode_option, NULL));
+
+ /* Paul Eggert tried the trivial test ($WRITER cf a b; $READER tvf a)
+ for a few tars and came up with the following interoperability
+ matrix:
+
+ WRITER
+ 1 2 3 4 5 6 7 8 9 READER
+ . . . . . . . . . 1 = SunOS 4.2 tar
+ # . . # # . . # # 2 = NEC SVR4.0.2 tar
+ . . . # # . . # . 3 = Solaris 2.1 tar
+ . . . . . . . . . 4 = GNU tar 1.11.1
+ . . . . . . . . . 5 = HP-UX 8.07 tar
+ . . . . . . . . . 6 = Ultrix 4.1
+ . . . . . . . . . 7 = AIX 3.2
+ . . . . . . . . . 8 = Hitachi HI-UX 1.03
+ . . . . . . . . . 9 = Omron UNIOS-B 4.3BSD 1.60Beta
+
+ . = works
+ # = ``impossible file type''
+
+ The following mask for old archive removes the `#'s in column 4
+ above, thus making GNU tar both a universal donor and a universal
+ acceptor for Paul's test. */
+
+ if (archive_format == V7_FORMAT || archive_format == USTAR_FORMAT)
+ MODE_TO_CHARS (st->stat.st_mode & MODE_ALL, header->header.mode);
+ else
+ MODE_TO_CHARS (st->stat.st_mode, header->header.mode);
+
+ {
+ uid_t uid = st->stat.st_uid;
+ if (archive_format == POSIX_FORMAT
+ && MAX_OCTAL_VAL (header->header.uid) < uid)
+ {
+ xheader_store ("uid", st, NULL);
+ uid = 0;
+ }
+ if (!UID_TO_CHARS (uid, header->header.uid))
+ return NULL;
+ }
+
+ {
+ gid_t gid = st->stat.st_gid;
+ if (archive_format == POSIX_FORMAT
+ && MAX_OCTAL_VAL (header->header.gid) < gid)
+ {
+ xheader_store ("gid", st, NULL);
+ gid = 0;
+ }
+ if (!GID_TO_CHARS (gid, header->header.gid))
+ return NULL;
+ }
+
+ {
+ off_t size = st->stat.st_size;
+ if (archive_format == POSIX_FORMAT
+ && MAX_OCTAL_VAL (header->header.size) < size)
+ {
+ xheader_store ("size", st, NULL);
+ size = 0;
+ }
+ if (!OFF_TO_CHARS (size, header->header.size))
+ return NULL;
+ }
+
+ {
+ struct timespec mtime = set_mtime_option ? mtime_option : st->mtime;
+ if (archive_format == POSIX_FORMAT)
+ {
+ if (MAX_OCTAL_VAL (header->header.mtime) < mtime.tv_sec
+ || mtime.tv_nsec != 0)
+ xheader_store ("mtime", st, &mtime);
+ if (MAX_OCTAL_VAL (header->header.mtime) < mtime.tv_sec)
+ mtime.tv_sec = 0;
+ }
+ if (!TIME_TO_CHARS (mtime.tv_sec, header->header.mtime))
+ return NULL;
+ }
+
+ /* FIXME */
+ if (S_ISCHR (st->stat.st_mode)
+ || S_ISBLK (st->stat.st_mode))
+ {
+ major_t devmajor = major (st->stat.st_rdev);
+ minor_t devminor = minor (st->stat.st_rdev);
+
+ if (archive_format == POSIX_FORMAT
+ && MAX_OCTAL_VAL (header->header.devmajor) < devmajor)
+ {
+ xheader_store ("devmajor", st, NULL);
+ devmajor = 0;