1 /* System-dependent calls for tar.
3 Copyright (C) 2003, 2004, 2005, 2006, 2007,
4 2008, 2010 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. */
30 sys_get_archive_stat (void)
36 sys_file_is_archive (struct tar_stat_info
*p
)
42 sys_save_archive_dev_ino (void)
47 sys_detect_dev_null_output (void)
49 static char const dev_null
[] = "nul";
51 dev_null_output
= (strcmp (archive_name_array
[0], dev_null
) == 0
52 || (! _isrmt (archive
)));
56 sys_wait_for_child (pid_t child_pid
, bool eof
)
61 sys_spawn_shell (void)
63 spawnl (P_WAIT
, getenv ("COMSPEC"), "-", 0);
66 /* stat() in djgpp's C library gives a constant number of 42 as the
67 uid and gid of a file. So, comparing an FTP'ed archive just after
68 unpack would fail on MSDOS. */
71 sys_compare_uid (struct stat
*a
, struct stat
*b
)
77 sys_compare_gid (struct stat
*a
, struct stat
*b
)
83 sys_compare_links (struct stat
*link_data
, struct stat
*stat_data
)
91 return write (fd
, "", 0);
95 sys_write_archive_buffer (void)
97 return full_write (archive
, record_start
->buffer
, record_size
);
100 /* Set ARCHIVE for writing, then compressing an archive. */
102 sys_child_open_for_compress (void)
104 FATAL_ERROR ((0, 0, _("Cannot use compressed or remote archives")));
107 /* Set ARCHIVE for uncompressing, then reading an archive. */
109 sys_child_open_for_uncompress (void)
111 FATAL_ERROR ((0, 0, _("Cannot use compressed or remote archives")));
116 extern union block
*record_start
; /* FIXME */
118 static struct stat archive_stat
; /* stat block for archive file */
121 sys_get_archive_stat (void)
123 return fstat (archive
, &archive_stat
) == 0;
127 sys_file_is_archive (struct tar_stat_info
*p
)
129 return (ar_dev
&& p
->stat
.st_dev
== ar_dev
&& p
->stat
.st_ino
== ar_ino
);
132 /* Save archive file inode and device numbers */
134 sys_save_archive_dev_ino (void)
136 if (!_isrmt (archive
) && S_ISREG (archive_stat
.st_mode
))
138 ar_dev
= archive_stat
.st_dev
;
139 ar_ino
= archive_stat
.st_ino
;
145 /* Detect if outputting to "/dev/null". */
147 sys_detect_dev_null_output (void)
149 static char const dev_null
[] = "/dev/null";
150 struct stat dev_null_stat
;
152 dev_null_output
= (strcmp (archive_name_array
[0], dev_null
) == 0
153 || (! _isrmt (archive
)
154 && S_ISCHR (archive_stat
.st_mode
)
155 && stat (dev_null
, &dev_null_stat
) == 0
156 && archive_stat
.st_dev
== dev_null_stat
.st_dev
157 && archive_stat
.st_ino
== dev_null_stat
.st_ino
));
161 sys_wait_for_child (pid_t child_pid
, bool eof
)
167 while (waitpid (child_pid
, &wait_status
, 0) == -1)
170 waitpid_error (use_compress_program_option
);
174 if (WIFSIGNALED (wait_status
))
176 int sig
= WTERMSIG (wait_status
);
177 if (!(!eof
&& sig
== SIGPIPE
))
178 FATAL_ERROR ((0, 0, _("Child died with signal %d"), sig
));
180 else if (WEXITSTATUS (wait_status
) != 0)
181 FATAL_ERROR ((0, 0, _("Child returned status %d"),
182 WEXITSTATUS (wait_status
)));
187 sys_spawn_shell (void)
190 const char *shell
= getenv ("SHELL");
196 priv_set_restore_linkdir ();
197 execlp (shell
, "-sh", "-i", (char *) 0);
203 while (waitpid (child
, &wait_status
, 0) == -1)
206 waitpid_error (shell
);
213 sys_compare_uid (struct stat
*a
, struct stat
*b
)
215 return a
->st_uid
== b
->st_uid
;
219 sys_compare_gid (struct stat
*a
, struct stat
*b
)
221 return a
->st_gid
== b
->st_gid
;
225 sys_compare_links (struct stat
*link_data
, struct stat
*stat_data
)
227 return stat_data
->st_dev
== link_data
->st_dev
228 && stat_data
->st_ino
== link_data
->st_ino
;
232 sys_truncate (int fd
)
234 off_t pos
= lseek (fd
, (off_t
) 0, SEEK_CUR
);
235 return pos
< 0 ? -1 : ftruncate (fd
, pos
);
238 /* Return nonzero if NAME is the name of a regular file, or if the file
239 does not exist (so it would be created as a regular file). */
241 is_regular_file (const char *name
)
245 if (stat (name
, &stbuf
) == 0)
246 return S_ISREG (stbuf
.st_mode
);
248 return errno
== ENOENT
;
252 sys_write_archive_buffer (void)
254 return rmtwrite (archive
, record_start
->buffer
, record_size
);
257 #define PREAD 0 /* read file descriptor from pipe() */
258 #define PWRITE 1 /* write file descriptor from pipe() */
260 /* Duplicate file descriptor FROM into becoming INTO.
261 INTO is closed first and has to be the next available slot. */
263 xdup2 (int from
, int into
)
267 int status
= close (into
);
269 if (status
!= 0 && errno
!= EBADF
)
272 FATAL_ERROR ((0, e
, _("Cannot close")));
280 FATAL_ERROR ((0, e
, _("Cannot dup")));
288 static void wait_for_grandchild (pid_t pid
) __attribute__ ((__noreturn__
));
290 /* Propagate any failure of the grandchild back to the parent. */
292 wait_for_grandchild (pid_t pid
)
297 while (waitpid (pid
, &wait_status
, 0) == -1)
300 waitpid_error (use_compress_program_option
);
304 if (WIFSIGNALED (wait_status
))
305 raise (WTERMSIG (wait_status
));
306 else if (WEXITSTATUS (wait_status
) != 0)
307 exit_code
= WEXITSTATUS (wait_status
);
312 /* Set ARCHIVE for writing, then compressing an archive. */
314 sys_child_open_for_compress (void)
318 pid_t grandchild_pid
;
322 child_pid
= xfork ();
326 /* The parent tar is still here! Just clean up. */
328 archive
= parent_pipe
[PWRITE
];
329 xclose (parent_pipe
[PREAD
]);
333 /* The new born child tar is here! */
335 set_program_name (_("tar (child)"));
336 signal (SIGPIPE
, SIG_DFL
);
338 xdup2 (parent_pipe
[PREAD
], STDIN_FILENO
);
339 xclose (parent_pipe
[PWRITE
]);
341 /* Check if we need a grandchild tar. This happens only if either:
342 a) the file is to be accessed by rmt: compressor doesn't know how;
343 b) the file is not a plain file. */
345 if (!_remdev (archive_name_array
[0])
346 && is_regular_file (archive_name_array
[0]))
349 maybe_backup_file (archive_name_array
[0], 1);
351 /* We don't need a grandchild tar. Open the archive and launch the
353 if (strcmp (archive_name_array
[0], "-"))
355 archive
= creat (archive_name_array
[0], MODE_RW
);
358 int saved_errno
= errno
;
363 open_fatal (archive_name_array
[0]);
365 xdup2 (archive
, STDOUT_FILENO
);
367 priv_set_restore_linkdir ();
368 execlp (use_compress_program_option
, use_compress_program_option
, NULL
);
369 exec_fatal (use_compress_program_option
);
372 /* We do need a grandchild tar. */
375 grandchild_pid
= xfork ();
377 if (grandchild_pid
== 0)
379 /* The newborn grandchild tar is here! Launch the compressor. */
381 set_program_name (_("tar (grandchild)"));
383 xdup2 (child_pipe
[PWRITE
], STDOUT_FILENO
);
384 xclose (child_pipe
[PREAD
]);
385 priv_set_restore_linkdir ();
386 execlp (use_compress_program_option
, use_compress_program_option
,
388 exec_fatal (use_compress_program_option
);
391 /* The child tar is still here! */
393 /* Prepare for reblocking the data from the compressor into the archive. */
395 xdup2 (child_pipe
[PREAD
], STDIN_FILENO
);
396 xclose (child_pipe
[PWRITE
]);
398 if (strcmp (archive_name_array
[0], "-") == 0)
399 archive
= STDOUT_FILENO
;
402 archive
= rmtcreat (archive_name_array
[0], MODE_RW
, rsh_command_option
);
404 open_fatal (archive_name_array
[0]);
407 /* Let's read out of the stdin pipe and write an archive. */
415 /* Assemble a record. */
417 for (length
= 0, cursor
= record_start
->buffer
;
418 length
< record_size
;
419 length
+= status
, cursor
+= status
)
421 size_t size
= record_size
- length
;
423 status
= safe_read (STDIN_FILENO
, cursor
, size
);
424 if (status
== SAFE_READ_ERROR
)
425 read_fatal (use_compress_program_option
);
430 /* Copy the record. */
434 /* We hit the end of the file. Write last record at
435 full length, as the only role of the grandchild is
436 doing proper reblocking. */
440 memset (record_start
->buffer
+ length
, 0, record_size
- length
);
441 status
= sys_write_archive_buffer ();
442 if (status
!= record_size
)
443 archive_write_error (status
);
446 /* There is nothing else to read, break out. */
450 status
= sys_write_archive_buffer ();
451 if (status
!= record_size
)
452 archive_write_error (status
);
455 wait_for_grandchild (grandchild_pid
);
459 run_decompress_program (void)
462 const char *p
, *prog
= NULL
;
464 for (p
= first_decompress_program (&i
); p
; p
= next_decompress_program (&i
))
468 WARNOPT (WARN_DECOMPRESS_PROGRAM
,
469 (0, errno
, _("cannot run %s"), prog
));
470 WARNOPT (WARN_DECOMPRESS_PROGRAM
,
471 (0, 0, _("trying %s"), p
));
474 execlp (p
, p
, "-d", NULL
);
477 FATAL_ERROR ((0, 0, _("unable to run decompression program")));
481 /* Set ARCHIVE for uncompressing, then reading an archive. */
483 sys_child_open_for_uncompress (void)
487 pid_t grandchild_pid
;
491 child_pid
= xfork ();
495 /* The parent tar is still here! Just clean up. */
497 archive
= parent_pipe
[PREAD
];
498 xclose (parent_pipe
[PWRITE
]);
502 /* The newborn child tar is here! */
504 set_program_name (_("tar (child)"));
505 signal (SIGPIPE
, SIG_DFL
);
507 xdup2 (parent_pipe
[PWRITE
], STDOUT_FILENO
);
508 xclose (parent_pipe
[PREAD
]);
510 /* Check if we need a grandchild tar. This happens only if either:
511 a) we're reading stdin: to force unblocking;
512 b) the file is to be accessed by rmt: compressor doesn't know how;
513 c) the file is not a plain file. */
515 if (strcmp (archive_name_array
[0], "-") != 0
516 && !_remdev (archive_name_array
[0])
517 && is_regular_file (archive_name_array
[0]))
519 /* We don't need a grandchild tar. Open the archive and lauch the
522 archive
= open (archive_name_array
[0], O_RDONLY
| O_BINARY
, MODE_RW
);
524 open_fatal (archive_name_array
[0]);
525 xdup2 (archive
, STDIN_FILENO
);
526 priv_set_restore_linkdir ();
527 run_decompress_program ();
530 /* We do need a grandchild tar. */
533 grandchild_pid
= xfork ();
535 if (grandchild_pid
== 0)
537 /* The newborn grandchild tar is here! Launch the uncompressor. */
539 set_program_name (_("tar (grandchild)"));
541 xdup2 (child_pipe
[PREAD
], STDIN_FILENO
);
542 xclose (child_pipe
[PWRITE
]);
543 priv_set_restore_linkdir ();
544 run_decompress_program ();
547 /* The child tar is still here! */
549 /* Prepare for unblocking the data from the archive into the
552 xdup2 (child_pipe
[PWRITE
], STDOUT_FILENO
);
553 xclose (child_pipe
[PREAD
]);
555 if (strcmp (archive_name_array
[0], "-") == 0)
556 archive
= STDIN_FILENO
;
558 archive
= rmtopen (archive_name_array
[0], O_RDONLY
| O_BINARY
,
559 MODE_RW
, rsh_command_option
);
561 open_fatal (archive_name_array
[0]);
563 /* Let's read the archive and pipe it into stdout. */
572 clear_read_error_count ();
575 status
= rmtread (archive
, record_start
->buffer
, record_size
);
576 if (status
== SAFE_READ_ERROR
)
578 archive_read_error ();
583 cursor
= record_start
->buffer
;
587 count
= maximum
< BLOCKSIZE
? maximum
: BLOCKSIZE
;
588 if (full_write (STDOUT_FILENO
, cursor
, count
) != count
)
589 write_error (use_compress_program_option
);
595 xclose (STDOUT_FILENO
);
597 wait_for_grandchild (grandchild_pid
);
603 dec_to_env (char const *envar
, uintmax_t num
)
605 char buf
[UINTMAX_STRSIZE_BOUND
];
608 numstr
= STRINGIFY_BIGINT (num
, buf
);
609 if (setenv (envar
, numstr
, 1) != 0)
614 time_to_env (char const *envar
, struct timespec t
)
616 char buf
[TIMESPEC_STRSIZE_BOUND
];
617 if (setenv (envar
, code_timespec (t
, buf
), 1) != 0)
622 oct_to_env (char const *envar
, unsigned long num
)
624 char buf
[1+1+(sizeof(unsigned long)*CHAR_BIT
+2)/3];
626 snprintf (buf
, sizeof buf
, "0%lo", num
);
627 if (setenv (envar
, buf
, 1) != 0)
632 str_to_env (char const *envar
, char const *str
)
636 if (setenv (envar
, str
, 1) != 0)
644 chr_to_env (char const *envar
, char c
)
649 if (setenv (envar
, buf
, 1) != 0)
654 stat_to_env (char *name
, char type
, struct tar_stat_info
*st
)
656 str_to_env ("TAR_VERSION", PACKAGE_VERSION
);
657 str_to_env ("TAR_ARCHIVE", *archive_name_cursor
);
658 dec_to_env ("TAR_VOLUME", archive_name_cursor
- archive_name_array
+ 1);
659 dec_to_env ("TAR_BLOCKING_FACTOR", blocking_factor
);
660 str_to_env ("TAR_FORMAT",
661 archive_format_string (current_format
== DEFAULT_FORMAT
?
662 archive_format
: current_format
));
663 chr_to_env ("TAR_FILETYPE", type
);
664 oct_to_env ("TAR_MODE", st
->stat
.st_mode
);
665 str_to_env ("TAR_FILENAME", name
);
666 str_to_env ("TAR_REALNAME", st
->file_name
);
667 str_to_env ("TAR_UNAME", st
->uname
);
668 str_to_env ("TAR_GNAME", st
->gname
);
669 time_to_env ("TAR_ATIME", st
->atime
);
670 time_to_env ("TAR_MTIME", st
->mtime
);
671 time_to_env ("TAR_CTIME", st
->ctime
);
672 dec_to_env ("TAR_SIZE", st
->stat
.st_size
);
673 dec_to_env ("TAR_UID", st
->stat
.st_uid
);
674 dec_to_env ("TAR_GID", st
->stat
.st_gid
);
680 dec_to_env ("TAR_MINOR", minor (st
->stat
.st_rdev
));
681 dec_to_env ("TAR_MAJOR", major (st
->stat
.st_rdev
));
682 unsetenv ("TAR_LINKNAME");
687 unsetenv ("TAR_MINOR");
688 unsetenv ("TAR_MAJOR");
689 str_to_env ("TAR_LINKNAME", st
->link_name
);
693 unsetenv ("TAR_MINOR");
694 unsetenv ("TAR_MAJOR");
695 unsetenv ("TAR_LINKNAME");
700 static pid_t global_pid
;
701 static RETSIGTYPE (*pipe_handler
) (int sig
);
704 sys_exec_command (char *file_name
, int typechar
, struct tar_stat_info
*st
)
710 pipe_handler
= signal (SIGPIPE
, SIG_IGN
);
711 global_pid
= xfork ();
720 xdup2 (p
[PREAD
], STDIN_FILENO
);
723 stat_to_env (file_name
, typechar
, st
);
727 argv
[2] = to_command_option
;
730 priv_set_restore_linkdir ();
731 execv ("/bin/sh", argv
);
733 exec_fatal (file_name
);
737 sys_wait_command (void)
744 signal (SIGPIPE
, pipe_handler
);
745 while (waitpid (global_pid
, &status
, 0) == -1)
749 waitpid_error (to_command_option
);
753 if (WIFEXITED (status
))
755 if (!ignore_command_error_option
&& WEXITSTATUS (status
))
756 ERROR ((0, 0, _("%lu: Child returned status %d"),
757 (unsigned long) global_pid
, WEXITSTATUS (status
)));
759 else if (WIFSIGNALED (status
))
761 WARN ((0, 0, _("%lu: Child terminated on signal %d"),
762 (unsigned long) global_pid
, WTERMSIG (status
)));
765 ERROR ((0, 0, _("%lu: Child terminated on unknown reason"),
766 (unsigned long) global_pid
));
772 sys_exec_info_script (const char **archive_name
, int volume_number
)
776 char uintbuf
[UINTMAX_STRSIZE_BOUND
];
778 static RETSIGTYPE (*saved_handler
) (int sig
);
781 saved_handler
= signal (SIGPIPE
, SIG_IGN
);
796 fp
= fdopen (p
[PREAD
], "r");
797 rc
= getline (&buf
, &size
, fp
);
800 if (rc
> 0 && buf
[rc
-1] == '\n')
803 while (waitpid (pid
, &status
, 0) == -1)
806 signal (SIGPIPE
, saved_handler
);
807 waitpid_error (info_script_option
);
811 signal (SIGPIPE
, saved_handler
);
813 if (WIFEXITED (status
))
815 if (WEXITSTATUS (status
) == 0 && rc
> 0)
819 return WEXITSTATUS (status
);
827 setenv ("TAR_VERSION", PACKAGE_VERSION
, 1);
828 setenv ("TAR_ARCHIVE", *archive_name
, 1);
829 setenv ("TAR_VOLUME", STRINGIFY_BIGINT (volume_number
, uintbuf
), 1);
830 setenv ("TAR_BLOCKING_FACTOR",
831 STRINGIFY_BIGINT (blocking_factor
, uintbuf
), 1);
832 setenv ("TAR_SUBCOMMAND", subcommand_string (subcommand_option
), 1);
833 setenv ("TAR_FORMAT",
834 archive_format_string (current_format
== DEFAULT_FORMAT
?
835 archive_format
: current_format
), 1);
836 setenv ("TAR_FD", STRINGIFY_BIGINT (p
[PWRITE
], uintbuf
), 1);
842 argv
[2] = (char *) info_script_option
;
845 priv_set_restore_linkdir ();
846 execv (argv
[0], argv
);
848 exec_fatal (info_script_option
);
852 sys_exec_checkpoint_script (const char *script_name
,
853 const char *archive_name
,
854 int checkpoint_number
)
858 char uintbuf
[UINTMAX_STRSIZE_BOUND
];
868 while (waitpid (pid
, &status
, 0) == -1)
871 waitpid_error (script_name
);
879 setenv ("TAR_VERSION", PACKAGE_VERSION
, 1);
880 setenv ("TAR_ARCHIVE", archive_name
, 1);
881 setenv ("TAR_CHECKPOINT", STRINGIFY_BIGINT (checkpoint_number
, uintbuf
), 1);
882 setenv ("TAR_BLOCKING_FACTOR",
883 STRINGIFY_BIGINT (blocking_factor
, uintbuf
), 1);
884 setenv ("TAR_SUBCOMMAND", subcommand_string (subcommand_option
), 1);
885 setenv ("TAR_FORMAT",
886 archive_format_string (current_format
== DEFAULT_FORMAT
?
887 archive_format
: current_format
), 1);
890 argv
[2] = (char *) script_name
;
893 priv_set_restore_linkdir ();
894 execv (argv
[0], argv
);
896 exec_fatal (script_name
);
899 #endif /* not MSDOS */