names.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  1. /* Various processing of names.
  2. Copyright (C) 1988, 1992, 1994, 1996, 1997, 1998, 1999, 2000, 2001,
  3. 2003, 2004, 2005, 2006, 2007 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. 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. /* File name arguments are processed in two stages: first a
  157. name_array (see below) is filled, then the names from it
  158. are moved into the namelist.
  159. This awkward process is needed only to implement --same-order option,
  160. which is meant to help process large archives on machines with
  161. limited memory. With this option on, namelist contains at most one
  162. entry, which diminishes the memory consumption.
  163. However, I very much doubt if we still need this -- Sergey */
  164. /* A name_array element contains entries of three types: */
  165. #define NELT_NAME 0 /* File name */
  166. #define NELT_CHDIR 1 /* Change directory request */
  167. #define NELT_FMASK 2 /* Change fnmatch options request */
  168. struct name_elt /* A name_array element. */
  169. {
  170. char type; /* Element type, see NELT_* constants above */
  171. union
  172. {
  173. const char *name; /* File or directory name */
  174. int matching_flags;/* fnmatch options if type == NELT_FMASK */
  175. } v;
  176. };
  177. static struct name_elt *name_array; /* store an array of names */
  178. static size_t allocated_names; /* how big is the array? */
  179. static size_t names; /* how many entries does it have? */
  180. static size_t name_index; /* how many of the entries have we scanned? */
  181. /* Check the size of name_array, reallocating it as necessary. */
  182. static void
  183. check_name_alloc ()
  184. {
  185. if (names == allocated_names)
  186. {
  187. if (allocated_names == 0)
  188. allocated_names = 10; /* Set initial allocation */
  189. name_array = x2nrealloc (name_array, &allocated_names,
  190. sizeof (name_array[0]));
  191. }
  192. }
  193. /* Add to name_array the file NAME with fnmatch options MATCHING_FLAGS */
  194. void
  195. name_add_name (const char *name, int matching_flags)
  196. {
  197. static int prev_flags = 0; /* FIXME: Or EXCLUDE_ANCHORED? */
  198. struct name_elt *ep;
  199. check_name_alloc ();
  200. ep = &name_array[names++];
  201. if (prev_flags != matching_flags)
  202. {
  203. ep->type = NELT_FMASK;
  204. ep->v.matching_flags = matching_flags;
  205. prev_flags = matching_flags;
  206. check_name_alloc ();
  207. ep = &name_array[names++];
  208. }
  209. ep->type = NELT_NAME;
  210. ep->v.name = name;
  211. }
  212. /* Add to name_array a chdir request for the directory NAME */
  213. void
  214. name_add_dir (const char *name)
  215. {
  216. struct name_elt *ep;
  217. check_name_alloc ();
  218. ep = &name_array[names++];
  219. ep->type = NELT_CHDIR;
  220. ep->v.name = name;
  221. }
  222. /* Names from external name file. */
  223. static char *name_buffer; /* buffer to hold the current file name */
  224. static size_t name_buffer_length; /* allocated length of name_buffer */
  225. /* Set up to gather file names for tar. They can either come from a
  226. file or were saved from decoding arguments. */
  227. void
  228. name_init (void)
  229. {
  230. name_buffer = xmalloc (NAME_FIELD_SIZE + 2);
  231. name_buffer_length = NAME_FIELD_SIZE;
  232. }
  233. void
  234. name_term (void)
  235. {
  236. free (name_buffer);
  237. free (name_array);
  238. }
  239. static int matching_flags; /* exclude_fnmatch options */
  240. /* Get the next NELT_NAME element from name_array. Result is in
  241. static storage and can't be relied upon across two calls.
  242. If CHANGE_DIRS is true, treat any entries of type NELT_CHDIR as
  243. the request to change to the given directory. If filename_terminator
  244. is NUL, CHANGE_DIRS is effectively always false.
  245. Entries of type NELT_FMASK cause updates of the matching_flags
  246. value. */
  247. struct name_elt *
  248. name_next_elt (int change_dirs)
  249. {
  250. static struct name_elt entry;
  251. const char *source;
  252. char *cursor;
  253. if (filename_terminator == '\0')
  254. change_dirs = 0;
  255. while (name_index != names)
  256. {
  257. struct name_elt *ep;
  258. size_t source_len;
  259. ep = &name_array[name_index++];
  260. if (ep->type == NELT_FMASK)
  261. {
  262. matching_flags = ep->v.matching_flags;
  263. continue;
  264. }
  265. source = ep->v.name;
  266. source_len = strlen (source);
  267. if (name_buffer_length < source_len)
  268. {
  269. do
  270. {
  271. name_buffer_length *= 2;
  272. if (! name_buffer_length)
  273. xalloc_die ();
  274. }
  275. while (name_buffer_length < source_len);
  276. free (name_buffer);
  277. name_buffer = xmalloc (name_buffer_length + 2);
  278. }
  279. strcpy (name_buffer, source);
  280. /* Zap trailing slashes. */
  281. cursor = name_buffer + strlen (name_buffer) - 1;
  282. while (cursor > name_buffer && ISSLASH (*cursor))
  283. *cursor-- = '\0';
  284. if (change_dirs && ep->type == NELT_CHDIR)
  285. {
  286. if (chdir (name_buffer) < 0)
  287. chdir_fatal (name_buffer);
  288. }
  289. else
  290. {
  291. if (unquote_option)
  292. unquote_string (name_buffer);
  293. if (incremental_option)
  294. register_individual_file (name_buffer);
  295. entry.type = ep->type;
  296. entry.v.name = name_buffer;
  297. return &entry;
  298. }
  299. }
  300. return NULL;
  301. }
  302. const char *
  303. name_next (int change_dirs)
  304. {
  305. struct name_elt *nelt = name_next_elt (change_dirs);
  306. return nelt ? nelt->v.name : NULL;
  307. }
  308. /* Gather names in a list for scanning. Could hash them later if we
  309. really care.
  310. If the names are already sorted to match the archive, we just read
  311. them one by one. name_gather reads the first one, and it is called
  312. by name_match as appropriate to read the next ones. At EOF, the
  313. last name read is just left in the buffer. This option lets users
  314. of small machines extract an arbitrary number of files by doing
  315. "tar t" and editing down the list of files. */
  316. void
  317. name_gather (void)
  318. {
  319. /* Buffer able to hold a single name. */
  320. static struct name *buffer;
  321. static size_t allocated_size;
  322. struct name_elt *ep;
  323. if (same_order_option)
  324. {
  325. static int change_dir;
  326. if (allocated_size == 0)
  327. {
  328. allocated_size = offsetof (struct name, name) + NAME_FIELD_SIZE + 1;
  329. buffer = xmalloc (allocated_size);
  330. /* FIXME: This memset is overkill, and ugly... */
  331. memset (buffer, 0, allocated_size);
  332. }
  333. while ((ep = name_next_elt (0)) && ep->type == NELT_CHDIR)
  334. change_dir = chdir_arg (xstrdup (ep->v.name));
  335. if (ep)
  336. {
  337. size_t needed_size;
  338. buffer->length = strlen (ep->v.name);
  339. needed_size = offsetof (struct name, name) + buffer->length + 1;
  340. if (allocated_size < needed_size)
  341. {
  342. do
  343. {
  344. allocated_size *= 2;
  345. if (! allocated_size)
  346. xalloc_die ();
  347. }
  348. while (allocated_size < needed_size);
  349. buffer = xrealloc (buffer, allocated_size);
  350. }
  351. buffer->change_dir = change_dir;
  352. strcpy (buffer->name, ep->v.name);
  353. buffer->next = 0;
  354. buffer->found_count = 0;
  355. buffer->matching_flags = matching_flags;
  356. namelist = buffer;
  357. nametail = &namelist->next;
  358. }
  359. else if (change_dir)
  360. addname (0, change_dir);
  361. }
  362. else
  363. {
  364. /* Non sorted names -- read them all in. */
  365. int change_dir = 0;
  366. for (;;)
  367. {
  368. int change_dir0 = change_dir;
  369. while ((ep = name_next_elt (0)) && ep->type == NELT_CHDIR)
  370. change_dir = chdir_arg (xstrdup (ep->v.name));
  371. if (ep)
  372. addname (ep->v.name, change_dir);
  373. else
  374. {
  375. if (change_dir != change_dir0)
  376. addname (0, change_dir);
  377. break;
  378. }
  379. }
  380. }
  381. }
  382. /* Add a name to the namelist. */
  383. struct name *
  384. addname (char const *string, int change_dir)
  385. {
  386. size_t length = string ? strlen (string) : 0;
  387. struct name *name = xmalloc (offsetof (struct name, name) + length + 1);
  388. if (string)
  389. strcpy (name->name, string);
  390. else
  391. name->name[0] = 0;
  392. name->next = NULL;
  393. name->length = length;
  394. name->found_count = 0;
  395. name->matching_flags = matching_flags;
  396. name->change_dir = change_dir;
  397. name->dir_contents = NULL;
  398. *nametail = name;
  399. nametail = &name->next;
  400. return name;
  401. }
  402. /* Find a match for FILE_NAME (whose string length is LENGTH) in the name
  403. list. */
  404. static struct name *
  405. namelist_match (char const *file_name, size_t length)
  406. {
  407. struct name *p;
  408. for (p = namelist; p; p = p->next)
  409. {
  410. if (p->name[0]
  411. && exclude_fnmatch (p->name, file_name, p->matching_flags))
  412. return p;
  413. }
  414. return NULL;
  415. }
  416. /* Return true if and only if name FILE_NAME (from an archive) matches any
  417. name from the namelist. */
  418. bool
  419. name_match (const char *file_name)
  420. {
  421. size_t length = strlen (file_name);
  422. while (1)
  423. {
  424. struct name *cursor = namelist;
  425. if (!cursor)
  426. return true;
  427. if (cursor->name[0] == 0)
  428. {
  429. chdir_do (cursor->change_dir);
  430. namelist = 0;
  431. nametail = &namelist;
  432. return true;
  433. }
  434. cursor = namelist_match (file_name, length);
  435. if (cursor)
  436. {
  437. if (!(ISSLASH (file_name[cursor->length]) && recursion_option)
  438. || cursor->found_count == 0)
  439. cursor->found_count++; /* remember it matched */
  440. if (starting_file_option)
  441. {
  442. free (namelist);
  443. namelist = 0;
  444. nametail = &namelist;
  445. }
  446. chdir_do (cursor->change_dir);
  447. /* We got a match. */
  448. return ISFOUND (cursor);
  449. }
  450. /* Filename from archive not found in namelist. If we have the whole
  451. namelist here, just return 0. Otherwise, read the next name in and
  452. compare it. If this was the last name, namelist->found_count will
  453. remain on. If not, we loop to compare the newly read name. */
  454. if (same_order_option && namelist->found_count)
  455. {
  456. name_gather (); /* read one more */
  457. if (namelist->found_count)
  458. return false;
  459. }
  460. else
  461. return false;
  462. }
  463. }
  464. /* Returns true if all names from the namelist were processed.
  465. P is the stat_info of the most recently processed entry.
  466. The decision is postponed until the next entry is read if:
  467. 1) P ended with a slash (i.e. it was a directory)
  468. 2) P matches any entry from the namelist *and* represents a subdirectory
  469. or a file lying under this entry (in the terms of directory structure).
  470. This is necessary to handle contents of directories. */
  471. bool
  472. all_names_found (struct tar_stat_info *p)
  473. {
  474. struct name const *cursor;
  475. size_t len;
  476. if (test_label_option)
  477. return true;
  478. if (!p->file_name || occurrence_option == 0 || p->had_trailing_slash)
  479. return false;
  480. len = strlen (p->file_name);
  481. for (cursor = namelist; cursor; cursor = cursor->next)
  482. {
  483. if ((cursor->name[0] && !WASFOUND (cursor))
  484. || (len >= cursor->length && ISSLASH (p->file_name[cursor->length])))
  485. return false;
  486. }
  487. return true;
  488. }
  489. static inline int
  490. is_pattern (const char *string)
  491. {
  492. return strchr (string, '*') || strchr (string, '[') || strchr (string, '?');
  493. }
  494. static void
  495. regex_usage_warning (const char *name)
  496. {
  497. static int warned_once = 0;
  498. if (warn_regex_usage && is_pattern (name))
  499. {
  500. warned_once = 1;
  501. WARN ((0, 0,
  502. /* TRANSLATORS: The following three msgids form a single sentence.
  503. */
  504. _("Pattern matching characters used in file names. Please,")));
  505. WARN ((0, 0,
  506. _("use --wildcards to enable pattern matching, or --no-wildcards to")));
  507. WARN ((0, 0,
  508. _("suppress this warning.")));
  509. }
  510. }
  511. /* Print the names of things in the namelist that were not matched. */
  512. void
  513. names_notfound (void)
  514. {
  515. struct name const *cursor;
  516. for (cursor = namelist; cursor; cursor = cursor->next)
  517. if (!WASFOUND (cursor) && cursor->name[0])
  518. {
  519. regex_usage_warning (cursor->name);
  520. if (cursor->found_count == 0)
  521. ERROR ((0, 0, _("%s: Not found in archive"),
  522. quotearg_colon (cursor->name)));
  523. else
  524. ERROR ((0, 0, _("%s: Required occurrence not found in archive"),
  525. quotearg_colon (cursor->name)));
  526. }
  527. /* Don't bother freeing the name list; we're about to exit. */
  528. namelist = 0;
  529. nametail = &namelist;
  530. if (same_order_option)
  531. {
  532. const char *name;
  533. while ((name = name_next (1)) != NULL)
  534. {
  535. regex_usage_warning (name);
  536. ERROR ((0, 0, _("%s: Not found in archive"),
  537. quotearg_colon (name)));
  538. }
  539. }
  540. }
  541. /* Sorting name lists. */
  542. /* Sort linked LIST of names, of given LENGTH, using COMPARE to order
  543. names. Return the sorted list. Apart from the type `struct name'
  544. and the definition of SUCCESSOR, this is a generic list-sorting
  545. function, but it's too painful to make it both generic and portable
  546. in C. */
  547. static struct name *
  548. merge_sort (struct name *list, int length,
  549. int (*compare) (struct name const*, struct name const*))
  550. {
  551. struct name *first_list;
  552. struct name *second_list;
  553. int first_length;
  554. int second_length;
  555. struct name *result;
  556. struct name **merge_point;
  557. struct name *cursor;
  558. int counter;
  559. # define SUCCESSOR(name) ((name)->next)
  560. if (length == 1)
  561. return list;
  562. if (length == 2)
  563. {
  564. if ((*compare) (list, SUCCESSOR (list)) > 0)
  565. {
  566. result = SUCCESSOR (list);
  567. SUCCESSOR (result) = list;
  568. SUCCESSOR (list) = 0;
  569. return result;
  570. }
  571. return list;
  572. }
  573. first_list = list;
  574. first_length = (length + 1) / 2;
  575. second_length = length / 2;
  576. for (cursor = list, counter = first_length - 1;
  577. counter;
  578. cursor = SUCCESSOR (cursor), counter--)
  579. continue;
  580. second_list = SUCCESSOR (cursor);
  581. SUCCESSOR (cursor) = 0;
  582. first_list = merge_sort (first_list, first_length, compare);
  583. second_list = merge_sort (second_list, second_length, compare);
  584. merge_point = &result;
  585. while (first_list && second_list)
  586. if ((*compare) (first_list, second_list) < 0)
  587. {
  588. cursor = SUCCESSOR (first_list);
  589. *merge_point = first_list;
  590. merge_point = &SUCCESSOR (first_list);
  591. first_list = cursor;
  592. }
  593. else
  594. {
  595. cursor = SUCCESSOR (second_list);
  596. *merge_point = second_list;
  597. merge_point = &SUCCESSOR (second_list);
  598. second_list = cursor;
  599. }
  600. if (first_list)
  601. *merge_point = first_list;
  602. else
  603. *merge_point = second_list;
  604. return result;
  605. #undef SUCCESSOR
  606. }
  607. /* A comparison function for sorting names. Put found names last;
  608. break ties by string comparison. */
  609. static int
  610. compare_names (struct name const *n1, struct name const *n2)
  611. {
  612. int found_diff = WASFOUND(n2) - WASFOUND(n1);
  613. return found_diff ? found_diff : strcmp (n1->name, n2->name);
  614. }
  615. /* Add all the dirs under NAME, which names a directory, to the namelist.
  616. If any of the files is a directory, recurse on the subdirectory.
  617. DEVICE is the device not to leave, if the -l option is specified. */
  618. static void
  619. add_hierarchy_to_namelist (struct name *name, dev_t device)
  620. {
  621. char *file_name = name->name;
  622. char *buffer = get_directory_contents (file_name, device);
  623. if (! buffer)
  624. name->dir_contents = "\0\0\0\0";
  625. else
  626. {
  627. size_t name_length = name->length;
  628. size_t allocated_length = (name_length >= NAME_FIELD_SIZE
  629. ? name_length + NAME_FIELD_SIZE
  630. : NAME_FIELD_SIZE);
  631. char *namebuf = xmalloc (allocated_length + 1);
  632. /* FIXME: + 2 above? */
  633. char *string;
  634. size_t string_length;
  635. int change_dir = name->change_dir;
  636. name->dir_contents = buffer;
  637. strcpy (namebuf, file_name);
  638. if (! ISSLASH (namebuf[name_length - 1]))
  639. {
  640. namebuf[name_length++] = '/';
  641. namebuf[name_length] = '\0';
  642. }
  643. for (string = buffer; *string; string += string_length + 1)
  644. {
  645. string_length = strlen (string);
  646. if (*string == 'D')
  647. {
  648. struct name *np;
  649. if (allocated_length <= name_length + string_length)
  650. {
  651. do
  652. {
  653. allocated_length *= 2;
  654. if (! allocated_length)
  655. xalloc_die ();
  656. }
  657. while (allocated_length <= name_length + string_length);
  658. namebuf = xrealloc (namebuf, allocated_length + 1);
  659. }
  660. strcpy (namebuf + name_length, string + 1);
  661. np = addname (namebuf, change_dir);
  662. add_hierarchy_to_namelist (np, device);
  663. }
  664. }
  665. free (namebuf);
  666. }
  667. }
  668. /* Collect all the names from argv[] (or whatever), expand them into a
  669. directory tree, and sort them. This gets only subdirectories, not
  670. all files. */
  671. void
  672. collect_and_sort_names (void)
  673. {
  674. struct name *name;
  675. struct name *next_name;
  676. int num_names;
  677. struct stat statbuf;
  678. name_gather ();
  679. if (listed_incremental_option)
  680. read_directory_file ();
  681. if (!namelist)
  682. addname (".", 0);
  683. for (name = namelist; name; name = next_name)
  684. {
  685. next_name = name->next;
  686. if (name->found_count || name->dir_contents)
  687. continue;
  688. if (name->matching_flags & EXCLUDE_WILDCARDS)
  689. /* NOTE: EXCLUDE_ANCHORED is not relevant here */
  690. /* FIXME: just skip regexps for now */
  691. continue;
  692. chdir_do (name->change_dir);
  693. if (name->name[0] == 0)
  694. continue;
  695. if (deref_stat (dereference_option, name->name, &statbuf) != 0)
  696. {
  697. stat_diag (name->name);
  698. continue;
  699. }
  700. if (S_ISDIR (statbuf.st_mode))
  701. {
  702. name->found_count++;
  703. add_hierarchy_to_namelist (name, statbuf.st_dev);
  704. }
  705. }
  706. num_names = 0;
  707. for (name = namelist; name; name = name->next)
  708. num_names++;
  709. namelist = merge_sort (namelist, num_names, compare_names);
  710. for (name = namelist; name; name = name->next)
  711. name->found_count = 0;
  712. if (listed_incremental_option)
  713. {
  714. for (name = namelist; name && name->name[0] == 0; name++)
  715. ;
  716. if (name)
  717. name->dir_contents = append_incremental_renames (name->dir_contents);
  718. }
  719. }
  720. /* This is like name_match, except that
  721. 1. It returns a pointer to the name it matched, and doesn't set FOUND
  722. in structure. The caller will have to do that if it wants to.
  723. 2. If the namelist is empty, it returns null, unlike name_match, which
  724. returns TRUE. */
  725. struct name *
  726. name_scan (const char *file_name)
  727. {
  728. size_t length = strlen (file_name);
  729. while (1)
  730. {
  731. struct name *cursor = namelist_match (file_name, length);
  732. if (cursor)
  733. return cursor;
  734. /* Filename from archive not found in namelist. If we have the whole
  735. namelist here, just return 0. Otherwise, read the next name in and
  736. compare it. If this was the last name, namelist->found_count will
  737. remain on. If not, we loop to compare the newly read name. */
  738. if (same_order_option && namelist && namelist->found_count)
  739. {
  740. name_gather (); /* read one more */
  741. if (namelist->found_count)
  742. return 0;
  743. }
  744. else
  745. return 0;
  746. }
  747. }
  748. /* This returns a name from the namelist which doesn't have ->found
  749. set. It sets ->found before returning, so successive calls will
  750. find and return all the non-found names in the namelist. */
  751. struct name *gnu_list_name;
  752. char *
  753. name_from_list (void)
  754. {
  755. if (!gnu_list_name)
  756. gnu_list_name = namelist;
  757. while (gnu_list_name
  758. && (gnu_list_name->found_count || gnu_list_name->name[0] == 0))
  759. gnu_list_name = gnu_list_name->next;
  760. if (gnu_list_name)
  761. {
  762. gnu_list_name->found_count++;
  763. chdir_do (gnu_list_name->change_dir);
  764. return gnu_list_name->name;
  765. }
  766. return 0;
  767. }
  768. void
  769. blank_name_list (void)
  770. {
  771. struct name *name;
  772. gnu_list_name = 0;
  773. for (name = namelist; name; name = name->next)
  774. name->found_count = 0;
  775. }
  776. /* Yield a newly allocated file name consisting of FILE_NAME concatenated to
  777. NAME, with an intervening slash if FILE_NAME does not already end in one. */
  778. char *
  779. new_name (const char *file_name, const char *name)
  780. {
  781. size_t file_name_len = strlen (file_name);
  782. size_t namesize = strlen (name) + 1;
  783. int slash = file_name_len && ! ISSLASH (file_name[file_name_len - 1]);
  784. char *buffer = xmalloc (file_name_len + slash + namesize);
  785. memcpy (buffer, file_name, file_name_len);
  786. buffer[file_name_len] = '/';
  787. memcpy (buffer + file_name_len + slash, name, namesize);
  788. return buffer;
  789. }
  790. /* Return nonzero if file NAME is excluded. */
  791. bool
  792. excluded_name (char const *name)
  793. {
  794. return excluded_file_name (excluded, name + FILE_SYSTEM_PREFIX_LEN (name));
  795. }
  796. /* Names to avoid dumping. */
  797. static Hash_table *avoided_name_table;
  798. /* Remember to not archive NAME. */
  799. void
  800. add_avoided_name (char const *name)
  801. {
  802. hash_string_insert (&avoided_name_table, name);
  803. }
  804. /* Should NAME be avoided when archiving? */
  805. bool
  806. is_avoided_name (char const *name)
  807. {
  808. return hash_string_lookup (avoided_name_table, name);
  809. }
  810. static Hash_table *individual_file_table;
  811. static void
  812. register_individual_file (char const *name)
  813. {
  814. struct stat st;
  815. if (deref_stat (dereference_option, name, &st) != 0)
  816. return; /* Will be complained about later */
  817. if (S_ISDIR (st.st_mode))
  818. return;
  819. hash_string_insert (&individual_file_table, name);
  820. }
  821. bool
  822. is_individual_file (char const *name)
  823. {
  824. return hash_string_lookup (individual_file_table, name);
  825. }
  826. /* Return the size of the prefix of FILE_NAME that is removed after
  827. stripping NUM leading file name components. NUM must be
  828. positive. */
  829. size_t
  830. stripped_prefix_len (char const *file_name, size_t num)
  831. {
  832. char const *p = file_name + FILE_SYSTEM_PREFIX_LEN (file_name);
  833. while (ISSLASH (*p))
  834. p++;
  835. while (*p)
  836. {
  837. bool slash = ISSLASH (*p);
  838. p++;
  839. if (slash)
  840. {
  841. if (--num == 0)
  842. return p - file_name;
  843. while (ISSLASH (*p))
  844. p++;
  845. }
  846. }
  847. return -1;
  848. }
  849. /* Return nonzero if NAME contains ".." as a file name component. */
  850. bool
  851. contains_dot_dot (char const *name)
  852. {
  853. char const *p = name + FILE_SYSTEM_PREFIX_LEN (name);
  854. for (;; p++)
  855. {
  856. if (p[0] == '.' && p[1] == '.' && (ISSLASH (p[2]) || !p[2]))
  857. return 1;
  858. while (! ISSLASH (*p))
  859. {
  860. if (! *p++)
  861. return 0;
  862. }
  863. }
  864. }