names.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  1. /* Various processing of names.
  2. Copyright (C) 1988, 1992, 1994, 1996, 1997, 1998, 1999, 2000, 2001,
  3. 2003, 2004, 2005, 2006 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 2, 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. struct group *getgrnam ();
  22. struct passwd *getpwnam ();
  23. #if ! HAVE_DECL_GETPWUID
  24. struct passwd *getpwuid ();
  25. #endif
  26. #if ! HAVE_DECL_GETGRGID
  27. struct group *getgrgid ();
  28. #endif
  29. /* Make sure you link with the proper libraries if you are running the
  30. Yellow Peril (thanks for the good laugh, Ian J.!), or, euh... NIS.
  31. This code should also be modified for non-UNIX systems to do something
  32. reasonable. */
  33. static char *cached_uname;
  34. static char *cached_gname;
  35. static uid_t cached_uid; /* valid only if cached_uname is not empty */
  36. static gid_t cached_gid; /* valid only if cached_gname is not empty */
  37. /* These variables are valid only if nonempty. */
  38. static char *cached_no_such_uname;
  39. static char *cached_no_such_gname;
  40. /* These variables are valid only if nonzero. It's not worth optimizing
  41. the case for weird systems where 0 is not a valid uid or gid. */
  42. static uid_t cached_no_such_uid;
  43. static gid_t cached_no_such_gid;
  44. static void register_individual_file (char const *name);
  45. /* Given UID, find the corresponding UNAME. */
  46. void
  47. uid_to_uname (uid_t uid, char **uname)
  48. {
  49. struct passwd *passwd;
  50. if (uid != 0 && uid == cached_no_such_uid)
  51. {
  52. *uname = xstrdup ("");
  53. return;
  54. }
  55. if (!cached_uname || uid != cached_uid)
  56. {
  57. passwd = getpwuid (uid);
  58. if (passwd)
  59. {
  60. cached_uid = uid;
  61. assign_string (&cached_uname, passwd->pw_name);
  62. }
  63. else
  64. {
  65. cached_no_such_uid = uid;
  66. *uname = xstrdup ("");
  67. return;
  68. }
  69. }
  70. *uname = xstrdup (cached_uname);
  71. }
  72. /* Given GID, find the corresponding GNAME. */
  73. void
  74. gid_to_gname (gid_t gid, char **gname)
  75. {
  76. struct group *group;
  77. if (gid != 0 && gid == cached_no_such_gid)
  78. {
  79. *gname = xstrdup ("");
  80. return;
  81. }
  82. if (!cached_gname || gid != cached_gid)
  83. {
  84. group = getgrgid (gid);
  85. if (group)
  86. {
  87. cached_gid = gid;
  88. assign_string (&cached_gname, group->gr_name);
  89. }
  90. else
  91. {
  92. cached_no_such_gid = gid;
  93. *gname = xstrdup ("");
  94. return;
  95. }
  96. }
  97. *gname = xstrdup (cached_gname);
  98. }
  99. /* Given UNAME, set the corresponding UID and return 1, or else, return 0. */
  100. int
  101. uname_to_uid (char const *uname, uid_t *uidp)
  102. {
  103. struct passwd *passwd;
  104. if (cached_no_such_uname
  105. && strcmp (uname, cached_no_such_uname) == 0)
  106. return 0;
  107. if (!cached_uname
  108. || uname[0] != cached_uname[0]
  109. || strcmp (uname, cached_uname) != 0)
  110. {
  111. passwd = getpwnam (uname);
  112. if (passwd)
  113. {
  114. cached_uid = passwd->pw_uid;
  115. assign_string (&cached_uname, passwd->pw_name);
  116. }
  117. else
  118. {
  119. assign_string (&cached_no_such_uname, uname);
  120. return 0;
  121. }
  122. }
  123. *uidp = cached_uid;
  124. return 1;
  125. }
  126. /* Given GNAME, set the corresponding GID and return 1, or else, return 0. */
  127. int
  128. gname_to_gid (char const *gname, gid_t *gidp)
  129. {
  130. struct group *group;
  131. if (cached_no_such_gname
  132. && strcmp (gname, cached_no_such_gname) == 0)
  133. return 0;
  134. if (!cached_gname
  135. || gname[0] != cached_gname[0]
  136. || strcmp (gname, cached_gname) != 0)
  137. {
  138. group = getgrnam (gname);
  139. if (group)
  140. {
  141. cached_gid = group->gr_gid;
  142. assign_string (&cached_gname, gname);
  143. }
  144. else
  145. {
  146. assign_string (&cached_no_such_gname, gname);
  147. return 0;
  148. }
  149. }
  150. *gidp = cached_gid;
  151. return 1;
  152. }
  153. /* Names from the command call. */
  154. static struct name *namelist; /* first name in list, if any */
  155. static struct name **nametail = &namelist; /* end of name list */
  156. static const char **name_array; /* store an array of names */
  157. static int allocated_names; /* how big is the array? */
  158. static int names; /* how many entries does it have? */
  159. static int name_index; /* how many of the entries have we scanned? */
  160. /* Initialize structures. */
  161. void
  162. init_names (void)
  163. {
  164. allocated_names = 10;
  165. name_array = xmalloc (sizeof (const char *) * allocated_names);
  166. names = 0;
  167. }
  168. /* Add NAME at end of name_array, reallocating it as necessary. */
  169. void
  170. name_add (const char *name)
  171. {
  172. if (names == allocated_names)
  173. {
  174. allocated_names *= 2;
  175. name_array =
  176. xrealloc (name_array, sizeof (const char *) * allocated_names);
  177. }
  178. name_array[names++] = name;
  179. }
  180. /* Names from external name file. */
  181. static char *name_buffer; /* buffer to hold the current file name */
  182. static size_t name_buffer_length; /* allocated length of name_buffer */
  183. /* FIXME: I should better check more closely. It seems at first glance that
  184. is_pattern is only used when reading a file, and ignored for all
  185. command line arguments. */
  186. static inline int
  187. is_pattern (const char *string)
  188. {
  189. return strchr (string, '*') || strchr (string, '[') || strchr (string, '?');
  190. }
  191. /* Set up to gather file names for tar. They can either come from a
  192. file or were saved from decoding arguments. */
  193. void
  194. name_init (void)
  195. {
  196. name_buffer = xmalloc (NAME_FIELD_SIZE + 2);
  197. name_buffer_length = NAME_FIELD_SIZE;
  198. }
  199. void
  200. name_term (void)
  201. {
  202. free (name_buffer);
  203. free (name_array);
  204. }
  205. /* Get the next name from ARGV or the file of names. Result is in
  206. static storage and can't be relied upon across two calls.
  207. If CHANGE_DIRS is true, treat a filename of the form "-C" as
  208. meaning that the next filename is the name of a directory to change
  209. to. If filename_terminator is NUL, CHANGE_DIRS is effectively
  210. always false. */
  211. char *
  212. name_next (int change_dirs)
  213. {
  214. const char *source;
  215. char *cursor;
  216. int chdir_flag = 0;
  217. if (filename_terminator == '\0')
  218. change_dirs = 0;
  219. while (name_index != names)
  220. {
  221. size_t source_len;
  222. source = name_array[name_index++];
  223. source_len = strlen (source);
  224. if (name_buffer_length < source_len)
  225. {
  226. do
  227. {
  228. name_buffer_length *= 2;
  229. if (! name_buffer_length)
  230. xalloc_die ();
  231. }
  232. while (name_buffer_length < source_len);
  233. free (name_buffer);
  234. name_buffer = xmalloc (name_buffer_length + 2);
  235. }
  236. strcpy (name_buffer, source);
  237. /* Zap trailing slashes. */
  238. cursor = name_buffer + strlen (name_buffer) - 1;
  239. while (cursor > name_buffer && ISSLASH (*cursor))
  240. *cursor-- = '\0';
  241. if (chdir_flag)
  242. {
  243. if (chdir (name_buffer) < 0)
  244. chdir_fatal (name_buffer);
  245. chdir_flag = 0;
  246. }
  247. else if (change_dirs && strcmp (name_buffer, "-C") == 0)
  248. chdir_flag = 1;
  249. else
  250. {
  251. if (unquote_option)
  252. unquote_string (name_buffer);
  253. if (incremental_option)
  254. register_individual_file (name_buffer);
  255. return name_buffer;
  256. }
  257. }
  258. return 0;
  259. }
  260. /* Gather names in a list for scanning. Could hash them later if we
  261. really care.
  262. If the names are already sorted to match the archive, we just read
  263. them one by one. name_gather reads the first one, and it is called
  264. by name_match as appropriate to read the next ones. At EOF, the
  265. last name read is just left in the buffer. This option lets users
  266. of small machines extract an arbitrary number of files by doing
  267. "tar t" and editing down the list of files. */
  268. void
  269. name_gather (void)
  270. {
  271. /* Buffer able to hold a single name. */
  272. static struct name *buffer;
  273. static size_t allocated_size;
  274. char const *name;
  275. if (same_order_option)
  276. {
  277. static int change_dir;
  278. if (allocated_size == 0)
  279. {
  280. allocated_size = offsetof (struct name, name) + NAME_FIELD_SIZE + 1;
  281. buffer = xmalloc (allocated_size);
  282. /* FIXME: This memset is overkill, and ugly... */
  283. memset (buffer, 0, allocated_size);
  284. }
  285. while ((name = name_next (0)) && strcmp (name, "-C") == 0)
  286. {
  287. char const *dir = name_next (0);
  288. if (! dir)
  289. FATAL_ERROR ((0, 0, _("Missing file name after -C")));
  290. change_dir = chdir_arg (xstrdup (dir));
  291. }
  292. if (name)
  293. {
  294. size_t needed_size;
  295. buffer->length = strlen (name);
  296. needed_size = offsetof (struct name, name) + buffer->length + 1;
  297. if (allocated_size < needed_size)
  298. {
  299. do
  300. {
  301. allocated_size *= 2;
  302. if (! allocated_size)
  303. xalloc_die ();
  304. }
  305. while (allocated_size < needed_size);
  306. buffer = xrealloc (buffer, allocated_size);
  307. }
  308. buffer->change_dir = change_dir;
  309. strcpy (buffer->name, name);
  310. buffer->next = 0;
  311. buffer->found_count = 0;
  312. namelist = buffer;
  313. nametail = &namelist->next;
  314. }
  315. else if (change_dir)
  316. addname (0, change_dir);
  317. }
  318. else
  319. {
  320. /* Non sorted names -- read them all in. */
  321. int change_dir = 0;
  322. for (;;)
  323. {
  324. int change_dir0 = change_dir;
  325. while ((name = name_next (0)) && strcmp (name, "-C") == 0)
  326. {
  327. char const *dir = name_next (0);
  328. if (! dir)
  329. FATAL_ERROR ((0, 0, _("Missing file name after -C")));
  330. change_dir = chdir_arg (xstrdup (dir));
  331. }
  332. if (name)
  333. addname (name, change_dir);
  334. else
  335. {
  336. if (change_dir != change_dir0)
  337. addname (0, change_dir);
  338. break;
  339. }
  340. }
  341. }
  342. }
  343. /* Add a name to the namelist. */
  344. struct name *
  345. addname (char const *string, int change_dir)
  346. {
  347. size_t length = string ? strlen (string) : 0;
  348. struct name *name = xmalloc (offsetof (struct name, name) + length + 1);
  349. if (string)
  350. {
  351. name->fake = 0;
  352. strcpy (name->name, string);
  353. }
  354. else
  355. {
  356. name->fake = 1;
  357. /* FIXME: This initialization (and the byte of memory that it
  358. initializes) is probably not needed, but we are currently in
  359. bug-fix mode so we'll leave it in for now. */
  360. name->name[0] = 0;
  361. }
  362. name->next = 0;
  363. name->length = length;
  364. name->found_count = 0;
  365. name->regexp = 0; /* assume not a regular expression */
  366. name->firstch = 1; /* assume first char is literal */
  367. name->change_dir = change_dir;
  368. name->dir_contents = 0;
  369. name->explicit = 1;
  370. if (string && is_pattern (string))
  371. {
  372. name->regexp = 1;
  373. if (string[0] == '*' || string[0] == '[' || string[0] == '?')
  374. name->firstch = 0;
  375. }
  376. *nametail = name;
  377. nametail = &name->next;
  378. return name;
  379. }
  380. /* Find a match for FILE_NAME (whose string length is LENGTH) in the name
  381. list. */
  382. static struct name *
  383. namelist_match (char const *file_name, size_t length, bool exact)
  384. {
  385. struct name *p;
  386. for (p = namelist; p; p = p->next)
  387. {
  388. /* If first chars don't match, quick skip. */
  389. if (p->firstch && p->name[0] != file_name[0])
  390. continue;
  391. if (p->regexp
  392. ? fnmatch (p->name, file_name, recursion_option) == 0
  393. : exact ? (p->length == length
  394. && memcmp (file_name, p->name, length) == 0)
  395. : (p->length <= length
  396. && (file_name[p->length] == '\0'
  397. || (ISSLASH (file_name[p->length]) && recursion_option))
  398. && memcmp (file_name, p->name, p->length) == 0))
  399. return p;
  400. }
  401. return 0;
  402. }
  403. /* Return true if and only if name FILE_NAME (from an archive) matches any
  404. name from the namelist. */
  405. int
  406. name_match (const char *file_name)
  407. {
  408. size_t length = strlen (file_name);
  409. while (1)
  410. {
  411. struct name *cursor = namelist;
  412. if (!cursor)
  413. return 1;
  414. if (cursor->fake)
  415. {
  416. chdir_do (cursor->change_dir);
  417. namelist = 0;
  418. nametail = &namelist;
  419. return 1;
  420. }
  421. cursor = namelist_match (file_name, length, false);
  422. if (cursor)
  423. {
  424. if (!(ISSLASH (file_name[cursor->length]) && recursion_option)
  425. || cursor->found_count == 0)
  426. cursor->found_count++; /* remember it matched */
  427. if (starting_file_option)
  428. {
  429. free (namelist);
  430. namelist = 0;
  431. nametail = &namelist;
  432. }
  433. chdir_do (cursor->change_dir);
  434. /* We got a match. */
  435. return ISFOUND (cursor);
  436. }
  437. /* Filename from archive not found in namelist. If we have the whole
  438. namelist here, just return 0. Otherwise, read the next name in and
  439. compare it. If this was the last name, namelist->found_count will
  440. remain on. If not, we loop to compare the newly read name. */
  441. if (same_order_option && namelist->found_count)
  442. {
  443. name_gather (); /* read one more */
  444. if (namelist->found_count)
  445. return 0;
  446. }
  447. else
  448. return 0;
  449. }
  450. }
  451. /* Returns true if all names from the namelist were processed.
  452. P is the stat_info of the most recently processed entry.
  453. The decision is postponed until the next entry is read if:
  454. 1) P ended with a slash (i.e. it was a directory)
  455. 2) P matches any entry from the namelist *and* represents a subdirectory
  456. or a file lying under this entry (in the terms of directory structure).
  457. This is necessary to handle contents of directories. */
  458. bool
  459. all_names_found (struct tar_stat_info *p)
  460. {
  461. struct name const *cursor;
  462. size_t len;
  463. if (test_label_option)
  464. return true;
  465. if (!p->file_name || occurrence_option == 0 || p->had_trailing_slash)
  466. return false;
  467. len = strlen (p->file_name);
  468. for (cursor = namelist; cursor; cursor = cursor->next)
  469. {
  470. if (cursor->regexp
  471. || (!WASFOUND(cursor) && !cursor->fake)
  472. || (len >= cursor->length && ISSLASH (p->file_name[cursor->length])))
  473. return false;
  474. }
  475. return true;
  476. }
  477. /* Print the names of things in the namelist that were not matched. */
  478. void
  479. names_notfound (void)
  480. {
  481. struct name const *cursor;
  482. for (cursor = namelist; cursor; cursor = cursor->next)
  483. if (!WASFOUND(cursor) && !cursor->fake)
  484. {
  485. if (cursor->found_count == 0)
  486. ERROR ((0, 0, _("%s: Not found in archive"),
  487. quotearg_colon (cursor->name)));
  488. else
  489. ERROR ((0, 0, _("%s: Required occurrence not found in archive"),
  490. quotearg_colon (cursor->name)));
  491. }
  492. /* Don't bother freeing the name list; we're about to exit. */
  493. namelist = 0;
  494. nametail = &namelist;
  495. if (same_order_option)
  496. {
  497. char *name;
  498. while ((name = name_next (1)) != NULL)
  499. ERROR ((0, 0, _("%s: Not found in archive"),
  500. quotearg_colon (name)));
  501. }
  502. }
  503. /* Sorting name lists. */
  504. /* Sort linked LIST of names, of given LENGTH, using COMPARE to order
  505. names. Return the sorted list. Apart from the type `struct name'
  506. and the definition of SUCCESSOR, this is a generic list-sorting
  507. function, but it's too painful to make it both generic and portable
  508. in C. */
  509. static struct name *
  510. merge_sort (struct name *list, int length,
  511. int (*compare) (struct name const*, struct name const*))
  512. {
  513. struct name *first_list;
  514. struct name *second_list;
  515. int first_length;
  516. int second_length;
  517. struct name *result;
  518. struct name **merge_point;
  519. struct name *cursor;
  520. int counter;
  521. # define SUCCESSOR(name) ((name)->next)
  522. if (length == 1)
  523. return list;
  524. if (length == 2)
  525. {
  526. if ((*compare) (list, SUCCESSOR (list)) > 0)
  527. {
  528. result = SUCCESSOR (list);
  529. SUCCESSOR (result) = list;
  530. SUCCESSOR (list) = 0;
  531. return result;
  532. }
  533. return list;
  534. }
  535. first_list = list;
  536. first_length = (length + 1) / 2;
  537. second_length = length / 2;
  538. for (cursor = list, counter = first_length - 1;
  539. counter;
  540. cursor = SUCCESSOR (cursor), counter--)
  541. continue;
  542. second_list = SUCCESSOR (cursor);
  543. SUCCESSOR (cursor) = 0;
  544. first_list = merge_sort (first_list, first_length, compare);
  545. second_list = merge_sort (second_list, second_length, compare);
  546. merge_point = &result;
  547. while (first_list && second_list)
  548. if ((*compare) (first_list, second_list) < 0)
  549. {
  550. cursor = SUCCESSOR (first_list);
  551. *merge_point = first_list;
  552. merge_point = &SUCCESSOR (first_list);
  553. first_list = cursor;
  554. }
  555. else
  556. {
  557. cursor = SUCCESSOR (second_list);
  558. *merge_point = second_list;
  559. merge_point = &SUCCESSOR (second_list);
  560. second_list = cursor;
  561. }
  562. if (first_list)
  563. *merge_point = first_list;
  564. else
  565. *merge_point = second_list;
  566. return result;
  567. #undef SUCCESSOR
  568. }
  569. /* A comparison function for sorting names. Put found names last;
  570. break ties by string comparison. */
  571. static int
  572. compare_names (struct name const *n1, struct name const *n2)
  573. {
  574. int found_diff = WASFOUND(n2) - WASFOUND(n1);
  575. return found_diff ? found_diff : strcmp (n1->name, n2->name);
  576. }
  577. /* Add all the dirs under NAME, which names a directory, to the namelist.
  578. If any of the files is a directory, recurse on the subdirectory.
  579. DEVICE is the device not to leave, if the -l option is specified. */
  580. static void
  581. add_hierarchy_to_namelist (struct name *name, dev_t device)
  582. {
  583. char *file_name = name->name;
  584. char *buffer = get_directory_contents (file_name, device);
  585. if (! buffer)
  586. name->dir_contents = "\0\0\0\0";
  587. else
  588. {
  589. size_t name_length = name->length;
  590. size_t allocated_length = (name_length >= NAME_FIELD_SIZE
  591. ? name_length + NAME_FIELD_SIZE
  592. : NAME_FIELD_SIZE);
  593. char *namebuf = xmalloc (allocated_length + 1);
  594. /* FIXME: + 2 above? */
  595. char *string;
  596. size_t string_length;
  597. int change_dir = name->change_dir;
  598. name->dir_contents = buffer;
  599. strcpy (namebuf, file_name);
  600. if (! ISSLASH (namebuf[name_length - 1]))
  601. {
  602. namebuf[name_length++] = '/';
  603. namebuf[name_length] = '\0';
  604. }
  605. for (string = buffer; *string; string += string_length + 1)
  606. {
  607. string_length = strlen (string);
  608. if (*string == 'D')
  609. {
  610. struct name *np;
  611. if (allocated_length <= name_length + string_length)
  612. {
  613. do
  614. {
  615. allocated_length *= 2;
  616. if (! allocated_length)
  617. xalloc_die ();
  618. }
  619. while (allocated_length <= name_length + string_length);
  620. namebuf = xrealloc (namebuf, allocated_length + 1);
  621. }
  622. strcpy (namebuf + name_length, string + 1);
  623. np = addname (namebuf, change_dir);
  624. np->explicit = 0;
  625. add_hierarchy_to_namelist (np, device);
  626. }
  627. }
  628. free (namebuf);
  629. }
  630. }
  631. /* Collect all the names from argv[] (or whatever), expand them into a
  632. directory tree, and sort them. This gets only subdirectories, not
  633. all files. */
  634. void
  635. collect_and_sort_names (void)
  636. {
  637. struct name *name;
  638. struct name *next_name;
  639. int num_names;
  640. struct stat statbuf;
  641. name_gather ();
  642. if (listed_incremental_option)
  643. read_directory_file ();
  644. if (!namelist)
  645. addname (".", 0);
  646. for (name = namelist; name; name = next_name)
  647. {
  648. next_name = name->next;
  649. if (name->found_count || name->dir_contents)
  650. continue;
  651. if (name->regexp) /* FIXME: just skip regexps for now */
  652. continue;
  653. chdir_do (name->change_dir);
  654. if (name->fake)
  655. continue;
  656. if (deref_stat (dereference_option, name->name, &statbuf) != 0)
  657. {
  658. stat_diag (name->name);
  659. continue;
  660. }
  661. if (S_ISDIR (statbuf.st_mode))
  662. {
  663. name->found_count++;
  664. add_hierarchy_to_namelist (name, statbuf.st_dev);
  665. }
  666. }
  667. num_names = 0;
  668. for (name = namelist; name; name = name->next)
  669. num_names++;
  670. namelist = merge_sort (namelist, num_names, compare_names);
  671. for (name = namelist; name; name = name->next)
  672. name->found_count = 0;
  673. if (listed_incremental_option)
  674. {
  675. for (name = namelist; name && name->fake; name++)
  676. ;
  677. if (name)
  678. name->dir_contents = append_incremental_renames (name->dir_contents);
  679. }
  680. }
  681. /* This is like name_match, except that
  682. 1. It returns a pointer to the name it matched, and doesn't set FOUND
  683. in structure. The caller will have to do that if it wants to.
  684. 2. If the namelist is empty, it returns null, unlike name_match, which
  685. returns TRUE.
  686. 3. The second argument (EXACT) controls matching algorithm. If it
  687. is TRUE, the exact matching is used. However, regular expressions are
  688. always matched as such, no matter what the value of EXACT is. */
  689. struct name *
  690. name_scan (const char *file_name, bool exact)
  691. {
  692. size_t length = strlen (file_name);
  693. while (1)
  694. {
  695. struct name *cursor = namelist_match (file_name, length, exact);
  696. if (cursor)
  697. return cursor;
  698. /* Filename from archive not found in namelist. If we have the whole
  699. namelist here, just return 0. Otherwise, read the next name in and
  700. compare it. If this was the last name, namelist->found_count will
  701. remain on. If not, we loop to compare the newly read name. */
  702. if (same_order_option && namelist && namelist->found_count)
  703. {
  704. name_gather (); /* read one more */
  705. if (namelist->found_count)
  706. return 0;
  707. }
  708. else
  709. return 0;
  710. }
  711. }
  712. /* This returns a name from the namelist which doesn't have ->found
  713. set. It sets ->found before returning, so successive calls will
  714. find and return all the non-found names in the namelist. */
  715. struct name *gnu_list_name;
  716. char *
  717. name_from_list (void)
  718. {
  719. if (!gnu_list_name)
  720. gnu_list_name = namelist;
  721. while (gnu_list_name && (gnu_list_name->found_count || gnu_list_name->fake))
  722. gnu_list_name = gnu_list_name->next;
  723. if (gnu_list_name)
  724. {
  725. gnu_list_name->found_count++;
  726. chdir_do (gnu_list_name->change_dir);
  727. return gnu_list_name->name;
  728. }
  729. return 0;
  730. }
  731. void
  732. blank_name_list (void)
  733. {
  734. struct name *name;
  735. gnu_list_name = 0;
  736. for (name = namelist; name; name = name->next)
  737. name->found_count = 0;
  738. }
  739. /* Yield a newly allocated file name consisting of FILE_NAME concatenated to
  740. NAME, with an intervening slash if FILE_NAME does not already end in one. */
  741. char *
  742. new_name (const char *file_name, const char *name)
  743. {
  744. size_t file_name_len = strlen (file_name);
  745. size_t namesize = strlen (name) + 1;
  746. int slash = file_name_len && ! ISSLASH (file_name[file_name_len - 1]);
  747. char *buffer = xmalloc (file_name_len + slash + namesize);
  748. memcpy (buffer, file_name, file_name_len);
  749. buffer[file_name_len] = '/';
  750. memcpy (buffer + file_name_len + slash, name, namesize);
  751. return buffer;
  752. }
  753. /* Return nonzero if file NAME is excluded. */
  754. bool
  755. excluded_name (char const *name)
  756. {
  757. return excluded_file_name (excluded, name + FILE_SYSTEM_PREFIX_LEN (name));
  758. }
  759. /* Names to avoid dumping. */
  760. static Hash_table *avoided_name_table;
  761. /* Remember to not archive NAME. */
  762. void
  763. add_avoided_name (char const *name)
  764. {
  765. hash_string_insert (&avoided_name_table, name);
  766. }
  767. /* Should NAME be avoided when archiving? */
  768. bool
  769. is_avoided_name (char const *name)
  770. {
  771. return hash_string_lookup (avoided_name_table, name);
  772. }
  773. static Hash_table *individual_file_table;
  774. static void
  775. register_individual_file (char const *name)
  776. {
  777. struct stat st;
  778. if (deref_stat (dereference_option, name, &st) != 0)
  779. return; /* Will be complained about later */
  780. if (S_ISDIR (st.st_mode))
  781. return;
  782. hash_string_insert (&individual_file_table, name);
  783. }
  784. bool
  785. is_individual_file (char const *name)
  786. {
  787. return hash_string_lookup (individual_file_table, name);
  788. }
  789. /* Return the size of the prefix of FILE_NAME that is removed after
  790. stripping NUM leading file name components. NUM must be
  791. positive. */
  792. size_t
  793. stripped_prefix_len (char const *file_name, size_t num)
  794. {
  795. char const *p = file_name + FILE_SYSTEM_PREFIX_LEN (file_name);
  796. while (ISSLASH (*p))
  797. p++;
  798. while (*p)
  799. {
  800. bool slash = ISSLASH (*p);
  801. p++;
  802. if (slash)
  803. {
  804. if (--num == 0)
  805. return p - file_name;
  806. while (ISSLASH (*p))
  807. p++;
  808. }
  809. }
  810. return -1;
  811. }
  812. /* Return nonzero if NAME contains ".." as a file name component. */
  813. bool
  814. contains_dot_dot (char const *name)
  815. {
  816. char const *p = name + FILE_SYSTEM_PREFIX_LEN (name);
  817. for (;; p++)
  818. {
  819. if (p[0] == '.' && p[1] == '.' && (ISSLASH (p[2]) || !p[2]))
  820. return 1;
  821. do
  822. {
  823. if (! *p++)
  824. return 0;
  825. }
  826. while (! ISSLASH (*p));
  827. }
  828. }