1 /* System-dependent calls for tar.
3 Copyright (C) 2003, 2004, 2005, 2006, 2007 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, write to the Free Software Foundation, Inc.,
17 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_drain_input_pipe (void)
61 sys_wait_for_child (pid_t child_pid
)
66 sys_spawn_shell (void)
68 spawnl (P_WAIT
, getenv ("COMSPEC"), "-", 0);
71 /* stat() in djgpp's C library gives a constant number of 42 as the
72 uid and gid of a file. So, comparing an FTP'ed archive just after
73 unpack would fail on MSDOS. */
76 sys_compare_uid (struct stat
*a
, struct stat
*b
)
82 sys_compare_gid (struct stat
*a
, struct stat
*b
)
88 sys_compare_links (struct stat
*link_data
, struct stat
*stat_data
)
96 return write (fd
, "", 0);
100 sys_write_archive_buffer (void)
102 return full_write (archive
, record_start
->buffer
, record_size
);
105 /* Set ARCHIVE for writing, then compressing an archive. */
107 sys_child_open_for_compress (void)
109 FATAL_ERROR ((0, 0, _("Cannot use compressed or remote archives")));
112 /* Set ARCHIVE for uncompressing, then reading an archive. */
114 sys_child_open_for_uncompress (void)
116 FATAL_ERROR ((0, 0, _("Cannot use compressed or remote archives")));
121 extern union block
*record_start
; /* FIXME */
123 static struct stat archive_stat
; /* stat block for archive file */
126 sys_get_archive_stat (void)
128 return fstat (archive
, &archive_stat
) == 0;
132 sys_file_is_archive (struct tar_stat_info
*p
)
134 return (ar_dev
&& p
->stat
.st_dev
== ar_dev
&& p
->stat
.st_ino
== ar_ino
);
137 /* Save archive file inode and device numbers */
139 sys_save_archive_dev_ino (void)
141 if (!_isrmt (archive
) && S_ISREG (archive_stat
.st_mode
))
143 ar_dev
= archive_stat
.st_dev
;
144 ar_ino
= archive_stat
.st_ino
;
150 /* Detect if outputting to "/dev/null". */
152 sys_detect_dev_null_output (void)
154 static char const dev_null
[] = "/dev/null";
155 struct stat dev_null_stat
;
157 dev_null_output
= (strcmp (archive_name_array
[0], dev_null
) == 0
158 || (! _isrmt (archive
)
159 && S_ISCHR (archive_stat
.st_mode
)
160 && stat (dev_null
, &dev_null_stat
) == 0
161 && archive_stat
.st_dev
== dev_null_stat
.st_dev
162 && archive_stat
.st_ino
== dev_null_stat
.st_ino
));
165 /* Manage to fully drain a pipe we might be reading, so to not break it on
166 the producer after the EOF block. FIXME: one of these days, GNU tar
167 might become clever enough to just stop working, once there is no more
168 work to do, we might have to revise this area in such time. */
171 sys_drain_input_pipe (void)
175 if (access_mode
== ACCESS_READ
176 && ! _isrmt (archive
)
177 && (S_ISFIFO (archive_stat
.st_mode
) || S_ISSOCK (archive_stat
.st_mode
)))
178 while ((r
= rmtread (archive
, record_start
->buffer
, record_size
)) != 0
179 && r
!= SAFE_READ_ERROR
)
184 sys_wait_for_child (pid_t child_pid
)
190 while (waitpid (child_pid
, &wait_status
, 0) == -1)
193 waitpid_error (use_compress_program_option
);
197 if (WIFSIGNALED (wait_status
))
198 ERROR ((0, 0, _("Child died with signal %d"),
199 WTERMSIG (wait_status
)));
200 else if (WEXITSTATUS (wait_status
) != 0)
201 ERROR ((0, 0, _("Child returned status %d"),
202 WEXITSTATUS (wait_status
)));
207 sys_spawn_shell (void)
210 const char *shell
= getenv ("SHELL");
216 execlp (shell
, "-sh", "-i", (char *) 0);
222 while (waitpid (child
, &wait_status
, 0) == -1)
225 waitpid_error (shell
);
232 sys_compare_uid (struct stat
*a
, struct stat
*b
)
234 return a
->st_uid
== b
->st_uid
;
238 sys_compare_gid (struct stat
*a
, struct stat
*b
)
240 return a
->st_gid
== b
->st_gid
;
244 sys_compare_links (struct stat
*link_data
, struct stat
*stat_data
)
246 return stat_data
->st_dev
== link_data
->st_dev
247 && stat_data
->st_ino
== link_data
->st_ino
;
251 sys_truncate (int fd
)
253 off_t pos
= lseek (fd
, (off_t
) 0, SEEK_CUR
);
254 return pos
< 0 ? -1 : ftruncate (fd
, pos
);
257 /* Return nonzero if NAME is the name of a regular file, or if the file
258 does not exist (so it would be created as a regular file). */
260 is_regular_file (const char *name
)
264 if (stat (name
, &stbuf
) == 0)
265 return S_ISREG (stbuf
.st_mode
);
267 return errno
== ENOENT
;
271 sys_write_archive_buffer (void)
273 return rmtwrite (archive
, record_start
->buffer
, record_size
);
276 #define PREAD 0 /* read file descriptor from pipe() */
277 #define PWRITE 1 /* write file descriptor from pipe() */
279 /* Duplicate file descriptor FROM into becoming INTO.
280 INTO is closed first and has to be the next available slot. */
282 xdup2 (int from
, int into
)
286 int status
= close (into
);
288 if (status
!= 0 && errno
!= EBADF
)
291 FATAL_ERROR ((0, e
, _("Cannot close")));
299 FATAL_ERROR ((0, e
, _("Cannot dup")));
307 /* Set ARCHIVE for writing, then compressing an archive. */
309 sys_child_open_for_compress (void)
313 pid_t grandchild_pid
;
318 child_pid
= xfork ();
322 /* The parent tar is still here! Just clean up. */
324 archive
= parent_pipe
[PWRITE
];
325 xclose (parent_pipe
[PREAD
]);
329 /* The new born child tar is here! */
331 program_name
= _("tar (child)");
333 xdup2 (parent_pipe
[PREAD
], STDIN_FILENO
);
334 xclose (parent_pipe
[PWRITE
]);
336 /* Check if we need a grandchild tar. This happens only if either:
337 a) the file is to be accessed by rmt: compressor doesn't know how;
338 b) the file is not a plain file. */
340 if (!_remdev (archive_name_array
[0])
341 && is_regular_file (archive_name_array
[0]))
344 maybe_backup_file (archive_name_array
[0], 1);
346 /* We don't need a grandchild tar. Open the archive and launch the
348 if (strcmp (archive_name_array
[0], "-"))
350 archive
= creat (archive_name_array
[0], MODE_RW
);
353 int saved_errno
= errno
;
358 open_fatal (archive_name_array
[0]);
360 xdup2 (archive
, STDOUT_FILENO
);
362 execlp (use_compress_program_option
, use_compress_program_option
, NULL
);
363 exec_fatal (use_compress_program_option
);
366 /* We do need a grandchild tar. */
369 grandchild_pid
= xfork ();
371 if (grandchild_pid
== 0)
373 /* The newborn grandchild tar is here! Launch the compressor. */
375 program_name
= _("tar (grandchild)");
377 xdup2 (child_pipe
[PWRITE
], STDOUT_FILENO
);
378 xclose (child_pipe
[PREAD
]);
379 execlp (use_compress_program_option
, use_compress_program_option
,
381 exec_fatal (use_compress_program_option
);
384 /* The child tar is still here! */
386 /* Prepare for reblocking the data from the compressor into the archive. */
388 xdup2 (child_pipe
[PREAD
], STDIN_FILENO
);
389 xclose (child_pipe
[PWRITE
]);
391 if (strcmp (archive_name_array
[0], "-") == 0)
392 archive
= STDOUT_FILENO
;
395 archive
= rmtcreat (archive_name_array
[0], MODE_RW
, rsh_command_option
);
397 open_fatal (archive_name_array
[0]);
400 /* Let's read out of the stdin pipe and write an archive. */
408 /* Assemble a record. */
410 for (length
= 0, cursor
= record_start
->buffer
;
411 length
< record_size
;
412 length
+= status
, cursor
+= status
)
414 size_t size
= record_size
- length
;
416 status
= safe_read (STDIN_FILENO
, cursor
, size
);
417 if (status
== SAFE_READ_ERROR
)
418 read_fatal (use_compress_program_option
);
423 /* Copy the record. */
427 /* We hit the end of the file. Write last record at
428 full length, as the only role of the grandchild is
429 doing proper reblocking. */
433 memset (record_start
->buffer
+ length
, 0, record_size
- length
);
434 status
= sys_write_archive_buffer ();
435 if (status
!= record_size
)
436 archive_write_error (status
);
439 /* There is nothing else to read, break out. */
443 status
= sys_write_archive_buffer ();
444 if (status
!= record_size
)
445 archive_write_error (status
);
448 /* Propagate any failure of the grandchild back to the parent. */
450 while (waitpid (grandchild_pid
, &wait_status
, 0) == -1)
453 waitpid_error (use_compress_program_option
);
457 if (WIFSIGNALED (wait_status
))
459 kill (child_pid
, WTERMSIG (wait_status
));
460 exit_status
= TAREXIT_FAILURE
;
462 else if (WEXITSTATUS (wait_status
) != 0)
463 exit_status
= WEXITSTATUS (wait_status
);
468 /* Set ARCHIVE for uncompressing, then reading an archive. */
470 sys_child_open_for_uncompress (void)
474 pid_t grandchild_pid
;
479 child_pid
= xfork ();
483 /* The parent tar is still here! Just clean up. */
485 archive
= parent_pipe
[PREAD
];
486 xclose (parent_pipe
[PWRITE
]);
490 /* The newborn child tar is here! */
492 program_name
= _("tar (child)");
494 xdup2 (parent_pipe
[PWRITE
], STDOUT_FILENO
);
495 xclose (parent_pipe
[PREAD
]);
497 /* Check if we need a grandchild tar. This happens only if either:
498 a) we're reading stdin: to force unblocking;
499 b) the file is to be accessed by rmt: compressor doesn't know how;
500 c) the file is not a plain file. */
502 if (strcmp (archive_name_array
[0], "-") != 0
503 && !_remdev (archive_name_array
[0])
504 && is_regular_file (archive_name_array
[0]))
506 /* We don't need a grandchild tar. Open the archive and lauch the
509 archive
= open (archive_name_array
[0], O_RDONLY
| O_BINARY
, MODE_RW
);
511 open_fatal (archive_name_array
[0]);
512 xdup2 (archive
, STDIN_FILENO
);
513 execlp (use_compress_program_option
, use_compress_program_option
,
515 exec_fatal (use_compress_program_option
);
518 /* We do need a grandchild tar. */
521 grandchild_pid
= xfork ();
523 if (grandchild_pid
== 0)
525 /* The newborn grandchild tar is here! Launch the uncompressor. */
527 program_name
= _("tar (grandchild)");
529 xdup2 (child_pipe
[PREAD
], STDIN_FILENO
);
530 xclose (child_pipe
[PWRITE
]);
531 execlp (use_compress_program_option
, use_compress_program_option
,
533 exec_fatal (use_compress_program_option
);
536 /* The child tar is still here! */
538 /* Prepare for unblocking the data from the archive into the
541 xdup2 (child_pipe
[PWRITE
], STDOUT_FILENO
);
542 xclose (child_pipe
[PREAD
]);
544 if (strcmp (archive_name_array
[0], "-") == 0)
545 archive
= STDIN_FILENO
;
547 archive
= rmtopen (archive_name_array
[0], O_RDONLY
| O_BINARY
,
548 MODE_RW
, rsh_command_option
);
550 open_fatal (archive_name_array
[0]);
552 /* Let's read the archive and pipe it into stdout. */
561 clear_read_error_count ();
564 status
= rmtread (archive
, record_start
->buffer
, record_size
);
565 if (status
== SAFE_READ_ERROR
)
567 archive_read_error ();
572 cursor
= record_start
->buffer
;
576 count
= maximum
< BLOCKSIZE
? maximum
: BLOCKSIZE
;
577 if (full_write (STDOUT_FILENO
, cursor
, count
) != count
)
578 write_error (use_compress_program_option
);
584 xclose (STDOUT_FILENO
);
586 /* Propagate any failure of the grandchild back to the parent. */
588 while (waitpid (grandchild_pid
, &wait_status
, 0) == -1)
591 waitpid_error (use_compress_program_option
);
595 if (WIFSIGNALED (wait_status
))
597 kill (child_pid
, WTERMSIG (wait_status
));
598 exit_status
= TAREXIT_FAILURE
;
600 else if (WEXITSTATUS (wait_status
) != 0)
601 exit_status
= WEXITSTATUS (wait_status
);
609 dec_to_env (char *envar
, uintmax_t num
)
611 char buf
[UINTMAX_STRSIZE_BOUND
];
614 numstr
= STRINGIFY_BIGINT (num
, buf
);
615 if (setenv (envar
, numstr
, 1) != 0)
620 time_to_env (char *envar
, struct timespec t
)
622 char buf
[TIMESPEC_STRSIZE_BOUND
];
623 if (setenv (envar
, code_timespec (t
, buf
), 1) != 0)
628 oct_to_env (char *envar
, unsigned long num
)
630 char buf
[1+1+(sizeof(unsigned long)*CHAR_BIT
+2)/3];
632 snprintf (buf
, sizeof buf
, "0%lo", num
);
633 if (setenv (envar
, buf
, 1) != 0)
638 str_to_env (char *envar
, char const *str
)
642 if (setenv (envar
, str
, 1) != 0)
650 chr_to_env (char *envar
, char c
)
655 if (setenv (envar
, buf
, 1) != 0)
660 stat_to_env (char *name
, char type
, struct tar_stat_info
*st
)
662 str_to_env ("TAR_VERSION", PACKAGE_VERSION
);
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");
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
);
720 xdup2 (p
[PREAD
], STDIN_FILENO
);
723 stat_to_env (file_name
, typechar
, st
);
727 argv
[2] = to_command_option
;
730 execv ("/bin/sh", argv
);
732 exec_fatal (file_name
);
736 sys_wait_command (void)
743 signal (SIGPIPE
, pipe_handler
);
744 while (waitpid (pid
, &status
, 0) == -1)
748 waitpid_error (to_command_option
);
752 if (WIFEXITED (status
))
754 if (!ignore_command_error_option
&& WEXITSTATUS (status
))
755 ERROR ((0, 0, _("%lu: Child returned status %d"),
756 (unsigned long) pid
, WEXITSTATUS (status
)));
758 else if (WIFSIGNALED (status
))
760 WARN ((0, 0, _("%lu: Child terminated on signal %d"),
761 (unsigned long) pid
, WTERMSIG (status
)));
764 ERROR ((0, 0, _("%lu: Child terminated on unknown reason"),
765 (unsigned long) pid
));
771 sys_exec_info_script (const char **archive_name
, int volume_number
)
775 char uintbuf
[UINTMAX_STRSIZE_BOUND
];
779 pipe_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 waitpid_error (info_script_option
);
808 if (WIFEXITED (status
))
810 if (WEXITSTATUS (status
) == 0 && rc
> 0)
814 return WEXITSTATUS (status
);
822 setenv ("TAR_VERSION", PACKAGE_VERSION
, 1);
823 setenv ("TAR_ARCHIVE", *archive_name
, 1);
824 setenv ("TAR_VOLUME", STRINGIFY_BIGINT (volume_number
, uintbuf
), 1);
825 setenv ("TAR_SUBCOMMAND", subcommand_string (subcommand_option
), 1);
826 setenv ("TAR_FORMAT",
827 archive_format_string (current_format
== DEFAULT_FORMAT
?
828 archive_format
: current_format
), 1);
829 setenv ("TAR_FD", STRINGIFY_BIGINT (p
[PWRITE
], uintbuf
), 1);
835 argv
[2] = (char*) info_script_option
;
838 execv (argv
[0], argv
);
840 exec_fatal (info_script_option
);
844 #endif /* not MSDOS */