1 /* System-dependent calls for tar.
3 Copyright 2003-2008, 2010, 2013-2014, 2016 Free Software Foundation,
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any later
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
14 Public License for more details.
16 You should have received a copy of the GNU General Public License along
17 with this program. If not, see <http://www.gnu.org/licenses/>. */
25 #include <wordsplit.h>
28 xexec (const char *cmd)
32 argv[0] = (char *) "/bin/sh";
33 argv[1] = (char *) "-c";
34 argv[2] = (char *) cmd;
37 execv ("/bin/sh", argv);
44 sys_get_archive_stat (void)
50 sys_file_is_archive (struct tar_stat_info *p)
56 sys_save_archive_dev_ino (void)
61 sys_detect_dev_null_output (void)
63 static char const dev_null[] = "nul";
65 dev_null_output = (strcmp (archive_name_array[0], dev_null) == 0
66 || (! _isrmt (archive)));
70 sys_wait_for_child (pid_t child_pid, bool eof)
75 sys_spawn_shell (void)
77 spawnl (P_WAIT, getenv ("COMSPEC"), "-", 0);
80 /* stat() in djgpp's C library gives a constant number of 42 as the
81 uid and gid of a file. So, comparing an FTP'ed archive just after
82 unpack would fail on MSDOS. */
85 sys_compare_uid (struct stat *a, struct stat *b)
91 sys_compare_gid (struct stat *a, struct stat *b)
97 sys_compare_links (struct stat *link_data, struct stat *stat_data)
103 sys_truncate (int fd)
105 return write (fd, "", 0);
109 sys_write_archive_buffer (void)
111 return full_write (archive, record_start->buffer, record_size);
114 /* Set ARCHIVE for writing, then compressing an archive. */
116 sys_child_open_for_compress (void)
118 FATAL_ERROR ((0, 0, _("Cannot use compressed or remote archives")));
121 /* Set ARCHIVE for uncompressing, then reading an archive. */
123 sys_child_open_for_uncompress (void)
125 FATAL_ERROR ((0, 0, _("Cannot use compressed or remote archives")));
130 extern union block *record_start; /* FIXME */
132 static struct stat archive_stat; /* stat block for archive file */
135 sys_get_archive_stat (void)
137 return fstat (archive, &archive_stat) == 0;
141 sys_file_is_archive (struct tar_stat_info *p)
143 return (ar_dev && p->stat.st_dev == ar_dev && p->stat.st_ino == ar_ino);
146 /* Save archive file inode and device numbers */
148 sys_save_archive_dev_ino (void)
150 if (!_isrmt (archive) && S_ISREG (archive_stat.st_mode))
152 ar_dev = archive_stat.st_dev;
153 ar_ino = archive_stat.st_ino;
159 /* Detect if outputting to "/dev/null". */
161 sys_detect_dev_null_output (void)
163 static char const dev_null[] = "/dev/null";
164 struct stat dev_null_stat;
166 dev_null_output = (strcmp (archive_name_array[0], dev_null) == 0
167 || (! _isrmt (archive)
168 && S_ISCHR (archive_stat.st_mode)
169 && stat (dev_null, &dev_null_stat) == 0
170 && archive_stat.st_dev == dev_null_stat.st_dev
171 && archive_stat.st_ino == dev_null_stat.st_ino));
175 sys_wait_for_child (pid_t child_pid, bool eof)
181 while (waitpid (child_pid, &wait_status, 0) == -1)
184 waitpid_error (use_compress_program_option);
188 if (WIFSIGNALED (wait_status))
190 int sig = WTERMSIG (wait_status);
191 if (!(!eof && sig == SIGPIPE))
192 FATAL_ERROR ((0, 0, _("Child died with signal %d"), sig));
194 else if (WEXITSTATUS (wait_status) != 0)
195 FATAL_ERROR ((0, 0, _("Child returned status %d"),
196 WEXITSTATUS (wait_status)));
201 sys_spawn_shell (void)
204 const char *shell = getenv ("SHELL");
210 priv_set_restore_linkdir ();
211 execlp (shell, "-sh", "-i", NULL);
217 while (waitpid (child, &wait_status, 0) == -1)
220 waitpid_error (shell);
227 sys_compare_uid (struct stat *a, struct stat *b)
229 return a->st_uid == b->st_uid;
233 sys_compare_gid (struct stat *a, struct stat *b)
235 return a->st_gid == b->st_gid;
239 sys_compare_links (struct stat *link_data, struct stat *stat_data)
241 return stat_data->st_dev == link_data->st_dev
242 && stat_data->st_ino == link_data->st_ino;
246 sys_truncate (int fd)
248 off_t pos = lseek (fd, (off_t) 0, SEEK_CUR);
249 return pos < 0 ? -1 : ftruncate (fd, pos);
252 /* Return nonzero if NAME is the name of a regular file, or if the file
253 does not exist (so it would be created as a regular file). */
255 is_regular_file (const char *name)
259 if (stat (name, &stbuf) == 0)
260 return S_ISREG (stbuf.st_mode);
262 return errno == ENOENT;
266 sys_write_archive_buffer (void)
268 return rmtwrite (archive, record_start->buffer, record_size);
271 #define PREAD 0 /* read file descriptor from pipe() */
272 #define PWRITE 1 /* write file descriptor from pipe() */
274 /* Duplicate file descriptor FROM into becoming INTO.
275 INTO is closed first and has to be the next available slot. */
277 xdup2 (int from, int into)
281 int status = close (into);
283 if (status != 0 && errno != EBADF)
286 FATAL_ERROR ((0, e, _("Cannot close")));
294 FATAL_ERROR ((0, e, _("Cannot dup")));
302 static void wait_for_grandchild (pid_t pid) __attribute__ ((__noreturn__));
304 /* Propagate any failure of the grandchild back to the parent. */
306 wait_for_grandchild (pid_t pid)
311 while (waitpid (pid, &wait_status, 0) == -1)
314 waitpid_error (use_compress_program_option);
318 if (WIFSIGNALED (wait_status))
319 raise (WTERMSIG (wait_status));
320 else if (WEXITSTATUS (wait_status) != 0)
321 exit_code = WEXITSTATUS (wait_status);
326 /* Set ARCHIVE for writing, then compressing an archive. */
328 sys_child_open_for_compress (void)
332 pid_t grandchild_pid;
335 signal (SIGPIPE, SIG_IGN);
337 child_pid = xfork ();
341 /* The parent tar is still here! Just clean up. */
343 archive = parent_pipe[PWRITE];
344 xclose (parent_pipe[PREAD]);
348 /* The new born child tar is here! */
350 set_program_name (_("tar (child)"));
351 signal (SIGPIPE, SIG_DFL);
353 xdup2 (parent_pipe[PREAD], STDIN_FILENO);
354 xclose (parent_pipe[PWRITE]);
356 /* Check if we need a grandchild tar. This happens only if either:
357 a) the file is to be accessed by rmt: compressor doesn't know how;
358 b) the file is not a plain file. */
360 if (!_remdev (archive_name_array[0])
361 && is_regular_file (archive_name_array[0]))
364 maybe_backup_file (archive_name_array[0], 1);
366 /* We don't need a grandchild tar. Open the archive and launch the
368 if (strcmp (archive_name_array[0], "-"))
370 archive = creat (archive_name_array[0], MODE_RW);
373 int saved_errno = errno;
378 open_fatal (archive_name_array[0]);
380 xdup2 (archive, STDOUT_FILENO);
382 priv_set_restore_linkdir ();
383 xexec (use_compress_program_option);
386 /* We do need a grandchild tar. */
389 grandchild_pid = xfork ();
391 if (grandchild_pid == 0)
393 /* The newborn grandchild tar is here! Launch the compressor. */
395 set_program_name (_("tar (grandchild)"));
397 xdup2 (child_pipe[PWRITE], STDOUT_FILENO);
398 xclose (child_pipe[PREAD]);
399 priv_set_restore_linkdir ();
400 xexec (use_compress_program_option);
403 /* The child tar is still here! */
405 /* Prepare for reblocking the data from the compressor into the archive. */
407 xdup2 (child_pipe[PREAD], STDIN_FILENO);
408 xclose (child_pipe[PWRITE]);
410 if (strcmp (archive_name_array[0], "-") == 0)
411 archive = STDOUT_FILENO;
414 archive = rmtcreat (archive_name_array[0], MODE_RW, rsh_command_option);
416 open_fatal (archive_name_array[0]);
419 /* Let's read out of the stdin pipe and write an archive. */
427 /* Assemble a record. */
429 for (length = 0, cursor = record_start->buffer;
430 length < record_size;
431 length += status, cursor += status)
433 size_t size = record_size - length;
435 status = safe_read (STDIN_FILENO, cursor, size);
436 if (status == SAFE_READ_ERROR)
437 read_fatal (use_compress_program_option);
442 /* Copy the record. */
446 /* We hit the end of the file. Write last record at
447 full length, as the only role of the grandchild is
448 doing proper reblocking. */
452 memset (record_start->buffer + length, 0, record_size - length);
453 status = sys_write_archive_buffer ();
454 if (status != record_size)
455 archive_write_error (status);
458 /* There is nothing else to read, break out. */
462 status = sys_write_archive_buffer ();
463 if (status != record_size)
464 archive_write_error (status);
467 wait_for_grandchild (grandchild_pid);
471 run_decompress_program (void)
474 const char *p, *prog = NULL;
476 int wsflags = (WRDSF_DEFFLAGS | WRDSF_ENV | WRDSF_DOOFFS) & ~WRDSF_NOVAR;
478 ws.ws_env = (const char **) environ;
481 for (p = first_decompress_program (&i); p; p = next_decompress_program (&i))
485 WARNOPT (WARN_DECOMPRESS_PROGRAM,
486 (0, errno, _("cannot run %s"), prog));
487 WARNOPT (WARN_DECOMPRESS_PROGRAM,
488 (0, 0, _("trying %s"), p));
490 if (wordsplit (p, &ws, wsflags))
491 FATAL_ERROR ((0, 0, _("cannot split string '%s': %s"),
492 p, wordsplit_strerror (&ws)));
493 wsflags |= WRDSF_REUSE;
494 memmove(ws.ws_wordv, ws.ws_wordv + ws.ws_offs,
495 sizeof(ws.ws_wordv[0])*ws.ws_wordc);
496 ws.ws_wordv[ws.ws_wordc] = (char *) "-d";
498 execvp (ws.ws_wordv[0], ws.ws_wordv);
499 ws.ws_wordv[ws.ws_wordc] = NULL;
502 FATAL_ERROR ((0, 0, _("unable to run decompression program")));
506 /* Set ARCHIVE for uncompressing, then reading an archive. */
508 sys_child_open_for_uncompress (void)
512 pid_t grandchild_pid;
516 child_pid = xfork ();
520 /* The parent tar is still here! Just clean up. */
522 archive = parent_pipe[PREAD];
523 xclose (parent_pipe[PWRITE]);
527 /* The newborn child tar is here! */
529 set_program_name (_("tar (child)"));
530 signal (SIGPIPE, SIG_DFL);
532 xdup2 (parent_pipe[PWRITE], STDOUT_FILENO);
533 xclose (parent_pipe[PREAD]);
535 /* Check if we need a grandchild tar. This happens only if either:
536 a) we're reading stdin: to force unblocking;
537 b) the file is to be accessed by rmt: compressor doesn't know how;
538 c) the file is not a plain file. */
540 if (strcmp (archive_name_array[0], "-") != 0
541 && !_remdev (archive_name_array[0])
542 && is_regular_file (archive_name_array[0]))
544 /* We don't need a grandchild tar. Open the archive and lauch the
547 archive = open (archive_name_array[0], O_RDONLY | O_BINARY, MODE_RW);
549 open_fatal (archive_name_array[0]);
550 xdup2 (archive, STDIN_FILENO);
551 priv_set_restore_linkdir ();
552 run_decompress_program ();
555 /* We do need a grandchild tar. */
558 grandchild_pid = xfork ();
560 if (grandchild_pid == 0)
562 /* The newborn grandchild tar is here! Launch the uncompressor. */
564 set_program_name (_("tar (grandchild)"));
566 xdup2 (child_pipe[PREAD], STDIN_FILENO);
567 xclose (child_pipe[PWRITE]);
568 priv_set_restore_linkdir ();
569 run_decompress_program ();
572 /* The child tar is still here! */
574 /* Prepare for unblocking the data from the archive into the
577 xdup2 (child_pipe[PWRITE], STDOUT_FILENO);
578 xclose (child_pipe[PREAD]);
580 if (strcmp (archive_name_array[0], "-") == 0)
581 archive = STDIN_FILENO;
583 archive = rmtopen (archive_name_array[0], O_RDONLY | O_BINARY,
584 MODE_RW, rsh_command_option);
586 open_fatal (archive_name_array[0]);
588 /* Let's read the archive and pipe it into stdout. */
597 clear_read_error_count ();
600 status = rmtread (archive, record_start->buffer, record_size);
601 if (status == SAFE_READ_ERROR)
603 archive_read_error ();
608 cursor = record_start->buffer;
612 count = maximum < BLOCKSIZE ? maximum : BLOCKSIZE;
613 if (full_write (STDOUT_FILENO, cursor, count) != count)
614 write_error (use_compress_program_option);
620 xclose (STDOUT_FILENO);
622 wait_for_grandchild (grandchild_pid);
628 dec_to_env (char const *envar, uintmax_t num)
630 char buf[UINTMAX_STRSIZE_BOUND];
633 numstr = STRINGIFY_BIGINT (num, buf);
634 if (setenv (envar, numstr, 1) != 0)
639 time_to_env (char const *envar, struct timespec t)
641 char buf[TIMESPEC_STRSIZE_BOUND];
642 if (setenv (envar, code_timespec (t, buf), 1) != 0)
647 oct_to_env (char const *envar, unsigned long num)
649 char buf[1+1+(sizeof(unsigned long)*CHAR_BIT+2)/3];
651 snprintf (buf, sizeof buf, "0%lo", num);
652 if (setenv (envar, buf, 1) != 0)
657 str_to_env (char const *envar, char const *str)
661 if (setenv (envar, str, 1) != 0)
669 chr_to_env (char const *envar, char c)
674 if (setenv (envar, buf, 1) != 0)
679 stat_to_env (char *name, char type, struct tar_stat_info *st)
681 str_to_env ("TAR_VERSION", PACKAGE_VERSION);
682 str_to_env ("TAR_ARCHIVE", *archive_name_cursor);
683 dec_to_env ("TAR_VOLUME", archive_name_cursor - archive_name_array + 1);
684 dec_to_env ("TAR_BLOCKING_FACTOR", blocking_factor);
685 str_to_env ("TAR_FORMAT",
686 archive_format_string (current_format == DEFAULT_FORMAT ?
687 archive_format : current_format));
688 chr_to_env ("TAR_FILETYPE", type);
689 oct_to_env ("TAR_MODE", st->stat.st_mode);
690 str_to_env ("TAR_FILENAME", name);
691 str_to_env ("TAR_REALNAME", st->file_name);
692 str_to_env ("TAR_UNAME", st->uname);
693 str_to_env ("TAR_GNAME", st->gname);
694 time_to_env ("TAR_ATIME", st->atime);
695 time_to_env ("TAR_MTIME", st->mtime);
696 time_to_env ("TAR_CTIME", st->ctime);
697 dec_to_env ("TAR_SIZE", st->stat.st_size);
698 dec_to_env ("TAR_UID", st->stat.st_uid);
699 dec_to_env ("TAR_GID", st->stat.st_gid);
705 dec_to_env ("TAR_MINOR", minor (st->stat.st_rdev));
706 dec_to_env ("TAR_MAJOR", major (st->stat.st_rdev));
707 unsetenv ("TAR_LINKNAME");
712 unsetenv ("TAR_MINOR");
713 unsetenv ("TAR_MAJOR");
714 str_to_env ("TAR_LINKNAME", st->link_name);
718 unsetenv ("TAR_MINOR");
719 unsetenv ("TAR_MAJOR");
720 unsetenv ("TAR_LINKNAME");
725 static pid_t global_pid;
726 static void (*pipe_handler) (int sig);
729 sys_exec_command (char *file_name, int typechar, struct tar_stat_info *st)
734 pipe_handler = signal (SIGPIPE, SIG_IGN);
735 global_pid = xfork ();
744 xdup2 (p[PREAD], STDIN_FILENO);
747 stat_to_env (file_name, typechar, st);
749 priv_set_restore_linkdir ();
750 xexec (to_command_option);
754 sys_wait_command (void)
761 signal (SIGPIPE, pipe_handler);
762 while (waitpid (global_pid, &status, 0) == -1)
766 waitpid_error (to_command_option);
770 if (WIFEXITED (status))
772 if (!ignore_command_error_option && WEXITSTATUS (status))
773 ERROR ((0, 0, _("%lu: Child returned status %d"),
774 (unsigned long) global_pid, WEXITSTATUS (status)));
776 else if (WIFSIGNALED (status))
778 WARN ((0, 0, _("%lu: Child terminated on signal %d"),
779 (unsigned long) global_pid, WTERMSIG (status)));
782 ERROR ((0, 0, _("%lu: Child terminated on unknown reason"),
783 (unsigned long) global_pid));
789 sys_exec_info_script (const char **archive_name, int volume_number)
792 char uintbuf[UINTMAX_STRSIZE_BOUND];
794 static void (*saved_handler) (int sig);
797 saved_handler = signal (SIGPIPE, SIG_IGN);
812 fp = fdopen (p[PREAD], "r");
813 rc = getline (&buf, &size, fp);
816 if (rc > 0 && buf[rc-1] == '\n')
819 while (waitpid (pid, &status, 0) == -1)
822 signal (SIGPIPE, saved_handler);
823 waitpid_error (info_script_option);
827 signal (SIGPIPE, saved_handler);
829 if (WIFEXITED (status))
831 if (WEXITSTATUS (status) == 0 && rc > 0)
835 return WEXITSTATUS (status);
843 setenv ("TAR_VERSION", PACKAGE_VERSION, 1);
844 setenv ("TAR_ARCHIVE", *archive_name, 1);
845 setenv ("TAR_VOLUME", STRINGIFY_BIGINT (volume_number, uintbuf), 1);
846 setenv ("TAR_BLOCKING_FACTOR",
847 STRINGIFY_BIGINT (blocking_factor, uintbuf), 1);
848 setenv ("TAR_SUBCOMMAND", subcommand_string (subcommand_option), 1);
849 setenv ("TAR_FORMAT",
850 archive_format_string (current_format == DEFAULT_FORMAT ?
851 archive_format : current_format), 1);
852 setenv ("TAR_FD", STRINGIFY_BIGINT (p[PWRITE], uintbuf), 1);
856 priv_set_restore_linkdir ();
857 xexec (info_script_option);
861 sys_exec_checkpoint_script (const char *script_name,
862 const char *archive_name,
863 int checkpoint_number)
866 char uintbuf[UINTMAX_STRSIZE_BOUND];
876 while (waitpid (pid, &status, 0) == -1)
879 waitpid_error (script_name);
887 setenv ("TAR_VERSION", PACKAGE_VERSION, 1);
888 setenv ("TAR_ARCHIVE", archive_name, 1);
889 setenv ("TAR_CHECKPOINT", STRINGIFY_BIGINT (checkpoint_number, uintbuf), 1);
890 setenv ("TAR_BLOCKING_FACTOR",
891 STRINGIFY_BIGINT (blocking_factor, uintbuf), 1);
892 setenv ("TAR_SUBCOMMAND", subcommand_string (subcommand_option), 1);
893 setenv ("TAR_FORMAT",
894 archive_format_string (current_format == DEFAULT_FORMAT ?
895 archive_format : current_format), 1);
896 priv_set_restore_linkdir ();
900 #endif /* not MSDOS */