names.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220
  1. /* Various processing of names.
  2. Copyright (C) 1988, 1992, 1994, 1996, 1997, 1998, 1999, 2000, 2001,
  3. 2003, 2004, 2005, 2006, 2007, 2009 Free Software Foundation, Inc.
  4. This program is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. This program is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  11. Public License for more details.
  12. You should have received a copy of the GNU General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  15. #include <system.h>
  16. #include <fnmatch.h>
  17. #include <hash.h>
  18. #include <quotearg.h>
  19. #include "common.h"
  20. /* User and group names. */
  21. /* Make sure you link with the proper libraries if you are running the
  22. Yellow Peril (thanks for the good laugh, Ian J.!), or, euh... NIS.
  23. This code should also be modified for non-UNIX systems to do something
  24. reasonable. */
  25. static char *cached_uname;
  26. static char *cached_gname;
  27. static uid_t cached_uid; /* valid only if cached_uname is not empty */
  28. static gid_t cached_gid; /* valid only if cached_gname is not empty */
  29. /* These variables are valid only if nonempty. */
  30. static char *cached_no_such_uname;
  31. static char *cached_no_such_gname;
  32. /* These variables are valid only if nonzero. It's not worth optimizing
  33. the case for weird systems where 0 is not a valid uid or gid. */
  34. static uid_t cached_no_such_uid;
  35. static gid_t cached_no_such_gid;
  36. static void register_individual_file (char const *name);
  37. /* Given UID, find the corresponding UNAME. */
  38. void
  39. uid_to_uname (uid_t uid, char **uname)
  40. {
  41. struct passwd *passwd;
  42. if (uid != 0 && uid == cached_no_such_uid)
  43. {
  44. *uname = xstrdup ("");
  45. return;
  46. }
  47. if (!cached_uname || uid != cached_uid)
  48. {
  49. passwd = getpwuid (uid);
  50. if (passwd)
  51. {
  52. cached_uid = uid;
  53. assign_string (&cached_uname, passwd->pw_name);
  54. }
  55. else
  56. {
  57. cached_no_such_uid = uid;
  58. *uname = xstrdup ("");
  59. return;
  60. }
  61. }
  62. *uname = xstrdup (cached_uname);
  63. }
  64. /* Given GID, find the corresponding GNAME. */
  65. void
  66. gid_to_gname (gid_t gid, char **gname)
  67. {
  68. struct group *group;
  69. if (gid != 0 && gid == cached_no_such_gid)
  70. {
  71. *gname = xstrdup ("");
  72. return;
  73. }
  74. if (!cached_gname || gid != cached_gid)
  75. {
  76. group = getgrgid (gid);
  77. if (group)
  78. {
  79. cached_gid = gid;
  80. assign_string (&cached_gname, group->gr_name);
  81. }
  82. else
  83. {
  84. cached_no_such_gid = gid;
  85. *gname = xstrdup ("");
  86. return;
  87. }
  88. }
  89. *gname = xstrdup (cached_gname);
  90. }
  91. /* Given UNAME, set the corresponding UID and return 1, or else, return 0. */
  92. int
  93. uname_to_uid (char const *uname, uid_t *uidp)
  94. {
  95. struct passwd *passwd;
  96. if (cached_no_such_uname
  97. && strcmp (uname, cached_no_such_uname) == 0)
  98. return 0;
  99. if (!cached_uname
  100. || uname[0] != cached_uname[0]
  101. || strcmp (uname, cached_uname) != 0)
  102. {
  103. passwd = getpwnam (uname);
  104. if (passwd)
  105. {
  106. cached_uid = passwd->pw_uid;
  107. assign_string (&cached_uname, passwd->pw_name);
  108. }
  109. else
  110. {
  111. assign_string (&cached_no_such_uname, uname);
  112. return 0;
  113. }
  114. }
  115. *uidp = cached_uid;
  116. return 1;
  117. }
  118. /* Given GNAME, set the corresponding GID and return 1, or else, return 0. */
  119. int
  120. gname_to_gid (char const *gname, gid_t *gidp)
  121. {
  122. struct group *group;
  123. if (cached_no_such_gname
  124. && strcmp (gname, cached_no_such_gname) == 0)
  125. return 0;
  126. if (!cached_gname
  127. || gname[0] != cached_gname[0]
  128. || strcmp (gname, cached_gname) != 0)
  129. {
  130. group = getgrnam (gname);
  131. if (group)
  132. {
  133. cached_gid = group->gr_gid;
  134. assign_string (&cached_gname, gname);
  135. }
  136. else
  137. {
  138. assign_string (&cached_no_such_gname, gname);
  139. return 0;
  140. }
  141. }
  142. *gidp = cached_gid;
  143. return 1;
  144. }
  145. static struct name *
  146. make_name (const char *file_name)
  147. {
  148. struct name *p = xzalloc (sizeof (*p));
  149. if (!file_name)
  150. file_name = "";
  151. p->name = xstrdup (file_name);
  152. p->length = strlen (p->name);
  153. return p;
  154. }
  155. static void
  156. free_name (struct name *p)
  157. {
  158. if (p)
  159. {
  160. free (p->name);
  161. free (p->caname);
  162. free (p);
  163. }
  164. }
  165. /* Names from the command call. */
  166. static struct name *namelist; /* first name in list, if any */
  167. static struct name *nametail; /* end of name list */
  168. /* File name arguments are processed in two stages: first a
  169. name_array (see below) is filled, then the names from it
  170. are moved into the namelist.
  171. This awkward process is needed only to implement --same-order option,
  172. which is meant to help process large archives on machines with
  173. limited memory. With this option on, namelist contains at most one
  174. entry, which diminishes the memory consumption.
  175. However, I very much doubt if we still need this -- Sergey */
  176. /* A name_array element contains entries of three types: */
  177. #define NELT_NAME 0 /* File name */
  178. #define NELT_CHDIR 1 /* Change directory request */
  179. #define NELT_FMASK 2 /* Change fnmatch options request */
  180. struct name_elt /* A name_array element. */
  181. {
  182. char type; /* Element type, see NELT_* constants above */
  183. union
  184. {
  185. const char *name; /* File or directory name */
  186. int matching_flags;/* fnmatch options if type == NELT_FMASK */
  187. } v;
  188. };
  189. static struct name_elt *name_array; /* store an array of names */
  190. static size_t allocated_entries; /* how big is the array? */
  191. static size_t entries; /* how many entries does it have? */
  192. static size_t scanned; /* how many of the entries have we scanned? */
  193. size_t name_count; /* how many of the entries are names? */
  194. /* Check the size of name_array, reallocating it as necessary. */
  195. static void
  196. check_name_alloc (void)
  197. {
  198. if (entries == allocated_entries)
  199. {
  200. if (allocated_entries == 0)
  201. allocated_entries = 10; /* Set initial allocation */
  202. name_array = x2nrealloc (name_array, &allocated_entries,
  203. sizeof (name_array[0]));
  204. }
  205. }
  206. /* Add to name_array the file NAME with fnmatch options MATCHING_FLAGS */
  207. void
  208. name_add_name (const char *name, int matching_flags)
  209. {
  210. static int prev_flags = 0; /* FIXME: Or EXCLUDE_ANCHORED? */
  211. struct name_elt *ep;
  212. check_name_alloc ();
  213. ep = &name_array[entries++];
  214. if (prev_flags != matching_flags)
  215. {
  216. ep->type = NELT_FMASK;
  217. ep->v.matching_flags = matching_flags;
  218. prev_flags = matching_flags;
  219. check_name_alloc ();
  220. ep = &name_array[entries++];
  221. }
  222. ep->type = NELT_NAME;
  223. ep->v.name = name;
  224. name_count++;
  225. }
  226. /* Add to name_array a chdir request for the directory NAME */
  227. void
  228. name_add_dir (const char *name)
  229. {
  230. struct name_elt *ep;
  231. check_name_alloc ();
  232. ep = &name_array[entries++];
  233. ep->type = NELT_CHDIR;
  234. ep->v.name = name;
  235. }
  236. /* Names from external name file. */
  237. static char *name_buffer; /* buffer to hold the current file name */
  238. static size_t name_buffer_length; /* allocated length of name_buffer */
  239. /* Set up to gather file names for tar. They can either come from a
  240. file or were saved from decoding arguments. */
  241. void
  242. name_init (void)
  243. {
  244. name_buffer = xmalloc (NAME_FIELD_SIZE + 2);
  245. name_buffer_length = NAME_FIELD_SIZE;
  246. }
  247. void
  248. name_term (void)
  249. {
  250. free (name_buffer);
  251. free (name_array);
  252. }
  253. static int matching_flags; /* exclude_fnmatch options */
  254. /* Get the next NELT_NAME element from name_array. Result is in
  255. static storage and can't be relied upon across two calls.
  256. If CHANGE_DIRS is true, treat any entries of type NELT_CHDIR as
  257. the request to change to the given directory.
  258. Entries of type NELT_FMASK cause updates of the matching_flags
  259. value. */
  260. static struct name_elt *
  261. name_next_elt (int change_dirs)
  262. {
  263. static struct name_elt entry;
  264. const char *source;
  265. char *cursor;
  266. while (scanned != entries)
  267. {
  268. struct name_elt *ep;
  269. size_t source_len;
  270. ep = &name_array[scanned++];
  271. if (ep->type == NELT_FMASK)
  272. {
  273. matching_flags = ep->v.matching_flags;
  274. continue;
  275. }
  276. source = ep->v.name;
  277. source_len = strlen (source);
  278. if (name_buffer_length < source_len)
  279. {
  280. do
  281. {
  282. name_buffer_length *= 2;
  283. if (! name_buffer_length)
  284. xalloc_die ();
  285. }
  286. while (name_buffer_length < source_len);
  287. free (name_buffer);
  288. name_buffer = xmalloc (name_buffer_length + 2);
  289. }
  290. strcpy (name_buffer, source);
  291. /* Zap trailing slashes. */
  292. cursor = name_buffer + strlen (name_buffer) - 1;
  293. while (cursor > name_buffer && ISSLASH (*cursor))
  294. *cursor-- = '\0';
  295. if (change_dirs && ep->type == NELT_CHDIR)
  296. {
  297. if (chdir (name_buffer) < 0)
  298. chdir_fatal (name_buffer);
  299. }
  300. else
  301. {
  302. if (unquote_option)
  303. unquote_string (name_buffer);
  304. if (incremental_option)
  305. register_individual_file (name_buffer);
  306. entry.type = ep->type;
  307. entry.v.name = name_buffer;
  308. return &entry;
  309. }
  310. }
  311. return NULL;
  312. }
  313. const char *
  314. name_next (int change_dirs)
  315. {
  316. struct name_elt *nelt = name_next_elt (change_dirs);
  317. return nelt ? nelt->v.name : NULL;
  318. }
  319. /* Gather names in a list for scanning. Could hash them later if we
  320. really care.
  321. If the names are already sorted to match the archive, we just read
  322. them one by one. name_gather reads the first one, and it is called
  323. by name_match as appropriate to read the next ones. At EOF, the
  324. last name read is just left in the buffer. This option lets users
  325. of small machines extract an arbitrary number of files by doing
  326. "tar t" and editing down the list of files. */
  327. void
  328. name_gather (void)
  329. {
  330. /* Buffer able to hold a single name. */
  331. static struct name *buffer = NULL;
  332. struct name_elt *ep;
  333. if (same_order_option)
  334. {
  335. static int change_dir;
  336. while ((ep = name_next_elt (0)) && ep->type == NELT_CHDIR)
  337. change_dir = chdir_arg (xstrdup (ep->v.name));
  338. if (ep)
  339. {
  340. free_name (buffer);
  341. buffer = make_name (ep->v.name);
  342. buffer->change_dir = change_dir;
  343. buffer->next = 0;
  344. buffer->found_count = 0;
  345. buffer->matching_flags = matching_flags;
  346. buffer->directory = NULL;
  347. buffer->parent = NULL;
  348. buffer->cmdline = true;
  349. namelist = nametail = buffer;
  350. }
  351. else if (change_dir)
  352. addname (0, change_dir, false, NULL);
  353. }
  354. else
  355. {
  356. /* Non sorted names -- read them all in. */
  357. int change_dir = 0;
  358. for (;;)
  359. {
  360. int change_dir0 = change_dir;
  361. while ((ep = name_next_elt (0)) && ep->type == NELT_CHDIR)
  362. change_dir = chdir_arg (xstrdup (ep->v.name));
  363. if (ep)
  364. addname (ep->v.name, change_dir, true, NULL);
  365. else
  366. {
  367. if (change_dir != change_dir0)
  368. addname (NULL, change_dir, false, NULL);
  369. break;
  370. }
  371. }
  372. }
  373. }
  374. /* Add a name to the namelist. */
  375. struct name *
  376. addname (char const *string, int change_dir, bool cmdline, struct name *parent)
  377. {
  378. struct name *name = make_name (string);
  379. name->prev = nametail;
  380. name->next = NULL;
  381. name->found_count = 0;
  382. name->matching_flags = matching_flags;
  383. name->change_dir = change_dir;
  384. name->directory = NULL;
  385. name->parent = parent;
  386. name->cmdline = cmdline;
  387. if (nametail)
  388. nametail->next = name;
  389. else
  390. namelist = name;
  391. nametail = name;
  392. return name;
  393. }
  394. /* Find a match for FILE_NAME (whose string length is LENGTH) in the name
  395. list. */
  396. static struct name *
  397. namelist_match (char const *file_name, size_t length)
  398. {
  399. struct name *p;
  400. for (p = namelist; p; p = p->next)
  401. {
  402. if (p->name[0]
  403. && exclude_fnmatch (p->name, file_name, p->matching_flags))
  404. return p;
  405. }
  406. return NULL;
  407. }
  408. void
  409. remname (struct name *name)
  410. {
  411. struct name *p;
  412. if ((p = name->prev) != NULL)
  413. p->next = name->next;
  414. else
  415. namelist = name->next;
  416. if ((p = name->next) != NULL)
  417. p->prev = name->prev;
  418. else
  419. nametail = name->prev;
  420. }
  421. /* Return true if and only if name FILE_NAME (from an archive) matches any
  422. name from the namelist. */
  423. bool
  424. name_match (const char *file_name)
  425. {
  426. size_t length = strlen (file_name);
  427. while (1)
  428. {
  429. struct name *cursor = namelist;
  430. if (!cursor)
  431. return true;
  432. if (cursor->name[0] == 0)
  433. {
  434. chdir_do (cursor->change_dir);
  435. namelist = NULL;
  436. nametail = NULL;
  437. return true;
  438. }
  439. cursor = namelist_match (file_name, length);
  440. if (cursor)
  441. {
  442. if (!(ISSLASH (file_name[cursor->length]) && recursion_option)
  443. || cursor->found_count == 0)
  444. cursor->found_count++; /* remember it matched */
  445. if (starting_file_option)
  446. {
  447. free (namelist);
  448. namelist = NULL;
  449. nametail = NULL;
  450. }
  451. chdir_do (cursor->change_dir);
  452. /* We got a match. */
  453. return ISFOUND (cursor);
  454. }
  455. /* Filename from archive not found in namelist. If we have the whole
  456. namelist here, just return 0. Otherwise, read the next name in and
  457. compare it. If this was the last name, namelist->found_count will
  458. remain on. If not, we loop to compare the newly read name. */
  459. if (same_order_option && namelist->found_count)
  460. {
  461. name_gather (); /* read one more */
  462. if (namelist->found_count)
  463. return false;
  464. }
  465. else
  466. return false;
  467. }
  468. }
  469. /* Returns true if all names from the namelist were processed.
  470. P is the stat_info of the most recently processed entry.
  471. The decision is postponed until the next entry is read if:
  472. 1) P ended with a slash (i.e. it was a directory)
  473. 2) P matches any entry from the namelist *and* represents a subdirectory
  474. or a file lying under this entry (in the terms of directory structure).
  475. This is necessary to handle contents of directories. */
  476. bool
  477. all_names_found (struct tar_stat_info *p)
  478. {
  479. struct name const *cursor;
  480. size_t len;
  481. if (!p->file_name || occurrence_option == 0 || p->had_trailing_slash)
  482. return false;
  483. len = strlen (p->file_name);
  484. for (cursor = namelist; cursor; cursor = cursor->next)
  485. {
  486. if ((cursor->name[0] && !WASFOUND (cursor))
  487. || (len >= cursor->length && ISSLASH (p->file_name[cursor->length])))
  488. return false;
  489. }
  490. return true;
  491. }
  492. static int
  493. regex_usage_warning (const char *name)
  494. {
  495. static int warned_once = 0;
  496. if (warn_regex_usage && fnmatch_pattern_has_wildcards (name, 0))
  497. {
  498. warned_once = 1;
  499. WARN ((0, 0,
  500. _("Pattern matching characters used in file names")));
  501. WARN ((0, 0,
  502. _("Use --wildcards to enable pattern matching,"
  503. " or --no-wildcards to suppress this warning")));
  504. }
  505. return warned_once;
  506. }
  507. /* Print the names of things in the namelist that were not matched. */
  508. void
  509. names_notfound (void)
  510. {
  511. struct name const *cursor;
  512. for (cursor = namelist; cursor; cursor = cursor->next)
  513. if (!WASFOUND (cursor) && cursor->name[0])
  514. {
  515. regex_usage_warning (cursor->name);
  516. ERROR ((0, 0,
  517. (cursor->found_count == 0) ?
  518. _("%s: Not found in archive") :
  519. _("%s: Required occurrence not found in archive"),
  520. quotearg_colon (cursor->name)));
  521. }
  522. /* Don't bother freeing the name list; we're about to exit. */
  523. namelist = NULL;
  524. nametail = NULL;
  525. if (same_order_option)
  526. {
  527. const char *name;
  528. while ((name = name_next (1)) != NULL)
  529. {
  530. regex_usage_warning (name);
  531. ERROR ((0, 0, _("%s: Not found in archive"),
  532. quotearg_colon (name)));
  533. }
  534. }
  535. }
  536. void
  537. label_notfound (void)
  538. {
  539. struct name const *cursor;
  540. if (!namelist)
  541. return;
  542. for (cursor = namelist; cursor; cursor = cursor->next)
  543. if (WASFOUND (cursor))
  544. return;
  545. if (verbose_option)
  546. error (0, 0, _("Archive label mismatch"));
  547. set_exit_status (TAREXIT_DIFFERS);
  548. for (cursor = namelist; cursor; cursor = cursor->next)
  549. {
  550. if (regex_usage_warning (cursor->name))
  551. break;
  552. }
  553. /* Don't bother freeing the name list; we're about to exit. */
  554. namelist = NULL;
  555. nametail = NULL;
  556. if (same_order_option)
  557. {
  558. const char *name;
  559. while ((name = name_next (1)) != NULL
  560. && regex_usage_warning (name) == 0)
  561. ;
  562. }
  563. }
  564. /* Sorting name lists. */
  565. /* Sort *singly* linked LIST of names, of given LENGTH, using COMPARE
  566. to order names. Return the sorted list. Note that after calling
  567. this function, the `prev' links in list elements are messed up.
  568. Apart from the type `struct name' and the definition of SUCCESSOR,
  569. this is a generic list-sorting function, but it's too painful to
  570. make it both generic and portable
  571. in C. */
  572. static struct name *
  573. merge_sort_sll (struct name *list, int length,
  574. int (*compare) (struct name const*, struct name const*))
  575. {
  576. struct name *first_list;
  577. struct name *second_list;
  578. int first_length;
  579. int second_length;
  580. struct name *result;
  581. struct name **merge_point;
  582. struct name *cursor;
  583. int counter;
  584. # define SUCCESSOR(name) ((name)->next)
  585. if (length == 1)
  586. return list;
  587. if (length == 2)
  588. {
  589. if ((*compare) (list, SUCCESSOR (list)) > 0)
  590. {
  591. result = SUCCESSOR (list);
  592. SUCCESSOR (result) = list;
  593. SUCCESSOR (list) = 0;
  594. return result;
  595. }
  596. return list;
  597. }
  598. first_list = list;
  599. first_length = (length + 1) / 2;
  600. second_length = length / 2;
  601. for (cursor = list, counter = first_length - 1;
  602. counter;
  603. cursor = SUCCESSOR (cursor), counter--)
  604. continue;
  605. second_list = SUCCESSOR (cursor);
  606. SUCCESSOR (cursor) = 0;
  607. first_list = merge_sort_sll (first_list, first_length, compare);
  608. second_list = merge_sort_sll (second_list, second_length, compare);
  609. merge_point = &result;
  610. while (first_list && second_list)
  611. if ((*compare) (first_list, second_list) < 0)
  612. {
  613. cursor = SUCCESSOR (first_list);
  614. *merge_point = first_list;
  615. merge_point = &SUCCESSOR (first_list);
  616. first_list = cursor;
  617. }
  618. else
  619. {
  620. cursor = SUCCESSOR (second_list);
  621. *merge_point = second_list;
  622. merge_point = &SUCCESSOR (second_list);
  623. second_list = cursor;
  624. }
  625. if (first_list)
  626. *merge_point = first_list;
  627. else
  628. *merge_point = second_list;
  629. return result;
  630. #undef SUCCESSOR
  631. }
  632. /* Sort doubly linked LIST of names, of given LENGTH, using COMPARE
  633. to order names. Return the sorted list. */
  634. static struct name *
  635. merge_sort (struct name *list, int length,
  636. int (*compare) (struct name const*, struct name const*))
  637. {
  638. struct name *head, *p, *prev;
  639. head = merge_sort_sll (list, length, compare);
  640. /* Fixup prev pointers */
  641. for (prev = NULL, p = head; p; prev = p, p = p->next)
  642. p->prev = prev;
  643. return head;
  644. }
  645. /* A comparison function for sorting names. Put found names last;
  646. break ties by string comparison. */
  647. static int
  648. compare_names_found (struct name const *n1, struct name const *n2)
  649. {
  650. int found_diff = WASFOUND (n2) - WASFOUND (n1);
  651. return found_diff ? found_diff : strcmp (n1->name, n2->name);
  652. }
  653. /* Simple comparison by names. */
  654. static int
  655. compare_names (struct name const *n1, struct name const *n2)
  656. {
  657. return strcmp (n1->name, n2->name);
  658. }
  659. /* Add all the dirs under ST to the namelist NAME, descending the
  660. directory hierarchy recursively. */
  661. static void
  662. add_hierarchy_to_namelist (struct tar_stat_info *st, struct name *name)
  663. {
  664. const char *buffer;
  665. name->directory = scan_directory (st);
  666. buffer = directory_contents (name->directory);
  667. if (buffer)
  668. {
  669. struct name *child_head = NULL, *child_tail = NULL;
  670. size_t name_length = name->length;
  671. size_t allocated_length = (name_length >= NAME_FIELD_SIZE
  672. ? name_length + NAME_FIELD_SIZE
  673. : NAME_FIELD_SIZE);
  674. char *namebuf = xmalloc (allocated_length + 1);
  675. /* FIXME: + 2 above? */
  676. const char *string;
  677. size_t string_length;
  678. int change_dir = name->change_dir;
  679. strcpy (namebuf, name->name);
  680. if (! ISSLASH (namebuf[name_length - 1]))
  681. {
  682. namebuf[name_length++] = '/';
  683. namebuf[name_length] = '\0';
  684. }
  685. for (string = buffer; *string; string += string_length + 1)
  686. {
  687. string_length = strlen (string);
  688. if (*string == 'D')
  689. {
  690. struct name *np;
  691. struct tar_stat_info subdir;
  692. int subfd;
  693. if (allocated_length <= name_length + string_length)
  694. {
  695. do
  696. {
  697. allocated_length *= 2;
  698. if (! allocated_length)
  699. xalloc_die ();
  700. }
  701. while (allocated_length <= name_length + string_length);
  702. namebuf = xrealloc (namebuf, allocated_length + 1);
  703. }
  704. strcpy (namebuf + name_length, string + 1);
  705. np = addname (namebuf, change_dir, false, name);
  706. if (!child_head)
  707. child_head = np;
  708. else
  709. child_tail->sibling = np;
  710. child_tail = np;
  711. tar_stat_init (&subdir);
  712. subdir.parent = st;
  713. if (st->fd < 0)
  714. {
  715. subfd = -1;
  716. errno = - st->fd;
  717. }
  718. else
  719. subfd = subfile_open (st, string + 1,
  720. open_read_flags | O_DIRECTORY);
  721. if (subfd < 0)
  722. open_diag (namebuf);
  723. else
  724. {
  725. subdir.fd = subfd;
  726. if (fstat (subfd, &subdir.stat) != 0)
  727. stat_diag (namebuf);
  728. else if (! (O_DIRECTORY || S_ISDIR (subdir.stat.st_mode)))
  729. {
  730. errno = ENOTDIR;
  731. open_diag (namebuf);
  732. }
  733. else
  734. {
  735. subdir.orig_file_name = xstrdup (namebuf);
  736. add_hierarchy_to_namelist (&subdir, np);
  737. restore_parent_fd (&subdir);
  738. }
  739. }
  740. tar_stat_destroy (&subdir);
  741. }
  742. }
  743. free (namebuf);
  744. name->child = child_head;
  745. }
  746. }
  747. /* Auxiliary functions for hashed table of struct name's. */
  748. static size_t
  749. name_hash (void const *entry, size_t n_buckets)
  750. {
  751. struct name const *name = entry;
  752. return hash_string (name->caname, n_buckets);
  753. }
  754. /* Compare two directories for equality of their names. */
  755. static bool
  756. name_compare (void const *entry1, void const *entry2)
  757. {
  758. struct name const *name1 = entry1;
  759. struct name const *name2 = entry2;
  760. return strcmp (name1->caname, name2->caname) == 0;
  761. }
  762. /* Rebase `name' member of CHILD and all its siblings to
  763. the new PARENT. */
  764. static void
  765. rebase_child_list (struct name *child, struct name *parent)
  766. {
  767. size_t old_prefix_len = child->parent->length;
  768. size_t new_prefix_len = parent->length;
  769. char *new_prefix = parent->name;
  770. for (; child; child = child->sibling)
  771. {
  772. size_t size = child->length - old_prefix_len + new_prefix_len;
  773. char *newp = xmalloc (size + 1);
  774. strcpy (newp, new_prefix);
  775. strcat (newp, child->name + old_prefix_len);
  776. free (child->name);
  777. child->name = newp;
  778. child->length = size;
  779. rebase_directory (child->directory,
  780. child->parent->name, old_prefix_len,
  781. new_prefix, new_prefix_len);
  782. }
  783. }
  784. /* Collect all the names from argv[] (or whatever), expand them into a
  785. directory tree, and sort them. This gets only subdirectories, not
  786. all files. */
  787. void
  788. collect_and_sort_names (void)
  789. {
  790. struct name *name;
  791. struct name *next_name, *prev_name = NULL;
  792. int num_names;
  793. Hash_table *nametab;
  794. name_gather ();
  795. if (!namelist)
  796. addname (".", 0, false, NULL);
  797. if (listed_incremental_option)
  798. {
  799. switch (chdir_count ())
  800. {
  801. case 0:
  802. break;
  803. case 1:
  804. if (namelist->change_dir == 0)
  805. USAGE_ERROR ((0, 0,
  806. _("Using -C option inside file list is not "
  807. "allowed with --listed-incremental")));
  808. break;
  809. default:
  810. USAGE_ERROR ((0, 0,
  811. _("Only one -C option is allowed with "
  812. "--listed-incremental")));
  813. }
  814. read_directory_file ();
  815. }
  816. num_names = 0;
  817. for (name = namelist; name; name = name->next, num_names++)
  818. {
  819. struct tar_stat_info st;
  820. if (name->found_count || name->directory)
  821. continue;
  822. if (name->matching_flags & EXCLUDE_WILDCARDS)
  823. /* NOTE: EXCLUDE_ANCHORED is not relevant here */
  824. /* FIXME: just skip regexps for now */
  825. continue;
  826. chdir_do (name->change_dir);
  827. if (name->name[0] == 0)
  828. continue;
  829. tar_stat_init (&st);
  830. if (deref_stat (name->name, &st.stat) != 0)
  831. {
  832. stat_diag (name->name);
  833. continue;
  834. }
  835. if (S_ISDIR (st.stat.st_mode))
  836. {
  837. int dir_fd = openat (chdir_fd, name->name,
  838. open_read_flags | O_DIRECTORY);
  839. if (dir_fd < 0)
  840. open_diag (name->name);
  841. else
  842. {
  843. st.fd = dir_fd;
  844. if (fstat (dir_fd, &st.stat) != 0)
  845. stat_diag (name->name);
  846. else if (O_DIRECTORY || S_ISDIR (st.stat.st_mode))
  847. {
  848. st.orig_file_name = xstrdup (name->name);
  849. name->found_count++;
  850. add_hierarchy_to_namelist (&st, name);
  851. }
  852. }
  853. }
  854. tar_stat_destroy (&st);
  855. }
  856. namelist = merge_sort (namelist, num_names, compare_names);
  857. num_names = 0;
  858. nametab = hash_initialize (0, 0,
  859. name_hash,
  860. name_compare, NULL);
  861. for (name = namelist; name; name = next_name)
  862. {
  863. next_name = name->next;
  864. name->caname = normalize_filename (name->name);
  865. if (prev_name)
  866. {
  867. struct name *p = hash_lookup (nametab, name);
  868. if (p)
  869. {
  870. /* Keep the one listed in the command line */
  871. if (!name->parent)
  872. {
  873. if (p->child)
  874. rebase_child_list (p->child, name);
  875. hash_delete (nametab, name);
  876. /* FIXME: remove_directory (p->caname); ? */
  877. remname (p);
  878. free_name (p);
  879. num_names--;
  880. }
  881. else
  882. {
  883. if (name->child)
  884. rebase_child_list (name->child, p);
  885. /* FIXME: remove_directory (name->caname); ? */
  886. remname (name);
  887. free_name (name);
  888. continue;
  889. }
  890. }
  891. }
  892. name->found_count = 0;
  893. if (!hash_insert (nametab, name))
  894. xalloc_die ();
  895. prev_name = name;
  896. num_names++;
  897. }
  898. nametail = prev_name;
  899. hash_free (nametab);
  900. namelist = merge_sort (namelist, num_names, compare_names_found);
  901. if (listed_incremental_option)
  902. {
  903. for (name = namelist; name && name->name[0] == 0; name++)
  904. ;
  905. if (name)
  906. append_incremental_renames (name->directory);
  907. }
  908. }
  909. /* This is like name_match, except that
  910. 1. It returns a pointer to the name it matched, and doesn't set FOUND
  911. in structure. The caller will have to do that if it wants to.
  912. 2. If the namelist is empty, it returns null, unlike name_match, which
  913. returns TRUE. */
  914. struct name *
  915. name_scan (const char *file_name)
  916. {
  917. size_t length = strlen (file_name);
  918. while (1)
  919. {
  920. struct name *cursor = namelist_match (file_name, length);
  921. if (cursor)
  922. return cursor;
  923. /* Filename from archive not found in namelist. If we have the whole
  924. namelist here, just return 0. Otherwise, read the next name in and
  925. compare it. If this was the last name, namelist->found_count will
  926. remain on. If not, we loop to compare the newly read name. */
  927. if (same_order_option && namelist && namelist->found_count)
  928. {
  929. name_gather (); /* read one more */
  930. if (namelist->found_count)
  931. return 0;
  932. }
  933. else
  934. return 0;
  935. }
  936. }
  937. /* This returns a name from the namelist which doesn't have ->found
  938. set. It sets ->found before returning, so successive calls will
  939. find and return all the non-found names in the namelist. */
  940. struct name *gnu_list_name;
  941. struct name const *
  942. name_from_list ()
  943. {
  944. if (!gnu_list_name)
  945. gnu_list_name = namelist;
  946. while (gnu_list_name
  947. && (gnu_list_name->found_count || gnu_list_name->name[0] == 0))
  948. gnu_list_name = gnu_list_name->next;
  949. if (gnu_list_name)
  950. {
  951. gnu_list_name->found_count++;
  952. chdir_do (gnu_list_name->change_dir);
  953. return gnu_list_name;
  954. }
  955. return NULL;
  956. }
  957. void
  958. blank_name_list (void)
  959. {
  960. struct name *name;
  961. gnu_list_name = 0;
  962. for (name = namelist; name; name = name->next)
  963. name->found_count = 0;
  964. }
  965. /* Yield a newly allocated file name consisting of FILE_NAME concatenated to
  966. NAME, with an intervening slash if FILE_NAME does not already end in one. */
  967. char *
  968. new_name (const char *file_name, const char *name)
  969. {
  970. size_t file_name_len = strlen (file_name);
  971. size_t namesize = strlen (name) + 1;
  972. int slash = file_name_len && ! ISSLASH (file_name[file_name_len - 1]);
  973. char *buffer = xmalloc (file_name_len + slash + namesize);
  974. memcpy (buffer, file_name, file_name_len);
  975. buffer[file_name_len] = '/';
  976. memcpy (buffer + file_name_len + slash, name, namesize);
  977. return buffer;
  978. }
  979. /* Return nonzero if file NAME is excluded. */
  980. bool
  981. excluded_name (char const *name)
  982. {
  983. return excluded_file_name (excluded, name + FILE_SYSTEM_PREFIX_LEN (name));
  984. }
  985. static Hash_table *individual_file_table;
  986. static void
  987. register_individual_file (char const *name)
  988. {
  989. struct stat st;
  990. if (deref_stat (name, &st) != 0)
  991. return; /* Will be complained about later */
  992. if (S_ISDIR (st.st_mode))
  993. return;
  994. hash_string_insert (&individual_file_table, name);
  995. }
  996. bool
  997. is_individual_file (char const *name)
  998. {
  999. return hash_string_lookup (individual_file_table, name);
  1000. }
  1001. /* Return the size of the prefix of FILE_NAME that is removed after
  1002. stripping NUM leading file name components. NUM must be
  1003. positive. */
  1004. size_t
  1005. stripped_prefix_len (char const *file_name, size_t num)
  1006. {
  1007. char const *p = file_name + FILE_SYSTEM_PREFIX_LEN (file_name);
  1008. while (ISSLASH (*p))
  1009. p++;
  1010. while (*p)
  1011. {
  1012. bool slash = ISSLASH (*p);
  1013. p++;
  1014. if (slash)
  1015. {
  1016. if (--num == 0)
  1017. return p - file_name;
  1018. while (ISSLASH (*p))
  1019. p++;
  1020. }
  1021. }
  1022. return -1;
  1023. }
  1024. /* Return nonzero if NAME contains ".." as a file name component. */
  1025. bool
  1026. contains_dot_dot (char const *name)
  1027. {
  1028. char const *p = name + FILE_SYSTEM_PREFIX_LEN (name);
  1029. for (;; p++)
  1030. {
  1031. if (p[0] == '.' && p[1] == '.' && (ISSLASH (p[2]) || !p[2]))
  1032. return 1;
  1033. while (! ISSLASH (*p))
  1034. {
  1035. if (! *p++)
  1036. return 0;
  1037. }
  1038. }
  1039. }