system.c 15 KB

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