X-Git-Url: https://git.dogcows.com/gitweb?a=blobdiff_plain;f=src%2Fcreate.c;h=b4b487f7225f0c3e5ceb23c096e9f4fc6b9fefb1;hb=4e59b9d7e33feb6712a1d482650f8786abbadcdf;hp=2a546659ebcf340291be14caae546bc85f587e03;hpb=345eb9f767b86613f85bd67329ba84b78e7b2464;p=chaz%2Ftar diff --git a/src/create.c b/src/create.c index 2a54665..b4b487f 100644 --- a/src/create.c +++ b/src/create.c @@ -1,5 +1,8 @@ /* Create a tar archive. - Copyright 1985, 92, 93, 94, 96, 97, 99, 2000 Free Software Foundation, Inc. + + Copyright (C) 1985, 1992, 1993, 1994, 1996, 1997, 1999, 2000, 2001, + 2003 Free Software Foundation, Inc. + Written by John Gilmore, on 1985-08-25. This program is free software; you can redistribute it and/or modify it @@ -47,6 +50,7 @@ struct link { dev_t dev; ino_t ino; + size_t nlink; char name[1]; }; @@ -113,7 +117,7 @@ to_base256 (int negative, uintmax_t value, char *where, size_t size) static void to_chars (int negative, uintmax_t value, size_t valsize, - uintmax_t (*substitute) PARAMS ((int *)), + uintmax_t (*substitute) (int *), char *where, size_t size, const char *type) { int base256_allowed = (archive_format == GNU_FORMAT @@ -355,7 +359,7 @@ write_eot (void) /* FIXME: Cross recursion between start_header and write_long! */ -static union block *start_header PARAMS ((const char *, struct stat *)); +static union block *start_header (const char *, struct stat *); static void write_long (const char *p, char type) @@ -370,7 +374,7 @@ write_long (const char *p, char type) header = start_header ("././@LongLink", &foo); header->header.typeflag = type; - finish_header (header); + finish_header (header, -1); header = find_next_block (); @@ -400,42 +404,7 @@ start_header (const char *name, struct stat *st) { union block *header; - if (!absolute_names_option) - { - size_t prefix_len = FILESYSTEM_PREFIX_LEN (name); - - if (prefix_len) - { - static int warned_once; - if (!warned_once) - { - warned_once = 1; - WARN ((0, 0, _("Removing leading `%.*s' from member names"), - (int) prefix_len, name)); - } - name += prefix_len; - } - - while (*name == '/') - { - static int warned_once; - if (!warned_once) - { - warned_once = 1; - WARN ((0, 0, _("Removing leading `/' from member names"))); - } - name++; - } - - { - static int warned_once; - if (! warned_once && contains_dot_dot (name)) - { - warned_once = 1; - WARN ((0, 0, _("Member names contain `..'"))); - } - } - } + name = safer_name_suffix (name, 0); if (sizeof header->header.name <= strlen (name)) write_long (name, GNUTYPE_LONGNAME); @@ -489,6 +458,8 @@ start_header (const char *name, struct stat *st) GID_TO_CHARS (st->st_gid, header->header.gid); OFF_TO_CHARS (st->st_size, header->header.size); TIME_TO_CHARS (st->st_mtime, header->header.mtime); + MAJOR_TO_CHARS (0, header->header.devmajor); + MINOR_TO_CHARS (0, header->header.devminor); if (incremental_option) if (archive_format == OLDGNU_FORMAT) @@ -533,9 +504,11 @@ start_header (const char *name, struct stat *st) } /* Finish off a filled-in header block and write it out. We also - print the file name and/or full info if verbose is on. */ + print the file name and/or full info if verbose is on. If BLOCK_ORDINAL + is not negative, is the block ordinal of the first record for this + file, which may be a preceding long name or long link record. */ void -finish_header (union block *header) +finish_header (union block *header, off_t block_ordinal) { size_t i; int sum; @@ -570,7 +543,7 @@ finish_header (union block *header) current_header = header; /* current_stat is already set up. */ current_format = archive_format; - print_header (); + print_header (block_ordinal); } set_next_block_after (header); @@ -592,21 +565,12 @@ zero_block_p (char *buffer) return 1; } -static void +void init_sparsearray (void) { - int counter; - - sp_array_size = 10; - - /* Make room for our scratch space -- initially is 10 elts long. */ - - sparsearray = xmalloc (sp_array_size * sizeof (struct sp_array)); - for (counter = 0; counter < sp_array_size; counter++) - { - sparsearray[counter].offset = 0; - sparsearray[counter].numbytes = 0; - } + if (! sp_array_size) + sp_array_size = SPARSES_IN_OLDGNU_HEADER; + sparsearray = xmalloc (sp_array_size * sizeof *sparsearray); } static off_t @@ -651,7 +615,7 @@ deal_with_sparse (char *name, union block *header) init_sparsearray (); clear_buffer (buffer); - while (0 < (count = safe_read (file, buffer, sizeof buffer))) + for (;;) { /* Realloc the scratch area as necessary. FIXME: should reallocate only at beginning of a new instance of non-zero data. */ @@ -663,6 +627,10 @@ deal_with_sparse (char *name, union block *header) 2 * sp_array_size * sizeof (struct sp_array)); sp_array_size *= 2; } + + count = safe_read (file, buffer, sizeof buffer); + if (count <= 0) + break; /* Process one block. */ @@ -825,7 +793,7 @@ create_archive (void) buffer = xrealloc (buffer, buffer_size); } memcpy (buffer, p, plen); - if (buffer[plen - 1] != '/') + if (! ISSLASH (buffer[plen - 1])) buffer[plen++] = '/'; q = gnu_list_name->dir_contents; if (q) @@ -880,10 +848,15 @@ compare_links (void const *entry1, void const *entry2) return ((link1->dev ^ link2->dev) | (link1->ino ^ link2->ino)) == 0; } +/* Table of all non-directories that we've written so far. Any time + we see another, we check the table and avoid dumping the data + again if we've done it once already. */ +static Hash_table *link_table; + /* Dump a single file, recursing on directories. P is the file name to dump. TOP_LEVEL tells whether this is a top-level call; zero - means no, positive means yes, and negative means an incremental - dump. PARENT_DEVICE is the device of P's + means no, positive means yes, and negative means the top level + of an incremental dump. PARENT_DEVICE is the device of P's parent directory; it is examined only if TOP_LEVEL is zero. Set global CURRENT_STAT to stat output for this file. */ @@ -900,6 +873,7 @@ dump_file (char *p, int top_level, dev_t parent_device) char save_typeflag; time_t original_ctime; struct utimbuf restore_times; + off_t block_ordinal = -1; /* FIXME: `header' might be used uninitialized in this function. Reported by Bruno Haible. */ @@ -971,7 +945,7 @@ dump_file (char *p, int top_level, dev_t parent_device) errno = 0; - directory = savedir (p, current_stat.st_size); + directory = savedir (p); if (! directory) { if (ignore_failed_read_option) @@ -987,7 +961,7 @@ dump_file (char *p, int top_level, dev_t parent_device) buflen = len + NAME_FIELD_SIZE; namebuf = xmalloc (buflen + 1); memcpy (namebuf, p, len); - while (len >= 1 && namebuf[len - 1] == '/') + while (len >= 1 && ISSLASH (namebuf[len - 1])) len--; namebuf[len++] = '/'; namebuf[len] = '\0'; @@ -1004,6 +978,7 @@ dump_file (char *p, int top_level, dev_t parent_device) reasons either, so until these are reported (anew?), just allow directory blocks to be written even with old archives. */ + block_ordinal = current_block_ordinal (); current_stat.st_size = 0; /* force 0 size on dir */ /* FIXME: If people could really read standard archives, this @@ -1025,7 +1000,7 @@ dump_file (char *p, int top_level, dev_t parent_device) /* If we're gnudumping, we aren't done yet so don't close it. */ if (!incremental_option) - finish_header (header); /* done with directory header */ + finish_header (header, block_ordinal); } if (incremental_option && gnu_list_name->dir_contents) @@ -1039,17 +1014,16 @@ dump_file (char *p, int top_level, dev_t parent_device) buffer = gnu_list_name->dir_contents; /* FOO */ totsize = 0; - for (p_buffer = buffer; p_buffer && *p_buffer;) - { - size_t tmp; - - tmp = strlen (p_buffer) + 1; - totsize += tmp; - p_buffer += tmp; - } + if (buffer) + for (p_buffer = buffer; *p_buffer; ) + { + size_t size = strlen (p_buffer) + 1; + totsize += size; + p_buffer += size; + } totsize++; OFF_TO_CHARS (totsize, header->header.size); - finish_header (header); + finish_header (header, block_ordinal); p_buffer = buffer; sizeleft = totsize; while (sizeleft > 0) @@ -1106,7 +1080,7 @@ dump_file (char *p, int top_level, dev_t parent_device) (entrylen = strlen (entry)) != 0; entry += entrylen + 1) { - if (buflen <= len + entrylen) + if (buflen < len + entrylen) { buflen = len + entrylen; namebuf = xrealloc (namebuf, buflen + 1); @@ -1128,46 +1102,23 @@ dump_file (char *p, int top_level, dev_t parent_device) return; else { - /* Check for multiple links. - - We maintain a table of all such files that we've written so - far. Any time we see another, we check the table and avoid - dumping the data again if we've done it once already. */ + /* Check for multiple links. */ - if (1 < current_stat.st_nlink) + if (1 < current_stat.st_nlink && link_table) { - static Hash_table *link_table; - struct link *lp = xmalloc (sizeof *lp + strlen (p)); + struct link lp; struct link *dup; - lp->ino = current_stat.st_ino; - lp->dev = current_stat.st_dev; - strcpy (lp->name, p); - - if (! ((link_table - || (link_table = hash_initialize (0, 0, hash_link, - compare_links, 0))) - && (dup = hash_insert (link_table, lp)))) - xalloc_die (); + lp.ino = current_stat.st_ino; + lp.dev = current_stat.st_dev; - if (dup != lp) + if ((dup = hash_lookup (link_table, &lp))) { /* We found a link. */ - char const *link_name = dup->name; - - free (lp); - - if (! absolute_names_option) - for (; *link_name == '/'; link_name++) - { - static int warned_once; - if (!warned_once) - { - warned_once = 1; - WARN ((0, 0, - _("Removing leading `/' from link names"))); - } - } + char const *link_name = safer_name_suffix (dup->name, 1); + dup->nlink--; + + block_ordinal = current_block_ordinal (); if (NAME_FIELD_SIZE <= strlen (link_name)) write_long (link_name, GNUTYPE_LONGLINK); assign_string (¤t_link_name, link_name); @@ -1180,14 +1131,15 @@ dump_file (char *p, int top_level, dev_t parent_device) header->header.linkname[NAME_FIELD_SIZE - 1] = 0; header->header.typeflag = LNKTYPE; - finish_header (header); + finish_header (header, block_ordinal); /* FIXME: Maybe remove from table after all links found? */ if (remove_files_option && unlink (p) != 0) unlink_error (p); - /* We dumped it. */ + /* We dumped it, and we don't need to put it in the + table again. */ return; } } @@ -1212,7 +1164,7 @@ dump_file (char *p, int top_level, dev_t parent_device) { /* Check the size of the file against the number of blocks allocated for it, counting both data and indirect blocks. - If there is a smaller number of blocks that would be + If there is a smaller number of blocks than would be necessary to accommodate a file of this size, this is safe to say that we have a sparse file: at least one of those blocks in the file is just a useless hole. For sparse @@ -1247,6 +1199,7 @@ dump_file (char *p, int top_level, dev_t parent_device) { int counter; + block_ordinal = current_block_ordinal (); header = start_header (p, ¤t_stat); header->header.typeflag = GNUTYPE_SPARSE; header_moved = 1; @@ -1316,7 +1269,10 @@ dump_file (char *p, int top_level, dev_t parent_device) /* If the file is sparse, we've already taken care of this. */ if (!header_moved) - header = start_header (p, ¤t_stat); + { + block_ordinal = current_block_ordinal (); + header = start_header (p, ¤t_stat); + } /* Mark contiguous files, if we support them. */ @@ -1325,7 +1281,7 @@ dump_file (char *p, int top_level, dev_t parent_device) isextended = header->oldgnu_header.isextended; save_typeflag = header->header.typeflag; - finish_header (header); + finish_header (header, block_ordinal); if (isextended) { int sparses_emitted = SPARSES_IN_OLDGNU_HEADER; @@ -1393,7 +1349,7 @@ dump_file (char *p, int top_level, dev_t parent_device) (p, current_stat.st_size - sizeleft, bufsize); goto padit; } - sizeleft -= bufsize; + sizeleft -= count; /* This is nonportable (the type of set_next_block_after's arg). */ @@ -1431,8 +1387,6 @@ dump_file (char *p, int top_level, dev_t parent_device) { char const *qp = quotearg_colon (p); WARN ((0, 0, _("%s: file changed as we read it"), qp)); - if (! ignore_failed_read_option) - exit_status = TAREXIT_FAILURE; } if (close (f) != 0) { @@ -1449,7 +1403,7 @@ dump_file (char *p, int top_level, dev_t parent_device) if (unlink (p) == -1) unlink_error (p); } - return; + goto file_was_dumped; /* File shrunk or gave error, pad out tape to match the size we specified in the header. */ @@ -1471,7 +1425,7 @@ dump_file (char *p, int top_level, dev_t parent_device) if (atime_preserve_option) utime (p, &restore_times); } - return; + goto file_was_dumped; } #ifdef HAVE_READLINK else if (S_ISLNK (current_stat.st_mode)) @@ -1496,18 +1450,21 @@ dump_file (char *p, int top_level, dev_t parent_device) write_long (buffer, GNUTYPE_LONGLINK); assign_string (¤t_link_name, buffer); + block_ordinal = current_block_ordinal (); current_stat.st_size = 0; /* force 0 size on symlink */ header = start_header (p, ¤t_stat); strncpy (header->header.linkname, buffer, NAME_FIELD_SIZE); header->header.linkname[NAME_FIELD_SIZE - 1] = '\0'; header->header.typeflag = SYMTYPE; - finish_header (header); /* nothing more to do to it */ + finish_header (header, block_ordinal); + /* nothing more to do to it */ + if (remove_files_option) { if (unlink (p) == -1) unlink_error (p); } - return; + goto file_was_dumped; } #endif else if (S_ISCHR (current_stat.st_mode)) @@ -1533,6 +1490,7 @@ dump_file (char *p, int top_level, dev_t parent_device) if (archive_format == V7_FORMAT) goto unknown; + block_ordinal = current_block_ordinal (); current_stat.st_size = 0; /* force 0 size */ header = start_header (p, ¤t_stat); header->header.typeflag = type; @@ -1543,17 +1501,61 @@ dump_file (char *p, int top_level, dev_t parent_device) MINOR_TO_CHARS (minor (current_stat.st_rdev), header->header.devminor); } - finish_header (header); + finish_header (header, block_ordinal); if (remove_files_option) { if (unlink (p) == -1) unlink_error (p); } - return; + goto file_was_dumped; unknown: WARN ((0, 0, _("%s: Unknown file type; file ignored"), quotearg_colon (p))); if (! ignore_failed_read_option) exit_status = TAREXIT_FAILURE; + return; + +file_was_dumped: + if (1 < current_stat.st_nlink) + { + struct link *dup; + struct link *lp = xmalloc (offsetof (struct link, name) + + strlen (p) + 1); + lp->ino = current_stat.st_ino; + lp->dev = current_stat.st_dev; + lp->nlink = current_stat.st_nlink; + strcpy (lp->name, p); + + if (! ((link_table + || (link_table = hash_initialize (0, 0, hash_link, + compare_links, 0))) + && (dup = hash_insert (link_table, lp)))) + xalloc_die (); + + if (dup != lp) + abort (); + lp->nlink--; + } + +} + +/* For each dumped file, check if all its links were dumped. Emit + warnings if it is not so. */ +void +check_links () +{ + struct link *lp; + + if (!link_table) + return; + + for (lp = hash_get_first (link_table); lp; + lp = hash_get_next (link_table, lp)) + { + if (lp->nlink) + { + WARN ((0, 0, _("Missing links to '%s'.\n"), lp->name)); + } + } }