create.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454
  1. /* Create a tar archive.
  2. Copyright (C) 1985, 1992, 1993 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. * Create a tar archive.
  17. *
  18. * Written 25 Aug 1985 by John Gilmore, ihnp4!hoptoad!gnu.
  19. */
  20. #ifdef _AIX
  21. #pragma alloca
  22. #endif
  23. #include <sys/types.h>
  24. #include <stdio.h>
  25. #include <errno.h>
  26. #ifndef STDC_HEADERS
  27. extern int errno;
  28. #endif
  29. #ifdef BSD42
  30. #include <sys/file.h>
  31. #else
  32. #ifndef V7
  33. #include <fcntl.h>
  34. #endif
  35. #endif
  36. #include "tar.h"
  37. #include "port.h"
  38. #ifndef __MSDOS__
  39. #include <pwd.h>
  40. #include <grp.h>
  41. #endif
  42. #if defined (_POSIX_VERSION)
  43. #include <utime.h>
  44. #else
  45. struct utimbuf
  46. {
  47. long actime;
  48. long modtime;
  49. };
  50. #endif
  51. extern struct stat hstat; /* Stat struct corresponding */
  52. #ifndef __MSDOS__
  53. extern dev_t ar_dev;
  54. extern ino_t ar_ino;
  55. #endif
  56. /* JF */
  57. extern struct name *gnu_list_name;
  58. /*
  59. * If there are no symbolic links, there is no lstat(). Use stat().
  60. */
  61. #ifndef S_ISLNK
  62. #define lstat stat
  63. #endif
  64. extern void print_header ();
  65. union record *start_header ();
  66. void blank_name_list ();
  67. int check_exclude ();
  68. PTR ck_malloc ();
  69. PTR ck_realloc ();
  70. void clear_buffer ();
  71. void close_archive ();
  72. void collect_and_sort_names ();
  73. int confirm ();
  74. int deal_with_sparse ();
  75. void find_new_file_size ();
  76. void finish_header ();
  77. int finish_sparse_file ();
  78. void finduname ();
  79. void findgname ();
  80. int is_dot_or_dotdot ();
  81. void open_archive ();
  82. char *name_next ();
  83. void name_close ();
  84. void to_oct ();
  85. void dump_file ();
  86. void write_dir_file ();
  87. void write_eot ();
  88. void write_long ();
  89. int zero_record ();
  90. /* This code moved from tar.h since create.c is the only file that cares
  91. about 'struct link's. This means that other files might not have to
  92. include sys/types.h any more. */
  93. struct link
  94. {
  95. struct link *next;
  96. dev_t dev;
  97. ino_t ino;
  98. short linkcount;
  99. char name[1];
  100. };
  101. struct link *linklist; /* Points to first link in list */
  102. static nolinks; /* Gets set if we run out of RAM */
  103. /*
  104. * "Scratch" space to store the information about a sparse file before
  105. * writing the info into the header or extended header
  106. */
  107. /* struct sp_array *sparsearray;*/
  108. /* number of elts storable in the sparsearray */
  109. /*int sparse_array_size = 10;*/
  110. void
  111. create_archive ()
  112. {
  113. register char *p;
  114. char *name_from_list ();
  115. open_archive (0); /* Open for writing */
  116. if (f_gnudump)
  117. {
  118. char *buf = ck_malloc (PATH_MAX);
  119. char *q, *bufp;
  120. collect_and_sort_names ();
  121. while (p = name_from_list ())
  122. dump_file (p, -1, 1);
  123. /* if(!f_dironly) { */
  124. blank_name_list ();
  125. while (p = name_from_list ())
  126. {
  127. strcpy (buf, p);
  128. if (p[strlen (p) - 1] != '/')
  129. strcat (buf, "/");
  130. bufp = buf + strlen (buf);
  131. for (q = gnu_list_name->dir_contents; q && *q; q += strlen (q) + 1)
  132. {
  133. if (*q == 'Y')
  134. {
  135. strcpy (bufp, q + 1);
  136. dump_file (buf, -1, 1);
  137. }
  138. }
  139. }
  140. /* } */
  141. free (buf);
  142. }
  143. else
  144. {
  145. while (p = name_next (1))
  146. dump_file (p, -1, 1);
  147. }
  148. write_eot ();
  149. close_archive ();
  150. if (f_gnudump)
  151. write_dir_file ();
  152. name_close ();
  153. }
  154. /*
  155. * Dump a single file. If it's a directory, recurse.
  156. * Result is 1 for success, 0 for failure.
  157. * Sets global "hstat" to stat() output for this file.
  158. */
  159. void
  160. dump_file (p, curdev, toplevel)
  161. char *p; /* File name to dump */
  162. int curdev; /* Device our parent dir was on */
  163. int toplevel; /* Whether we are a toplevel call */
  164. {
  165. union record *header;
  166. char type;
  167. extern char *save_name; /* JF for multi-volume support */
  168. extern long save_totsize;
  169. extern long save_sizeleft;
  170. union record *exhdr;
  171. char save_linkflag;
  172. extern time_t new_time;
  173. int critical_error = 0;
  174. struct utimbuf restore_times;
  175. /* int sparse_ind = 0;*/
  176. if (f_confirm && !confirm ("add", p))
  177. return;
  178. /*
  179. * Use stat if following (rather than dumping) 4.2BSD's
  180. * symbolic links. Otherwise, use lstat (which, on non-4.2
  181. * systems, is #define'd to stat anyway.
  182. */
  183. #ifdef STX_HIDDEN /* AIX */
  184. if (0 != f_follow_links ?
  185. statx (p, &hstat, STATSIZE, STX_HIDDEN) :
  186. statx (p, &hstat, STATSIZE, STX_HIDDEN | STX_LINK))
  187. #else
  188. if (0 != f_follow_links ? stat (p, &hstat) : lstat (p, &hstat))
  189. #endif
  190. {
  191. badperror:
  192. msg_perror ("can't add file %s", p);
  193. badfile:
  194. if (!f_ignore_failed_read || critical_error)
  195. errors++;
  196. return;
  197. }
  198. restore_times.actime = hstat.st_atime;
  199. restore_times.modtime = hstat.st_mtime;
  200. #ifdef S_ISHIDDEN
  201. if (S_ISHIDDEN (hstat.st_mode))
  202. {
  203. char *new = (char *) alloca (strlen (p) + 2);
  204. if (new)
  205. {
  206. strcpy (new, p);
  207. strcat (new, "@");
  208. p = new;
  209. }
  210. }
  211. #endif
  212. /* See if we only want new files, and check if this one is too old to
  213. put in the archive. */
  214. if (f_new_files
  215. && !f_gnudump
  216. && new_time > hstat.st_mtime
  217. && !S_ISDIR (hstat.st_mode)
  218. && (f_new_files > 1 || new_time > hstat.st_ctime))
  219. {
  220. if (curdev == -1)
  221. {
  222. msg ("%s: is unchanged; not dumped", p);
  223. }
  224. return;
  225. }
  226. #ifndef __MSDOS__
  227. /* See if we are trying to dump the archive */
  228. if (ar_dev && hstat.st_dev == ar_dev && hstat.st_ino == ar_ino)
  229. {
  230. msg ("%s is the archive; not dumped", p);
  231. return;
  232. }
  233. #endif
  234. /*
  235. * Check for multiple links.
  236. *
  237. * We maintain a list of all such files that we've written so
  238. * far. Any time we see another, we check the list and
  239. * avoid dumping the data again if we've done it once already.
  240. */
  241. if (hstat.st_nlink > 1
  242. && (S_ISREG (hstat.st_mode)
  243. #ifdef S_ISCTG
  244. || S_ISCTG (hstat.st_mode)
  245. #endif
  246. #ifdef S_ISCHR
  247. || S_ISCHR (hstat.st_mode)
  248. #endif
  249. #ifdef S_ISBLK
  250. || S_ISBLK (hstat.st_mode)
  251. #endif
  252. #ifdef S_ISFIFO
  253. || S_ISFIFO (hstat.st_mode)
  254. #endif
  255. ))
  256. {
  257. register struct link *lp;
  258. /* First quick and dirty. Hashing, etc later FIXME */
  259. for (lp = linklist; lp; lp = lp->next)
  260. {
  261. if (lp->ino == hstat.st_ino &&
  262. lp->dev == hstat.st_dev)
  263. {
  264. char *link_name = lp->name;
  265. /* We found a link. */
  266. while (!f_absolute_paths && *link_name == '/')
  267. {
  268. static int link_warn = 0;
  269. if (!link_warn)
  270. {
  271. msg ("Removing leading / from absolute links");
  272. link_warn++;
  273. }
  274. link_name++;
  275. }
  276. if (link_name - lp->name >= NAMSIZ)
  277. write_long (link_name, LF_LONGLINK);
  278. current_link_name = link_name;
  279. hstat.st_size = 0;
  280. header = start_header (p, &hstat);
  281. if (header == NULL)
  282. {
  283. critical_error = 1;
  284. goto badfile;
  285. }
  286. strncpy (header->header.arch_linkname,
  287. link_name, NAMSIZ);
  288. /* Force null truncated */
  289. header->header.arch_linkname[NAMSIZ - 1] = 0;
  290. header->header.linkflag = LF_LINK;
  291. finish_header (header);
  292. /* FIXME: Maybe remove from list after all links found? */
  293. if (f_remove_files)
  294. {
  295. if (unlink (p) == -1)
  296. msg_perror ("cannot remove %s", p);
  297. }
  298. return; /* We dumped it */
  299. }
  300. }
  301. /* Not found. Add it to the list of possible links. */
  302. lp = (struct link *) ck_malloc ((unsigned) (sizeof (struct link) + strlen (p)));
  303. if (!lp)
  304. {
  305. if (!nolinks)
  306. {
  307. msg (
  308. "no memory for links, they will be dumped as separate files");
  309. nolinks++;
  310. }
  311. }
  312. lp->ino = hstat.st_ino;
  313. lp->dev = hstat.st_dev;
  314. strcpy (lp->name, p);
  315. lp->next = linklist;
  316. linklist = lp;
  317. }
  318. /*
  319. * This is not a link to a previously dumped file, so dump it.
  320. */
  321. if (S_ISREG (hstat.st_mode)
  322. #ifdef S_ISCTG
  323. || S_ISCTG (hstat.st_mode)
  324. #endif
  325. )
  326. {
  327. int f; /* File descriptor */
  328. long bufsize, count;
  329. long sizeleft;
  330. register union record *start;
  331. int header_moved;
  332. char isextended = 0;
  333. int upperbound;
  334. /* int end_nulls = 0; */
  335. header_moved = 0;
  336. #ifdef BSD42
  337. if (f_sparse_files)
  338. {
  339. /*
  340. * JK - This is the test for sparseness: whether the
  341. * "size" of the file matches the number of blocks
  342. * allocated for it. If there is a smaller number
  343. * of blocks that would be necessary to accommodate
  344. * a file of this size, we have a sparse file, i.e.,
  345. * at least one of those records in the file is just
  346. * a useless hole.
  347. */
  348. #ifdef hpux /* Nice of HPUX to gratuitiously change it, huh? - mib */
  349. if (hstat.st_size - (hstat.st_blocks * 1024) > 1024)
  350. #else
  351. if (hstat.st_size - (hstat.st_blocks * RECORDSIZE) > RECORDSIZE)
  352. #endif
  353. {
  354. int filesize = hstat.st_size;
  355. register int i;
  356. header = start_header (p, &hstat);
  357. if (header == NULL)
  358. {
  359. critical_error = 1;
  360. goto badfile;
  361. }
  362. header->header.linkflag = LF_SPARSE;
  363. header_moved++;
  364. /*
  365. * Call the routine that figures out the
  366. * layout of the sparse file in question.
  367. * UPPERBOUND is the index of the last
  368. * element of the "sparsearray," i.e.,
  369. * the number of elements it needed to
  370. * describe the file.
  371. */
  372. upperbound = deal_with_sparse (p, header);
  373. /*
  374. * See if we'll need an extended header
  375. * later
  376. */
  377. if (upperbound > SPARSE_IN_HDR - 1)
  378. header->header.isextended++;
  379. /*
  380. * We store the "real" file size so
  381. * we can show that in case someone wants
  382. * to list the archive, i.e., tar tvf <file>.
  383. * It might be kind of disconcerting if the
  384. * shrunken file size was the one that showed
  385. * up.
  386. */
  387. to_oct ((long) hstat.st_size, 1 + 12,
  388. header->header.realsize);
  389. /*
  390. * This will be the new "size" of the
  391. * file, i.e., the size of the file
  392. * minus the records of holes that we're
  393. * skipping over.
  394. */
  395. find_new_file_size (&filesize, upperbound);
  396. hstat.st_size = filesize;
  397. to_oct ((long) filesize, 1 + 12,
  398. header->header.size);
  399. /* to_oct((long) end_nulls, 1+12,
  400. header->header.ending_blanks);*/
  401. for (i = 0; i < SPARSE_IN_HDR; i++)
  402. {
  403. if (!sparsearray[i].numbytes)
  404. break;
  405. to_oct (sparsearray[i].offset, 1 + 12,
  406. header->header.sp[i].offset);
  407. to_oct (sparsearray[i].numbytes, 1 + 12,
  408. header->header.sp[i].numbytes);
  409. }
  410. }
  411. }
  412. #else
  413. upperbound = SPARSE_IN_HDR - 1;
  414. #endif
  415. sizeleft = hstat.st_size;
  416. /* Don't bother opening empty, world readable files. */
  417. if (sizeleft > 0 || 0444 != (0444 & hstat.st_mode))
  418. {
  419. f = open (p, O_RDONLY | O_BINARY);
  420. if (f < 0)
  421. goto badperror;
  422. }
  423. else
  424. {
  425. f = -1;
  426. }
  427. /* If the file is sparse, we've already taken care of this */
  428. if (!header_moved)
  429. {
  430. header = start_header (p, &hstat);
  431. if (header == NULL)
  432. {
  433. if (f >= 0)
  434. (void) close (f);
  435. critical_error = 1;
  436. goto badfile;
  437. }
  438. }
  439. #ifdef S_ISCTG
  440. /* Mark contiguous files, if we support them */
  441. if (f_standard && S_ISCTG (hstat.st_mode))
  442. {
  443. header->header.linkflag = LF_CONTIG;
  444. }
  445. #endif
  446. isextended = header->header.isextended;
  447. save_linkflag = header->header.linkflag;
  448. finish_header (header);
  449. if (isextended)
  450. {
  451. /* int sum = 0;*/
  452. register int i;
  453. /* register union record *exhdr;*/
  454. /* int arraybound = SPARSE_EXT_HDR;*/
  455. /* static */ int index_offset = SPARSE_IN_HDR;
  456. extend:exhdr = findrec ();
  457. if (exhdr == NULL)
  458. {
  459. critical_error = 1;
  460. goto badfile;
  461. }
  462. bzero (exhdr->charptr, RECORDSIZE);
  463. for (i = 0; i < SPARSE_EXT_HDR; i++)
  464. {
  465. if (i + index_offset > upperbound)
  466. break;
  467. to_oct ((long) sparsearray[i + index_offset].numbytes,
  468. 1 + 12,
  469. exhdr->ext_hdr.sp[i].numbytes);
  470. to_oct ((long) sparsearray[i + index_offset].offset,
  471. 1 + 12,
  472. exhdr->ext_hdr.sp[i].offset);
  473. }
  474. userec (exhdr);
  475. /* sum += i;
  476. if (sum < upperbound)
  477. goto extend;*/
  478. if (index_offset + i <= upperbound)
  479. {
  480. index_offset += i;
  481. exhdr->ext_hdr.isextended++;
  482. goto extend;
  483. }
  484. }
  485. if (save_linkflag == LF_SPARSE)
  486. {
  487. if (finish_sparse_file (f, &sizeleft, hstat.st_size, p))
  488. goto padit;
  489. }
  490. else
  491. while (sizeleft > 0)
  492. {
  493. if (f_multivol)
  494. {
  495. save_name = p;
  496. save_sizeleft = sizeleft;
  497. save_totsize = hstat.st_size;
  498. }
  499. start = findrec ();
  500. bufsize = endofrecs ()->charptr - start->charptr;
  501. if (sizeleft < bufsize)
  502. {
  503. /* Last read -- zero out area beyond */
  504. bufsize = (int) sizeleft;
  505. count = bufsize % RECORDSIZE;
  506. if (count)
  507. bzero (start->charptr + sizeleft,
  508. (int) (RECORDSIZE - count));
  509. }
  510. count = read (f, start->charptr, bufsize);
  511. if (count < 0)
  512. {
  513. msg_perror ("read error at byte %ld, reading\
  514. %d bytes, in file %s", hstat.st_size - sizeleft, bufsize, p);
  515. goto padit;
  516. }
  517. sizeleft -= count;
  518. /* This is nonportable (the type of userec's arg). */
  519. userec (start + (count - 1) / RECORDSIZE);
  520. if (count == bufsize)
  521. continue;
  522. msg ("file %s shrunk by %d bytes, padding with zeros.", p, sizeleft);
  523. goto padit; /* Short read */
  524. }
  525. if (f_multivol)
  526. save_name = 0;
  527. if (f >= 0)
  528. (void) close (f);
  529. if (f_remove_files)
  530. {
  531. if (unlink (p) == -1)
  532. msg_perror ("cannot remove %s", p);
  533. }
  534. if (f_atime_preserve)
  535. utime (p, &restore_times);
  536. return;
  537. /*
  538. * File shrunk or gave error, pad out tape to match
  539. * the size we specified in the header.
  540. */
  541. padit:
  542. while (sizeleft > 0)
  543. {
  544. save_sizeleft = sizeleft;
  545. start = findrec ();
  546. bzero (start->charptr, RECORDSIZE);
  547. userec (start);
  548. sizeleft -= RECORDSIZE;
  549. }
  550. if (f_multivol)
  551. save_name = 0;
  552. if (f >= 0)
  553. (void) close (f);
  554. if (f_atime_preserve)
  555. utime (p, &restore_times);
  556. return;
  557. }
  558. #ifdef S_ISLNK
  559. else if (S_ISLNK (hstat.st_mode))
  560. {
  561. int size;
  562. char *buf = alloca (PATH_MAX + 1);
  563. size = readlink (p, buf, PATH_MAX + 1);
  564. if (size < 0)
  565. goto badperror;
  566. buf[size] = '\0';
  567. if (size >= NAMSIZ)
  568. write_long (buf, LF_LONGLINK);
  569. current_link_name = buf;
  570. hstat.st_size = 0; /* Force 0 size on symlink */
  571. header = start_header (p, &hstat);
  572. if (header == NULL)
  573. {
  574. critical_error = 1;
  575. goto badfile;
  576. }
  577. strncpy (header->header.arch_linkname, buf, NAMSIZ);
  578. header->header.arch_linkname[NAMSIZ - 1] = '\0';
  579. header->header.linkflag = LF_SYMLINK;
  580. finish_header (header); /* Nothing more to do to it */
  581. if (f_remove_files)
  582. {
  583. if (unlink (p) == -1)
  584. msg_perror ("cannot remove %s", p);
  585. }
  586. return;
  587. }
  588. #endif
  589. else if (S_ISDIR (hstat.st_mode))
  590. {
  591. register DIR *dirp;
  592. register struct dirent *d;
  593. char *namebuf;
  594. int buflen;
  595. register int len;
  596. int our_device = hstat.st_dev;
  597. /* Build new prototype name */
  598. len = strlen (p);
  599. buflen = len + NAMSIZ;
  600. namebuf = ck_malloc (buflen + 1);
  601. strncpy (namebuf, p, buflen);
  602. while (len >= 1 && '/' == namebuf[len - 1])
  603. len--; /* Delete trailing slashes */
  604. namebuf[len++] = '/'; /* Now add exactly one back */
  605. namebuf[len] = '\0'; /* Make sure null-terminated */
  606. /*
  607. * Output directory header record with permissions
  608. * FIXME, do this AFTER files, to avoid R/O dir problems?
  609. * If old archive format, don't write record at all.
  610. */
  611. if (!f_oldarch)
  612. {
  613. hstat.st_size = 0; /* Force 0 size on dir */
  614. /*
  615. * If people could really read standard archives,
  616. * this should be: (FIXME)
  617. header = start_header(f_standard? p: namebuf, &hstat);
  618. * but since they'd interpret LF_DIR records as
  619. * regular files, we'd better put the / on the name.
  620. */
  621. header = start_header (namebuf, &hstat);
  622. if (header == NULL)
  623. {
  624. critical_error = 1;
  625. goto badfile; /* eg name too long */
  626. }
  627. if (f_gnudump)
  628. header->header.linkflag = LF_DUMPDIR;
  629. else if (f_standard)
  630. header->header.linkflag = LF_DIR;
  631. /* If we're gnudumping, we aren't done yet so don't close it. */
  632. if (!f_gnudump)
  633. finish_header (header); /* Done with directory header */
  634. }
  635. if (f_gnudump)
  636. {
  637. int sizeleft;
  638. int totsize;
  639. int bufsize;
  640. union record *start;
  641. int count;
  642. char *buf, *p_buf;
  643. buf = gnu_list_name->dir_contents; /* FOO */
  644. totsize = 0;
  645. for (p_buf = buf; p_buf && *p_buf;)
  646. {
  647. int tmp;
  648. tmp = strlen (p_buf) + 1;
  649. totsize += tmp;
  650. p_buf += tmp;
  651. }
  652. totsize++;
  653. to_oct ((long) totsize, 1 + 12, header->header.size);
  654. finish_header (header);
  655. p_buf = buf;
  656. sizeleft = totsize;
  657. while (sizeleft > 0)
  658. {
  659. if (f_multivol)
  660. {
  661. save_name = p;
  662. save_sizeleft = sizeleft;
  663. save_totsize = totsize;
  664. }
  665. start = findrec ();
  666. bufsize = endofrecs ()->charptr - start->charptr;
  667. if (sizeleft < bufsize)
  668. {
  669. bufsize = sizeleft;
  670. count = bufsize % RECORDSIZE;
  671. if (count)
  672. bzero (start->charptr + sizeleft, RECORDSIZE - count);
  673. }
  674. bcopy (p_buf, start->charptr, bufsize);
  675. sizeleft -= bufsize;
  676. p_buf += bufsize;
  677. userec (start + (bufsize - 1) / RECORDSIZE);
  678. }
  679. if (f_multivol)
  680. save_name = 0;
  681. if (f_atime_preserve)
  682. utime (p, &restore_times);
  683. return;
  684. }
  685. /* Now output all the files in the directory */
  686. #if 0
  687. if (f_dironly)
  688. return; /* Unless the cmdline said not to */
  689. #endif
  690. /*
  691. * See if we are crossing from one file system to another,
  692. * and avoid doing so if the user only wants to dump one file system.
  693. */
  694. if (f_local_filesys && !toplevel && curdev != hstat.st_dev)
  695. {
  696. if (f_verbose)
  697. msg ("%s: is on a different filesystem; not dumped", p);
  698. return;
  699. }
  700. errno = 0;
  701. dirp = opendir (p);
  702. if (!dirp)
  703. {
  704. if (errno)
  705. {
  706. msg_perror ("can't open directory %s", p);
  707. }
  708. else
  709. {
  710. msg ("error opening directory %s",
  711. p);
  712. }
  713. return;
  714. }
  715. /* Hack to remove "./" from the front of all the file names */
  716. if (len == 2 && namebuf[0] == '.' && namebuf[1] == '/')
  717. len = 0;
  718. /* Should speed this up by cd-ing into the dir, FIXME */
  719. while (NULL != (d = readdir (dirp)))
  720. {
  721. /* Skip . and .. */
  722. if (is_dot_or_dotdot (d->d_name))
  723. continue;
  724. if (NLENGTH (d) + len >= buflen)
  725. {
  726. buflen = len + NLENGTH (d);
  727. namebuf = ck_realloc (namebuf, buflen + 1);
  728. /* namebuf[len]='\0';
  729. msg("file name %s%s too long",
  730. namebuf, d->d_name);
  731. continue; */
  732. }
  733. strcpy (namebuf + len, d->d_name);
  734. if (f_exclude && check_exclude (namebuf))
  735. continue;
  736. dump_file (namebuf, our_device, 0);
  737. }
  738. closedir (dirp);
  739. free (namebuf);
  740. if (f_atime_preserve)
  741. utime (p, &restore_times);
  742. return;
  743. }
  744. #ifdef S_ISCHR
  745. else if (S_ISCHR (hstat.st_mode))
  746. {
  747. type = LF_CHR;
  748. }
  749. #endif
  750. #ifdef S_ISBLK
  751. else if (S_ISBLK (hstat.st_mode))
  752. {
  753. type = LF_BLK;
  754. }
  755. #endif
  756. /* Avoid screwy apollo lossage where S_IFIFO == S_IFSOCK */
  757. #if (_ISP__M68K == 0) && (_ISP__A88K == 0) && defined(S_ISFIFO)
  758. else if (S_ISFIFO (hstat.st_mode))
  759. {
  760. type = LF_FIFO;
  761. }
  762. #endif
  763. #ifdef S_ISSOCK
  764. else if (S_ISSOCK (hstat.st_mode))
  765. {
  766. type = LF_FIFO;
  767. }
  768. #endif
  769. else
  770. goto unknown;
  771. if (!f_standard)
  772. goto unknown;
  773. hstat.st_size = 0; /* Force 0 size */
  774. header = start_header (p, &hstat);
  775. if (header == NULL)
  776. {
  777. critical_error = 1;
  778. goto badfile; /* eg name too long */
  779. }
  780. header->header.linkflag = type;
  781. #if defined(S_IFBLK) || defined(S_IFCHR)
  782. if (type != LF_FIFO)
  783. {
  784. to_oct ((long) major (hstat.st_rdev), 8,
  785. header->header.devmajor);
  786. to_oct ((long) minor (hstat.st_rdev), 8,
  787. header->header.devminor);
  788. }
  789. #endif
  790. finish_header (header);
  791. if (f_remove_files)
  792. {
  793. if (unlink (p) == -1)
  794. msg_perror ("cannot remove %s", p);
  795. }
  796. return;
  797. unknown:
  798. msg ("%s: Unknown file type; file ignored.", p);
  799. }
  800. int
  801. finish_sparse_file (fd, sizeleft, fullsize, name)
  802. int fd;
  803. long *sizeleft, fullsize;
  804. char *name;
  805. {
  806. union record *start;
  807. char tempbuf[RECORDSIZE];
  808. int bufsize, sparse_ind = 0, count;
  809. long pos;
  810. long nwritten = 0;
  811. while (*sizeleft > 0)
  812. {
  813. start = findrec ();
  814. bzero (start->charptr, RECORDSIZE);
  815. bufsize = sparsearray[sparse_ind].numbytes;
  816. if (!bufsize)
  817. { /* we blew it, maybe */
  818. msg ("Wrote %ld of %ld bytes to file %s",
  819. fullsize - *sizeleft, fullsize, name);
  820. break;
  821. }
  822. pos = lseek (fd, sparsearray[sparse_ind++].offset, 0);
  823. /*
  824. * If the number of bytes to be written here exceeds
  825. * the size of the temporary buffer, do it in steps.
  826. */
  827. while (bufsize > RECORDSIZE)
  828. {
  829. /* if (amt_read) {
  830. count = read(fd, start->charptr+amt_read, RECORDSIZE-amt_read);
  831. bufsize -= RECORDSIZE - amt_read;
  832. amt_read = 0;
  833. userec(start);
  834. start = findrec();
  835. bzero(start->charptr, RECORDSIZE);
  836. }*/
  837. /* store the data */
  838. count = read (fd, start->charptr, RECORDSIZE);
  839. if (count < 0)
  840. {
  841. msg_perror ("read error at byte %ld, reading %d bytes, in file %s",
  842. fullsize - *sizeleft, bufsize, name);
  843. return 1;
  844. }
  845. bufsize -= count;
  846. *sizeleft -= count;
  847. userec (start);
  848. nwritten += RECORDSIZE; /* XXX */
  849. start = findrec ();
  850. bzero (start->charptr, RECORDSIZE);
  851. }
  852. clear_buffer (tempbuf);
  853. count = read (fd, tempbuf, bufsize);
  854. bcopy (tempbuf, start->charptr, RECORDSIZE);
  855. if (count < 0)
  856. {
  857. msg_perror ("read error at byte %ld, reading %d bytes, in file %s",
  858. fullsize - *sizeleft, bufsize, name);
  859. return 1;
  860. }
  861. /* if (amt_read >= RECORDSIZE) {
  862. amt_read = 0;
  863. userec(start+(count-1)/RECORDSIZE);
  864. if (count != bufsize) {
  865. msg("file %s shrunk by %d bytes, padding with zeros.", name, sizeleft);
  866. return 1;
  867. }
  868. start = findrec();
  869. } else
  870. amt_read += bufsize;*/
  871. nwritten += count; /* XXX */
  872. *sizeleft -= count;
  873. userec (start);
  874. }
  875. free (sparsearray);
  876. /* printf ("Amount actually written is (I hope) %d.\n", nwritten); */
  877. /* userec(start+(count-1)/RECORDSIZE);*/
  878. return 0;
  879. }
  880. void
  881. init_sparsearray ()
  882. {
  883. register int i;
  884. sp_array_size = 10;
  885. /*
  886. * Make room for our scratch space -- initially is 10 elts long
  887. */
  888. sparsearray = (struct sp_array *) ck_malloc (sp_array_size * sizeof (struct sp_array));
  889. for (i = 0; i < sp_array_size; i++)
  890. {
  891. sparsearray[i].offset = 0;
  892. sparsearray[i].numbytes = 0;
  893. }
  894. }
  895. /*
  896. * Okay, we've got a sparse file on our hands -- now, what we need to do is
  897. * make a pass through the file and carefully note where any data is, i.e.,
  898. * we want to find how far into the file each instance of data is, and how
  899. * many bytes are there. We store this information in the sparsearray,
  900. * which will later be translated into header information. For now, we use
  901. * the sparsearray as convenient storage.
  902. *
  903. * As a side note, this routine is a mess. If I could have found a cleaner
  904. * way to do it, I would have. If anyone wants to find a nicer way to do
  905. * this, feel free.
  906. */
  907. /* There is little point in trimming small amounts of null data at the */
  908. /* head and tail of blocks -- it's ok if we only avoid dumping blocks */
  909. /* of complete null data */
  910. int
  911. deal_with_sparse (name, header, nulls_at_end)
  912. char *name;
  913. union record *header;
  914. int nulls_at_end;
  915. {
  916. long numbytes = 0;
  917. long offset = 0;
  918. /* long save_offset;*/
  919. int fd;
  920. /* int current_size = hstat.st_size;*/
  921. int sparse_ind = 0, cc;
  922. char buf[RECORDSIZE];
  923. #if 0
  924. int read_last_data = 0; /* did we just read the last record? */
  925. #endif
  926. int amidst_data = 0;
  927. header->header.isextended = 0;
  928. /*
  929. * Can't open the file -- this problem will be caught later on,
  930. * so just return.
  931. */
  932. if ((fd = open (name, O_RDONLY)) < 0)
  933. return 0;
  934. init_sparsearray ();
  935. clear_buffer (buf);
  936. while ((cc = read (fd, buf, sizeof buf)) != 0)
  937. {
  938. if (sparse_ind > sp_array_size - 1)
  939. {
  940. /*
  941. * realloc the scratch area, since we've run out of room --
  942. */
  943. sparsearray = (struct sp_array *)
  944. ck_realloc (sparsearray,
  945. 2 * sp_array_size * (sizeof (struct sp_array)));
  946. sp_array_size *= 2;
  947. }
  948. if (cc == sizeof buf)
  949. {
  950. if (zero_record (buf))
  951. {
  952. if (amidst_data)
  953. {
  954. sparsearray[sparse_ind++].numbytes
  955. = numbytes;
  956. amidst_data = 0;
  957. }
  958. }
  959. else
  960. { /* !zero_record(buf) */
  961. if (amidst_data)
  962. numbytes += cc;
  963. else
  964. {
  965. amidst_data = 1;
  966. numbytes = cc;
  967. sparsearray[sparse_ind].offset
  968. = offset;
  969. }
  970. }
  971. }
  972. else if (cc < sizeof buf)
  973. {
  974. /* This has to be the last bit of the file, so this */
  975. /* is somewhat shorter than the above. */
  976. if (!zero_record (buf))
  977. {
  978. if (!amidst_data)
  979. {
  980. amidst_data = 1;
  981. numbytes = cc;
  982. sparsearray[sparse_ind].offset
  983. = offset;
  984. }
  985. else
  986. numbytes += cc;
  987. }
  988. }
  989. offset += cc;
  990. clear_buffer (buf);
  991. }
  992. if (amidst_data)
  993. sparsearray[sparse_ind++].numbytes = numbytes;
  994. else
  995. {
  996. sparsearray[sparse_ind].offset = offset-1;
  997. sparsearray[sparse_ind++].numbytes = 1;
  998. }
  999. close (fd);
  1000. return sparse_ind - 1;
  1001. }
  1002. /*
  1003. * Just zeroes out the buffer so we don't confuse ourselves with leftover
  1004. * data.
  1005. */
  1006. void
  1007. clear_buffer (buf)
  1008. char *buf;
  1009. {
  1010. register int i;
  1011. for (i = 0; i < RECORDSIZE; i++)
  1012. buf[i] = '\0';
  1013. }
  1014. #if 0 /* I'm leaving this as a monument to Joy Kendall, who wrote it -mib */
  1015. /*
  1016. * JK -
  1017. * This routine takes a character array, and tells where within that array
  1018. * the data can be found. It skips over any zeros, and sets the first
  1019. * non-zero point in the array to be the "start", and continues until it
  1020. * finds non-data again, which is marked as the "end." This routine is
  1021. * mainly for 1) seeing how far into a file we must lseek to data, given
  1022. * that we have a sparse file, and 2) determining the "real size" of the
  1023. * file, i.e., the number of bytes in the sparse file that are data, as
  1024. * opposed to the zeros we are trying to skip.
  1025. */
  1026. where_is_data (from, to, buffer)
  1027. int *from, *to;
  1028. char *buffer;
  1029. {
  1030. register int i = 0;
  1031. register int save_to = *to;
  1032. int amidst_data = 0;
  1033. while (!buffer[i])
  1034. i++;
  1035. *from = i;
  1036. if (*from < 16) /* don't bother */
  1037. *from = 0;
  1038. /* keep going to make sure there isn't more real
  1039. data in this record */
  1040. while (i < RECORDSIZE)
  1041. {
  1042. if (!buffer[i])
  1043. {
  1044. if (amidst_data)
  1045. {
  1046. save_to = i;
  1047. amidst_data = 0;
  1048. }
  1049. i++;
  1050. }
  1051. else if (buffer[i])
  1052. {
  1053. if (!amidst_data)
  1054. amidst_data = 1;
  1055. i++;
  1056. }
  1057. }
  1058. if (i == RECORDSIZE)
  1059. *to = i;
  1060. else
  1061. *to = save_to;
  1062. }
  1063. #endif
  1064. /* Note that this routine is only called if zero_record returned true */
  1065. #if 0 /* But we actually don't need it at all. */
  1066. where_is_data (from, to, buffer)
  1067. int *from, *to;
  1068. char *buffer;
  1069. {
  1070. char *fp, *tp;
  1071. for (fp = buffer; !*fp; fp++)
  1072. ;
  1073. for (tp = buffer + RECORDSIZE - 1; !*tp; tp--)
  1074. ;
  1075. *from = fp - buffer;
  1076. *to = tp - buffer + 1;
  1077. }
  1078. #endif
  1079. /*
  1080. * Takes a recordful of data and basically cruises through it to see if
  1081. * it's made *entirely* of zeros, returning a 0 the instant it finds
  1082. * something that is a non-zero, i.e., useful data.
  1083. */
  1084. int
  1085. zero_record (buffer)
  1086. char *buffer;
  1087. {
  1088. register int i;
  1089. for (i = 0; i < RECORDSIZE; i++)
  1090. if (buffer[i] != '\000')
  1091. return 0;
  1092. return 1;
  1093. }
  1094. void
  1095. find_new_file_size (filesize, highest_index)
  1096. int *filesize;
  1097. int highest_index;
  1098. {
  1099. register int i;
  1100. *filesize = 0;
  1101. for (i = 0; sparsearray[i].numbytes && i <= highest_index; i++)
  1102. *filesize += sparsearray[i].numbytes;
  1103. }
  1104. /*
  1105. * Make a header block for the file name whose stat info is st .
  1106. * Return header pointer for success, NULL if the name is too long.
  1107. */
  1108. union record *
  1109. start_header (name, st)
  1110. char *name;
  1111. register struct stat *st;
  1112. {
  1113. register union record *header;
  1114. if (strlen (name) >= NAMSIZ)
  1115. write_long (name, LF_LONGNAME);
  1116. header = (union record *) findrec ();
  1117. bzero (header->charptr, sizeof (*header)); /* XXX speed up */
  1118. /*
  1119. * Check the file name and put it in the record.
  1120. */
  1121. if (!f_absolute_paths)
  1122. {
  1123. static int warned_once = 0;
  1124. #ifdef __MSDOS__
  1125. if (name[1] == ':')
  1126. {
  1127. name += 2;
  1128. if (!warned_once++)
  1129. msg ("Removing drive spec from names in the archive");
  1130. }
  1131. #endif
  1132. while ('/' == *name)
  1133. {
  1134. name++; /* Force relative path */
  1135. if (!warned_once++)
  1136. msg ("Removing leading / from absolute path names in the archive.");
  1137. }
  1138. }
  1139. current_file_name = name;
  1140. strncpy (header->header.arch_name, name, NAMSIZ);
  1141. header->header.arch_name[NAMSIZ - 1] = '\0';
  1142. to_oct ((long) (f_oldarch ? (st->st_mode & 07777) : st->st_mode),
  1143. 8, header->header.mode);
  1144. to_oct ((long) st->st_uid, 8, header->header.uid);
  1145. to_oct ((long) st->st_gid, 8, header->header.gid);
  1146. to_oct ((long) st->st_size, 1 + 12, header->header.size);
  1147. to_oct ((long) st->st_mtime, 1 + 12, header->header.mtime);
  1148. /* header->header.linkflag is left as null */
  1149. if (f_gnudump)
  1150. {
  1151. to_oct ((long) st->st_atime, 1 + 12, header->header.atime);
  1152. to_oct ((long) st->st_ctime, 1 + 12, header->header.ctime);
  1153. }
  1154. #ifndef NONAMES
  1155. /* Fill in new Unix Standard fields if desired. */
  1156. if (f_standard)
  1157. {
  1158. header->header.linkflag = LF_NORMAL; /* New default */
  1159. strcpy (header->header.magic, TMAGIC); /* Mark as Unix Std */
  1160. finduname (header->header.uname, st->st_uid);
  1161. findgname (header->header.gname, st->st_gid);
  1162. }
  1163. #endif
  1164. return header;
  1165. }
  1166. /*
  1167. * Finish off a filled-in header block and write it out.
  1168. * We also print the file name and/or full info if verbose is on.
  1169. */
  1170. void
  1171. finish_header (header)
  1172. register union record *header;
  1173. {
  1174. register int i, sum;
  1175. register char *p;
  1176. bcopy (CHKBLANKS, header->header.chksum, sizeof (header->header.chksum));
  1177. sum = 0;
  1178. p = header->charptr;
  1179. for (i = sizeof (*header); --i >= 0;)
  1180. {
  1181. /*
  1182. * We can't use unsigned char here because of old compilers,
  1183. * e.g. V7.
  1184. */
  1185. sum += 0xFF & *p++;
  1186. }
  1187. /*
  1188. * Fill in the checksum field. It's formatted differently
  1189. * from the other fields: it has [6] digits, a null, then a
  1190. * space -- rather than digits, a space, then a null.
  1191. * We use to_oct then write the null in over to_oct's space.
  1192. * The final space is already there, from checksumming, and
  1193. * to_oct doesn't modify it.
  1194. *
  1195. * This is a fast way to do:
  1196. * (void) sprintf(header->header.chksum, "%6o", sum);
  1197. */
  1198. to_oct ((long) sum, 8, header->header.chksum);
  1199. header->header.chksum[6] = '\0'; /* Zap the space */
  1200. userec (header);
  1201. if (f_verbose)
  1202. {
  1203. extern union record *head;/* Points to current tape header */
  1204. extern int head_standard; /* Tape header is in ANSI format */
  1205. /* These globals are parameters to print_header, sigh */
  1206. head = header;
  1207. /* hstat is already set up */
  1208. head_standard = f_standard;
  1209. print_header ();
  1210. }
  1211. return;
  1212. }
  1213. /*
  1214. * Quick and dirty octal conversion.
  1215. * Converts long "value" into a "digs"-digit field at "where",
  1216. * including a trailing space and room for a null. "digs"==3 means
  1217. * 1 digit, a space, and room for a null.
  1218. *
  1219. * We assume the trailing null is already there and don't fill it in.
  1220. * This fact is used by start_header and finish_header, so don't change it!
  1221. *
  1222. * This should be equivalent to:
  1223. * (void) sprintf(where, "%*lo ", digs-2, value);
  1224. * except that sprintf fills in the trailing null and we don't.
  1225. */
  1226. void
  1227. to_oct (value, digs, where)
  1228. register long value;
  1229. register int digs;
  1230. register char *where;
  1231. {
  1232. --digs; /* Trailing null slot is left alone */
  1233. where[--digs] = ' '; /* Put in the space, though */
  1234. /* Produce the digits -- at least one */
  1235. do
  1236. {
  1237. where[--digs] = '0' + (char) (value & 7); /* one octal digit */
  1238. value >>= 3;
  1239. }
  1240. while (digs > 0 && value != 0);
  1241. /* Leading spaces, if necessary */
  1242. while (digs > 0)
  1243. where[--digs] = ' ';
  1244. }
  1245. /*
  1246. * Write the EOT record(s).
  1247. * We actually zero at least one record, through the end of the block.
  1248. * Old tar writes garbage after two zeroed records -- and PDtar used to.
  1249. */
  1250. void
  1251. write_eot ()
  1252. {
  1253. union record *p;
  1254. int bufsize;
  1255. p = findrec ();
  1256. if (p)
  1257. {
  1258. bufsize = endofrecs ()->charptr - p->charptr;
  1259. bzero (p->charptr, bufsize);
  1260. userec (p);
  1261. }
  1262. }
  1263. /* Write a LF_LONGLINK or LF_LONGNAME record. */
  1264. void
  1265. write_long (p, type)
  1266. char *p;
  1267. char type;
  1268. {
  1269. int size = strlen (p) + 1;
  1270. int bufsize;
  1271. union record *header;
  1272. struct stat foo;
  1273. bzero (&foo, sizeof foo);
  1274. foo.st_size = size;
  1275. header = start_header ("././@LongLink", &foo);
  1276. header->header.linkflag = type;
  1277. finish_header (header);
  1278. header = findrec ();
  1279. bufsize = endofrecs ()->charptr - header->charptr;
  1280. while (bufsize < size)
  1281. {
  1282. bcopy (p, header->charptr, bufsize);
  1283. p += bufsize;
  1284. size -= bufsize;
  1285. userec (header + (bufsize - 1) / RECORDSIZE);
  1286. header = findrec ();
  1287. bufsize = endofrecs ()->charptr - header->charptr;
  1288. }
  1289. bcopy (p, header->charptr, size);
  1290. bzero (header->charptr + size, bufsize - size);
  1291. userec (header + (size - 1) / RECORDSIZE);
  1292. }