names.c 23 KB

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