1 /* System-dependent calls for tar.
3 Copyright 2003-2008, 2010, 2013 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 3, or (at your option) any later
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13 Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include <wordsplit.h>
27 xexec (const char *cmd)
31 ws.ws_env = (const char **) environ;
32 if (wordsplit (cmd, &ws, (WRDSF_DEFFLAGS | WRDSF_ENV) & ~WRDSF_NOVAR))
33 FATAL_ERROR ((0, 0, _("cannot split string '%s': %s"),
34 cmd, wordsplit_strerror (&ws)));
35 execvp (ws.ws_wordv[0], ws.ws_wordv);
42 sys_get_archive_stat (void)
48 sys_file_is_archive (struct tar_stat_info *p)
54 sys_save_archive_dev_ino (void)
59 sys_detect_dev_null_output (void)
61 static char const dev_null[] = "nul";
63 dev_null_output = (strcmp (archive_name_array[0], dev_null) == 0
64 || (! _isrmt (archive)));
68 sys_wait_for_child (pid_t child_pid, bool eof)
73 sys_spawn_shell (void)
75 spawnl (P_WAIT, getenv ("COMSPEC"), "-", 0);
78 /* stat() in djgpp's C library gives a constant number of 42 as the
79 uid and gid of a file. So, comparing an FTP'ed archive just after
80 unpack would fail on MSDOS. */
83 sys_compare_uid (struct stat *a, struct stat *b)
89 sys_compare_gid (struct stat *a, struct stat *b)
95 sys_compare_links (struct stat *link_data, struct stat *stat_data)
101 sys_truncate (int fd)
103 return write (fd, "", 0);
107 sys_write_archive_buffer (void)
109 return full_write (archive, record_start->buffer, record_size);
112 /* Set ARCHIVE for writing, then compressing an archive. */
114 sys_child_open_for_compress (void)
116 FATAL_ERROR ((0, 0, _("Cannot use compressed or remote archives")));
119 /* Set ARCHIVE for uncompressing, then reading an archive. */
121 sys_child_open_for_uncompress (void)
123 FATAL_ERROR ((0, 0, _("Cannot use compressed or remote archives")));
128 extern union block *record_start; /* FIXME */
130 static struct stat archive_stat; /* stat block for archive file */
133 sys_get_archive_stat (void)
135 return fstat (archive, &archive_stat) == 0;
139 sys_file_is_archive (struct tar_stat_info *p)
141 return (ar_dev && p->stat.st_dev == ar_dev && p->stat.st_ino == ar_ino);
144 /* Save archive file inode and device numbers */
146 sys_save_archive_dev_ino (void)
148 if (!_isrmt (archive) && S_ISREG (archive_stat.st_mode))
150 ar_dev = archive_stat.st_dev;
151 ar_ino = archive_stat.st_ino;
157 /* Detect if outputting to "/dev/null". */
159 sys_detect_dev_null_output (void)
161 static char const dev_null[] = "/dev/null";
162 struct stat dev_null_stat;
164 dev_null_output = (strcmp (archive_name_array[0], dev_null) == 0
165 || (! _isrmt (archive)
166 && S_ISCHR (archive_stat.st_mode)
167 && stat (dev_null, &dev_null_stat) == 0
168 && archive_stat.st_dev == dev_null_stat.st_dev
169 && archive_stat.st_ino == dev_null_stat.st_ino));
173 sys_wait_for_child (pid_t child_pid, bool eof)
179 while (waitpid (child_pid, &wait_status, 0) == -1)
182 waitpid_error (use_compress_program_option);
186 if (WIFSIGNALED (wait_status))
188 int sig = WTERMSIG (wait_status);
189 if (!(!eof && sig == SIGPIPE))
190 FATAL_ERROR ((0, 0, _("Child died with signal %d"), sig));
192 else if (WEXITSTATUS (wait_status) != 0)
193 FATAL_ERROR ((0, 0, _("Child returned status %d"),
194 WEXITSTATUS (wait_status)));
199 sys_spawn_shell (void)
202 const char *shell = getenv ("SHELL");
208 priv_set_restore_linkdir ();
209 execlp (shell, "-sh", "-i", NULL);
215 while (waitpid (child, &wait_status, 0) == -1)
218 waitpid_error (shell);
225 sys_compare_uid (struct stat *a, struct stat *b)
227 return a->st_uid == b->st_uid;
231 sys_compare_gid (struct stat *a, struct stat *b)
233 return a->st_gid == b->st_gid;
237 sys_compare_links (struct stat *link_data, struct stat *stat_data)
239 return stat_data->st_dev == link_data->st_dev
240 && stat_data->st_ino == link_data->st_ino;
244 sys_truncate (int fd)
246 off_t pos = lseek (fd, (off_t) 0, SEEK_CUR);
247 return pos < 0 ? -1 : ftruncate (fd, pos);
250 /* Return nonzero if NAME is the name of a regular file, or if the file
251 does not exist (so it would be created as a regular file). */
253 is_regular_file (const char *name)
257 if (stat (name, &stbuf) == 0)
258 return S_ISREG (stbuf.st_mode);
260 return errno == ENOENT;
264 sys_write_archive_buffer (void)
266 return rmtwrite (archive, record_start->buffer, record_size);
269 #define PREAD 0 /* read file descriptor from pipe() */
270 #define PWRITE 1 /* write file descriptor from pipe() */
272 /* Duplicate file descriptor FROM into becoming INTO.
273 INTO is closed first and has to be the next available slot. */
275 xdup2 (int from, int into)
279 int status = close (into);
281 if (status != 0 && errno != EBADF)
284 FATAL_ERROR ((0, e, _("Cannot close")));
292 FATAL_ERROR ((0, e, _("Cannot dup")));
300 static void wait_for_grandchild (pid_t pid) __attribute__ ((__noreturn__));
302 /* Propagate any failure of the grandchild back to the parent. */
304 wait_for_grandchild (pid_t pid)
309 while (waitpid (pid, &wait_status, 0) == -1)
312 waitpid_error (use_compress_program_option);
316 if (WIFSIGNALED (wait_status))
317 raise (WTERMSIG (wait_status));
318 else if (WEXITSTATUS (wait_status) != 0)
319 exit_code = WEXITSTATUS (wait_status);
324 /* Set ARCHIVE for writing, then compressing an archive. */
326 sys_child_open_for_compress (void)
330 pid_t grandchild_pid;
334 child_pid = xfork ();
338 /* The parent tar is still here! Just clean up. */
340 archive = parent_pipe[PWRITE];
341 xclose (parent_pipe[PREAD]);
345 /* The new born child tar is here! */
347 set_program_name (_("tar (child)"));
348 signal (SIGPIPE, SIG_DFL);
350 xdup2 (parent_pipe[PREAD], STDIN_FILENO);
351 xclose (parent_pipe[PWRITE]);
353 /* Check if we need a grandchild tar. This happens only if either:
354 a) the file is to be accessed by rmt: compressor doesn't know how;
355 b) the file is not a plain file. */
357 if (!_remdev (archive_name_array[0])
358 && is_regular_file (archive_name_array[0]))
361 maybe_backup_file (archive_name_array[0], 1);
363 /* We don't need a grandchild tar. Open the archive and launch the
365 if (strcmp (archive_name_array[0], "-"))
367 archive = creat (archive_name_array[0], MODE_RW);
370 int saved_errno = errno;
375 open_fatal (archive_name_array[0]);
377 xdup2 (archive, STDOUT_FILENO);
379 priv_set_restore_linkdir ();
380 xexec (use_compress_program_option);
383 /* We do need a grandchild tar. */
386 grandchild_pid = xfork ();
388 if (grandchild_pid == 0)
390 /* The newborn grandchild tar is here! Launch the compressor. */
392 set_program_name (_("tar (grandchild)"));
394 xdup2 (child_pipe[PWRITE], STDOUT_FILENO);
395 xclose (child_pipe[PREAD]);
396 priv_set_restore_linkdir ();
397 xexec (use_compress_program_option);
400 /* The child tar is still here! */
402 /* Prepare for reblocking the data from the compressor into the archive. */
404 xdup2 (child_pipe[PREAD], STDIN_FILENO);
405 xclose (child_pipe[PWRITE]);
407 if (strcmp (archive_name_array[0], "-") == 0)
408 archive = STDOUT_FILENO;
411 archive = rmtcreat (archive_name_array[0], MODE_RW, rsh_command_option);
413 open_fatal (archive_name_array[0]);
416 /* Let's read out of the stdin pipe and write an archive. */
424 /* Assemble a record. */
426 for (length = 0, cursor = record_start->buffer;
427 length < record_size;
428 length += status, cursor += status)
430 size_t size = record_size - length;
432 status = safe_read (STDIN_FILENO, cursor, size);
433 if (status == SAFE_READ_ERROR)
434 read_fatal (use_compress_program_option);
439 /* Copy the record. */
443 /* We hit the end of the file. Write last record at
444 full length, as the only role of the grandchild is
445 doing proper reblocking. */
449 memset (record_start->buffer + length, 0, record_size - length);
450 status = sys_write_archive_buffer ();
451 if (status != record_size)
452 archive_write_error (status);
455 /* There is nothing else to read, break out. */
459 status = sys_write_archive_buffer ();
460 if (status != record_size)
461 archive_write_error (status);
464 wait_for_grandchild (grandchild_pid);
468 run_decompress_program (void)
471 const char *p, *prog = NULL;
473 int wsflags = (WRDSF_DEFFLAGS | WRDSF_ENV | WRDSF_DOOFFS) & ~WRDSF_NOVAR;
475 ws.ws_env = (const char **) environ;
478 for (p = first_decompress_program (&i); p; p = next_decompress_program (&i))
482 WARNOPT (WARN_DECOMPRESS_PROGRAM,
483 (0, errno, _("cannot run %s"), prog));
484 WARNOPT (WARN_DECOMPRESS_PROGRAM,
485 (0, 0, _("trying %s"), p));
487 if (wordsplit (p, &ws, wsflags))
488 FATAL_ERROR ((0, 0, _("cannot split string '%s': %s"),
489 p, wordsplit_strerror (&ws)));
490 wsflags |= WRDSF_REUSE;
491 memmove(ws.ws_wordv, ws.ws_wordv + ws.ws_offs,
492 sizeof(ws.ws_wordv[0])*ws.ws_wordc);
493 ws.ws_wordv[ws.ws_wordc] = (char *) "-d";
495 execvp (ws.ws_wordv[0], ws.ws_wordv);
496 ws.ws_wordv[ws.ws_wordc] = NULL;
499 FATAL_ERROR ((0, 0, _("unable to run decompression program")));
503 /* Set ARCHIVE for uncompressing, then reading an archive. */
505 sys_child_open_for_uncompress (void)
509 pid_t grandchild_pid;
513 child_pid = xfork ();
517 /* The parent tar is still here! Just clean up. */
519 archive = parent_pipe[PREAD];
520 xclose (parent_pipe[PWRITE]);
524 /* The newborn child tar is here! */
526 set_program_name (_("tar (child)"));
527 signal (SIGPIPE, SIG_DFL);
529 xdup2 (parent_pipe[PWRITE], STDOUT_FILENO);
530 xclose (parent_pipe[PREAD]);
532 /* Check if we need a grandchild tar. This happens only if either:
533 a) we're reading stdin: to force unblocking;
534 b) the file is to be accessed by rmt: compressor doesn't know how;
535 c) the file is not a plain file. */
537 if (strcmp (archive_name_array[0], "-") != 0
538 && !_remdev (archive_name_array[0])
539 && is_regular_file (archive_name_array[0]))
541 /* We don't need a grandchild tar. Open the archive and lauch the
544 archive = open (archive_name_array[0], O_RDONLY | O_BINARY, MODE_RW);
546 open_fatal (archive_name_array[0]);
547 xdup2 (archive, STDIN_FILENO);
548 priv_set_restore_linkdir ();
549 run_decompress_program ();
552 /* We do need a grandchild tar. */
555 grandchild_pid = xfork ();
557 if (grandchild_pid == 0)
559 /* The newborn grandchild tar is here! Launch the uncompressor. */
561 set_program_name (_("tar (grandchild)"));
563 xdup2 (child_pipe[PREAD], STDIN_FILENO);
564 xclose (child_pipe[PWRITE]);
565 priv_set_restore_linkdir ();
566 run_decompress_program ();
569 /* The child tar is still here! */
571 /* Prepare for unblocking the data from the archive into the
574 xdup2 (child_pipe[PWRITE], STDOUT_FILENO);
575 xclose (child_pipe[PREAD]);
577 if (strcmp (archive_name_array[0], "-") == 0)
578 archive = STDIN_FILENO;
580 archive = rmtopen (archive_name_array[0], O_RDONLY | O_BINARY,
581 MODE_RW, rsh_command_option);
583 open_fatal (archive_name_array[0]);
585 /* Let's read the archive and pipe it into stdout. */
594 clear_read_error_count ();
597 status = rmtread (archive, record_start->buffer, record_size);
598 if (status == SAFE_READ_ERROR)
600 archive_read_error ();
605 cursor = record_start->buffer;
609 count = maximum < BLOCKSIZE ? maximum : BLOCKSIZE;
610 if (full_write (STDOUT_FILENO, cursor, count) != count)
611 write_error (use_compress_program_option);
617 xclose (STDOUT_FILENO);
619 wait_for_grandchild (grandchild_pid);
625 dec_to_env (char const *envar, uintmax_t num)
627 char buf[UINTMAX_STRSIZE_BOUND];
630 numstr = STRINGIFY_BIGINT (num, buf);
631 if (setenv (envar, numstr, 1) != 0)
636 time_to_env (char const *envar, struct timespec t)
638 char buf[TIMESPEC_STRSIZE_BOUND];
639 if (setenv (envar, code_timespec (t, buf), 1) != 0)
644 oct_to_env (char const *envar, unsigned long num)
646 char buf[1+1+(sizeof(unsigned long)*CHAR_BIT+2)/3];
648 snprintf (buf, sizeof buf, "0%lo", num);
649 if (setenv (envar, buf, 1) != 0)
654 str_to_env (char const *envar, char const *str)
658 if (setenv (envar, str, 1) != 0)
666 chr_to_env (char const *envar, char c)
671 if (setenv (envar, buf, 1) != 0)
676 stat_to_env (char *name, char type, struct tar_stat_info *st)
678 str_to_env ("TAR_VERSION", PACKAGE_VERSION);
679 str_to_env ("TAR_ARCHIVE", *archive_name_cursor);
680 dec_to_env ("TAR_VOLUME", archive_name_cursor - archive_name_array + 1);
681 dec_to_env ("TAR_BLOCKING_FACTOR", blocking_factor);
682 str_to_env ("TAR_FORMAT",
683 archive_format_string (current_format == DEFAULT_FORMAT ?
684 archive_format : current_format));
685 chr_to_env ("TAR_FILETYPE", type);
686 oct_to_env ("TAR_MODE", st->stat.st_mode);
687 str_to_env ("TAR_FILENAME", name);
688 str_to_env ("TAR_REALNAME", st->file_name);
689 str_to_env ("TAR_UNAME", st->uname);
690 str_to_env ("TAR_GNAME", st->gname);
691 time_to_env ("TAR_ATIME", st->atime);
692 time_to_env ("TAR_MTIME", st->mtime);
693 time_to_env ("TAR_CTIME", st->ctime);
694 dec_to_env ("TAR_SIZE", st->stat.st_size);
695 dec_to_env ("TAR_UID", st->stat.st_uid);
696 dec_to_env ("TAR_GID", st->stat.st_gid);
702 dec_to_env ("TAR_MINOR", minor (st->stat.st_rdev));
703 dec_to_env ("TAR_MAJOR", major (st->stat.st_rdev));
704 unsetenv ("TAR_LINKNAME");
709 unsetenv ("TAR_MINOR");
710 unsetenv ("TAR_MAJOR");
711 str_to_env ("TAR_LINKNAME", st->link_name);
715 unsetenv ("TAR_MINOR");
716 unsetenv ("TAR_MAJOR");
717 unsetenv ("TAR_LINKNAME");
722 static pid_t global_pid;
723 static RETSIGTYPE (*pipe_handler) (int sig);
726 sys_exec_command (char *file_name, int typechar, struct tar_stat_info *st)
731 pipe_handler = signal (SIGPIPE, SIG_IGN);
732 global_pid = xfork ();
741 xdup2 (p[PREAD], STDIN_FILENO);
744 stat_to_env (file_name, typechar, st);
746 priv_set_restore_linkdir ();
747 xexec (to_command_option);
751 sys_wait_command (void)
758 signal (SIGPIPE, pipe_handler);
759 while (waitpid (global_pid, &status, 0) == -1)
763 waitpid_error (to_command_option);
767 if (WIFEXITED (status))
769 if (!ignore_command_error_option && WEXITSTATUS (status))
770 ERROR ((0, 0, _("%lu: Child returned status %d"),
771 (unsigned long) global_pid, WEXITSTATUS (status)));
773 else if (WIFSIGNALED (status))
775 WARN ((0, 0, _("%lu: Child terminated on signal %d"),
776 (unsigned long) global_pid, WTERMSIG (status)));
779 ERROR ((0, 0, _("%lu: Child terminated on unknown reason"),
780 (unsigned long) global_pid));
786 sys_exec_info_script (const char **archive_name, int volume_number)
789 char uintbuf[UINTMAX_STRSIZE_BOUND];
791 static RETSIGTYPE (*saved_handler) (int sig);
794 saved_handler = signal (SIGPIPE, SIG_IGN);
809 fp = fdopen (p[PREAD], "r");
810 rc = getline (&buf, &size, fp);
813 if (rc > 0 && buf[rc-1] == '\n')
816 while (waitpid (pid, &status, 0) == -1)
819 signal (SIGPIPE, saved_handler);
820 waitpid_error (info_script_option);
824 signal (SIGPIPE, saved_handler);
826 if (WIFEXITED (status))
828 if (WEXITSTATUS (status) == 0 && rc > 0)
832 return WEXITSTATUS (status);
840 setenv ("TAR_VERSION", PACKAGE_VERSION, 1);
841 setenv ("TAR_ARCHIVE", *archive_name, 1);
842 setenv ("TAR_VOLUME", STRINGIFY_BIGINT (volume_number, uintbuf), 1);
843 setenv ("TAR_BLOCKING_FACTOR",
844 STRINGIFY_BIGINT (blocking_factor, uintbuf), 1);
845 setenv ("TAR_SUBCOMMAND", subcommand_string (subcommand_option), 1);
846 setenv ("TAR_FORMAT",
847 archive_format_string (current_format == DEFAULT_FORMAT ?
848 archive_format : current_format), 1);
849 setenv ("TAR_FD", STRINGIFY_BIGINT (p[PWRITE], uintbuf), 1);
853 priv_set_restore_linkdir ();
854 xexec (info_script_option);
858 sys_exec_checkpoint_script (const char *script_name,
859 const char *archive_name,
860 int checkpoint_number)
863 char uintbuf[UINTMAX_STRSIZE_BOUND];
873 while (waitpid (pid, &status, 0) == -1)
876 waitpid_error (script_name);
884 setenv ("TAR_VERSION", PACKAGE_VERSION, 1);
885 setenv ("TAR_ARCHIVE", archive_name, 1);
886 setenv ("TAR_CHECKPOINT", STRINGIFY_BIGINT (checkpoint_number, uintbuf), 1);
887 setenv ("TAR_BLOCKING_FACTOR",
888 STRINGIFY_BIGINT (blocking_factor, uintbuf), 1);
889 setenv ("TAR_SUBCOMMAND", subcommand_string (subcommand_option), 1);
890 setenv ("TAR_FORMAT",
891 archive_format_string (current_format == DEFAULT_FORMAT ?
892 archive_format : current_format), 1);
893 priv_set_restore_linkdir ();
897 #endif /* not MSDOS */