names.c 33 KB

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