1 /* System-dependent calls for tar.
3 Copyright (C) 2003, 2004, 2005, 2006, 2007,
4 2008 Free Software Foundation, Inc.
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, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
29 sys_get_archive_stat (void)
35 sys_file_is_archive (struct tar_stat_info *p)
41 sys_save_archive_dev_ino (void)
46 sys_detect_dev_null_output (void)
48 static char const dev_null[] = "nul";
50 dev_null_output = (strcmp (archive_name_array[0], dev_null) == 0
51 || (! _isrmt (archive)));
55 sys_wait_for_child (pid_t child_pid, bool eof)
60 sys_spawn_shell (void)
62 spawnl (P_WAIT, getenv ("COMSPEC"), "-", 0);
65 /* stat() in djgpp's C library gives a constant number of 42 as the
66 uid and gid of a file. So, comparing an FTP'ed archive just after
67 unpack would fail on MSDOS. */
70 sys_compare_uid (struct stat *a, struct stat *b)
76 sys_compare_gid (struct stat *a, struct stat *b)
82 sys_compare_links (struct stat *link_data, struct stat *stat_data)
90 return write (fd, "", 0);
94 sys_write_archive_buffer (void)
96 return full_write (archive, record_start->buffer, record_size);
99 /* Set ARCHIVE for writing, then compressing an archive. */
101 sys_child_open_for_compress (void)
103 FATAL_ERROR ((0, 0, _("Cannot use compressed or remote archives")));
106 /* Set ARCHIVE for uncompressing, then reading an archive. */
108 sys_child_open_for_uncompress (void)
110 FATAL_ERROR ((0, 0, _("Cannot use compressed or remote archives")));
115 extern union block *record_start; /* FIXME */
117 static struct stat archive_stat; /* stat block for archive file */
120 sys_get_archive_stat (void)
122 return fstat (archive, &archive_stat) == 0;
126 sys_file_is_archive (struct tar_stat_info *p)
128 return (ar_dev && p->stat.st_dev == ar_dev && p->stat.st_ino == ar_ino);
131 /* Save archive file inode and device numbers */
133 sys_save_archive_dev_ino (void)
135 if (!_isrmt (archive) && S_ISREG (archive_stat.st_mode))
137 ar_dev = archive_stat.st_dev;
138 ar_ino = archive_stat.st_ino;
144 /* Detect if outputting to "/dev/null". */
146 sys_detect_dev_null_output (void)
148 static char const dev_null[] = "/dev/null";
149 struct stat dev_null_stat;
151 dev_null_output = (strcmp (archive_name_array[0], dev_null) == 0
152 || (! _isrmt (archive)
153 && S_ISCHR (archive_stat.st_mode)
154 && stat (dev_null, &dev_null_stat) == 0
155 && archive_stat.st_dev == dev_null_stat.st_dev
156 && archive_stat.st_ino == dev_null_stat.st_ino));
160 sys_wait_for_child (pid_t child_pid, bool eof)
166 while (waitpid (child_pid, &wait_status, 0) == -1)
169 waitpid_error (use_compress_program_option);
173 if (WIFSIGNALED (wait_status))
175 int sig = WTERMSIG (wait_status);
176 if (!(!eof && sig == SIGPIPE))
177 FATAL_ERROR ((0, 0, _("Child died with signal %d"), sig));
179 else if (WEXITSTATUS (wait_status) != 0)
180 FATAL_ERROR ((0, 0, _("Child returned status %d"),
181 WEXITSTATUS (wait_status)));
186 sys_spawn_shell (void)
189 const char *shell = getenv ("SHELL");
195 execlp (shell, "-sh", "-i", (char *) 0);
201 while (waitpid (child, &wait_status, 0) == -1)
204 waitpid_error (shell);
211 sys_compare_uid (struct stat *a, struct stat *b)
213 return a->st_uid == b->st_uid;
217 sys_compare_gid (struct stat *a, struct stat *b)
219 return a->st_gid == b->st_gid;
223 sys_compare_links (struct stat *link_data, struct stat *stat_data)
225 return stat_data->st_dev == link_data->st_dev
226 && stat_data->st_ino == link_data->st_ino;
230 sys_truncate (int fd)
232 off_t pos = lseek (fd, (off_t) 0, SEEK_CUR);
233 return pos < 0 ? -1 : ftruncate (fd, pos);
236 /* Return nonzero if NAME is the name of a regular file, or if the file
237 does not exist (so it would be created as a regular file). */
239 is_regular_file (const char *name)
243 if (stat (name, &stbuf) == 0)
244 return S_ISREG (stbuf.st_mode);
246 return errno == ENOENT;
250 sys_write_archive_buffer (void)
252 return rmtwrite (archive, record_start->buffer, record_size);
255 #define PREAD 0 /* read file descriptor from pipe() */
256 #define PWRITE 1 /* write file descriptor from pipe() */
258 /* Duplicate file descriptor FROM into becoming INTO.
259 INTO is closed first and has to be the next available slot. */
261 xdup2 (int from, int into)
265 int status = close (into);
267 if (status != 0 && errno != EBADF)
270 FATAL_ERROR ((0, e, _("Cannot close")));
278 FATAL_ERROR ((0, e, _("Cannot dup")));
286 void wait_for_grandchild (pid_t pid) __attribute__ ((__noreturn__));
288 /* Propagate any failure of the grandchild back to the parent. */
290 wait_for_grandchild (pid_t pid)
295 while (waitpid (pid, &wait_status, 0) == -1)
298 waitpid_error (use_compress_program_option);
302 if (WIFSIGNALED (wait_status))
303 raise (WTERMSIG (wait_status));
304 else if (WEXITSTATUS (wait_status) != 0)
305 exit_code = WEXITSTATUS (wait_status);
310 /* Set ARCHIVE for writing, then compressing an archive. */
312 sys_child_open_for_compress (void)
316 pid_t grandchild_pid;
320 child_pid = xfork ();
324 /* The parent tar is still here! Just clean up. */
326 archive = parent_pipe[PWRITE];
327 xclose (parent_pipe[PREAD]);
331 /* The new born child tar is here! */
333 set_program_name (_("tar (child)"));
334 signal (SIGPIPE, SIG_DFL);
336 xdup2 (parent_pipe[PREAD], STDIN_FILENO);
337 xclose (parent_pipe[PWRITE]);
339 /* Check if we need a grandchild tar. This happens only if either:
340 a) the file is to be accessed by rmt: compressor doesn't know how;
341 b) the file is not a plain file. */
343 if (!_remdev (archive_name_array[0])
344 && is_regular_file (archive_name_array[0]))
347 maybe_backup_file (archive_name_array[0], 1);
349 /* We don't need a grandchild tar. Open the archive and launch the
351 if (strcmp (archive_name_array[0], "-"))
353 archive = creat (archive_name_array[0], MODE_RW);
356 int saved_errno = errno;
361 open_fatal (archive_name_array[0]);
363 xdup2 (archive, STDOUT_FILENO);
365 execlp (use_compress_program_option, use_compress_program_option, NULL);
366 exec_fatal (use_compress_program_option);
369 /* We do need a grandchild tar. */
372 grandchild_pid = xfork ();
374 if (grandchild_pid == 0)
376 /* The newborn grandchild tar is here! Launch the compressor. */
378 set_program_name (_("tar (grandchild)"));
380 xdup2 (child_pipe[PWRITE], STDOUT_FILENO);
381 xclose (child_pipe[PREAD]);
382 execlp (use_compress_program_option, use_compress_program_option,
384 exec_fatal (use_compress_program_option);
387 /* The child tar is still here! */
389 /* Prepare for reblocking the data from the compressor into the archive. */
391 xdup2 (child_pipe[PREAD], STDIN_FILENO);
392 xclose (child_pipe[PWRITE]);
394 if (strcmp (archive_name_array[0], "-") == 0)
395 archive = STDOUT_FILENO;
398 archive = rmtcreat (archive_name_array[0], MODE_RW, rsh_command_option);
400 open_fatal (archive_name_array[0]);
403 /* Let's read out of the stdin pipe and write an archive. */
411 /* Assemble a record. */
413 for (length = 0, cursor = record_start->buffer;
414 length < record_size;
415 length += status, cursor += status)
417 size_t size = record_size - length;
419 status = safe_read (STDIN_FILENO, cursor, size);
420 if (status == SAFE_READ_ERROR)
421 read_fatal (use_compress_program_option);
426 /* Copy the record. */
430 /* We hit the end of the file. Write last record at
431 full length, as the only role of the grandchild is
432 doing proper reblocking. */
436 memset (record_start->buffer + length, 0, record_size - length);
437 status = sys_write_archive_buffer ();
438 if (status != record_size)
439 archive_write_error (status);
442 /* There is nothing else to read, break out. */
446 status = sys_write_archive_buffer ();
447 if (status != record_size)
448 archive_write_error (status);
451 wait_for_grandchild (grandchild_pid);
454 /* Set ARCHIVE for uncompressing, then reading an archive. */
456 sys_child_open_for_uncompress (void)
460 pid_t grandchild_pid;
464 child_pid = xfork ();
468 /* The parent tar is still here! Just clean up. */
470 archive = parent_pipe[PREAD];
471 xclose (parent_pipe[PWRITE]);
475 /* The newborn child tar is here! */
477 set_program_name (_("tar (child)"));
478 signal (SIGPIPE, SIG_DFL);
480 xdup2 (parent_pipe[PWRITE], STDOUT_FILENO);
481 xclose (parent_pipe[PREAD]);
483 /* Check if we need a grandchild tar. This happens only if either:
484 a) we're reading stdin: to force unblocking;
485 b) the file is to be accessed by rmt: compressor doesn't know how;
486 c) the file is not a plain file. */
488 if (strcmp (archive_name_array[0], "-") != 0
489 && !_remdev (archive_name_array[0])
490 && is_regular_file (archive_name_array[0]))
492 /* We don't need a grandchild tar. Open the archive and lauch the
495 archive = open (archive_name_array[0], O_RDONLY | O_BINARY, MODE_RW);
497 open_fatal (archive_name_array[0]);
498 xdup2 (archive, STDIN_FILENO);
499 execlp (use_compress_program_option, use_compress_program_option,
501 exec_fatal (use_compress_program_option);
504 /* We do need a grandchild tar. */
507 grandchild_pid = xfork ();
509 if (grandchild_pid == 0)
511 /* The newborn grandchild tar is here! Launch the uncompressor. */
513 set_program_name (_("tar (grandchild)"));
515 xdup2 (child_pipe[PREAD], STDIN_FILENO);
516 xclose (child_pipe[PWRITE]);
517 execlp (use_compress_program_option, use_compress_program_option,
519 exec_fatal (use_compress_program_option);
522 /* The child tar is still here! */
524 /* Prepare for unblocking the data from the archive into the
527 xdup2 (child_pipe[PWRITE], STDOUT_FILENO);
528 xclose (child_pipe[PREAD]);
530 if (strcmp (archive_name_array[0], "-") == 0)
531 archive = STDIN_FILENO;
533 archive = rmtopen (archive_name_array[0], O_RDONLY | O_BINARY,
534 MODE_RW, rsh_command_option);
536 open_fatal (archive_name_array[0]);
538 /* Let's read the archive and pipe it into stdout. */
547 clear_read_error_count ();
550 status = rmtread (archive, record_start->buffer, record_size);
551 if (status == SAFE_READ_ERROR)
553 archive_read_error ();
558 cursor = record_start->buffer;
562 count = maximum < BLOCKSIZE ? maximum : BLOCKSIZE;
563 if (full_write (STDOUT_FILENO, cursor, count) != count)
564 write_error (use_compress_program_option);
570 xclose (STDOUT_FILENO);
572 wait_for_grandchild (grandchild_pid);
578 dec_to_env (char *envar, uintmax_t num)
580 char buf[UINTMAX_STRSIZE_BOUND];
583 numstr = STRINGIFY_BIGINT (num, buf);
584 if (setenv (envar, numstr, 1) != 0)
589 time_to_env (char *envar, struct timespec t)
591 char buf[TIMESPEC_STRSIZE_BOUND];
592 if (setenv (envar, code_timespec (t, buf), 1) != 0)
597 oct_to_env (char *envar, unsigned long num)
599 char buf[1+1+(sizeof(unsigned long)*CHAR_BIT+2)/3];
601 snprintf (buf, sizeof buf, "0%lo", num);
602 if (setenv (envar, buf, 1) != 0)
607 str_to_env (char *envar, char const *str)
611 if (setenv (envar, str, 1) != 0)
619 chr_to_env (char *envar, char c)
624 if (setenv (envar, buf, 1) != 0)
629 stat_to_env (char *name, char type, struct tar_stat_info *st)
631 str_to_env ("TAR_VERSION", PACKAGE_VERSION);
632 str_to_env ("TAR_ARCHIVE", *archive_name_cursor);
633 dec_to_env ("TAR_VOLUME", archive_name_cursor - archive_name_array + 1);
634 dec_to_env ("TAR_BLOCKING_FACTOR", blocking_factor);
635 str_to_env ("TAR_FORMAT",
636 archive_format_string (current_format == DEFAULT_FORMAT ?
637 archive_format : current_format));
638 chr_to_env ("TAR_FILETYPE", type);
639 oct_to_env ("TAR_MODE", st->stat.st_mode);
640 str_to_env ("TAR_FILENAME", name);
641 str_to_env ("TAR_REALNAME", st->file_name);
642 str_to_env ("TAR_UNAME", st->uname);
643 str_to_env ("TAR_GNAME", st->gname);
644 time_to_env ("TAR_ATIME", st->atime);
645 time_to_env ("TAR_MTIME", st->mtime);
646 time_to_env ("TAR_CTIME", st->ctime);
647 dec_to_env ("TAR_SIZE", st->stat.st_size);
648 dec_to_env ("TAR_UID", st->stat.st_uid);
649 dec_to_env ("TAR_GID", st->stat.st_gid);
655 dec_to_env ("TAR_MINOR", minor (st->stat.st_rdev));
656 dec_to_env ("TAR_MAJOR", major (st->stat.st_rdev));
657 unsetenv ("TAR_LINKNAME");
662 unsetenv ("TAR_MINOR");
663 unsetenv ("TAR_MAJOR");
664 str_to_env ("TAR_LINKNAME", st->link_name);
668 unsetenv ("TAR_MINOR");
669 unsetenv ("TAR_MAJOR");
670 unsetenv ("TAR_LINKNAME");
675 static pid_t global_pid;
676 static RETSIGTYPE (*pipe_handler) (int sig);
679 sys_exec_command (char *file_name, int typechar, struct tar_stat_info *st)
685 pipe_handler = signal (SIGPIPE, SIG_IGN);
686 global_pid = xfork ();
695 xdup2 (p[PREAD], STDIN_FILENO);
698 stat_to_env (file_name, typechar, st);
702 argv[2] = to_command_option;
705 execv ("/bin/sh", argv);
707 exec_fatal (file_name);
711 sys_wait_command (void)
718 signal (SIGPIPE, pipe_handler);
719 while (waitpid (global_pid, &status, 0) == -1)
723 waitpid_error (to_command_option);
727 if (WIFEXITED (status))
729 if (!ignore_command_error_option && WEXITSTATUS (status))
730 ERROR ((0, 0, _("%lu: Child returned status %d"),
731 (unsigned long) global_pid, WEXITSTATUS (status)));
733 else if (WIFSIGNALED (status))
735 WARN ((0, 0, _("%lu: Child terminated on signal %d"),
736 (unsigned long) global_pid, WTERMSIG (status)));
739 ERROR ((0, 0, _("%lu: Child terminated on unknown reason"),
740 (unsigned long) global_pid));
746 sys_exec_info_script (const char **archive_name, int volume_number)
750 char uintbuf[UINTMAX_STRSIZE_BOUND];
752 static RETSIGTYPE (*saved_handler) (int sig);
755 saved_handler = signal (SIGPIPE, SIG_IGN);
770 fp = fdopen (p[PREAD], "r");
771 rc = getline (&buf, &size, fp);
774 if (rc > 0 && buf[rc-1] == '\n')
777 while (waitpid (pid, &status, 0) == -1)
780 signal (SIGPIPE, saved_handler);
781 waitpid_error (info_script_option);
785 signal (SIGPIPE, saved_handler);
787 if (WIFEXITED (status))
789 if (WEXITSTATUS (status) == 0 && rc > 0)
793 return WEXITSTATUS (status);
801 setenv ("TAR_VERSION", PACKAGE_VERSION, 1);
802 setenv ("TAR_ARCHIVE", *archive_name, 1);
803 setenv ("TAR_VOLUME", STRINGIFY_BIGINT (volume_number, uintbuf), 1);
804 setenv ("TAR_BLOCKING_FACTOR",
805 STRINGIFY_BIGINT (blocking_factor, uintbuf), 1);
806 setenv ("TAR_SUBCOMMAND", subcommand_string (subcommand_option), 1);
807 setenv ("TAR_FORMAT",
808 archive_format_string (current_format == DEFAULT_FORMAT ?
809 archive_format : current_format), 1);
810 setenv ("TAR_FD", STRINGIFY_BIGINT (p[PWRITE], uintbuf), 1);
816 argv[2] = (char*) info_script_option;
819 execv (argv[0], argv);
821 exec_fatal (info_script_option);
825 sys_exec_checkpoint_script (const char *script_name,
826 const char *archive_name,
827 int checkpoint_number)
831 char uintbuf[UINTMAX_STRSIZE_BOUND];
841 while (waitpid (pid, &status, 0) == -1)
844 waitpid_error (script_name);
852 setenv ("TAR_VERSION", PACKAGE_VERSION, 1);
853 setenv ("TAR_ARCHIVE", archive_name, 1);
854 setenv ("TAR_CHECKPOINT", STRINGIFY_BIGINT (checkpoint_number, uintbuf), 1);
855 setenv ("TAR_BLOCKING_FACTOR",
856 STRINGIFY_BIGINT (blocking_factor, uintbuf), 1);
857 setenv ("TAR_SUBCOMMAND", subcommand_string (subcommand_option), 1);
858 setenv ("TAR_FORMAT",
859 archive_format_string (current_format == DEFAULT_FORMAT ?
860 archive_format : current_format), 1);
863 argv[2] = (char*) script_name;
866 execv (argv[0], argv);
868 exec_fatal (script_name);
871 #endif /* not MSDOS */