extract.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  1. /* Extract files from a tar archive.
  2. Copyright (C) 1988, 1992 Free Software Foundation
  3. This file is part of GNU Tar.
  4. GNU Tar is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Tar is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Tar; see the file COPYING. If not, write to
  14. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  15. /*
  16. * Extract files from a tar archive.
  17. *
  18. * Written 19 Nov 1985 by John Gilmore, ihnp4!hoptoad!gnu.
  19. */
  20. #include <stdio.h>
  21. #include <errno.h>
  22. #ifndef STDC_HEADERS
  23. extern int errno;
  24. #endif
  25. #include <sys/types.h>
  26. #include <time.h>
  27. time_t time();
  28. #ifdef BSD42
  29. #include <sys/file.h>
  30. #else
  31. #ifndef V7
  32. #include <fcntl.h>
  33. #endif
  34. #endif
  35. #ifdef NO_OPEN3
  36. /* We need the #define's even though we don't use them. */
  37. #include "open3.h"
  38. #endif
  39. #ifdef EMUL_OPEN3
  40. /* Simulated 3-argument open for systems that don't have it */
  41. #include "open3.h"
  42. #endif
  43. #include "tar.h"
  44. #include "port.h"
  45. #if defined(_POSIX_VERSION)
  46. #include <utime.h>
  47. #endif
  48. extern FILE *msg_file;
  49. extern union record *head; /* Points to current tape header */
  50. extern struct stat hstat; /* Stat struct corresponding */
  51. extern int head_standard; /* Tape header is in ANSI format */
  52. extern char *save_name;
  53. extern long save_totsize;
  54. extern long save_sizeleft;
  55. int confirm();
  56. void decode_header();
  57. void extract_mangle();
  58. void extract_sparse_file();
  59. long from_oct();
  60. void gnu_restore();
  61. extern void print_header();
  62. extern void skip_file();
  63. extern void skip_extended_headers();
  64. extern void pr_mkdir();
  65. void saverec();
  66. int make_dirs(); /* Makes required directories */
  67. static time_t now = 0; /* Current time */
  68. static we_are_root = 0; /* True if our effective uid == 0 */
  69. static int notumask = ~0; /* Masks out bits user doesn't want */
  70. /*
  71. * "Scratch" space to store the information about a sparse file before
  72. * writing the info into the header or extended header
  73. */
  74. /*struct sp_array *sparsearray;*/
  75. /* number of elts storable in the sparsearray */
  76. /*int sp_array_size = 10;*/
  77. struct saved_dir_info
  78. {
  79. char *path;
  80. int mode;
  81. int atime;
  82. int mtime;
  83. struct saved_dir_info *next;
  84. };
  85. struct saved_dir_info *saved_dir_info_head;
  86. /*
  87. * Set up to extract files.
  88. */
  89. void
  90. extr_init()
  91. {
  92. int ourmask;
  93. now = time((time_t *)0);
  94. if (geteuid() == 0)
  95. we_are_root = 1;
  96. /*
  97. * We need to know our umask. But if f_use_protection is set,
  98. * leave our kernel umask at 0, and our "notumask" at ~0.
  99. */
  100. ourmask = umask(0); /* Read it */
  101. if (!f_use_protection) {
  102. (void) umask (ourmask); /* Set it back how it was */
  103. notumask = ~ourmask; /* Make umask override permissions */
  104. }
  105. }
  106. /*
  107. * Extract a file from the archive.
  108. */
  109. void
  110. extract_archive()
  111. {
  112. register char *data;
  113. int fd, check, namelen, written, openflag;
  114. long size;
  115. time_t acc_upd_times[2];
  116. register int skipcrud;
  117. register int i;
  118. /* int sparse_ind = 0;*/
  119. union record *exhdr;
  120. struct saved_dir_info *tmp;
  121. /* int end_nulls; */
  122. char **longp;
  123. char *bp;
  124. saverec(&head); /* Make sure it sticks around */
  125. userec(head); /* And go past it in the archive */
  126. decode_header(head, &hstat, &head_standard, 1); /* Snarf fields */
  127. if(f_confirm && !confirm("extract",current_file_name)) {
  128. if (head->header.isextended)
  129. skip_extended_headers();
  130. skip_file((long)hstat.st_size);
  131. saverec((union record **)0);
  132. return;
  133. }
  134. /* Print the record from 'head' and 'hstat' */
  135. if (f_verbose)
  136. print_header();
  137. /*
  138. * Check for fully specified pathnames and other atrocities.
  139. *
  140. * Note, we can't just make a pointer to the new file name,
  141. * since saverec() might move the header and adjust "head".
  142. * We have to start from "head" every time we want to touch
  143. * the header record.
  144. */
  145. skipcrud = 0;
  146. while (!f_absolute_paths
  147. && '/' == current_file_name[skipcrud]) {
  148. static int warned_once = 0;
  149. skipcrud++; /* Force relative path */
  150. if (!warned_once++) {
  151. msg("Removing leading / from absolute path names in the archive.");
  152. }
  153. }
  154. switch (head->header.linkflag) {
  155. default:
  156. msg("Unknown file type '%c' for %s, extracted as normal file",
  157. head->header.linkflag, skipcrud+current_file_name);
  158. /* FALL THRU */
  159. /*
  160. * JK - What we want to do if the file is sparse is loop through
  161. * the array of sparse structures in the header and read in
  162. * and translate the character strings representing 1) the offset
  163. * at which to write and 2) how many bytes to write into numbers,
  164. * which we store into the scratch array, "sparsearray". This
  165. * array makes our life easier the same way it did in creating
  166. * the tar file that had to deal with a sparse file.
  167. *
  168. * After we read in the first five (at most) sparse structures,
  169. * we check to see if the file has an extended header, i.e.,
  170. * if more sparse structures are needed to describe the contents
  171. * of the new file. If so, we read in the extended headers
  172. * and continue to store their contents into the sparsearray.
  173. */
  174. case LF_SPARSE:
  175. sp_array_size = 10;
  176. sparsearray = (struct sp_array *) malloc(sp_array_size * sizeof(struct sp_array));
  177. for (i = 0; i < SPARSE_IN_HDR; i++) {
  178. sparsearray[i].offset =
  179. from_oct(1+12, head->header.sp[i].offset);
  180. sparsearray[i].numbytes =
  181. from_oct(1+12, head->header.sp[i].numbytes);
  182. if (!sparsearray[i].numbytes)
  183. break;
  184. }
  185. /* end_nulls = from_oct(1+12, head->header.ending_blanks);*/
  186. if (head->header.isextended) {
  187. /* read in the list of extended headers
  188. and translate them into the sparsearray
  189. as before */
  190. /* static */ int ind = SPARSE_IN_HDR;
  191. for (;;) {
  192. exhdr = findrec();
  193. for (i = 0; i < SPARSE_EXT_HDR; i++) {
  194. if (i+ind > sp_array_size-1) {
  195. /*
  196. * realloc the scratch area
  197. * since we've run out of room --
  198. */
  199. sparsearray = (struct sp_array *)
  200. realloc(sparsearray,
  201. 2 * sp_array_size * (sizeof(struct sp_array)));
  202. sp_array_size *= 2;
  203. }
  204. if (!exhdr->ext_hdr.sp[i].numbytes)
  205. break;
  206. sparsearray[i+ind].offset =
  207. from_oct(1+12, exhdr->ext_hdr.sp[i].offset);
  208. sparsearray[i+ind].numbytes =
  209. from_oct(1+12, exhdr->ext_hdr.sp[i].numbytes);
  210. }
  211. if (!exhdr->ext_hdr.isextended)
  212. break;
  213. else {
  214. ind += SPARSE_EXT_HDR;
  215. userec(exhdr);
  216. }
  217. }
  218. userec(exhdr);
  219. }
  220. /* FALL THRU */
  221. case LF_OLDNORMAL:
  222. case LF_NORMAL:
  223. case LF_CONTIG:
  224. /*
  225. * Appears to be a file.
  226. * See if it's really a directory.
  227. */
  228. namelen = strlen(skipcrud+current_file_name)-1;
  229. if (current_file_name[skipcrud+namelen] == '/')
  230. goto really_dir;
  231. /* FIXME, deal with protection issues */
  232. again_file:
  233. openflag = (f_keep?
  234. O_BINARY|O_NDELAY|O_WRONLY|O_CREAT|O_EXCL:
  235. O_BINARY|O_NDELAY|O_WRONLY|O_CREAT|O_TRUNC)
  236. | ((head->header.linkflag == LF_SPARSE) ? 0 : O_APPEND);
  237. /*
  238. * JK - The last | is a kludge to solve the problem
  239. * the O_APPEND flag causes with files we are
  240. * trying to make sparse: when a file is opened
  241. * with O_APPEND, it writes to the last place
  242. * that something was written, thereby ignoring
  243. * any lseeks that we have done. We add this
  244. * extra condition to make it able to lseek when
  245. * a file is sparse, i.e., we don't open the new
  246. * file with this flag. (Grump -- this bug caused
  247. * me to waste a good deal of time, I might add)
  248. */
  249. if(f_exstdout) {
  250. fd = 1;
  251. goto extract_file;
  252. }
  253. #ifdef O_CTG
  254. /*
  255. * Contiguous files (on the Masscomp) have to specify
  256. * the size in the open call that creates them.
  257. */
  258. if (head->header.linkflag == LF_CONTIG)
  259. fd = open((longname ? longname : head->header.name)
  260. + skipcrud,
  261. openflag | O_CTG,
  262. hstat.st_mode, hstat.st_size);
  263. else
  264. #endif
  265. {
  266. #ifdef NO_OPEN3
  267. /*
  268. * On raw V7 we won't let them specify -k (f_keep), but
  269. * we just bull ahead and create the files.
  270. */
  271. fd = creat((longname
  272. ? longname
  273. : head->header.name) + skipcrud,
  274. hstat.st_mode);
  275. #else
  276. /*
  277. * With 3-arg open(), we can do this up right.
  278. */
  279. fd = open(skipcrud + current_file_name,
  280. openflag, hstat.st_mode);
  281. #endif
  282. }
  283. if (fd < 0) {
  284. if (make_dirs(skipcrud + current_file_name))
  285. goto again_file;
  286. msg_perror("Could not create file %s",
  287. skipcrud + current_file_name);
  288. if (head->header.isextended)
  289. skip_extended_headers();
  290. skip_file((long)hstat.st_size);
  291. goto quit;
  292. }
  293. extract_file:
  294. if (head->header.linkflag == LF_SPARSE) {
  295. char *name;
  296. int namelen;
  297. /*
  298. * Kludge alert. NAME is assigned to header.name
  299. * because during the extraction, the space that
  300. * contains the header will get scribbled on, and
  301. * the name will get munged, so any error messages
  302. * that happen to contain the filename will look
  303. * REAL interesting unless we do this.
  304. */
  305. namelen = strlen(skipcrud + current_file_name);
  306. name = (char *) malloc((sizeof(char)) * namelen);
  307. bcopy(skipcrud+current_file_name, name, namelen);
  308. size = hstat.st_size;
  309. extract_sparse_file(fd, &size, hstat.st_size, name);
  310. }
  311. else
  312. for (size = hstat.st_size;
  313. size > 0;
  314. size -= written) {
  315. /* long offset,
  316. numbytes;*/
  317. if(f_multivol) {
  318. save_name=current_file_name;
  319. save_totsize=hstat.st_size;
  320. save_sizeleft=size;
  321. }
  322. /*
  323. * Locate data, determine max length
  324. * writeable, write it, record that
  325. * we have used the data, then check
  326. * if the write worked.
  327. */
  328. data = findrec()->charptr;
  329. if (data == NULL) { /* Check it... */
  330. msg("Unexpected EOF on archive file");
  331. break;
  332. }
  333. /*
  334. * JK - If the file is sparse, use the sparsearray
  335. * that we created before to lseek into the new
  336. * file the proper amount, and to see how many
  337. * bytes we want to write at that position.
  338. */
  339. /* if (head->header.linkflag == LF_SPARSE) {
  340. off_t pos;
  341. pos = lseek(fd, (off_t) sparsearray[sparse_ind].offset, 0);
  342. printf("%d at %d\n", (int) pos, sparse_ind);
  343. written = sparsearray[sparse_ind++].numbytes;
  344. } else*/
  345. written = endofrecs()->charptr - data;
  346. if (written > size)
  347. written = size;
  348. errno = 0;
  349. check = write(fd, data, written);
  350. /*
  351. * The following is in violation of strict
  352. * typing, since the arg to userec
  353. * should be a struct rec *. FIXME.
  354. */
  355. userec((union record *)(data + written - 1));
  356. if (check == written) continue;
  357. /*
  358. * Error in writing to file.
  359. * Print it, skip to next file in archive.
  360. */
  361. if(check<0)
  362. msg_perror("couldn't write to file %s",
  363. skipcrud + current_file_name);
  364. else
  365. msg("could only write %d of %d bytes to file %s",
  366. written,check,skipcrud + current_file_name);
  367. skip_file((long)(size - written));
  368. break; /* Still do the close, mod time, chmod, etc */
  369. }
  370. if(f_multivol)
  371. save_name = 0;
  372. /* If writing to stdout, don't try to do anything
  373. to the filename; it doesn't exist, or we don't
  374. want to touch it anyway */
  375. if(f_exstdout)
  376. break;
  377. /* if (head->header.isextended) {
  378. register union record *exhdr;
  379. register int i;
  380. for (i = 0; i < 21; i++) {
  381. long offset;
  382. if (!exhdr->ext_hdr.sp[i].numbytes)
  383. break;
  384. offset = from_oct(1+12,
  385. exhdr->ext_hdr.sp[i].offset);
  386. written = from_oct(1+12,
  387. exhdr->ext_hdr.sp[i].numbytes);
  388. lseek(fd, offset, 0);
  389. check = write(fd, data, written);
  390. if (check == written) continue;
  391. }
  392. }*/
  393. check = close(fd);
  394. if (check < 0) {
  395. msg_perror("Error while closing %s",
  396. skipcrud + current_file_name);
  397. }
  398. set_filestat:
  399. /*
  400. * If we are root, set the owner and group of the extracted
  401. * file. This does what is wanted both on real Unix and on
  402. * System V. If we are running as a user, we extract as that
  403. * user; if running as root, we extract as the original owner.
  404. */
  405. if (we_are_root || f_do_chown) {
  406. if (chown(skipcrud + current_file_name,
  407. hstat.st_uid, hstat.st_gid) < 0) {
  408. msg_perror("cannot chown file %s to uid %d gid %d",
  409. skipcrud + current_file_name,
  410. hstat.st_uid,hstat.st_gid);
  411. }
  412. }
  413. /*
  414. * Set the modified time of the file.
  415. *
  416. * Note that we set the accessed time to "now", which
  417. * is really "the time we started extracting files".
  418. * unless f_gnudump is used, in which case .st_atime is used
  419. */
  420. if (!f_modified) {
  421. /* fixme if f_gnudump should set ctime too, but how? */
  422. if(f_gnudump)
  423. acc_upd_times[0]=hstat.st_atime;
  424. else acc_upd_times[0] = now; /* Accessed now */
  425. acc_upd_times[1] = hstat.st_mtime; /* Mod'd */
  426. if (utime(skipcrud + current_file_name,
  427. acc_upd_times) < 0) {
  428. msg_perror("couldn't change access and modification times of %s",skipcrud + current_file_name);
  429. }
  430. }
  431. /* We do the utime before the chmod because some versions of
  432. utime are broken and trash the modes of the file. Since
  433. we then change the mode anyway, we don't care. . . */
  434. /*
  435. * If '-k' is not set, open() or creat() could have saved
  436. * the permission bits from a previously created file,
  437. * ignoring the ones we specified.
  438. * Even if -k is set, if the file has abnormal
  439. * mode bits, we must chmod since writing or chown() has
  440. * probably reset them.
  441. *
  442. * If -k is set, we know *we* created this file, so the mode
  443. * bits were set by our open(). If the file is "normal", we
  444. * skip the chmod. This works because we did umask(0) if -p
  445. * is set, so umask will have left the specified mode alone.
  446. */
  447. if ((!f_keep)
  448. || (hstat.st_mode & (S_ISUID|S_ISGID|S_ISVTX))) {
  449. if (chmod(skipcrud + current_file_name,
  450. notumask & (int)hstat.st_mode) < 0) {
  451. msg_perror("cannot change mode of file %s to %ld",
  452. skipcrud + current_file_name,
  453. notumask & (int)hstat.st_mode);
  454. }
  455. }
  456. quit:
  457. break;
  458. case LF_LINK:
  459. again_link:
  460. {
  461. struct stat st1,st2;
  462. check = link (current_link_name, skipcrud + current_file_name);
  463. if (check == 0)
  464. break;
  465. if (make_dirs(skipcrud + current_file_name))
  466. goto again_link;
  467. if(f_gnudump && errno==EEXIST)
  468. break;
  469. if(stat(current_link_name, &st1) == 0
  470. && stat(current_file_name + skipcrud, &st2)==0
  471. && st1.st_dev==st2.st_dev
  472. && st1.st_ino==st2.st_ino)
  473. break;
  474. msg_perror("Could not link %s to %s",
  475. skipcrud + current_file_name,
  476. current_link_name);
  477. }
  478. break;
  479. #ifdef S_ISLNK
  480. case LF_SYMLINK:
  481. again_symlink:
  482. check = symlink(current_link_name,
  483. skipcrud + current_file_name);
  484. /* FIXME, don't worry uid, gid, etc... */
  485. if (check == 0)
  486. break;
  487. if (make_dirs(current_file_name + skipcrud))
  488. goto again_symlink;
  489. msg_perror("Could not create symlink to %s",
  490. current_link_name);
  491. break;
  492. #endif
  493. #ifdef S_IFCHR
  494. case LF_CHR:
  495. hstat.st_mode |= S_IFCHR;
  496. goto make_node;
  497. #endif
  498. #ifdef S_IFBLK
  499. case LF_BLK:
  500. hstat.st_mode |= S_IFBLK;
  501. #endif
  502. #if defined(S_IFCHR) || defined(S_IFBLK)
  503. make_node:
  504. check = mknod(current_file_name + skipcrud,
  505. (int) hstat.st_mode, (int) hstat.st_rdev);
  506. if (check != 0) {
  507. if (make_dirs(skipcrud + current_file_name))
  508. goto make_node;
  509. msg_perror("Could not make %s",
  510. current_file_name + skipcrud);
  511. break;
  512. };
  513. goto set_filestat;
  514. #endif
  515. #ifdef S_ISFIFO
  516. /* If local system doesn't support FIFOs, use default case */
  517. case LF_FIFO:
  518. make_fifo:
  519. check = mkfifo(current_file_name + skipcrud,
  520. (int) hstat.st_mode);
  521. if (check != 0) {
  522. if (make_dirs(current_file_name + skipcrud))
  523. goto make_fifo;
  524. msg_perror("Could not make %s",
  525. skipcrud + current_file_name);
  526. break;
  527. };
  528. goto set_filestat;
  529. #endif
  530. case LF_DIR:
  531. case LF_DUMPDIR:
  532. namelen = strlen(current_file_name) + skipcrud - 1;
  533. really_dir:
  534. /* Check for trailing /, and zap as many as we find. */
  535. while (namelen
  536. && current_file_name[skipcrud+namelen] == '/')
  537. current_file_name[skipcrud+namelen--] = '\0';
  538. if(f_gnudump) { /* Read the entry and delete files
  539. that aren't listed in the archive */
  540. gnu_restore(skipcrud);
  541. } else if(head->header.linkflag==LF_DUMPDIR)
  542. skip_file((long)(hstat.st_size));
  543. again_dir:
  544. check = mkdir(skipcrud+current_file_name,
  545. (we_are_root ? 0 : 0300) | (int)hstat.st_mode);
  546. if (check != 0) {
  547. struct stat st1;
  548. if (make_dirs(skipcrud+current_file_name))
  549. goto again_dir;
  550. /* If we're trying to create '.', let it be. */
  551. if (current_file_name[skipcrud+namelen] == '.' &&
  552. (namelen==0 ||
  553. current_file_name[skipcrud+namelen-1]=='/'))
  554. goto check_perms;
  555. if( errno==EEXIST
  556. && stat(skipcrud+current_file_name,&st1)==0
  557. && (S_ISDIR(st1.st_mode)))
  558. break;
  559. msg_perror("Could not create directory %s",skipcrud+current_file_name);
  560. break;
  561. }
  562. check_perms:
  563. if (!we_are_root && 0300 != (0300 & (int) hstat.st_mode)) {
  564. hstat.st_mode |= 0300;
  565. msg("Added write and execute permission to directory %s",
  566. skipcrud+current_file_name);
  567. }
  568. if (f_modified)
  569. goto set_filestat;
  570. tmp = (struct saved_dir_info *) malloc (sizeof (struct saved_dir_info));
  571. tmp->path = malloc (strlen (skipcrud + current_file_name) + 1);
  572. strcpy (tmp->path, skipcrud + current_file_name);
  573. tmp->mode = hstat.st_mode;
  574. tmp->atime = hstat.st_atime;
  575. tmp->mtime = hstat.st_mtime;
  576. tmp->next = saved_dir_info_head;
  577. saved_dir_info_head = tmp;
  578. case LF_VOLHDR:
  579. if(f_verbose) {
  580. printf("Reading %s\n", current_file_name);
  581. }
  582. break;
  583. case LF_NAMES:
  584. extract_mangle(head);
  585. break;
  586. case LF_MULTIVOL:
  587. msg("Can't extract '%s'--file is continued from another volume\n",current_file_name);
  588. skip_file((long)hstat.st_size);
  589. break;
  590. case LF_LONGNAME:
  591. case LF_LONGLINK:
  592. msg ("Visible long name error\n");
  593. skip_file ((long)hstat.st_size);
  594. break;
  595. }
  596. /* We don't need to save it any longer. */
  597. saverec((union record **) 0); /* Unsave it */
  598. }
  599. /*
  600. * After a file/link/symlink/dir creation has failed, see if
  601. * it's because some required directory was not present, and if
  602. * so, create all required dirs.
  603. */
  604. int
  605. make_dirs(pathname)
  606. char *pathname;
  607. {
  608. char *p; /* Points into path */
  609. int madeone = 0; /* Did we do anything yet? */
  610. int save_errno = errno; /* Remember caller's errno */
  611. int check;
  612. if (errno != ENOENT)
  613. return 0; /* Not our problem */
  614. for (p = index(pathname, '/'); p != NULL; p = index(p+1, '/')) {
  615. /* Avoid mkdir of empty string, if leading or double '/' */
  616. if (p == pathname || p[-1] == '/')
  617. continue;
  618. /* Avoid mkdir where last part of path is '.' */
  619. if (p[-1] == '.' && (p == pathname+1 || p[-2] == '/'))
  620. continue;
  621. *p = 0; /* Truncate the path there */
  622. check = mkdir (pathname, 0777); /* Try to create it as a dir */
  623. if (check == 0) {
  624. /* Fix ownership */
  625. if (we_are_root) {
  626. if (chown(pathname, hstat.st_uid,
  627. hstat.st_gid) < 0) {
  628. msg_perror("cannot change owner of %s to uid %d gid %d",pathname,hstat.st_uid,hstat.st_gid);
  629. }
  630. }
  631. pr_mkdir(pathname, p-pathname, notumask&0777);
  632. madeone++; /* Remember if we made one */
  633. *p = '/';
  634. continue;
  635. }
  636. *p = '/';
  637. if (errno == EEXIST) /* Directory already exists */
  638. continue;
  639. /*
  640. * Some other error in the mkdir. We return to the caller.
  641. */
  642. break;
  643. }
  644. errno = save_errno; /* Restore caller's errno */
  645. return madeone; /* Tell them to retry if we made one */
  646. }
  647. void
  648. extract_sparse_file(fd, sizeleft, totalsize, name)
  649. int fd;
  650. long *sizeleft,
  651. totalsize;
  652. char *name;
  653. {
  654. /* register char *data;*/
  655. union record *datarec;
  656. int sparse_ind = 0;
  657. int written,
  658. count;
  659. /* assuming sizeleft is initially totalsize */
  660. while (*sizeleft > 0) {
  661. datarec = findrec();
  662. if (datarec == NULL) {
  663. msg("Unexpected EOF on archive file");
  664. return;
  665. }
  666. lseek(fd, sparsearray[sparse_ind].offset, 0);
  667. written = sparsearray[sparse_ind++].numbytes;
  668. while (written > RECORDSIZE) {
  669. count = write(fd, datarec->charptr, RECORDSIZE);
  670. if (count < 0)
  671. msg_perror("couldn't write to file %s", name);
  672. written -= count;
  673. *sizeleft -= count;
  674. userec(datarec);
  675. datarec = findrec();
  676. }
  677. count = write(fd, datarec->charptr, written);
  678. if (count < 0) {
  679. msg_perror("couldn't write to file %s", name);
  680. } else if (count != written) {
  681. msg("could only write %d of %d bytes to file %s", totalsize - *sizeleft, totalsize, name);
  682. skip_file((long) (*sizeleft));
  683. }
  684. written -= count;
  685. *sizeleft -= count;
  686. userec(datarec);
  687. }
  688. free(sparsearray);
  689. /* if (end_nulls) {
  690. register int i;
  691. printf("%d\n", (int) end_nulls);
  692. for (i = 0; i < end_nulls; i++)
  693. write(fd, "\000", 1);
  694. }*/
  695. userec(datarec);
  696. }
  697. /* Set back the utime and mode for all the extracted directories. */
  698. void restore_saved_dir_info ()
  699. {
  700. time_t acc_upd_times[2];
  701. struct saved_dir_info *tmp;
  702. while (saved_dir_info_head != NULL)
  703. {
  704. /* fixme if f_gnudump should set ctime too, but how? */
  705. if(f_gnudump)
  706. acc_upd_times[0]=saved_dir_info_head -> atime;
  707. else acc_upd_times[0] = now; /* Accessed now */
  708. acc_upd_times[1] = saved_dir_info_head -> mtime; /* Mod'd */
  709. if (utime(saved_dir_info_head -> path, acc_upd_times) < 0) {
  710. msg_perror("couldn't change access and modification times of %s",
  711. saved_dir_info_head -> path);
  712. }
  713. if ((!f_keep) || (saved_dir_info_head -> mode & (S_ISUID|S_ISGID|S_ISVTX)))
  714. {
  715. if (chmod(saved_dir_info_head -> path,
  716. notumask & saved_dir_info_head -> mode) < 0) {
  717. msg_perror("cannot change mode of file %s to %ld",
  718. saved_dir_info_head -> path,
  719. notumask & saved_dir_info_head -> mode);
  720. }
  721. }
  722. saved_dir_info_head = saved_dir_info_head -> next;
  723. }
  724. }