names.c 22 KB

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