void sys_detect_dev_null_output (void) { static char const dev_null[] = "nul"; dev_null_output = (strcmp (archive_name_array[0], dev_null) == 0 || (! _isrmt (archive))); }
/* Save archive file inode and device numbers */ void sys_save_archive_dev_ino (void) { if (!_isrmt (archive) && S_ISREG (archive_stat.st_mode)) { ar_dev = archive_stat.st_dev; ar_ino = archive_stat.st_ino; } else ar_dev = 0; }
void check_type (char *dev, int desc) { struct stat stats; if (_isrmt (desc)) return; if (fstat (desc, &stats) == -1) stat_error (dev); if ((stats.st_mode & S_IFMT) != S_IFCHR) error (1, 0, _("%s is not a character special file"), dev); }
/* Detect if outputting to "/dev/null". */ void sys_detect_dev_null_output (void) { static char const dev_null[] = "/dev/null"; struct stat dev_null_stat; dev_null_output = (strcmp (archive_name_array[0], dev_null) == 0 || (! _isrmt (archive) && S_ISCHR (archive_stat.st_mode) && stat (dev_null, &dev_null_stat) == 0 && archive_stat.st_dev == dev_null_stat.st_dev && archive_stat.st_ino == dev_null_stat.st_ino)); }
void delete_archive_members (void) { enum read_header logical_status = HEADER_STILL_UNREAD; enum read_header previous_status = HEADER_STILL_UNREAD; /* FIXME: Should clean the routine before cleaning these variables :-( */ struct name *name; off_t blocks_to_skip = 0; off_t blocks_to_keep = 0; int kept_blocks_in_record; name_gather (); open_archive (ACCESS_UPDATE); acting_as_filter = strcmp (archive_name_array[0], "-") == 0; do { enum read_header status = read_header (true); switch (status) { case HEADER_STILL_UNREAD: abort (); case HEADER_SUCCESS: if ((name = name_scan (current_stat_info.file_name)) == NULL) { skip_member (); break; } name->found_count++; if (!ISFOUND(name)) { skip_member (); break; } /* Fall through. */ case HEADER_SUCCESS_EXTENDED: logical_status = status; break; case HEADER_ZERO_BLOCK: if (ignore_zeros_option) { set_next_block_after (current_header); break; } /* Fall through. */ case HEADER_END_OF_FILE: logical_status = HEADER_END_OF_FILE; break; case HEADER_FAILURE: set_next_block_after (current_header); switch (previous_status) { case HEADER_STILL_UNREAD: WARN ((0, 0, _("This does not look like a tar archive"))); /* Fall through. */ case HEADER_SUCCESS: case HEADER_SUCCESS_EXTENDED: case HEADER_ZERO_BLOCK: ERROR ((0, 0, _("Skipping to next header"))); /* Fall through. */ case HEADER_FAILURE: break; case HEADER_END_OF_FILE: abort (); } break; } previous_status = status; } while (logical_status == HEADER_STILL_UNREAD); records_skipped = records_read - 1; new_record = xmalloc (record_size); if (logical_status == HEADER_SUCCESS || logical_status == HEADER_SUCCESS_EXTENDED) { write_archive_to_stdout = false; /* Save away blocks before this one in this record. */ new_blocks = current_block - record_start; if (new_blocks) memcpy (new_record, record_start, new_blocks * BLOCKSIZE); if (logical_status == HEADER_SUCCESS) { /* FIXME: Pheew! This is crufty code! */ logical_status = HEADER_STILL_UNREAD; goto flush_file; } /* FIXME: Solaris 2.4 Sun cc (the ANSI one, not the old K&R) says: "delete.c", line 223: warning: loop not entered at top Reported by Bruno Haible. */ while (1) { enum read_header status; /* Fill in a record. */ if (current_block == record_end) flush_archive (); status = read_header (false); xheader_decode (¤t_stat_info); if (status == HEADER_ZERO_BLOCK && ignore_zeros_option) { set_next_block_after (current_header); continue; } if (status == HEADER_END_OF_FILE || status == HEADER_ZERO_BLOCK) { logical_status = HEADER_END_OF_FILE; break; } if (status == HEADER_FAILURE) { ERROR ((0, 0, _("Deleting non-header from archive"))); set_next_block_after (current_header); continue; } /* Found another header. */ if ((name = name_scan (current_stat_info.file_name)) != NULL) { name->found_count++; if (ISFOUND(name)) { flush_file: set_next_block_after (current_header); blocks_to_skip = (current_stat_info.stat.st_size + BLOCKSIZE - 1) / BLOCKSIZE; while (record_end - current_block <= blocks_to_skip) { blocks_to_skip -= (record_end - current_block); flush_archive (); } current_block += blocks_to_skip; blocks_to_skip = 0; continue; } } /* Copy header. */ if (extended_header.size) { write_recent_bytes (extended_header.buffer, extended_header.size); } else { write_recent_blocks (recent_long_name, recent_long_name_blocks); write_recent_blocks (recent_long_link, recent_long_link_blocks); } new_record[new_blocks] = *current_header; new_blocks++; blocks_to_keep = (current_stat_info.stat.st_size + BLOCKSIZE - 1) / BLOCKSIZE; set_next_block_after (current_header); if (new_blocks == blocking_factor) write_record (1); /* Copy data. */ kept_blocks_in_record = record_end - current_block; if (kept_blocks_in_record > blocks_to_keep) kept_blocks_in_record = blocks_to_keep; while (blocks_to_keep) { int count; if (current_block == record_end) { flush_read (); current_block = record_start; kept_blocks_in_record = blocking_factor; if (kept_blocks_in_record > blocks_to_keep) kept_blocks_in_record = blocks_to_keep; } count = kept_blocks_in_record; if (blocking_factor - new_blocks < count) count = blocking_factor - new_blocks; if (! count) abort (); memcpy (new_record + new_blocks, current_block, count * BLOCKSIZE); new_blocks += count; current_block += count; blocks_to_keep -= count; kept_blocks_in_record -= count; if (new_blocks == blocking_factor) write_record (1); } } if (logical_status == HEADER_END_OF_FILE) { /* Write the end of tape. FIXME: we can't use write_eot here, as it gets confused when the input is at end of file. */ int total_zero_blocks = 0; do { int zero_blocks = blocking_factor - new_blocks; memset (new_record + new_blocks, 0, BLOCKSIZE * zero_blocks); total_zero_blocks += zero_blocks; write_record (total_zero_blocks < 2); } while (total_zero_blocks < 2); } if (! acting_as_filter && ! _isrmt (archive)) { if (sys_truncate (archive)) truncate_warn (archive_name_array[0]); } } free (new_record); close_archive (); names_notfound (); }
void delete_archive_members (void) { enum read_header logical_status = HEADER_STILL_UNREAD; enum read_header previous_status = HEADER_STILL_UNREAD; /* FIXME: Should clean the routine before cleaning these variables :-( */ struct name *name; off_t blocks_to_skip = 0; off_t blocks_to_keep = 0; int kept_blocks_in_record; name_gather (); open_archive (ACCESS_UPDATE); acting_as_filter = strcmp (archive_name_array[0], "-") == 0; do { enum read_header status = read_header (); switch (status) { case HEADER_STILL_UNREAD: abort (); case HEADER_SUCCESS: if (name = name_scan (current_file_name), !name) { skip_member (); break; } name->found = 1; logical_status = HEADER_SUCCESS; break; case HEADER_ZERO_BLOCK: if (ignore_zeros_option) { set_next_block_after (current_header); break; } /* Fall through. */ case HEADER_END_OF_FILE: logical_status = HEADER_END_OF_FILE; break; case HEADER_FAILURE: set_next_block_after (current_header); switch (previous_status) { case HEADER_STILL_UNREAD: WARN ((0, 0, _("This does not look like a tar archive"))); /* Fall through. */ case HEADER_SUCCESS: case HEADER_ZERO_BLOCK: ERROR ((0, 0, _("Skipping to next header"))); /* Fall through. */ case HEADER_FAILURE: break; case HEADER_END_OF_FILE: abort (); } break; } previous_status = status; } while (logical_status == HEADER_STILL_UNREAD); new_record = xmalloc (record_size); if (logical_status == HEADER_SUCCESS) { write_archive_to_stdout = 0; /* Save away blocks before this one in this record. */ new_blocks = current_block - record_start; if (new_blocks) memcpy (new_record, record_start, new_blocks * BLOCKSIZE); #if 0 /* FIXME: Old code, before the goto was inserted. To be redesigned. */ skip_member (); #endif logical_status = HEADER_STILL_UNREAD; goto flush_file; /* FIXME: Solaris 2.4 Sun cc (the ANSI one, not the old K&R) says: "delete.c", line 223: warning: loop not entered at top Reported by Bruno Haible. */ while (1) { enum read_header status; /* Fill in a record. */ if (current_block == record_end) { flush_archive (); records_read++; } status = read_header (); if (status == HEADER_ZERO_BLOCK && ignore_zeros_option) { set_next_block_after (current_header); continue; } if (status == HEADER_END_OF_FILE || status == HEADER_ZERO_BLOCK) { logical_status = HEADER_END_OF_FILE; break; } if (status == HEADER_FAILURE) { ERROR ((0, 0, _("Deleting non-header from archive"))); set_next_block_after (current_header); continue; } /* Found another header. */ if (name = name_scan (current_file_name), name) { name->found = 1; flush_file: set_next_block_after (current_header); blocks_to_skip = (current_stat.st_size + BLOCKSIZE - 1) / BLOCKSIZE; while (record_end - current_block <= blocks_to_skip) { blocks_to_skip -= (record_end - current_block); flush_archive (); records_read++; } current_block += blocks_to_skip; blocks_to_skip = 0; continue; } /* Copy header. */ new_record[new_blocks] = *current_header; new_blocks++; blocks_to_keep = (current_stat.st_size + BLOCKSIZE - 1) / BLOCKSIZE; set_next_block_after (current_header); if (new_blocks == blocking_factor) write_record (1); /* Copy data. */ kept_blocks_in_record = record_end - current_block; if (kept_blocks_in_record > blocks_to_keep) kept_blocks_in_record = blocks_to_keep; while (blocks_to_keep) { int count; if (current_block == record_end) { flush_read (); records_read++; current_block = record_start; kept_blocks_in_record = blocking_factor; if (kept_blocks_in_record > blocks_to_keep) kept_blocks_in_record = blocks_to_keep; } count = kept_blocks_in_record; if (blocking_factor - new_blocks < count) count = blocking_factor - new_blocks; if (! count) abort (); memcpy (new_record + new_blocks, current_block, count * BLOCKSIZE); new_blocks += count; current_block += count; blocks_to_keep -= count; kept_blocks_in_record -= count; if (new_blocks == blocking_factor) write_record (1); } } } if (logical_status == HEADER_END_OF_FILE) { /* Write the end of tape. FIXME: we can't use write_eot here, as it gets confused when the input is at end of file. */ int total_zero_blocks = 0; do { int zero_blocks = blocking_factor - new_blocks; memset (new_record + new_blocks, 0, BLOCKSIZE * zero_blocks); total_zero_blocks += zero_blocks; write_record (total_zero_blocks < 2); } while (total_zero_blocks < 2); } free (new_record); if (! acting_as_filter && ! _isrmt (archive)) { #if MSDOS int status = write (archive, "", 0); #else off_t pos = lseek (archive, (off_t) 0, SEEK_CUR); int status = pos < 0 ? -1 : ftruncate (archive, pos); #endif if (status != 0) truncate_warn (archive_name_array[0]); } close_archive (); names_notfound (); }
void process_copy_out (void) { int res; /* result of functions */ dynamic_string input_name; /* name of file read from stdin */ int handle; /* output file descriptor */ struct stat stat_info; /* temporary stat record */ assert (header_writer != NULL); assert (eof_writer != NULL); /* Initialize the copy out. */ ds_init (&input_name, 128); #ifdef __MSDOS__ setmode (archive_des, O_BINARY); #endif #if DOSWIN if (!isatty (archive_des)) setmode (archive_des, O_BINARY); #endif /* Check whether the output file might be a tape. */ handle = archive_des; if (_isrmt (handle)) { output_is_special = 1; output_is_seekable = 0; } else { if (fstat (handle, &stat_info)) error (1, errno, _("standard output is closed")); output_is_special = ( #ifdef S_ISBLK S_ISBLK (stat_info.st_mode) || #endif S_ISCHR (stat_info.st_mode)); output_is_seekable = S_ISREG (stat_info.st_mode); } #if DOSWIN /* We might as well give the user an opportunity to recover from "No space on device" even if they are writing to disk files. It is specifically handy with floppies. */ output_is_special = !isatty (handle); #endif if (append_option) { process_copy_in (); prepare_append (handle); } /* Copy files with names read from stdin. */ while (get_next_file_name (&input_name)) dump_one_file (1, handle, &input_name); writeout_final_defers (handle); /* The collection is complete; append the trailer. */ (*eof_writer) (handle); /* Fill up the output block. */ tape_clear_rest_of_block (handle); tape_empty_output_buffer (handle); if (dot_option) fputc ('\n', stderr); if (! quiet_option) { res = (output_bytes + io_block_size - 1) / io_block_size; if (res == 1) fprintf (stderr, _("1 block\n")); else fprintf (stderr, _("%d blocks\n"), res); } }