X-Git-Url: https://git.brokenzipper.com/gitweb?a=blobdiff_plain;f=src%2Fbuffer.c;h=bd1cfe13ee62753f2d0f1fdd2de45f65d8da0975;hb=9d681f81303b47fe74813065ebeb3c6ecf9ab735;hp=1ad48f0657e9adf549333d75f7745cf9f4b819fa;hpb=aa985330d59a033156b0d793f880de952eafa4e5;p=chaz%2Ftar diff --git a/src/buffer.c b/src/buffer.c index 1ad48f0..bd1cfe1 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -50,7 +50,7 @@ time_t time (); /* Variables. */ -static tarlong total_written; /* bytes written on all volumes */ +static tarlong prev_written; /* bytes written on previous volumes */ static tarlong bytes_written; /* bytes written on this volume */ /* FIXME: The following four variables should ideally be static to this @@ -144,18 +144,11 @@ myfork (void) #endif /* DEBUG FORK */ -void -init_total_written (void) -{ - clear_tarlong (total_written); - clear_tarlong (bytes_written); -} - void print_total_written (void) { fprintf (stderr, _("Total bytes written: ")); - print_tarlong (total_written, stderr); + fprintf (stderr, TARLONG_FORMAT, prev_written + bytes_written); fprintf (stderr, "\n"); } @@ -307,13 +300,10 @@ is_regular_file (const char *name) { struct stat stbuf; - if (stat (name, &stbuf) < 0) - return 1; - - if (S_ISREG (stbuf.st_mode)) - return 1; - - return 0; + if (stat (name, &stbuf) == 0) + return S_ISREG (stbuf.st_mode); + else + return errno == ENOENT; } static ssize_t @@ -383,7 +373,7 @@ child_open_for_compress (void) /* We don't need a grandchild tar. Open the archive and launch the compressor. */ - archive = creat (archive_name_array[0], 0666); + archive = creat (archive_name_array[0], MODE_RW); if (archive < 0) { int saved_errno = errno; @@ -434,7 +424,7 @@ child_open_for_compress (void) if (strcmp (archive_name_array[0], "-") == 0) archive = STDOUT_FILENO; else - archive = rmtcreat (archive_name_array[0], 0666, rsh_command_option); + archive = rmtcreat (archive_name_array[0], MODE_RW, rsh_command_option); if (archive < 0) FATAL_ERROR ((0, errno, _("Cannot open archive %s"), archive_name_array[0])); @@ -457,7 +447,7 @@ child_open_for_compress (void) if (size < BLOCKSIZE) size = BLOCKSIZE; - status = full_read (STDIN_FILENO, cursor, size); + status = safe_read (STDIN_FILENO, cursor, size); if (status <= 0) break; } @@ -543,7 +533,7 @@ child_open_for_uncompress (void) /* We don't need a grandchild tar. Open the archive and lauch the uncompressor. */ - archive = open (archive_name_array[0], O_RDONLY | O_BINARY, 0666); + archive = open (archive_name_array[0], O_RDONLY | O_BINARY, MODE_RW); if (archive < 0) FATAL_ERROR ((0, errno, _("Cannot open archive %s"), archive_name_array[0])); @@ -588,7 +578,7 @@ child_open_for_uncompress (void) archive = STDIN_FILENO; else archive = rmtopen (archive_name_array[0], O_RDONLY | O_BINARY, - 0666, rsh_command_option); + MODE_RW, rsh_command_option); if (archive < 0) FATAL_ERROR ((0, errno, _("Cannot open archive %s"), archive_name_array[0])); @@ -776,13 +766,13 @@ open_archive (enum access_mode access) } else if (verify_option) archive = rmtopen (archive_name_array[0], O_RDWR | O_CREAT | O_BINARY, - 0666, rsh_command_option); + MODE_RW, rsh_command_option); else switch (access) { case ACCESS_READ: - archive = rmtopen (archive_name_array[0], O_RDONLY | O_BINARY, 0666, - rsh_command_option); + archive = rmtopen (archive_name_array[0], O_RDONLY | O_BINARY, + MODE_RW, rsh_command_option); break; case ACCESS_WRITE: @@ -791,12 +781,13 @@ open_archive (enum access_mode access) maybe_backup_file (archive_name_array[0], 1); backed_up_flag = 1; } - archive = rmtcreat (archive_name_array[0], 0666, rsh_command_option); + archive = rmtcreat (archive_name_array[0], MODE_RW, + rsh_command_option); break; case ACCESS_UPDATE: archive = rmtopen (archive_name_array[0], O_RDWR | O_CREAT | O_BINARY, - 0666, rsh_command_option); + MODE_RW, rsh_command_option); break; } @@ -807,8 +798,7 @@ open_archive (enum access_mode access) if (backed_up_flag) undo_last_backup (); - FATAL_ERROR ((0, saved_errno, _("Cannot open %s"), - archive_name_array[0])); + FATAL_ERROR ((0, saved_errno, "%s", archive_name_array[0])); } #if !MSDOS @@ -821,8 +811,9 @@ open_archive (enum access_mode access) dev_null_output = (strcmp (archive_name_array[0], dev_null) == 0 || (! _isrmt (archive) - && stat (dev_null, &dev_null_stat) == 0 && S_ISCHR (archive_stat.st_mode) + && stat (dev_null, &dev_null_stat) == 0 + && S_ISCHR (dev_null_stat.st_mode) && archive_stat.st_rdev == dev_null_stat.st_rdev)); } @@ -896,8 +887,7 @@ flush_write (void) if (checkpoint_option && !(++checkpoint % 10)) WARN ((0, 0, _("Write checkpoint %d"), checkpoint)); - if (!zerop_tarlong (tape_length_option) - && !lessp_tarlong (bytes_written, tape_length_option)) + if (tape_length_option && tape_length_option <= bytes_written) { errno = ENOSPC; /* FIXME: errno should be read-only */ status = 0; @@ -908,11 +898,9 @@ flush_write (void) status = write_archive_buffer (); if (status != record_size && !multi_volume_option) write_error (status); - else if (totals_option) - add_to_tarlong (total_written, record_size); if (status > 0) - add_to_tarlong (bytes_written, status); + bytes_written += status; if (status == record_size) { @@ -954,7 +942,9 @@ flush_write (void) if (!new_volume (ACCESS_WRITE)) return; - clear_tarlong (bytes_written); + if (totals_option) + prev_written += bytes_written; + bytes_written = 0; if (volume_label_option && real_s_name[0]) { @@ -1008,10 +998,9 @@ flush_write (void) status = write_archive_buffer (); if (status != record_size) write_error (status); - else if (totals_option) - add_to_tarlong (total_written, record_size); - add_to_tarlong (bytes_written, record_size); + bytes_written += status; + if (copy_back) { record_start += copy_back; @@ -1115,27 +1104,29 @@ flush_read (void) write_error (status); } if (multi_volume_option) - if (save_name) - { - char *cursor = save_name; + { + if (save_name) + { + char *cursor = save_name; #if MSDOS - if (cursor[1] == ':') - cursor += 2; + if (cursor[1] == ':') + cursor += 2; #endif - while (*cursor == '/') - cursor++; + while (*cursor == '/') + cursor++; - strcpy (real_s_name, cursor); - real_s_sizeleft = save_sizeleft; - real_s_totsize = save_totsize; - } - else - { - real_s_name[0] = '\0'; - real_s_totsize = 0; - real_s_sizeleft = 0; - } + strcpy (real_s_name, cursor); + real_s_sizeleft = save_sizeleft; + real_s_totsize = save_totsize; + } + else + { + real_s_name[0] = '\0'; + real_s_totsize = 0; + real_s_sizeleft = 0; + } + } error_loop: status = rmtread (archive, record_start->buffer, record_size); @@ -1358,12 +1349,12 @@ backspace_output (void) #endif { - off_t position = rmtlseek (archive, (off_t) 0, 1); + off_t position = rmtlseek (archive, (off_t) 0, SEEK_CUR); /* Seek back to the beginning of this record and start writing there. */ position -= record_size; - if (rmtlseek (archive, position, 0) != position) + if (rmtlseek (archive, position, SEEK_SET) != position) { /* Lseek failed. Try a different method. */ @@ -1398,8 +1389,7 @@ close_archive (void) if (access_mode == ACCESS_READ && ! _isrmt (archive) - && S_ISFIFO (archive_stat.st_mode) - && ! ending_file_option) + && S_ISFIFO (archive_stat.st_mode)) while (rmtread (archive, record_start->buffer, record_size) > 0) continue; #endif @@ -1409,8 +1399,8 @@ close_archive (void) #if MSDOS int status = write (archive, "", 0); #else - off_t pos = lseek (archive, (off_t) 0, 1); - int status = pos == -1 ? -1 : ftruncate (archive, pos); + off_t pos = lseek (archive, (off_t) 0, SEEK_CUR); + int status = pos < 0 ? -1 : ftruncate (archive, pos); #endif if (status != 0) WARN ((0, errno, _("WARNING: Cannot truncate %s"), @@ -1441,29 +1431,31 @@ close_archive (void) continue; if (child != -1) - if (WIFSIGNALED (wait_status) + { + if (WIFSIGNALED (wait_status) #if 0 - && !WIFSTOPPED (wait_status) + && !WIFSTOPPED (wait_status) #endif - ) - { - /* SIGPIPE is OK, everything else is a problem. */ + ) + { + /* SIGPIPE is OK, everything else is a problem. */ - if (WTERMSIG (wait_status) != SIGPIPE) - ERROR ((0, 0, _("Child died with signal %d%s"), - WTERMSIG (wait_status), - WCOREDUMP (wait_status) ? _(" (core dumped)") : "")); - } - else - { - /* Child voluntarily terminated -- but why? /bin/sh returns - SIGPIPE + 128 if its child, then do nothing. */ + if (WTERMSIG (wait_status) != SIGPIPE) + ERROR ((0, 0, _("Child died with signal %d%s"), + WTERMSIG (wait_status), + WCOREDUMP (wait_status) ? _(" (core dumped)") : "")); + } + else + { + /* Child voluntarily terminated -- but why? /bin/sh returns + SIGPIPE + 128 if its child, then do nothing. */ - if (WEXITSTATUS (wait_status) != (SIGPIPE + 128) - && WEXITSTATUS (wait_status)) - ERROR ((0, 0, _("Child returned status %d"), - WEXITSTATUS (wait_status))); - } + if (WEXITSTATUS (wait_status) != (SIGPIPE + 128) + && WEXITSTATUS (wait_status)) + ERROR ((0, 0, _("Child returned status %d"), + WEXITSTATUS (wait_status))); + } + } } #endif /* !MSDOS */ @@ -1670,24 +1662,25 @@ tryagain: } if (verify_option) - archive = rmtopen (*archive_name_cursor, O_RDWR | O_CREAT, 0666, + archive = rmtopen (*archive_name_cursor, O_RDWR | O_CREAT, MODE_RW, rsh_command_option); else switch (access) { case ACCESS_READ: - archive = rmtopen (*archive_name_cursor, O_RDONLY, 0666, + archive = rmtopen (*archive_name_cursor, O_RDONLY, MODE_RW, rsh_command_option); break; case ACCESS_WRITE: if (backup_option) maybe_backup_file (*archive_name_cursor, 1); - archive = rmtcreat (*archive_name_cursor, 0666, rsh_command_option); + archive = rmtcreat (*archive_name_cursor, MODE_RW, + rsh_command_option); break; case ACCESS_UPDATE: - archive = rmtopen (*archive_name_cursor, O_RDWR | O_CREAT, 0666, + archive = rmtopen (*archive_name_cursor, O_RDWR | O_CREAT, MODE_RW, rsh_command_option); break; }