📄 buffer.c
字号:
/* Buffer management for tar. Copyright (C) 1988, 1992, 1993, 1994, 1996, 1997, 1999, 2000, 2001, 2003, 2004, 2005, 2006, 2007 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 under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */#include <system.h>#include <system-ioctl.h>#include <signal.h>#include <closeout.h>#include <fnmatch.h>#include <human.h>#include <quotearg.h>#include "common.h"#include <rmt.h>/* Number of retries before giving up on read. */#define READ_ERROR_MAX 10/* Globbing pattern to append to volume label if initial match failed. */#define VOLUME_LABEL_APPEND " Volume [1-9]*"/* Variables. */static tarlong prev_written; /* bytes written on previous volumes */static tarlong bytes_written; /* bytes written on this volume */static void *record_buffer[2]; /* allocated memory */union block *record_buffer_aligned[2];static int record_index;/* FIXME: The following variables should ideally be static to this module. However, this cannot be done yet. The cleanup continues! */union block *record_start; /* start of record of archive */union block *record_end; /* last+1 block of archive record */union block *current_block; /* current block of archive */enum access_mode access_mode; /* how do we handle the archive */off_t records_read; /* number of records read from this archive */off_t records_written; /* likewise, for records written */extern off_t records_skipped; /* number of records skipped at the start of the archive, defined in delete.c */ static off_t record_start_block; /* block ordinal at record_start *//* Where we write list messages (not errors, not interactions) to. */FILE *stdlis;static void backspace_output (void);/* PID of child program, if compress_option or remote archive access. */static pid_t child_pid;/* Error recovery stuff */static int read_error_count;/* Have we hit EOF yet? */static bool hit_eof;static bool read_full_records = false;/* We're reading, but we just read the last block and it's time to update. Declared in update.c As least EXTERN like this one as possible. (?? --gray) FIXME: Either eliminate it or move it to common.h.*/extern bool time_to_start_writing;bool write_archive_to_stdout;void (*flush_write_ptr) (size_t);void (*flush_read_ptr) (void);char *volume_label;char *continued_file_name;uintmax_t continued_file_size;uintmax_t continued_file_offset;static int volno = 1; /* which volume of a multi-volume tape we're on */static int global_volno = 1; /* volume number to print in external messages */bool write_archive_to_stdout;/* Used by flush_read and flush_write to store the real info about saved names. */static char *real_s_name;static off_t real_s_totsize;static off_t real_s_sizeleft;/* Multi-volume tracking support */static char *save_name; /* name of the file we are currently writing */static off_t save_totsize; /* total size of file we are writing, only valid if save_name is nonzero */static off_t save_sizeleft; /* where we are in the file we are writing, only valid if save_name is nonzero */static struct tar_stat_info dummy;voidbuffer_write_global_xheader (){ xheader_write_global (&dummy.xhdr);}voidmv_begin (struct tar_stat_info *st){ if (multi_volume_option) { assign_string (&save_name, st->orig_file_name); save_totsize = save_sizeleft = st->stat.st_size; }}voidmv_end (){ if (multi_volume_option) assign_string (&save_name, 0);}voidmv_total_size (off_t size){ save_totsize = size;}voidmv_size_left (off_t size){ save_sizeleft = size;}/* Functions. */voidclear_read_error_count (void){ read_error_count = 0;}/* Time-related functions */double duration;voidset_start_time (){ gettime (&start_time); volume_start_time = start_time; last_stat_time = start_time;}voidset_volume_start_time (){ gettime (&volume_start_time); last_stat_time = volume_start_time;}voidcompute_duration (){ struct timespec now; gettime (&now); duration += ((now.tv_sec - last_stat_time.tv_sec) + (now.tv_nsec - last_stat_time.tv_nsec) / 1e9); gettime (&last_stat_time);}/* Compression detection */enum compress_type { ct_none, ct_compress, ct_gzip, ct_bzip2, ct_lzma};struct zip_magic{ enum compress_type type; size_t length; char *magic; char *program; char *option;};static struct zip_magic const magic[] = { { ct_none, }, { ct_compress, 2, "\037\235", "compress", "-Z" }, { ct_gzip, 2, "\037\213", "gzip", "-z" }, { ct_bzip2, 3, "BZh", "bzip2", "-j" }, { ct_lzma, 6, "\xFFLZMA", "lzma", "--lzma" }, /* FIXME: ???? */};#define NMAGIC (sizeof(magic)/sizeof(magic[0]))#define compress_option(t) magic[t].option#define compress_program(t) magic[t].program/* Check if the file ARCHIVE is a compressed archive. */enum compress_typecheck_compressed_archive (bool *pshort){ struct zip_magic const *p; bool sfr; bool temp; if (!pshort) pshort = &temp; /* Prepare global data needed for find_next_block: */ record_end = record_start; /* set up for 1st record = # 0 */ sfr = read_full_records; read_full_records = true; /* Suppress fatal error on reading a partial record */ *pshort = find_next_block () == 0; /* Restore global values */ read_full_records = sfr; if (tar_checksum (record_start, true) == HEADER_SUCCESS) /* Probably a valid header */ return ct_none; for (p = magic + 1; p < magic + NMAGIC; p++) if (memcmp (record_start->buffer, p->magic, p->length) == 0) return p->type; return ct_none;}/* Open an archive named archive_name_array[0]. Detect if it is a compressed archive of known type and use corresponding decompression program if so */intopen_compressed_archive (){ archive = rmtopen (archive_name_array[0], O_RDONLY | O_BINARY, MODE_RW, rsh_command_option); if (archive == -1) return archive; if (!multi_volume_option) { bool shortfile; enum compress_type type = check_compressed_archive (&shortfile); if (type == ct_none) { if (shortfile) ERROR ((0, 0, _("This does not look like a tar archive"))); return archive; } /* FD is not needed any more */ rmtclose (archive); hit_eof = false; /* It might have been set by find_next_block in check_compressed_archive */ /* Open compressed archive */ use_compress_program_option = compress_program (type); child_pid = sys_child_open_for_uncompress (); read_full_records = true; } records_read = 0; record_end = record_start; /* set up for 1st record = # 0 */ return archive;}static voidprint_stats (FILE *fp, const char *text, tarlong numbytes){ char bytes[sizeof (tarlong) * CHAR_BIT]; char abbr[LONGEST_HUMAN_READABLE + 1]; char rate[LONGEST_HUMAN_READABLE + 1]; int human_opts = human_autoscale | human_base_1024 | human_SI | human_B; sprintf (bytes, TARLONG_FORMAT, numbytes); fprintf (fp, "%s: %s (%s, %s/s)\n", text, bytes, human_readable (numbytes, abbr, human_opts, 1, 1), (0 < duration && numbytes / duration < (uintmax_t) -1 ? human_readable (numbytes / duration, rate, human_opts, 1, 1) : "?"));}voidprint_total_stats (){ switch (subcommand_option) { case CREATE_SUBCOMMAND: case CAT_SUBCOMMAND: case UPDATE_SUBCOMMAND: case APPEND_SUBCOMMAND: /* Amanda 2.4.1p1 looks for "Total bytes written: [0-9][0-9]*". */ print_stats (stderr, _("Total bytes written"), prev_written + bytes_written); break; case DELETE_SUBCOMMAND: { char buf[UINTMAX_STRSIZE_BOUND]; print_stats (stderr, _("Total bytes read"), records_read * record_size); print_stats (stderr, _("Total bytes written"), prev_written + bytes_written); fprintf (stderr, _("Total bytes deleted: %s\n"), STRINGIFY_BIGINT ((records_read - records_skipped) * record_size - (prev_written + bytes_written), buf)); } break; case EXTRACT_SUBCOMMAND: case LIST_SUBCOMMAND: case DIFF_SUBCOMMAND: print_stats (stderr, _("Total bytes read"), records_read * record_size); break; default: abort (); }}/* Compute and return the block ordinal at current_block. */off_tcurrent_block_ordinal (void){ return record_start_block + (current_block - record_start);}/* If the EOF flag is set, reset it, as well as current_block, etc. */voidreset_eof (void){ if (hit_eof) { hit_eof = false; current_block = record_start; record_end = record_start + blocking_factor; access_mode = ACCESS_WRITE; }}/* Return the location of the next available input or output block. Return zero for EOF. Once we have returned zero, we just keep returning it, to avoid accidentally going on to the next file on the tape. */union block *find_next_block (void){ if (current_block == record_end) { if (hit_eof) return 0; flush_archive (); if (current_block == record_end) { hit_eof = true; return 0; } } return current_block;}/* Indicate that we have used all blocks up thru BLOCK. */voidset_next_block_after (union block *block){ while (block >= current_block) current_block++; /* Do *not* flush the archive here. If we do, the same argument to set_next_block_after could mean the next block (if the input record is exactly one block long), which is not what is intended. */ if (current_block > record_end) abort ();}/* Return the number of bytes comprising the space between POINTER through the end of the current buffer of blocks. This space is available for filling with data, or taking data from. POINTER is usually (but not always) the result of previous find_next_block call. */size_tavailable_space_after (union block *pointer){ return record_end->buffer - pointer->buffer;}/* Close file having descriptor FD, and abort if close unsuccessful. */voidxclose (int fd){ if (close (fd) != 0) close_error (_("(pipe)"));}static voidinit_buffer (){ if (! record_buffer_aligned[record_index]) record_buffer_aligned[record_index] = page_aligned_alloc (&record_buffer[record_index], record_size); record_start = record_buffer_aligned[record_index]; current_block = record_start; record_end = record_start + blocking_factor;}/* Open an archive file. The argument specifies whether we are reading or writing, or both. */static void_open_archive (enum access_mode wanted_access){ int backed_up_flag = 0; if (record_size == 0) FATAL_ERROR ((0, 0, _("Invalid value for record_size"))); if (archive_names == 0) FATAL_ERROR ((0, 0, _("No archive name given"))); tar_stat_destroy (¤t_stat_info); save_name = 0; real_s_name = 0; record_index = 0; init_buffer (); /* When updating the archive, we start with reading. */ access_mode = wanted_access == ACCESS_UPDATE ? ACCESS_READ : wanted_access; read_full_records = read_full_records_option; records_read = 0; if (use_compress_program_option) { switch (wanted_access) { case ACCESS_READ: child_pid = sys_child_open_for_uncompress (); read_full_records = true; record_end = record_start; /* set up for 1st record = # 0 */ break; case ACCESS_WRITE: child_pid = sys_child_open_for_compress (); break; case ACCESS_UPDATE: abort (); /* Should not happen */ break; } if (!index_file_name && wanted_access == ACCESS_WRITE && strcmp (archive_name_array[0], "-") == 0) stdlis = stderr; } else if (strcmp (archive_name_array[0], "-") == 0) { read_full_records = true; /* could be a pipe, be safe */ if (verify_option) FATAL_ERROR ((0, 0, _("Cannot verify stdin/stdout archive"))); switch (wanted_access) { case ACCESS_READ: { bool shortfile; enum compress_type type; archive = STDIN_FILENO; type = check_compressed_archive (&shortfile); if (type != ct_none) FATAL_ERROR ((0, 0, _("Archive is compressed. Use %s option"), compress_option (type))); if (shortfile) ERROR ((0, 0, _("This does not look like a tar archive"))); } break; case ACCESS_WRITE: archive = STDOUT_FILENO; if (!index_file_name) stdlis = stderr; break; case ACCESS_UPDATE: archive = STDIN_FILENO; write_archive_to_stdout = true; record_end = record_start; /* set up for 1st record = # 0 */ if (!index_file_name) stdlis = stderr; break; } } else if (verify_option) archive = rmtopen (archive_name_array[0], O_RDWR | O_CREAT | O_BINARY, MODE_RW, rsh_command_option); else switch (wanted_access) { case ACCESS_READ: archive = open_compressed_archive (); break; case ACCESS_WRITE: if (backup_option) { maybe_backup_file (archive_name_array[0], 1); backed_up_flag = 1; } archive = rmtcreat (archive_name_array[0], MODE_RW, rsh_command_option); break; case ACCESS_UPDATE:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -