system.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. /* System-dependent calls for tar.
  2. Copyright (C) 2003 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify it
  4. under the terms of the GNU General Public License as published by the
  5. Free Software Foundation; either version 2, or (at your option) any later
  6. version.
  7. This program is distributed in the hope that it will be useful, but
  8. WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  10. Public License for more details.
  11. You should have received a copy of the GNU General Public License along
  12. with this program; if not, write to the Free Software Foundation, Inc.,
  13. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  14. #include "system.h"
  15. #include "common.h"
  16. #include "rmt.h"
  17. #include <signal.h>
  18. void
  19. sys_stat_nanoseconds(struct tar_stat_info *stat)
  20. {
  21. #if defined(HAVE_STRUCT_STAT_ST_SPARE1)
  22. stat->atime_nsec = stat->stat.st_spare1 * 1000;
  23. stat->mtime_nsec = stat->stat.st_spare2 * 1000;
  24. stat->ctime_nsec = stat->stat.st_spare3 * 1000;
  25. #elif defined(HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
  26. stat->atime_nsec = stat->stat.st_atim.tv_nsec;
  27. stat->mtime_nsec = stat->stat.st_mtim.tv_nsec;
  28. stat->ctime_nsec = stat->stat.st_ctim.tv_nsec;
  29. #elif defined(HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC)
  30. stat->atime_nsec = stat->stat.st_atimespec.tv_nsec;
  31. stat->mtime_nsec = stat->stat.st_mtimespec.tv_nsec;
  32. stat->ctime_nsec = stat->stat.st_ctimespec.tv_nsec;
  33. #elif defined(HAVE_STRUCT_STAT_ST_ATIMENSEC)
  34. stat->atime_nsec = stat->stat.st_atimensec;
  35. stat->mtime_nsec = stat->stat.st_mtimensec;
  36. stat->ctime_nsec = stat->stat.st_ctimensec;
  37. #else
  38. stat->atime_nsec = stat->mtime_nsec = stat->ctime_nsec = 0;
  39. #endif
  40. }
  41. int
  42. sys_utimes(char *file_name, struct timeval tvp[3])
  43. {
  44. #ifdef HAVE_UTIMES
  45. return utimes (file_name, tvp);
  46. #else
  47. struct utimbuf utimbuf;
  48. utimbuf.actime = tvp[0].tv_sec;
  49. utimbuf.modtime = tvp[1].tv_sec;
  50. return utime (file_name, &utimbuf);
  51. #endif
  52. }
  53. #if MSDOS
  54. bool
  55. sys_get_archive_stat ()
  56. {
  57. return 0;
  58. }
  59. bool
  60. sys_file_is_archive (struct tar_stat_info *p)
  61. {
  62. return false;
  63. }
  64. void
  65. sys_save_archive_dev_ino ()
  66. {
  67. }
  68. void
  69. sys_detect_dev_null_output ()
  70. {
  71. static char const dev_null[] = "nul";
  72. dev_null_output = (strcmp (archive_name_array[0], dev_null) == 0
  73. || (! _isrmt (archive)));
  74. }
  75. void
  76. sys_drain_input_pipe ()
  77. {
  78. }
  79. void
  80. sys_wait_for_child (pid_t child_pid)
  81. {
  82. }
  83. void
  84. sys_spawn_shell ()
  85. {
  86. spawnl (P_WAIT, getenv ("COMSPEC"), "-", 0);
  87. }
  88. /* stat() in djgpp's C library gives a constant number of 42 as the
  89. uid and gid of a file. So, comparing an FTP'ed archive just after
  90. unpack would fail on MSDOS. */
  91. bool
  92. sys_compare_uid (struct stat *a, struct stat *b)
  93. {
  94. return true;
  95. }
  96. bool
  97. sys_compare_gid (struct stat *a, struct stat *b)
  98. {
  99. return true;
  100. }
  101. void
  102. sys_compare_links (struct stat *link_data, struct stat *stat_data)
  103. {
  104. return true;
  105. }
  106. int
  107. sys_truncate (int fd)
  108. {
  109. return write (fd, "", 0);
  110. }
  111. void
  112. sys_reset_uid_gid ()
  113. {
  114. }
  115. ssize_t
  116. sys_write_archive_buffer (void)
  117. {
  118. ssize_t status;
  119. ssize_t written = 0;
  120. while (0 <= (status = full_write (archive, record_start->buffer + written,
  121. record_size - written)))
  122. {
  123. written += status;
  124. if (written == record_size)
  125. break;
  126. }
  127. return written ? written : status;
  128. }
  129. /* Set ARCHIVE for writing, then compressing an archive. */
  130. void
  131. sys_child_open_for_compress (void)
  132. {
  133. FATAL_ERROR ((0, 0, _("Cannot use compressed or remote archives")));
  134. }
  135. /* Set ARCHIVE for uncompressing, then reading an archive. */
  136. void
  137. sys_child_open_for_uncompress (void)
  138. {
  139. FATAL_ERROR ((0, 0, _("Cannot use compressed or remote archives")));
  140. }
  141. #else
  142. extern union block *record_start; /* FIXME */
  143. static struct stat archive_stat; /* stat block for archive file */
  144. bool
  145. sys_get_archive_stat ()
  146. {
  147. return fstat (archive, &archive_stat) == 0;
  148. }
  149. bool
  150. sys_file_is_archive (struct tar_stat_info *p)
  151. {
  152. return (ar_dev && p->stat.st_dev == ar_dev && p->stat.st_ino == ar_ino);
  153. }
  154. /* Save archive file inode and device numbers */
  155. void
  156. sys_save_archive_dev_ino ()
  157. {
  158. if (!_isrmt (archive) && S_ISREG (archive_stat.st_mode))
  159. {
  160. ar_dev = archive_stat.st_dev;
  161. ar_ino = archive_stat.st_ino;
  162. }
  163. else
  164. ar_dev = 0;
  165. }
  166. /* Detect if outputting to "/dev/null". */
  167. void
  168. sys_detect_dev_null_output ()
  169. {
  170. static char const dev_null[] = "/dev/null";
  171. struct stat dev_null_stat;
  172. dev_null_output = (strcmp (archive_name_array[0], dev_null) == 0
  173. || (! _isrmt (archive)
  174. && S_ISCHR (archive_stat.st_mode)
  175. && stat (dev_null, &dev_null_stat) == 0
  176. && archive_stat.st_dev == dev_null_stat.st_dev
  177. && archive_stat.st_ino == dev_null_stat.st_ino));
  178. }
  179. /* Manage to fully drain a pipe we might be reading, so to not break it on
  180. the producer after the EOF block. FIXME: one of these days, GNU tar
  181. might become clever enough to just stop working, once there is no more
  182. work to do, we might have to revise this area in such time. */
  183. void
  184. sys_drain_input_pipe ()
  185. {
  186. if (access_mode == ACCESS_READ
  187. && ! _isrmt (archive)
  188. && (S_ISFIFO (archive_stat.st_mode) || S_ISSOCK (archive_stat.st_mode)))
  189. while (rmtread (archive, record_start->buffer, record_size) > 0)
  190. continue;
  191. }
  192. void
  193. sys_wait_for_child (pid_t child_pid)
  194. {
  195. if (child_pid)
  196. {
  197. int wait_status;
  198. while (waitpid (child_pid, &wait_status, 0) == -1)
  199. if (errno != EINTR)
  200. {
  201. waitpid_error (use_compress_program_option);
  202. break;
  203. }
  204. if (WIFSIGNALED (wait_status))
  205. ERROR ((0, 0, _("Child died with signal %d"),
  206. WTERMSIG (wait_status)));
  207. else if (WEXITSTATUS (wait_status) != 0)
  208. ERROR ((0, 0, _("Child returned status %d"),
  209. WEXITSTATUS (wait_status)));
  210. }
  211. }
  212. void
  213. sys_spawn_shell ()
  214. {
  215. pid_t child;
  216. const char *shell = getenv ("SHELL");
  217. if (! shell)
  218. shell = "/bin/sh";
  219. child = xfork ();
  220. if (child == 0)
  221. {
  222. execlp (shell, "-sh", "-i", (char *) 0);
  223. exec_fatal (shell);
  224. }
  225. else
  226. {
  227. int wait_status;
  228. while (waitpid (child, &wait_status, 0) == -1)
  229. if (errno != EINTR)
  230. {
  231. waitpid_error (shell);
  232. break;
  233. }
  234. }
  235. }
  236. bool
  237. sys_compare_uid (struct stat *a, struct stat *b)
  238. {
  239. return a->st_uid == b->st_uid;
  240. }
  241. bool
  242. sys_compare_gid (struct stat *a, struct stat *b)
  243. {
  244. return a->st_gid == b->st_gid;
  245. }
  246. bool
  247. sys_compare_links (struct stat *link_data, struct stat *stat_data)
  248. {
  249. return stat_data->st_dev == link_data->st_dev
  250. && stat_data->st_ino == link_data->st_ino;
  251. }
  252. int
  253. sys_truncate (int fd)
  254. {
  255. off_t pos = lseek (fd, (off_t) 0, SEEK_CUR);
  256. return pos < 0 ? -1 : ftruncate (fd, pos);
  257. }
  258. void
  259. sys_reset_uid_gid ()
  260. {
  261. setuid (getuid ());
  262. setgid (getgid ());
  263. }
  264. /* Return nonzero if NAME is the name of a regular file, or if the file
  265. does not exist (so it would be created as a regular file). */
  266. static int
  267. is_regular_file (const char *name)
  268. {
  269. struct stat stbuf;
  270. if (stat (name, &stbuf) == 0)
  271. return S_ISREG (stbuf.st_mode);
  272. else
  273. return errno == ENOENT;
  274. }
  275. ssize_t
  276. sys_write_archive_buffer (void)
  277. {
  278. ssize_t status;
  279. ssize_t written = 0;
  280. while (0 <= (status = rmtwrite (archive, record_start->buffer + written,
  281. record_size - written)))
  282. {
  283. written += status;
  284. if (written == record_size
  285. || _isrmt (archive)
  286. || ! (S_ISFIFO (archive_stat.st_mode)
  287. || S_ISSOCK (archive_stat.st_mode)))
  288. break;
  289. }
  290. return written ? written : status;
  291. }
  292. #define PREAD 0 /* read file descriptor from pipe() */
  293. #define PWRITE 1 /* write file descriptor from pipe() */
  294. /* Duplicate file descriptor FROM into becoming INTO.
  295. INTO is closed first and has to be the next available slot. */
  296. static void
  297. xdup2 (int from, int into)
  298. {
  299. if (from != into)
  300. {
  301. int status = close (into);
  302. if (status != 0 && errno != EBADF)
  303. {
  304. int e = errno;
  305. FATAL_ERROR ((0, e, _("Cannot close")));
  306. }
  307. status = dup (from);
  308. if (status != into)
  309. {
  310. if (status < 0)
  311. {
  312. int e = errno;
  313. FATAL_ERROR ((0, e, _("Cannot dup")));
  314. }
  315. abort ();
  316. }
  317. xclose (from);
  318. }
  319. }
  320. /* Set ARCHIVE for writing, then compressing an archive. */
  321. pid_t
  322. sys_child_open_for_compress (void)
  323. {
  324. int parent_pipe[2];
  325. int child_pipe[2];
  326. pid_t grandchild_pid;
  327. pid_t child_pid;
  328. int wait_status;
  329. xpipe (parent_pipe);
  330. child_pid = xfork ();
  331. if (child_pid > 0)
  332. {
  333. /* The parent tar is still here! Just clean up. */
  334. archive = parent_pipe[PWRITE];
  335. xclose (parent_pipe[PREAD]);
  336. return child_pid;
  337. }
  338. /* The new born child tar is here! */
  339. program_name = _("tar (child)");
  340. xdup2 (parent_pipe[PREAD], STDIN_FILENO);
  341. xclose (parent_pipe[PWRITE]);
  342. /* Check if we need a grandchild tar. This happens only if either:
  343. a) we are writing stdout: to force reblocking;
  344. b) the file is to be accessed by rmt: compressor doesn't know how;
  345. c) the file is not a plain file. */
  346. if (strcmp (archive_name_array[0], "-") != 0
  347. && !_remdev (archive_name_array[0])
  348. && is_regular_file (archive_name_array[0]))
  349. {
  350. if (backup_option)
  351. maybe_backup_file (archive_name_array[0], 1);
  352. /* We don't need a grandchild tar. Open the archive and launch the
  353. compressor. */
  354. archive = creat (archive_name_array[0], MODE_RW);
  355. if (archive < 0)
  356. {
  357. int saved_errno = errno;
  358. if (backup_option)
  359. undo_last_backup ();
  360. errno = saved_errno;
  361. open_fatal (archive_name_array[0]);
  362. }
  363. xdup2 (archive, STDOUT_FILENO);
  364. execlp (use_compress_program_option, use_compress_program_option,
  365. (char *) 0);
  366. exec_fatal (use_compress_program_option);
  367. }
  368. /* We do need a grandchild tar. */
  369. xpipe (child_pipe);
  370. grandchild_pid = xfork ();
  371. if (grandchild_pid == 0)
  372. {
  373. /* The newborn grandchild tar is here! Launch the compressor. */
  374. program_name = _("tar (grandchild)");
  375. xdup2 (child_pipe[PWRITE], STDOUT_FILENO);
  376. xclose (child_pipe[PREAD]);
  377. execlp (use_compress_program_option, use_compress_program_option,
  378. (char *) 0);
  379. exec_fatal (use_compress_program_option);
  380. }
  381. /* The child tar is still here! */
  382. /* Prepare for reblocking the data from the compressor into the archive. */
  383. xdup2 (child_pipe[PREAD], STDIN_FILENO);
  384. xclose (child_pipe[PWRITE]);
  385. if (strcmp (archive_name_array[0], "-") == 0)
  386. archive = STDOUT_FILENO;
  387. else
  388. {
  389. archive = rmtcreat (archive_name_array[0], MODE_RW, rsh_command_option);
  390. if (archive < 0)
  391. open_fatal (archive_name_array[0]);
  392. }
  393. /* Let's read out of the stdin pipe and write an archive. */
  394. while (1)
  395. {
  396. ssize_t status = 0;
  397. char *cursor;
  398. size_t length;
  399. /* Assemble a record. */
  400. for (length = 0, cursor = record_start->buffer;
  401. length < record_size;
  402. length += status, cursor += status)
  403. {
  404. size_t size = record_size - length;
  405. status = safe_read (STDIN_FILENO, cursor, size);
  406. if (status <= 0)
  407. break;
  408. }
  409. if (status < 0)
  410. read_fatal (use_compress_program_option);
  411. /* Copy the record. */
  412. if (status == 0)
  413. {
  414. /* We hit the end of the file. Write last record at
  415. full length, as the only role of the grandchild is
  416. doing proper reblocking. */
  417. if (length > 0)
  418. {
  419. memset (record_start->buffer + length, 0, record_size - length);
  420. status = sys_write_archive_buffer ();
  421. if (status != record_size)
  422. archive_write_error (status);
  423. }
  424. /* There is nothing else to read, break out. */
  425. break;
  426. }
  427. status = sys_write_archive_buffer ();
  428. if (status != record_size)
  429. archive_write_error (status);
  430. }
  431. /* Propagate any failure of the grandchild back to the parent. */
  432. while (waitpid (grandchild_pid, &wait_status, 0) == -1)
  433. if (errno != EINTR)
  434. {
  435. waitpid_error (use_compress_program_option);
  436. break;
  437. }
  438. if (WIFSIGNALED (wait_status))
  439. {
  440. kill (child_pid, WTERMSIG (wait_status));
  441. exit_status = TAREXIT_FAILURE;
  442. }
  443. else if (WEXITSTATUS (wait_status) != 0)
  444. exit_status = WEXITSTATUS (wait_status);
  445. exit (exit_status);
  446. }
  447. /* Set ARCHIVE for uncompressing, then reading an archive. */
  448. pid_t
  449. sys_child_open_for_uncompress (void)
  450. {
  451. int parent_pipe[2];
  452. int child_pipe[2];
  453. pid_t grandchild_pid;
  454. pid_t child_pid;
  455. int wait_status;
  456. xpipe (parent_pipe);
  457. child_pid = xfork ();
  458. if (child_pid > 0)
  459. {
  460. /* The parent tar is still here! Just clean up. */
  461. read_full_records_option = true;
  462. archive = parent_pipe[PREAD];
  463. xclose (parent_pipe[PWRITE]);
  464. return child_pid;
  465. }
  466. /* The newborn child tar is here! */
  467. program_name = _("tar (child)");
  468. xdup2 (parent_pipe[PWRITE], STDOUT_FILENO);
  469. xclose (parent_pipe[PREAD]);
  470. /* Check if we need a grandchild tar. This happens only if either:
  471. a) we're reading stdin: to force unblocking;
  472. b) the file is to be accessed by rmt: compressor doesn't know how;
  473. c) the file is not a plain file. */
  474. if (strcmp (archive_name_array[0], "-") != 0
  475. && !_remdev (archive_name_array[0])
  476. && is_regular_file (archive_name_array[0]))
  477. {
  478. /* We don't need a grandchild tar. Open the archive and lauch the
  479. uncompressor. */
  480. archive = open (archive_name_array[0], O_RDONLY | O_BINARY, MODE_RW);
  481. if (archive < 0)
  482. open_fatal (archive_name_array[0]);
  483. xdup2 (archive, STDIN_FILENO);
  484. execlp (use_compress_program_option, use_compress_program_option,
  485. "-d", (char *) 0);
  486. exec_fatal (use_compress_program_option);
  487. }
  488. /* We do need a grandchild tar. */
  489. xpipe (child_pipe);
  490. grandchild_pid = xfork ();
  491. if (grandchild_pid == 0)
  492. {
  493. /* The newborn grandchild tar is here! Launch the uncompressor. */
  494. program_name = _("tar (grandchild)");
  495. xdup2 (child_pipe[PREAD], STDIN_FILENO);
  496. xclose (child_pipe[PWRITE]);
  497. execlp (use_compress_program_option, use_compress_program_option,
  498. "-d", (char *) 0);
  499. exec_fatal (use_compress_program_option);
  500. }
  501. /* The child tar is still here! */
  502. /* Prepare for unblocking the data from the archive into the
  503. uncompressor. */
  504. xdup2 (child_pipe[PWRITE], STDOUT_FILENO);
  505. xclose (child_pipe[PREAD]);
  506. if (strcmp (archive_name_array[0], "-") == 0)
  507. archive = STDIN_FILENO;
  508. else
  509. archive = rmtopen (archive_name_array[0], O_RDONLY | O_BINARY,
  510. MODE_RW, rsh_command_option);
  511. if (archive < 0)
  512. open_fatal (archive_name_array[0]);
  513. /* Let's read the archive and pipe it into stdout. */
  514. while (1)
  515. {
  516. char *cursor;
  517. size_t maximum;
  518. size_t count;
  519. ssize_t status;
  520. clear_read_error_count ();
  521. error_loop:
  522. status = rmtread (archive, record_start->buffer, record_size);
  523. if (status < 0)
  524. {
  525. archive_read_error ();
  526. goto error_loop;
  527. }
  528. if (status == 0)
  529. break;
  530. cursor = record_start->buffer;
  531. maximum = status;
  532. while (maximum)
  533. {
  534. count = maximum < BLOCKSIZE ? maximum : BLOCKSIZE;
  535. if (full_write (STDOUT_FILENO, cursor, count) != count)
  536. write_error (use_compress_program_option);
  537. cursor += count;
  538. maximum -= count;
  539. }
  540. }
  541. xclose (STDOUT_FILENO);
  542. /* Propagate any failure of the grandchild back to the parent. */
  543. while (waitpid (grandchild_pid, &wait_status, 0) == -1)
  544. if (errno != EINTR)
  545. {
  546. waitpid_error (use_compress_program_option);
  547. break;
  548. }
  549. if (WIFSIGNALED (wait_status))
  550. {
  551. kill (child_pid, WTERMSIG (wait_status));
  552. exit_status = TAREXIT_FAILURE;
  553. }
  554. else if (WEXITSTATUS (wait_status) != 0)
  555. exit_status = WEXITSTATUS (wait_status);
  556. exit (exit_status);
  557. }
  558. #endif /* not MSDOS */