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
);
458 /* Set ARCHIVE for uncompressing, then reading an archive. */
460 sys_child_open_for_uncompress (void)
464 pid_t grandchild_pid
;
468 child_pid
= xfork ();
472 /* The parent tar is still here! Just clean up. */
474 archive
= parent_pipe
[PREAD
];
475 xclose (parent_pipe
[PWRITE
]);
479 /* The newborn child tar is here! */
481 set_program_name (_("tar (child)"));
482 signal (SIGPIPE
, SIG_DFL
);
484 xdup2 (parent_pipe
[PWRITE
], STDOUT_FILENO
);
485 xclose (parent_pipe
[PREAD
]);
487 /* Check if we need a grandchild tar. This happens only if either:
488 a) we're reading stdin: to force unblocking;
489 b) the file is to be accessed by rmt: compressor doesn't know how;
490 c) the file is not a plain file. */
492 if (strcmp (archive_name_array
[0], "-") != 0
493 && !_remdev (archive_name_array
[0])
494 && is_regular_file (archive_name_array
[0]))
496 /* We don't need a grandchild tar. Open the archive and lauch the
499 archive
= open (archive_name_array
[0], O_RDONLY
| O_BINARY
, MODE_RW
);
501 open_fatal (archive_name_array
[0]);
502 xdup2 (archive
, STDIN_FILENO
);
503 priv_set_restore_linkdir ();
504 execlp (use_compress_program_option
, use_compress_program_option
,
506 exec_fatal (use_compress_program_option
);
509 /* We do need a grandchild tar. */
512 grandchild_pid
= xfork ();
514 if (grandchild_pid
== 0)
516 /* The newborn grandchild tar is here! Launch the uncompressor. */
518 set_program_name (_("tar (grandchild)"));
520 xdup2 (child_pipe
[PREAD
], STDIN_FILENO
);
521 xclose (child_pipe
[PWRITE
]);
522 priv_set_restore_linkdir ();
523 execlp (use_compress_program_option
, use_compress_program_option
,
525 exec_fatal (use_compress_program_option
);
528 /* The child tar is still here! */
530 /* Prepare for unblocking the data from the archive into the
533 xdup2 (child_pipe
[PWRITE
], STDOUT_FILENO
);
534 xclose (child_pipe
[PREAD
]);
536 if (strcmp (archive_name_array
[0], "-") == 0)
537 archive
= STDIN_FILENO
;
539 archive
= rmtopen (archive_name_array
[0], O_RDONLY
| O_BINARY
,
540 MODE_RW
, rsh_command_option
);
542 open_fatal (archive_name_array
[0]);
544 /* Let's read the archive and pipe it into stdout. */
553 clear_read_error_count ();
556 status
= rmtread (archive
, record_start
->buffer
, record_size
);
557 if (status
== SAFE_READ_ERROR
)
559 archive_read_error ();
564 cursor
= record_start
->buffer
;
568 count
= maximum
< BLOCKSIZE
? maximum
: BLOCKSIZE
;
569 if (full_write (STDOUT_FILENO
, cursor
, count
) != count
)
570 write_error (use_compress_program_option
);
576 xclose (STDOUT_FILENO
);
578 wait_for_grandchild (grandchild_pid
);
584 dec_to_env (char const *envar
, uintmax_t num
)
586 char buf
[UINTMAX_STRSIZE_BOUND
];
589 numstr
= STRINGIFY_BIGINT (num
, buf
);
590 if (setenv (envar
, numstr
, 1) != 0)
595 time_to_env (char const *envar
, struct timespec t
)
597 char buf
[TIMESPEC_STRSIZE_BOUND
];
598 if (setenv (envar
, code_timespec (t
, buf
), 1) != 0)
603 oct_to_env (char const *envar
, unsigned long num
)
605 char buf
[1+1+(sizeof(unsigned long)*CHAR_BIT
+2)/3];
607 snprintf (buf
, sizeof buf
, "0%lo", num
);
608 if (setenv (envar
, buf
, 1) != 0)
613 str_to_env (char const *envar
, char const *str
)
617 if (setenv (envar
, str
, 1) != 0)
625 chr_to_env (char const *envar
, char c
)
630 if (setenv (envar
, buf
, 1) != 0)
635 stat_to_env (char *name
, char type
, struct tar_stat_info
*st
)
637 str_to_env ("TAR_VERSION", PACKAGE_VERSION
);
638 str_to_env ("TAR_ARCHIVE", *archive_name_cursor
);
639 dec_to_env ("TAR_VOLUME", archive_name_cursor
- archive_name_array
+ 1);
640 dec_to_env ("TAR_BLOCKING_FACTOR", blocking_factor
);
641 str_to_env ("TAR_FORMAT",
642 archive_format_string (current_format
== DEFAULT_FORMAT
?
643 archive_format
: current_format
));
644 chr_to_env ("TAR_FILETYPE", type
);
645 oct_to_env ("TAR_MODE", st
->stat
.st_mode
);
646 str_to_env ("TAR_FILENAME", name
);
647 str_to_env ("TAR_REALNAME", st
->file_name
);
648 str_to_env ("TAR_UNAME", st
->uname
);
649 str_to_env ("TAR_GNAME", st
->gname
);
650 time_to_env ("TAR_ATIME", st
->atime
);
651 time_to_env ("TAR_MTIME", st
->mtime
);
652 time_to_env ("TAR_CTIME", st
->ctime
);
653 dec_to_env ("TAR_SIZE", st
->stat
.st_size
);
654 dec_to_env ("TAR_UID", st
->stat
.st_uid
);
655 dec_to_env ("TAR_GID", st
->stat
.st_gid
);
661 dec_to_env ("TAR_MINOR", minor (st
->stat
.st_rdev
));
662 dec_to_env ("TAR_MAJOR", major (st
->stat
.st_rdev
));
663 unsetenv ("TAR_LINKNAME");
668 unsetenv ("TAR_MINOR");
669 unsetenv ("TAR_MAJOR");
670 str_to_env ("TAR_LINKNAME", st
->link_name
);
674 unsetenv ("TAR_MINOR");
675 unsetenv ("TAR_MAJOR");
676 unsetenv ("TAR_LINKNAME");
681 static pid_t global_pid
;
682 static RETSIGTYPE (*pipe_handler
) (int sig
);
685 sys_exec_command (char *file_name
, int typechar
, struct tar_stat_info
*st
)
691 pipe_handler
= signal (SIGPIPE
, SIG_IGN
);
692 global_pid
= xfork ();
701 xdup2 (p
[PREAD
], STDIN_FILENO
);
704 stat_to_env (file_name
, typechar
, st
);
708 argv
[2] = to_command_option
;
711 priv_set_restore_linkdir ();
712 execv ("/bin/sh", argv
);
714 exec_fatal (file_name
);
718 sys_wait_command (void)
725 signal (SIGPIPE
, pipe_handler
);
726 while (waitpid (global_pid
, &status
, 0) == -1)
730 waitpid_error (to_command_option
);
734 if (WIFEXITED (status
))
736 if (!ignore_command_error_option
&& WEXITSTATUS (status
))
737 ERROR ((0, 0, _("%lu: Child returned status %d"),
738 (unsigned long) global_pid
, WEXITSTATUS (status
)));
740 else if (WIFSIGNALED (status
))
742 WARN ((0, 0, _("%lu: Child terminated on signal %d"),
743 (unsigned long) global_pid
, WTERMSIG (status
)));
746 ERROR ((0, 0, _("%lu: Child terminated on unknown reason"),
747 (unsigned long) global_pid
));
753 sys_exec_info_script (const char **archive_name
, int volume_number
)
757 char uintbuf
[UINTMAX_STRSIZE_BOUND
];
759 static RETSIGTYPE (*saved_handler
) (int sig
);
762 saved_handler
= signal (SIGPIPE
, SIG_IGN
);
777 fp
= fdopen (p
[PREAD
], "r");
778 rc
= getline (&buf
, &size
, fp
);
781 if (rc
> 0 && buf
[rc
-1] == '\n')
784 while (waitpid (pid
, &status
, 0) == -1)
787 signal (SIGPIPE
, saved_handler
);
788 waitpid_error (info_script_option
);
792 signal (SIGPIPE
, saved_handler
);
794 if (WIFEXITED (status
))
796 if (WEXITSTATUS (status
) == 0 && rc
> 0)
800 return WEXITSTATUS (status
);
808 setenv ("TAR_VERSION", PACKAGE_VERSION
, 1);
809 setenv ("TAR_ARCHIVE", *archive_name
, 1);
810 setenv ("TAR_VOLUME", STRINGIFY_BIGINT (volume_number
, uintbuf
), 1);
811 setenv ("TAR_BLOCKING_FACTOR",
812 STRINGIFY_BIGINT (blocking_factor
, uintbuf
), 1);
813 setenv ("TAR_SUBCOMMAND", subcommand_string (subcommand_option
), 1);
814 setenv ("TAR_FORMAT",
815 archive_format_string (current_format
== DEFAULT_FORMAT
?
816 archive_format
: current_format
), 1);
817 setenv ("TAR_FD", STRINGIFY_BIGINT (p
[PWRITE
], uintbuf
), 1);
823 argv
[2] = (char *) info_script_option
;
826 priv_set_restore_linkdir ();
827 execv (argv
[0], argv
);
829 exec_fatal (info_script_option
);
833 sys_exec_checkpoint_script (const char *script_name
,
834 const char *archive_name
,
835 int checkpoint_number
)
839 char uintbuf
[UINTMAX_STRSIZE_BOUND
];
849 while (waitpid (pid
, &status
, 0) == -1)
852 waitpid_error (script_name
);
860 setenv ("TAR_VERSION", PACKAGE_VERSION
, 1);
861 setenv ("TAR_ARCHIVE", archive_name
, 1);
862 setenv ("TAR_CHECKPOINT", STRINGIFY_BIGINT (checkpoint_number
, uintbuf
), 1);
863 setenv ("TAR_BLOCKING_FACTOR",
864 STRINGIFY_BIGINT (blocking_factor
, uintbuf
), 1);
865 setenv ("TAR_SUBCOMMAND", subcommand_string (subcommand_option
), 1);
866 setenv ("TAR_FORMAT",
867 archive_format_string (current_format
== DEFAULT_FORMAT
?
868 archive_format
: current_format
), 1);
871 argv
[2] = (char *) script_name
;
874 priv_set_restore_linkdir ();
875 execv (argv
[0], argv
);
877 exec_fatal (script_name
);
880 #endif /* not MSDOS */