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/>. */
28 sys_get_archive_stat (void)
34 sys_file_is_archive (struct tar_stat_info
*p
)
40 sys_save_archive_dev_ino (void)
45 sys_detect_dev_null_output (void)
47 static char const dev_null
[] = "nul";
49 dev_null_output
= (strcmp (archive_name_array
[0], dev_null
) == 0
50 || (! _isrmt (archive
)));
54 sys_wait_for_child (pid_t child_pid
, bool eof
)
59 sys_spawn_shell (void)
61 spawnl (P_WAIT
, getenv ("COMSPEC"), "-", 0);
64 /* stat() in djgpp's C library gives a constant number of 42 as the
65 uid and gid of a file. So, comparing an FTP'ed archive just after
66 unpack would fail on MSDOS. */
69 sys_compare_uid (struct stat
*a
, struct stat
*b
)
75 sys_compare_gid (struct stat
*a
, struct stat
*b
)
81 sys_compare_links (struct stat
*link_data
, struct stat
*stat_data
)
89 return write (fd
, "", 0);
93 sys_write_archive_buffer (void)
95 return full_write (archive
, record_start
->buffer
, record_size
);
98 /* Set ARCHIVE for writing, then compressing an archive. */
100 sys_child_open_for_compress (void)
102 FATAL_ERROR ((0, 0, _("Cannot use compressed or remote archives")));
105 /* Set ARCHIVE for uncompressing, then reading an archive. */
107 sys_child_open_for_uncompress (void)
109 FATAL_ERROR ((0, 0, _("Cannot use compressed or remote archives")));
114 extern union block
*record_start
; /* FIXME */
116 static struct stat archive_stat
; /* stat block for archive file */
119 sys_get_archive_stat (void)
121 return fstat (archive
, &archive_stat
) == 0;
125 sys_file_is_archive (struct tar_stat_info
*p
)
127 return (ar_dev
&& p
->stat
.st_dev
== ar_dev
&& p
->stat
.st_ino
== ar_ino
);
130 /* Save archive file inode and device numbers */
132 sys_save_archive_dev_ino (void)
134 if (!_isrmt (archive
) && S_ISREG (archive_stat
.st_mode
))
136 ar_dev
= archive_stat
.st_dev
;
137 ar_ino
= archive_stat
.st_ino
;
143 /* Detect if outputting to "/dev/null". */
145 sys_detect_dev_null_output (void)
147 static char const dev_null
[] = "/dev/null";
148 struct stat dev_null_stat
;
150 dev_null_output
= (strcmp (archive_name_array
[0], dev_null
) == 0
151 || (! _isrmt (archive
)
152 && S_ISCHR (archive_stat
.st_mode
)
153 && stat (dev_null
, &dev_null_stat
) == 0
154 && archive_stat
.st_dev
== dev_null_stat
.st_dev
155 && archive_stat
.st_ino
== dev_null_stat
.st_ino
));
159 sys_wait_for_child (pid_t child_pid
, bool eof
)
165 while (waitpid (child_pid
, &wait_status
, 0) == -1)
168 waitpid_error (use_compress_program_option
);
172 if (WIFSIGNALED (wait_status
))
174 int sig
= WTERMSIG (wait_status
);
175 if (!(!eof
&& sig
== SIGPIPE
))
176 FATAL_ERROR ((0, 0, _("Child died with signal %d"), sig
));
178 else if (WEXITSTATUS (wait_status
) != 0)
179 FATAL_ERROR ((0, 0, _("Child returned status %d"),
180 WEXITSTATUS (wait_status
)));
185 sys_spawn_shell (void)
188 const char *shell
= getenv ("SHELL");
194 priv_set_restore_linkdir ();
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 static 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 priv_set_restore_linkdir ();
366 execlp (use_compress_program_option
, use_compress_program_option
, NULL
);
367 exec_fatal (use_compress_program_option
);
370 /* We do need a grandchild tar. */
373 grandchild_pid
= xfork ();
375 if (grandchild_pid
== 0)
377 /* The newborn grandchild tar is here! Launch the compressor. */
379 set_program_name (_("tar (grandchild)"));
381 xdup2 (child_pipe
[PWRITE
], STDOUT_FILENO
);
382 xclose (child_pipe
[PREAD
]);
383 priv_set_restore_linkdir ();
384 execlp (use_compress_program_option
, use_compress_program_option
,
386 exec_fatal (use_compress_program_option
);
389 /* The child tar is still here! */
391 /* Prepare for reblocking the data from the compressor into the archive. */
393 xdup2 (child_pipe
[PREAD
], STDIN_FILENO
);
394 xclose (child_pipe
[PWRITE
]);
396 if (strcmp (archive_name_array
[0], "-") == 0)
397 archive
= STDOUT_FILENO
;
400 archive
= rmtcreat (archive_name_array
[0], MODE_RW
, rsh_command_option
);
402 open_fatal (archive_name_array
[0]);
405 /* Let's read out of the stdin pipe and write an archive. */
413 /* Assemble a record. */
415 for (length
= 0, cursor
= record_start
->buffer
;
416 length
< record_size
;
417 length
+= status
, cursor
+= status
)
419 size_t size
= record_size
- length
;
421 status
= safe_read (STDIN_FILENO
, cursor
, size
);
422 if (status
== SAFE_READ_ERROR
)
423 read_fatal (use_compress_program_option
);
428 /* Copy the record. */
432 /* We hit the end of the file. Write last record at
433 full length, as the only role of the grandchild is
434 doing proper reblocking. */
438 memset (record_start
->buffer
+ length
, 0, record_size
- length
);
439 status
= sys_write_archive_buffer ();
440 if (status
!= record_size
)
441 archive_write_error (status
);
444 /* There is nothing else to read, break out. */
448 status
= sys_write_archive_buffer ();
449 if (status
!= record_size
)
450 archive_write_error (status
);
453 wait_for_grandchild (grandchild_pid
);
457 run_decompress_program (void)
460 const char *p
, *prog
= NULL
;
462 for (p
= first_decompress_program (&i
); p
; p
= next_decompress_program (&i
))
466 WARNOPT (WARN_DECOMPRESS_PROGRAM
,
467 (0, errno
, _("cannot run %s"), prog
));
468 WARNOPT (WARN_DECOMPRESS_PROGRAM
,
469 (0, 0, _("trying %s"), p
));
472 execlp (p
, p
, "-d", NULL
);
475 FATAL_ERROR ((0, 0, _("unable to run decompression program")));
479 /* Set ARCHIVE for uncompressing, then reading an archive. */
481 sys_child_open_for_uncompress (void)
485 pid_t grandchild_pid
;
489 child_pid
= xfork ();
493 /* The parent tar is still here! Just clean up. */
495 archive
= parent_pipe
[PREAD
];
496 xclose (parent_pipe
[PWRITE
]);
500 /* The newborn child tar is here! */
502 set_program_name (_("tar (child)"));
503 signal (SIGPIPE
, SIG_DFL
);
505 xdup2 (parent_pipe
[PWRITE
], STDOUT_FILENO
);
506 xclose (parent_pipe
[PREAD
]);
508 /* Check if we need a grandchild tar. This happens only if either:
509 a) we're reading stdin: to force unblocking;
510 b) the file is to be accessed by rmt: compressor doesn't know how;
511 c) the file is not a plain file. */
513 if (strcmp (archive_name_array
[0], "-") != 0
514 && !_remdev (archive_name_array
[0])
515 && is_regular_file (archive_name_array
[0]))
517 /* We don't need a grandchild tar. Open the archive and lauch the
520 archive
= open (archive_name_array
[0], O_RDONLY
| O_BINARY
, MODE_RW
);
522 open_fatal (archive_name_array
[0]);
523 xdup2 (archive
, STDIN_FILENO
);
524 priv_set_restore_linkdir ();
525 run_decompress_program ();
528 /* We do need a grandchild tar. */
531 grandchild_pid
= xfork ();
533 if (grandchild_pid
== 0)
535 /* The newborn grandchild tar is here! Launch the uncompressor. */
537 set_program_name (_("tar (grandchild)"));
539 xdup2 (child_pipe
[PREAD
], STDIN_FILENO
);
540 xclose (child_pipe
[PWRITE
]);
541 priv_set_restore_linkdir ();
542 run_decompress_program ();
545 /* The child tar is still here! */
547 /* Prepare for unblocking the data from the archive into the
550 xdup2 (child_pipe
[PWRITE
], STDOUT_FILENO
);
551 xclose (child_pipe
[PREAD
]);
553 if (strcmp (archive_name_array
[0], "-") == 0)
554 archive
= STDIN_FILENO
;
556 archive
= rmtopen (archive_name_array
[0], O_RDONLY
| O_BINARY
,
557 MODE_RW
, rsh_command_option
);
559 open_fatal (archive_name_array
[0]);
561 /* Let's read the archive and pipe it into stdout. */
570 clear_read_error_count ();
573 status
= rmtread (archive
, record_start
->buffer
, record_size
);
574 if (status
== SAFE_READ_ERROR
)
576 archive_read_error ();
581 cursor
= record_start
->buffer
;
585 count
= maximum
< BLOCKSIZE
? maximum
: BLOCKSIZE
;
586 if (full_write (STDOUT_FILENO
, cursor
, count
) != count
)
587 write_error (use_compress_program_option
);
593 xclose (STDOUT_FILENO
);
595 wait_for_grandchild (grandchild_pid
);
601 dec_to_env (char const *envar
, uintmax_t num
)
603 char buf
[UINTMAX_STRSIZE_BOUND
];
606 numstr
= STRINGIFY_BIGINT (num
, buf
);
607 if (setenv (envar
, numstr
, 1) != 0)
612 time_to_env (char const *envar
, struct timespec t
)
614 char buf
[TIMESPEC_STRSIZE_BOUND
];
615 if (setenv (envar
, code_timespec (t
, buf
), 1) != 0)
620 oct_to_env (char const *envar
, unsigned long num
)
622 char buf
[1+1+(sizeof(unsigned long)*CHAR_BIT
+2)/3];
624 snprintf (buf
, sizeof buf
, "0%lo", num
);
625 if (setenv (envar
, buf
, 1) != 0)
630 str_to_env (char const *envar
, char const *str
)
634 if (setenv (envar
, str
, 1) != 0)
642 chr_to_env (char const *envar
, char c
)
647 if (setenv (envar
, buf
, 1) != 0)
652 stat_to_env (char *name
, char type
, struct tar_stat_info
*st
)
654 str_to_env ("TAR_VERSION", PACKAGE_VERSION
);
655 str_to_env ("TAR_ARCHIVE", *archive_name_cursor
);
656 dec_to_env ("TAR_VOLUME", archive_name_cursor
- archive_name_array
+ 1);
657 dec_to_env ("TAR_BLOCKING_FACTOR", blocking_factor
);
658 str_to_env ("TAR_FORMAT",
659 archive_format_string (current_format
== DEFAULT_FORMAT
?
660 archive_format
: current_format
));
661 chr_to_env ("TAR_FILETYPE", type
);
662 oct_to_env ("TAR_MODE", st
->stat
.st_mode
);
663 str_to_env ("TAR_FILENAME", name
);
664 str_to_env ("TAR_REALNAME", st
->file_name
);
665 str_to_env ("TAR_UNAME", st
->uname
);
666 str_to_env ("TAR_GNAME", st
->gname
);
667 time_to_env ("TAR_ATIME", st
->atime
);
668 time_to_env ("TAR_MTIME", st
->mtime
);
669 time_to_env ("TAR_CTIME", st
->ctime
);
670 dec_to_env ("TAR_SIZE", st
->stat
.st_size
);
671 dec_to_env ("TAR_UID", st
->stat
.st_uid
);
672 dec_to_env ("TAR_GID", st
->stat
.st_gid
);
678 dec_to_env ("TAR_MINOR", minor (st
->stat
.st_rdev
));
679 dec_to_env ("TAR_MAJOR", major (st
->stat
.st_rdev
));
680 unsetenv ("TAR_LINKNAME");
685 unsetenv ("TAR_MINOR");
686 unsetenv ("TAR_MAJOR");
687 str_to_env ("TAR_LINKNAME", st
->link_name
);
691 unsetenv ("TAR_MINOR");
692 unsetenv ("TAR_MAJOR");
693 unsetenv ("TAR_LINKNAME");
698 static pid_t global_pid
;
699 static RETSIGTYPE (*pipe_handler
) (int sig
);
702 sys_exec_command (char *file_name
, int typechar
, struct tar_stat_info
*st
)
708 pipe_handler
= signal (SIGPIPE
, SIG_IGN
);
709 global_pid
= xfork ();
718 xdup2 (p
[PREAD
], STDIN_FILENO
);
721 stat_to_env (file_name
, typechar
, st
);
723 argv
[0] = (char *) "/bin/sh";
724 argv
[1] = (char *) "-c";
725 argv
[2] = to_command_option
;
728 priv_set_restore_linkdir ();
729 execv ("/bin/sh", argv
);
731 exec_fatal (file_name
);
735 sys_wait_command (void)
742 signal (SIGPIPE
, pipe_handler
);
743 while (waitpid (global_pid
, &status
, 0) == -1)
747 waitpid_error (to_command_option
);
751 if (WIFEXITED (status
))
753 if (!ignore_command_error_option
&& WEXITSTATUS (status
))
754 ERROR ((0, 0, _("%lu: Child returned status %d"),
755 (unsigned long) global_pid
, WEXITSTATUS (status
)));
757 else if (WIFSIGNALED (status
))
759 WARN ((0, 0, _("%lu: Child terminated on signal %d"),
760 (unsigned long) global_pid
, WTERMSIG (status
)));
763 ERROR ((0, 0, _("%lu: Child terminated on unknown reason"),
764 (unsigned long) global_pid
));
770 sys_exec_info_script (const char **archive_name
, int volume_number
)
774 char uintbuf
[UINTMAX_STRSIZE_BOUND
];
776 static RETSIGTYPE (*saved_handler
) (int sig
);
779 saved_handler
= signal (SIGPIPE
, SIG_IGN
);
794 fp
= fdopen (p
[PREAD
], "r");
795 rc
= getline (&buf
, &size
, fp
);
798 if (rc
> 0 && buf
[rc
-1] == '\n')
801 while (waitpid (pid
, &status
, 0) == -1)
804 signal (SIGPIPE
, saved_handler
);
805 waitpid_error (info_script_option
);
809 signal (SIGPIPE
, saved_handler
);
811 if (WIFEXITED (status
))
813 if (WEXITSTATUS (status
) == 0 && rc
> 0)
817 return WEXITSTATUS (status
);
825 setenv ("TAR_VERSION", PACKAGE_VERSION
, 1);
826 setenv ("TAR_ARCHIVE", *archive_name
, 1);
827 setenv ("TAR_VOLUME", STRINGIFY_BIGINT (volume_number
, uintbuf
), 1);
828 setenv ("TAR_BLOCKING_FACTOR",
829 STRINGIFY_BIGINT (blocking_factor
, uintbuf
), 1);
830 setenv ("TAR_SUBCOMMAND", subcommand_string (subcommand_option
), 1);
831 setenv ("TAR_FORMAT",
832 archive_format_string (current_format
== DEFAULT_FORMAT
?
833 archive_format
: current_format
), 1);
834 setenv ("TAR_FD", STRINGIFY_BIGINT (p
[PWRITE
], uintbuf
), 1);
838 argv
[0] = (char *) "/bin/sh";
839 argv
[1] = (char *) "-c";
840 argv
[2] = (char *) info_script_option
;
843 priv_set_restore_linkdir ();
844 execv (argv
[0], argv
);
846 exec_fatal (info_script_option
);
850 sys_exec_checkpoint_script (const char *script_name
,
851 const char *archive_name
,
852 int checkpoint_number
)
856 char uintbuf
[UINTMAX_STRSIZE_BOUND
];
866 while (waitpid (pid
, &status
, 0) == -1)
869 waitpid_error (script_name
);
877 setenv ("TAR_VERSION", PACKAGE_VERSION
, 1);
878 setenv ("TAR_ARCHIVE", archive_name
, 1);
879 setenv ("TAR_CHECKPOINT", STRINGIFY_BIGINT (checkpoint_number
, uintbuf
), 1);
880 setenv ("TAR_BLOCKING_FACTOR",
881 STRINGIFY_BIGINT (blocking_factor
, uintbuf
), 1);
882 setenv ("TAR_SUBCOMMAND", subcommand_string (subcommand_option
), 1);
883 setenv ("TAR_FORMAT",
884 archive_format_string (current_format
== DEFAULT_FORMAT
?
885 archive_format
: current_format
), 1);
886 argv
[0] = (char *) "/bin/sh";
887 argv
[1] = (char *) "-c";
888 argv
[2] = (char *) script_name
;
891 priv_set_restore_linkdir ();
892 execv (argv
[0], argv
);
894 exec_fatal (script_name
);
897 #endif /* not MSDOS */