names.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993
  1. /* Various processing of names.
  2. Copyright 1988-2022 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 3, 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, see <http://www.gnu.org/licenses/>. */
  13. #include <system.h>
  14. #include <fnmatch.h>
  15. #include <hash.h>
  16. #include <quotearg.h>
  17. #include <wordsplit.h>
  18. #include <argp.h>
  19. #include "common.h"
  20. static void name_add_option (int option, const char *arg);
  21. static void name_add_dir (const char *name);
  22. static void name_add_file (const char *name);
  23. enum
  24. {
  25. ADD_FILE_OPTION = 256,
  26. EXCLUDE_BACKUPS_OPTION,
  27. EXCLUDE_CACHES_OPTION,
  28. EXCLUDE_CACHES_UNDER_OPTION,
  29. EXCLUDE_CACHES_ALL_OPTION,
  30. EXCLUDE_OPTION,
  31. EXCLUDE_IGNORE_OPTION,
  32. EXCLUDE_IGNORE_RECURSIVE_OPTION,
  33. EXCLUDE_TAG_OPTION,
  34. EXCLUDE_TAG_UNDER_OPTION,
  35. EXCLUDE_TAG_ALL_OPTION,
  36. EXCLUDE_VCS_OPTION,
  37. EXCLUDE_VCS_IGNORES_OPTION,
  38. IGNORE_CASE_OPTION,
  39. NO_IGNORE_CASE_OPTION,
  40. ANCHORED_OPTION,
  41. NO_ANCHORED_OPTION,
  42. RECURSION_OPTION,
  43. NO_RECURSION_OPTION,
  44. UNQUOTE_OPTION,
  45. NO_UNQUOTE_OPTION,
  46. NO_VERBATIM_FILES_FROM_OPTION,
  47. NO_WILDCARDS_MATCH_SLASH_OPTION,
  48. NO_WILDCARDS_OPTION,
  49. NULL_OPTION,
  50. NO_NULL_OPTION,
  51. VERBATIM_FILES_FROM_OPTION,
  52. WILDCARDS_MATCH_SLASH_OPTION,
  53. WILDCARDS_OPTION
  54. };
  55. enum
  56. {
  57. GRH_LOCAL,
  58. GRID_LOCAL,
  59. GRH_MATCH,
  60. GRID_MATCH,
  61. };
  62. static struct argp_option names_options[] = {
  63. {NULL, 0, NULL, 0,
  64. N_("Local file name selection:"), GRH_LOCAL },
  65. {"add-file", ADD_FILE_OPTION, N_("FILE"), 0,
  66. N_("add given FILE to the archive (useful if its name starts with a dash)"), GRID_LOCAL },
  67. {"directory", 'C', N_("DIR"), 0,
  68. N_("change to directory DIR"), GRID_LOCAL },
  69. {"files-from", 'T', N_("FILE"), 0,
  70. N_("get names to extract or create from FILE"), GRID_LOCAL },
  71. {"null", NULL_OPTION, 0, 0,
  72. N_("-T reads null-terminated names; implies --verbatim-files-from"),
  73. GRID_LOCAL },
  74. {"no-null", NO_NULL_OPTION, 0, 0,
  75. N_("disable the effect of the previous --null option"), GRID_LOCAL },
  76. {"unquote", UNQUOTE_OPTION, 0, 0,
  77. N_("unquote input file or member names (default)"), GRID_LOCAL },
  78. {"no-unquote", NO_UNQUOTE_OPTION, 0, 0,
  79. N_("do not unquote input file or member names"), GRID_LOCAL },
  80. {"verbatim-files-from", VERBATIM_FILES_FROM_OPTION, 0, 0,
  81. N_("-T reads file names verbatim (no escape or option handling)"), GRID_LOCAL },
  82. {"no-verbatim-files-from", NO_VERBATIM_FILES_FROM_OPTION, 0, 0,
  83. N_("-T treats file names starting with dash as options (default)"),
  84. GRID_LOCAL },
  85. {"exclude", EXCLUDE_OPTION, N_("PATTERN"), 0,
  86. N_("exclude files, given as a PATTERN"), GRID_LOCAL },
  87. {"exclude-from", 'X', N_("FILE"), 0,
  88. N_("exclude patterns listed in FILE"), GRID_LOCAL },
  89. {"exclude-caches", EXCLUDE_CACHES_OPTION, 0, 0,
  90. N_("exclude contents of directories containing CACHEDIR.TAG, "
  91. "except for the tag file itself"), GRID_LOCAL },
  92. {"exclude-caches-under", EXCLUDE_CACHES_UNDER_OPTION, 0, 0,
  93. N_("exclude everything under directories containing CACHEDIR.TAG"),
  94. GRID_LOCAL },
  95. {"exclude-caches-all", EXCLUDE_CACHES_ALL_OPTION, 0, 0,
  96. N_("exclude directories containing CACHEDIR.TAG"), GRID_LOCAL },
  97. {"exclude-tag", EXCLUDE_TAG_OPTION, N_("FILE"), 0,
  98. N_("exclude contents of directories containing FILE, except"
  99. " for FILE itself"), GRID_LOCAL },
  100. {"exclude-ignore", EXCLUDE_IGNORE_OPTION, N_("FILE"), 0,
  101. N_("read exclude patterns for each directory from FILE, if it exists"),
  102. GRID_LOCAL },
  103. {"exclude-ignore-recursive", EXCLUDE_IGNORE_RECURSIVE_OPTION, N_("FILE"), 0,
  104. N_("read exclude patterns for each directory and its subdirectories "
  105. "from FILE, if it exists"), GRID_LOCAL },
  106. {"exclude-tag-under", EXCLUDE_TAG_UNDER_OPTION, N_("FILE"), 0,
  107. N_("exclude everything under directories containing FILE"), GRID_LOCAL },
  108. {"exclude-tag-all", EXCLUDE_TAG_ALL_OPTION, N_("FILE"), 0,
  109. N_("exclude directories containing FILE"), GRID_LOCAL },
  110. {"exclude-vcs", EXCLUDE_VCS_OPTION, NULL, 0,
  111. N_("exclude version control system directories"), GRID_LOCAL },
  112. {"exclude-vcs-ignores", EXCLUDE_VCS_IGNORES_OPTION, NULL, 0,
  113. N_("read exclude patterns from the VCS ignore files"), GRID_LOCAL },
  114. {"exclude-backups", EXCLUDE_BACKUPS_OPTION, NULL, 0,
  115. N_("exclude backup and lock files"), GRID_LOCAL },
  116. {"recursion", RECURSION_OPTION, 0, 0,
  117. N_("recurse into directories (default)"), GRID_LOCAL },
  118. {"no-recursion", NO_RECURSION_OPTION, 0, 0,
  119. N_("avoid descending automatically in directories"), GRID_LOCAL },
  120. {NULL, 0, NULL, 0,
  121. N_("File name matching options (affect both exclude and include patterns):"),
  122. GRH_MATCH },
  123. {"anchored", ANCHORED_OPTION, 0, 0,
  124. N_("patterns match file name start"), GRID_MATCH },
  125. {"no-anchored", NO_ANCHORED_OPTION, 0, 0,
  126. N_("patterns match after any '/' (default for exclusion)"), GRID_MATCH },
  127. {"ignore-case", IGNORE_CASE_OPTION, 0, 0,
  128. N_("ignore case"), GRID_MATCH },
  129. {"no-ignore-case", NO_IGNORE_CASE_OPTION, 0, 0,
  130. N_("case sensitive matching (default)"), GRID_MATCH },
  131. {"wildcards", WILDCARDS_OPTION, 0, 0,
  132. N_("use wildcards (default for exclusion)"), GRID_MATCH },
  133. {"no-wildcards", NO_WILDCARDS_OPTION, 0, 0,
  134. N_("verbatim string matching"), GRID_MATCH },
  135. {"wildcards-match-slash", WILDCARDS_MATCH_SLASH_OPTION, 0, 0,
  136. N_("wildcards match '/' (default for exclusion)"), GRID_MATCH },
  137. {"no-wildcards-match-slash", NO_WILDCARDS_MATCH_SLASH_OPTION, 0, 0,
  138. N_("wildcards do not match '/'"), GRID_MATCH },
  139. {NULL}
  140. };
  141. static struct argp_option const *
  142. file_selection_option (int key)
  143. {
  144. struct argp_option *p;
  145. for (p = names_options;
  146. !(p->name == NULL && p->key == 0 && p->doc == NULL); p++)
  147. if (p->key == key)
  148. return p;
  149. return NULL;
  150. }
  151. static char const *
  152. file_selection_option_name (int key)
  153. {
  154. struct argp_option const *opt = file_selection_option (key);
  155. return opt ? opt->name : NULL;
  156. }
  157. static bool
  158. is_file_selection_option (int key)
  159. {
  160. return file_selection_option (key) != NULL;
  161. }
  162. /* Either NL or NUL, as decided by the --null option. */
  163. static char filename_terminator = '\n';
  164. /* Treat file names read from -T input verbatim */
  165. static bool verbatim_files_from_option;
  166. static error_t
  167. names_parse_opt (int key, char *arg, struct argp_state *state)
  168. {
  169. switch (key)
  170. {
  171. case 'C':
  172. name_add_dir (arg);
  173. break;
  174. case 'T':
  175. name_add_file (arg);
  176. break;
  177. case ADD_FILE_OPTION:
  178. name_add_name (arg);
  179. break;
  180. case ARGP_KEY_ERROR:
  181. {
  182. struct tar_args *args = state->input;
  183. if (args->loc->source == OPTS_FILE)
  184. {
  185. error (0, 0, _("%s:%lu: unrecognized option"), args->loc->name,
  186. (unsigned long) args->loc->line);
  187. set_exit_status (TAREXIT_FAILURE);
  188. }
  189. return ARGP_ERR_UNKNOWN;
  190. }
  191. default:
  192. if (is_file_selection_option (key))
  193. name_add_option (key, arg);
  194. else
  195. return ARGP_ERR_UNKNOWN;
  196. }
  197. return 0;
  198. }
  199. /* Wildcard matching settings */
  200. enum wildcards
  201. {
  202. default_wildcards, /* For exclusion == enable_wildcards,
  203. for inclusion == disable_wildcards */
  204. disable_wildcards,
  205. enable_wildcards
  206. };
  207. static enum wildcards wildcards = default_wildcards;
  208. /* Wildcard settings (--wildcards/--no-wildcards) */
  209. static int matching_flags = 0;
  210. /* exclude_fnmatch options */
  211. static int include_anchored = EXCLUDE_ANCHORED;
  212. /* Pattern anchoring options used for file inclusion */
  213. #define EXCLUDE_OPTIONS \
  214. (((wildcards != disable_wildcards) ? EXCLUDE_WILDCARDS : 0) \
  215. | matching_flags \
  216. | recursion_option)
  217. #define INCLUDE_OPTIONS \
  218. (((wildcards == enable_wildcards) ? EXCLUDE_WILDCARDS : 0) \
  219. | include_anchored \
  220. | matching_flags \
  221. | recursion_option)
  222. static char const * const vcs_file_table[] = {
  223. /* CVS: */
  224. "CVS",
  225. ".cvsignore",
  226. /* RCS: */
  227. "RCS",
  228. /* SCCS: */
  229. "SCCS",
  230. /* SVN: */
  231. ".svn",
  232. /* git: */
  233. ".git",
  234. ".gitignore",
  235. ".gitattributes",
  236. ".gitmodules",
  237. /* Arch: */
  238. ".arch-ids",
  239. "{arch}",
  240. "=RELEASE-ID",
  241. "=meta-update",
  242. "=update",
  243. /* Bazaar */
  244. ".bzr",
  245. ".bzrignore",
  246. ".bzrtags",
  247. /* Mercurial */
  248. ".hg",
  249. ".hgignore",
  250. ".hgtags",
  251. /* darcs */
  252. "_darcs",
  253. NULL
  254. };
  255. static char const * const backup_file_table[] = {
  256. ".#*",
  257. "*~",
  258. "#*#",
  259. NULL
  260. };
  261. static void
  262. add_exclude_array (char const * const * fv, int opts)
  263. {
  264. int i;
  265. for (i = 0; fv[i]; i++)
  266. add_exclude (excluded, fv[i], opts);
  267. }
  268. static void
  269. handle_file_selection_option (int key, const char *arg)
  270. {
  271. switch (key)
  272. {
  273. case EXCLUDE_BACKUPS_OPTION:
  274. add_exclude_array (backup_file_table, EXCLUDE_WILDCARDS);
  275. break;
  276. case EXCLUDE_OPTION:
  277. add_exclude (excluded, arg, EXCLUDE_OPTIONS);
  278. break;
  279. case EXCLUDE_CACHES_OPTION:
  280. add_exclusion_tag ("CACHEDIR.TAG", exclusion_tag_contents,
  281. cachedir_file_p);
  282. break;
  283. case EXCLUDE_CACHES_UNDER_OPTION:
  284. add_exclusion_tag ("CACHEDIR.TAG", exclusion_tag_under,
  285. cachedir_file_p);
  286. break;
  287. case EXCLUDE_CACHES_ALL_OPTION:
  288. add_exclusion_tag ("CACHEDIR.TAG", exclusion_tag_all,
  289. cachedir_file_p);
  290. break;
  291. case EXCLUDE_IGNORE_OPTION:
  292. excfile_add (arg, EXCL_NON_RECURSIVE);
  293. break;
  294. case EXCLUDE_IGNORE_RECURSIVE_OPTION:
  295. excfile_add (arg, EXCL_RECURSIVE);
  296. break;
  297. case EXCLUDE_TAG_OPTION:
  298. add_exclusion_tag (arg, exclusion_tag_contents, NULL);
  299. break;
  300. case EXCLUDE_TAG_UNDER_OPTION:
  301. add_exclusion_tag (arg, exclusion_tag_under, NULL);
  302. break;
  303. case EXCLUDE_TAG_ALL_OPTION:
  304. add_exclusion_tag (arg, exclusion_tag_all, NULL);
  305. break;
  306. case EXCLUDE_VCS_OPTION:
  307. add_exclude_array (vcs_file_table, 0);
  308. break;
  309. case EXCLUDE_VCS_IGNORES_OPTION:
  310. exclude_vcs_ignores ();
  311. break;
  312. case RECURSION_OPTION:
  313. recursion_option = FNM_LEADING_DIR;
  314. break;
  315. case NO_RECURSION_OPTION:
  316. recursion_option = 0;
  317. break;
  318. case UNQUOTE_OPTION:
  319. unquote_option = true;
  320. break;
  321. case NO_UNQUOTE_OPTION:
  322. unquote_option = false;
  323. break;
  324. case NULL_OPTION:
  325. filename_terminator = '\0';
  326. verbatim_files_from_option = true;
  327. break;
  328. case NO_NULL_OPTION:
  329. filename_terminator = '\n';
  330. verbatim_files_from_option = false;
  331. break;
  332. case 'X':
  333. if (add_exclude_file (add_exclude, excluded, arg, EXCLUDE_OPTIONS, '\n')
  334. != 0)
  335. {
  336. int e = errno;
  337. FATAL_ERROR ((0, e, "%s", quotearg_colon (arg)));
  338. }
  339. break;
  340. case ANCHORED_OPTION:
  341. matching_flags |= EXCLUDE_ANCHORED;
  342. break;
  343. case NO_ANCHORED_OPTION:
  344. include_anchored = 0; /* Clear the default for command line args */
  345. matching_flags &= ~ EXCLUDE_ANCHORED;
  346. break;
  347. case IGNORE_CASE_OPTION:
  348. matching_flags |= FNM_CASEFOLD;
  349. break;
  350. case NO_IGNORE_CASE_OPTION:
  351. matching_flags &= ~ FNM_CASEFOLD;
  352. break;
  353. case WILDCARDS_OPTION:
  354. wildcards = enable_wildcards;
  355. break;
  356. case NO_WILDCARDS_OPTION:
  357. wildcards = disable_wildcards;
  358. break;
  359. case WILDCARDS_MATCH_SLASH_OPTION:
  360. matching_flags &= ~ FNM_FILE_NAME;
  361. break;
  362. case NO_WILDCARDS_MATCH_SLASH_OPTION:
  363. matching_flags |= FNM_FILE_NAME;
  364. break;
  365. case VERBATIM_FILES_FROM_OPTION:
  366. verbatim_files_from_option = true;
  367. break;
  368. case NO_VERBATIM_FILES_FROM_OPTION:
  369. verbatim_files_from_option = false;
  370. break;
  371. default:
  372. FATAL_ERROR ((0, 0, "unhandled positional option %d", key));
  373. }
  374. }
  375. struct argp names_argp = {
  376. names_options,
  377. names_parse_opt,
  378. NULL,
  379. NULL,
  380. NULL,
  381. NULL,
  382. NULL
  383. };
  384. /* User and group names. */
  385. /* Make sure you link with the proper libraries if you are running the
  386. Yellow Peril (thanks for the good laugh, Ian J.!), or, euh... NIS.
  387. This code should also be modified for non-UNIX systems to do something
  388. reasonable. */
  389. static char *cached_uname;
  390. static char *cached_gname;
  391. static uid_t cached_uid; /* valid only if cached_uname is not empty */
  392. static gid_t cached_gid; /* valid only if cached_gname is not empty */
  393. /* These variables are valid only if nonempty. */
  394. static char *cached_no_such_uname;
  395. static char *cached_no_such_gname;
  396. /* These variables are valid only if nonzero. It's not worth optimizing
  397. the case for weird systems where 0 is not a valid uid or gid. */
  398. static uid_t cached_no_such_uid;
  399. static gid_t cached_no_such_gid;
  400. /* Given UID, find the corresponding UNAME. */
  401. void
  402. uid_to_uname (uid_t uid, char **uname)
  403. {
  404. struct passwd *passwd;
  405. if (uid != 0 && uid == cached_no_such_uid)
  406. {
  407. *uname = xstrdup ("");
  408. return;
  409. }
  410. if (!cached_uname || uid != cached_uid)
  411. {
  412. passwd = getpwuid (uid);
  413. if (passwd)
  414. {
  415. cached_uid = uid;
  416. assign_string (&cached_uname, passwd->pw_name);
  417. }
  418. else
  419. {
  420. cached_no_such_uid = uid;
  421. *uname = xstrdup ("");
  422. return;
  423. }
  424. }
  425. *uname = xstrdup (cached_uname);
  426. }
  427. /* Given GID, find the corresponding GNAME. */
  428. void
  429. gid_to_gname (gid_t gid, char **gname)
  430. {
  431. struct group *group;
  432. if (gid != 0 && gid == cached_no_such_gid)
  433. {
  434. *gname = xstrdup ("");
  435. return;
  436. }
  437. if (!cached_gname || gid != cached_gid)
  438. {
  439. group = getgrgid (gid);
  440. if (group)
  441. {
  442. cached_gid = gid;
  443. assign_string (&cached_gname, group->gr_name);
  444. }
  445. else
  446. {
  447. cached_no_such_gid = gid;
  448. *gname = xstrdup ("");
  449. return;
  450. }
  451. }
  452. *gname = xstrdup (cached_gname);
  453. }
  454. /* Given UNAME, set the corresponding UID and return 1, or else, return 0. */
  455. int
  456. uname_to_uid (char const *uname, uid_t *uidp)
  457. {
  458. struct passwd *passwd;
  459. if (cached_no_such_uname
  460. && strcmp (uname, cached_no_such_uname) == 0)
  461. return 0;
  462. if (!cached_uname
  463. || uname[0] != cached_uname[0]
  464. || strcmp (uname, cached_uname) != 0)
  465. {
  466. passwd = getpwnam (uname);
  467. if (passwd)
  468. {
  469. cached_uid = passwd->pw_uid;
  470. assign_string (&cached_uname, passwd->pw_name);
  471. }
  472. else
  473. {
  474. assign_string (&cached_no_such_uname, uname);
  475. return 0;
  476. }
  477. }
  478. *uidp = cached_uid;
  479. return 1;
  480. }
  481. /* Given GNAME, set the corresponding GID and return 1, or else, return 0. */
  482. int
  483. gname_to_gid (char const *gname, gid_t *gidp)
  484. {
  485. struct group *group;
  486. if (cached_no_such_gname
  487. && strcmp (gname, cached_no_such_gname) == 0)
  488. return 0;
  489. if (!cached_gname
  490. || gname[0] != cached_gname[0]
  491. || strcmp (gname, cached_gname) != 0)
  492. {
  493. group = getgrnam (gname);
  494. if (group)
  495. {
  496. cached_gid = group->gr_gid;
  497. assign_string (&cached_gname, gname);
  498. }
  499. else
  500. {
  501. assign_string (&cached_no_such_gname, gname);
  502. return 0;
  503. }
  504. }
  505. *gidp = cached_gid;
  506. return 1;
  507. }
  508. static struct name *
  509. make_name (const char *file_name)
  510. {
  511. struct name *p = xzalloc (sizeof (*p));
  512. if (!file_name)
  513. file_name = "";
  514. p->name = xstrdup (file_name);
  515. p->length = strlen (p->name);
  516. return p;
  517. }
  518. static void
  519. free_name (struct name *p)
  520. {
  521. if (p)
  522. {
  523. free (p->name);
  524. free (p->caname);
  525. free (p);
  526. }
  527. }
  528. /* Names from the command call. */
  529. static struct name *namelist; /* first name in list, if any */
  530. static struct name *nametail; /* end of name list */
  531. /* File name arguments are processed in two stages: first a
  532. name element list (see below) is filled, then the names from it
  533. are moved into the namelist.
  534. This awkward process is needed only to implement --same-order option,
  535. which is meant to help process large archives on machines with
  536. limited memory. With this option on, namelist contains at most one
  537. entry, which diminishes the memory consumption.
  538. However, I very much doubt if we still need this -- Sergey */
  539. /* A name_list element contains entries of three types: */
  540. enum nelt_type
  541. {
  542. NELT_NAME, /* File name */
  543. NELT_CHDIR, /* Change directory request */
  544. NELT_FILE, /* Read file names from that file */
  545. NELT_NOOP, /* No operation */
  546. NELT_OPTION /* Filename-selection option */
  547. };
  548. struct name_elt /* A name_array element. */
  549. {
  550. struct name_elt *next, *prev;
  551. enum nelt_type type; /* Element type, see NELT_* constants above */
  552. union
  553. {
  554. const char *name; /* File or directory name */
  555. struct /* File, if type == NELT_FILE */
  556. {
  557. const char *name;/* File name */
  558. size_t line; /* Input line number */
  559. int term; /* File name terminator in the list */
  560. bool verbatim; /* Verbatim handling of file names: no white-space
  561. trimming, no option processing */
  562. FILE *fp;
  563. } file;
  564. struct
  565. {
  566. int option;
  567. char const *arg;
  568. } opt; /* NELT_OPTION */
  569. } v;
  570. };
  571. static struct name_elt *name_head; /* store a list of names */
  572. /* how many of the entries are file names? */
  573. enum files_count filename_args = FILES_NONE;
  574. static struct name_elt *
  575. name_elt_alloc (void)
  576. {
  577. struct name_elt *elt;
  578. elt = xmalloc (sizeof (*elt));
  579. if (!name_head)
  580. {
  581. name_head = elt;
  582. name_head->prev = name_head->next = NULL;
  583. name_head->type = NELT_NOOP;
  584. elt = xmalloc (sizeof (*elt));
  585. }
  586. elt->prev = name_head->prev;
  587. if (name_head->prev)
  588. name_head->prev->next = elt;
  589. elt->next = name_head;
  590. name_head->prev = elt;
  591. return elt;
  592. }
  593. static void
  594. name_list_adjust (void)
  595. {
  596. if (name_head)
  597. while (name_head->prev)
  598. name_head = name_head->prev;
  599. }
  600. /* For error-reporting purposes, keep a doubly-linked list of unconsumed file
  601. selection options. The option is deemed unconsumed unless followed by one
  602. or more file/member name arguments. When archive creation is requested,
  603. each file selection option encountered is pushed into the list. The list
  604. is cleared upon encountering a file name argument.
  605. If the list is not empty when all arguments have been processed, an error
  606. message is issued reporting the options that had no effect.
  607. For simplicity, only a tail pointer of the list is maintained.
  608. */
  609. struct name_elt *unconsumed_option_tail;
  610. /* Push an option to the list */
  611. static void
  612. unconsumed_option_push (struct name_elt *elt)
  613. {
  614. elt->next = NULL;
  615. elt->prev = unconsumed_option_tail;
  616. if (unconsumed_option_tail)
  617. unconsumed_option_tail->next = elt;
  618. unconsumed_option_tail = elt;
  619. }
  620. /* Clear the unconsumed option list */
  621. static void
  622. unconsumed_option_free (void)
  623. {
  624. while (unconsumed_option_tail)
  625. {
  626. struct name_elt *elt = unconsumed_option_tail;
  627. unconsumed_option_tail = unconsumed_option_tail->prev;
  628. free (elt);
  629. }
  630. }
  631. /* Report any options that have not been consumed */
  632. static void
  633. unconsumed_option_report (void)
  634. {
  635. if (unconsumed_option_tail)
  636. {
  637. struct name_elt *elt;
  638. ERROR ((0, 0, _("The following options were used after any non-optional arguments in archive create or update mode. These options are positional and affect only arguments that follow them. Please, rearrange them properly.")));
  639. elt = unconsumed_option_tail;
  640. while (elt->prev)
  641. elt = elt->prev;
  642. while (elt)
  643. {
  644. switch (elt->type)
  645. {
  646. case NELT_CHDIR:
  647. ERROR ((0, 0, _("-C %s has no effect"), quote (elt->v.name)));
  648. break;
  649. case NELT_OPTION:
  650. if (elt->v.opt.arg)
  651. ERROR ((0, 0, _("--%s %s has no effect"),
  652. file_selection_option_name (elt->v.opt.option),
  653. quote (elt->v.opt.arg)));
  654. else
  655. ERROR ((0, 0, _("--%s has no effect"),
  656. file_selection_option_name (elt->v.opt.option)));
  657. break;
  658. default:
  659. break;
  660. }
  661. elt = elt->next;
  662. }
  663. unconsumed_option_free ();
  664. }
  665. }
  666. static void
  667. name_list_advance (void)
  668. {
  669. struct name_elt *elt = name_head;
  670. name_head = elt->next;
  671. if (name_head)
  672. name_head->prev = NULL;
  673. if (elt->type == NELT_OPTION || elt->type == NELT_CHDIR)
  674. {
  675. if (subcommand_option == CREATE_SUBCOMMAND
  676. || subcommand_option == UPDATE_SUBCOMMAND)
  677. unconsumed_option_push (elt);
  678. }
  679. else
  680. {
  681. if (elt->type != NELT_NOOP)
  682. unconsumed_option_free ();
  683. free (elt);
  684. }
  685. }
  686. /* Add to name_array the file NAME with fnmatch options MATFLAGS */
  687. void
  688. name_add_name (const char *name)
  689. {
  690. struct name_elt *ep = name_elt_alloc ();
  691. ep->type = NELT_NAME;
  692. ep->v.name = name;
  693. switch (filename_args)
  694. {
  695. case FILES_NONE:
  696. filename_args = FILES_ONE;
  697. break;
  698. case FILES_ONE:
  699. filename_args = FILES_MANY;
  700. break;
  701. default:
  702. break;
  703. }
  704. }
  705. static void
  706. name_add_option (int option, const char *arg)
  707. {
  708. struct name_elt *elt = name_elt_alloc ();
  709. elt->type = NELT_OPTION;
  710. elt->v.opt.option = option;
  711. elt->v.opt.arg = arg;
  712. }
  713. /* Add to name_array a chdir request for the directory NAME */
  714. static void
  715. name_add_dir (const char *name)
  716. {
  717. struct name_elt *ep = name_elt_alloc ();
  718. ep->type = NELT_CHDIR;
  719. ep->v.name = name;
  720. }
  721. static void
  722. name_add_file (const char *name)
  723. {
  724. struct name_elt *ep = name_elt_alloc ();
  725. ep->type = NELT_FILE;
  726. ep->v.file.name = name;
  727. ep->v.file.line = 0;
  728. ep->v.file.fp = NULL;
  729. /* We don't know beforehand how many files are listed.
  730. Assume more than one. */
  731. filename_args = FILES_MANY;
  732. }
  733. /* Names from external name file. */
  734. static char *name_buffer; /* buffer to hold the current file name */
  735. static size_t name_buffer_length; /* allocated length of name_buffer */
  736. /* Set up to gather file names for tar. They can either come from a
  737. file or were saved from decoding arguments. */
  738. void
  739. name_init (void)
  740. {
  741. name_buffer = xmalloc (NAME_FIELD_SIZE + 2);
  742. name_buffer_length = NAME_FIELD_SIZE;
  743. name_list_adjust ();
  744. }
  745. void
  746. name_term (void)
  747. {
  748. free (name_buffer);
  749. }
  750. /* Prevent recursive inclusion of the same file */
  751. struct file_id_list
  752. {
  753. struct file_id_list *next;
  754. ino_t ino;
  755. dev_t dev;
  756. const char *from_file;
  757. };
  758. static struct file_id_list *file_id_list;
  759. /* Return the name of the file from which the file names and options
  760. are being read.
  761. */
  762. static const char *
  763. file_list_name (void)
  764. {
  765. struct name_elt *elt;
  766. for (elt = name_head; elt; elt = elt->next)
  767. if (elt->type == NELT_FILE && elt->v.file.fp)
  768. return elt->v.file.name;
  769. return _("command line");
  770. }
  771. static int
  772. add_file_id (const char *filename)
  773. {
  774. struct file_id_list *p;
  775. struct stat st;
  776. const char *reading_from;
  777. if (stat (filename, &st))
  778. stat_fatal (filename);
  779. reading_from = file_list_name ();
  780. for (p = file_id_list; p; p = p->next)
  781. if (p->ino == st.st_ino && p->dev == st.st_dev)
  782. {
  783. int oldc = set_char_quoting (NULL, ':', 1);
  784. ERROR ((0, 0,
  785. _("%s: file list requested from %s already read from %s"),
  786. quotearg_n (0, filename),
  787. reading_from, p->from_file));
  788. set_char_quoting (NULL, ':', oldc);
  789. return 1;
  790. }
  791. p = xmalloc (sizeof *p);
  792. p->next = file_id_list;
  793. p->ino = st.st_ino;
  794. p->dev = st.st_dev;
  795. p->from_file = reading_from;
  796. file_id_list = p;
  797. return 0;
  798. }
  799. /* Chop trailing slashes. */
  800. static void
  801. chopslash (char *str)
  802. {
  803. char *p = str + strlen (str) - 1;
  804. while (p > str && ISSLASH (*p))
  805. *p-- = '\0';
  806. }
  807. enum read_file_list_state /* Result of reading file name from the list file */
  808. {
  809. file_list_success, /* OK, name read successfully */
  810. file_list_end, /* End of list file */
  811. file_list_zero, /* Zero separator encountered where it should not */
  812. file_list_skip /* Empty (zero-length) entry encountered, skip it */
  813. };
  814. /* Read from FP a sequence of characters up to TERM and put them
  815. into STK.
  816. */
  817. static enum read_file_list_state
  818. read_name_from_file (struct name_elt *ent)
  819. {
  820. int c;
  821. size_t counter = 0;
  822. FILE *fp = ent->v.file.fp;
  823. int term = ent->v.file.term;
  824. ++ent->v.file.line;
  825. for (c = getc (fp); c != EOF && c != term; c = getc (fp))
  826. {
  827. if (counter == name_buffer_length)
  828. name_buffer = x2realloc (name_buffer, &name_buffer_length);
  829. name_buffer[counter++] = c;
  830. if (c == 0)
  831. {
  832. /* We have read a zero separator. The file possibly is
  833. zero-separated */
  834. return file_list_zero;
  835. }
  836. }
  837. if (counter == 0 && c != EOF)
  838. return file_list_skip;
  839. if (counter == name_buffer_length)
  840. name_buffer = x2realloc (name_buffer, &name_buffer_length);
  841. name_buffer[counter] = 0;
  842. return (counter == 0 && c == EOF) ? file_list_end : file_list_success;
  843. }
  844. static int
  845. handle_option (const char *str, struct name_elt const *ent)
  846. {
  847. struct wordsplit ws;
  848. int i;
  849. struct option_locus loc;
  850. while (*str && isspace (*str))
  851. ++str;
  852. if (*str != '-')
  853. return 1;
  854. ws.ws_offs = 1;
  855. if (wordsplit (str, &ws, WRDSF_DEFFLAGS|WRDSF_DOOFFS))
  856. FATAL_ERROR ((0, 0, _("cannot split string '%s': %s"),
  857. str, wordsplit_strerror (&ws)));
  858. ws.ws_wordv[0] = (char *) program_name;
  859. loc.source = OPTS_FILE;
  860. loc.name = ent->v.file.name;
  861. loc.line = ent->v.file.line;
  862. more_options (ws.ws_wordc+ws.ws_offs, ws.ws_wordv, &loc);
  863. for (i = 0; i < ws.ws_wordc+ws.ws_offs; i++)
  864. ws.ws_wordv[i] = NULL;
  865. wordsplit_free (&ws);
  866. return 0;
  867. }
  868. static int
  869. read_next_name (struct name_elt *ent, struct name_elt *ret)
  870. {
  871. if (!ent->v.file.fp)
  872. {
  873. if (!strcmp (ent->v.file.name, "-"))
  874. {
  875. request_stdin ("-T");
  876. ent->v.file.fp = stdin;
  877. }
  878. else
  879. {
  880. if (add_file_id (ent->v.file.name))
  881. {
  882. name_list_advance ();
  883. return 1;
  884. }
  885. if ((ent->v.file.fp = fopen (ent->v.file.name, "r")) == NULL)
  886. open_fatal (ent->v.file.name);
  887. }
  888. ent->v.file.term = filename_terminator;
  889. ent->v.file.verbatim = verbatim_files_from_option;
  890. }
  891. while (1)
  892. {
  893. switch (read_name_from_file (ent))
  894. {
  895. case file_list_skip:
  896. continue;
  897. case file_list_zero:
  898. WARNOPT (WARN_FILENAME_WITH_NULS,
  899. (0, 0, N_("%s: file name read contains nul character"),
  900. quotearg_colon (ent->v.file.name)));
  901. ent->v.file.term = 0;
  902. FALLTHROUGH;
  903. case file_list_success:
  904. if (!ent->v.file.verbatim)
  905. {
  906. if (unquote_option)
  907. unquote_string (name_buffer);
  908. if (handle_option (name_buffer, ent) == 0)
  909. {
  910. name_list_adjust ();
  911. return 1;
  912. }
  913. }
  914. chopslash (name_buffer);
  915. ret->type = NELT_NAME;
  916. ret->v.name = name_buffer;
  917. return 0;
  918. case file_list_end:
  919. if (strcmp (ent->v.file.name, "-"))
  920. fclose (ent->v.file.fp);
  921. ent->v.file.fp = NULL;
  922. name_list_advance ();
  923. return 1;
  924. }
  925. }
  926. }
  927. static void
  928. copy_name (struct name_elt *ep)
  929. {
  930. const char *source;
  931. size_t source_len;
  932. source = ep->v.name;
  933. source_len = strlen (source);
  934. while (name_buffer_length <= source_len)
  935. name_buffer = x2realloc(name_buffer, &name_buffer_length);
  936. strcpy (name_buffer, source);
  937. chopslash (name_buffer);
  938. }
  939. /* Get the next NELT_NAME element from name_array. Result is in
  940. static storage and can't be relied upon across two calls.
  941. If CHANGE_DIRS is true, treat any entries of type NELT_CHDIR as
  942. the request to change to the given directory.
  943. */
  944. static struct name_elt *
  945. name_next_elt (int change_dirs)
  946. {
  947. static struct name_elt entry;
  948. struct name_elt *ep;
  949. while ((ep = name_head) != NULL)
  950. {
  951. switch (ep->type)
  952. {
  953. case NELT_NOOP:
  954. name_list_advance ();
  955. break;
  956. case NELT_FILE:
  957. if (read_next_name (ep, &entry) == 0)
  958. return &entry;
  959. continue;
  960. case NELT_CHDIR:
  961. if (change_dirs)
  962. {
  963. chdir_do (chdir_arg (xstrdup (ep->v.name)));
  964. name_list_advance ();
  965. break;
  966. }
  967. FALLTHROUGH;
  968. case NELT_NAME:
  969. copy_name (ep);
  970. if (unquote_option)
  971. unquote_string (name_buffer);
  972. entry.type = ep->type;
  973. entry.v.name = name_buffer;
  974. name_list_advance ();
  975. return &entry;
  976. case NELT_OPTION:
  977. handle_file_selection_option (ep->v.opt.option, ep->v.opt.arg);
  978. name_list_advance ();
  979. continue;
  980. }
  981. }
  982. unconsumed_option_report ();
  983. return NULL;
  984. }
  985. const char *
  986. name_next (int change_dirs)
  987. {
  988. struct name_elt *nelt = name_next_elt (change_dirs);
  989. return nelt ? nelt->v.name : NULL;
  990. }
  991. /* Gather names in a list for scanning. Could hash them later if we
  992. really care.
  993. If the names are already sorted to match the archive, we just read
  994. them one by one. name_gather reads the first one, and it is called
  995. by name_match as appropriate to read the next ones. At EOF, the
  996. last name read is just left in the buffer. This option lets users
  997. of small machines extract an arbitrary number of files by doing
  998. "tar t" and editing down the list of files. */
  999. void
  1000. name_gather (void)
  1001. {
  1002. /* Buffer able to hold a single name. */
  1003. static struct name *buffer = NULL;
  1004. struct name_elt *ep;
  1005. if (same_order_option)
  1006. {
  1007. static int change_dir;
  1008. while ((ep = name_next_elt (0)) && ep->type == NELT_CHDIR)
  1009. change_dir = chdir_arg (xstrdup (ep->v.name));
  1010. if (ep)
  1011. {
  1012. free_name (buffer);
  1013. buffer = make_name (ep->v.name);
  1014. buffer->change_dir = change_dir;
  1015. buffer->next = 0;
  1016. buffer->found_count = 0;
  1017. buffer->matching_flags = INCLUDE_OPTIONS;
  1018. buffer->directory = NULL;
  1019. buffer->parent = NULL;
  1020. buffer->cmdline = true;
  1021. namelist = nametail = buffer;
  1022. }
  1023. else if (change_dir)
  1024. addname (0, change_dir, false, NULL);
  1025. }
  1026. else
  1027. {
  1028. /* Non sorted names -- read them all in. */
  1029. int change_dir = 0;
  1030. for (;;)
  1031. {
  1032. int change_dir0 = change_dir;
  1033. while ((ep = name_next_elt (0)) && ep->type == NELT_CHDIR)
  1034. change_dir = chdir_arg (xstrdup (ep->v.name));
  1035. if (ep)
  1036. addname (ep->v.name, change_dir, true, NULL);
  1037. else
  1038. {
  1039. if (change_dir != change_dir0)
  1040. addname (NULL, change_dir, false, NULL);
  1041. break;
  1042. }
  1043. }
  1044. }
  1045. }
  1046. /* Add a name to the namelist. */
  1047. struct name *
  1048. addname (char const *string, int change_dir, bool cmdline, struct name *parent)
  1049. {
  1050. struct name *name = make_name (string);
  1051. name->prev = nametail;
  1052. name->next = NULL;
  1053. name->found_count = 0;
  1054. name->matching_flags = INCLUDE_OPTIONS;
  1055. name->change_dir = change_dir;
  1056. name->directory = NULL;
  1057. name->parent = parent;
  1058. name->cmdline = cmdline;
  1059. if (nametail)
  1060. nametail->next = name;
  1061. else
  1062. namelist = name;
  1063. nametail = name;
  1064. return name;
  1065. }
  1066. void
  1067. add_starting_file (char const *file_name)
  1068. {
  1069. struct name *name = make_name (file_name);
  1070. if (starting_file_option)
  1071. {
  1072. struct name *head = namelist;
  1073. remname (head);
  1074. free_name (head);
  1075. }
  1076. name->prev = NULL;
  1077. name->next = namelist;
  1078. namelist = name;
  1079. if (!nametail)
  1080. nametail = namelist;
  1081. name->found_count = 0;
  1082. name->matching_flags = INCLUDE_OPTIONS;
  1083. name->change_dir = 0;
  1084. name->directory = NULL;
  1085. name->parent = NULL;
  1086. name->cmdline = true;
  1087. starting_file_option = true;
  1088. }
  1089. /* Find a match for FILE_NAME (whose string length is LENGTH) in the name
  1090. list. */
  1091. static struct name *
  1092. namelist_match (char const *file_name, size_t length)
  1093. {
  1094. struct name *p;
  1095. for (p = namelist; p; p = p->next)
  1096. {
  1097. if (p->name[0]
  1098. && exclude_fnmatch (p->name, file_name, p->matching_flags))
  1099. return p;
  1100. }
  1101. return NULL;
  1102. }
  1103. void
  1104. remname (struct name *name)
  1105. {
  1106. struct name *p;
  1107. if ((p = name->prev) != NULL)
  1108. p->next = name->next;
  1109. else
  1110. namelist = name->next;
  1111. if ((p = name->next) != NULL)
  1112. p->prev = name->prev;
  1113. else
  1114. nametail = name->prev;
  1115. }
  1116. /* Return true if and only if name FILE_NAME (from an archive) matches any
  1117. name from the namelist. */
  1118. bool
  1119. name_match (const char *file_name)
  1120. {
  1121. size_t length = strlen (file_name);
  1122. while (1)
  1123. {
  1124. struct name *cursor = namelist;
  1125. if (!cursor)
  1126. return true;
  1127. if (cursor->name[0] == 0)
  1128. {
  1129. chdir_do (cursor->change_dir);
  1130. namelist = NULL;
  1131. nametail = NULL;
  1132. return true;
  1133. }
  1134. cursor = namelist_match (file_name, length);
  1135. if (starting_file_option)
  1136. {
  1137. /* If starting_file_option is set, the head of the list is the name
  1138. of the member to start extraction from. Skip the match unless it
  1139. is head. */
  1140. if (cursor == namelist)
  1141. starting_file_option = false;
  1142. else
  1143. cursor = NULL;
  1144. }
  1145. if (cursor)
  1146. {
  1147. if (!(ISSLASH (file_name[cursor->length]) && recursion_option)
  1148. || cursor->found_count == 0)
  1149. cursor->found_count++; /* remember it matched */
  1150. chdir_do (cursor->change_dir);
  1151. /* We got a match. */
  1152. return ISFOUND (cursor);
  1153. }
  1154. /* Filename from archive not found in namelist. If we have the whole
  1155. namelist here, just return 0. Otherwise, read the next name in and
  1156. compare it. If this was the last name, namelist->found_count will
  1157. remain on. If not, we loop to compare the newly read name. */
  1158. if (same_order_option && namelist->found_count)
  1159. {
  1160. name_gather (); /* read one more */
  1161. if (namelist->found_count)
  1162. return false;
  1163. }
  1164. else
  1165. return false;
  1166. }
  1167. }
  1168. /* Returns true if all names from the namelist were processed.
  1169. P is the stat_info of the most recently processed entry.
  1170. The decision is postponed until the next entry is read if:
  1171. 1) P ended with a slash (i.e. it was a directory)
  1172. 2) P matches any entry from the namelist *and* represents a subdirectory
  1173. or a file lying under this entry (in the terms of directory structure).
  1174. This is necessary to handle contents of directories. */
  1175. bool
  1176. all_names_found (struct tar_stat_info *p)
  1177. {
  1178. struct name const *cursor;
  1179. size_t len;
  1180. if (!p->file_name || occurrence_option == 0 || p->had_trailing_slash)
  1181. return false;
  1182. len = strlen (p->file_name);
  1183. for (cursor = namelist; cursor; cursor = cursor->next)
  1184. {
  1185. if ((cursor->name[0] && !WASFOUND (cursor))
  1186. || (len >= cursor->length && ISSLASH (p->file_name[cursor->length])))
  1187. return false;
  1188. }
  1189. return true;
  1190. }
  1191. static int
  1192. regex_usage_warning (const char *name)
  1193. {
  1194. static int warned_once = 0;
  1195. /* Warn about implicit use of the wildcards in command line arguments.
  1196. (Default for tar prior to 1.15.91, but changed afterwards) */
  1197. if (wildcards == default_wildcards
  1198. && fnmatch_pattern_has_wildcards (name, 0))
  1199. {
  1200. warned_once = 1;
  1201. WARN ((0, 0,
  1202. _("Pattern matching characters used in file names")));
  1203. WARN ((0, 0,
  1204. _("Use --wildcards to enable pattern matching,"
  1205. " or --no-wildcards to suppress this warning")));
  1206. }
  1207. return warned_once;
  1208. }
  1209. /* Print the names of things in the namelist that were not matched. */
  1210. void
  1211. names_notfound (void)
  1212. {
  1213. struct name const *cursor;
  1214. for (cursor = namelist; cursor; cursor = cursor->next)
  1215. if (!WASFOUND (cursor) && cursor->name[0])
  1216. {
  1217. regex_usage_warning (cursor->name);
  1218. ERROR ((0, 0,
  1219. (cursor->found_count == 0) ?
  1220. _("%s: Not found in archive") :
  1221. _("%s: Required occurrence not found in archive"),
  1222. quotearg_colon (cursor->name)));
  1223. }
  1224. /* Don't bother freeing the name list; we're about to exit. */
  1225. namelist = NULL;
  1226. nametail = NULL;
  1227. if (same_order_option)
  1228. {
  1229. const char *name;
  1230. while ((name = name_next (1)) != NULL)
  1231. {
  1232. regex_usage_warning (name);
  1233. ERROR ((0, 0, _("%s: Not found in archive"),
  1234. quotearg_colon (name)));
  1235. }
  1236. }
  1237. }
  1238. void
  1239. label_notfound (void)
  1240. {
  1241. struct name const *cursor;
  1242. if (!namelist)
  1243. return;
  1244. for (cursor = namelist; cursor; cursor = cursor->next)
  1245. if (WASFOUND (cursor))
  1246. return;
  1247. if (verbose_option)
  1248. error (0, 0, _("Archive label mismatch"));
  1249. set_exit_status (TAREXIT_DIFFERS);
  1250. for (cursor = namelist; cursor; cursor = cursor->next)
  1251. {
  1252. if (regex_usage_warning (cursor->name))
  1253. break;
  1254. }
  1255. /* Don't bother freeing the name list; we're about to exit. */
  1256. namelist = NULL;
  1257. nametail = NULL;
  1258. if (same_order_option)
  1259. {
  1260. const char *name;
  1261. while ((name = name_next (1)) != NULL
  1262. && regex_usage_warning (name) == 0)
  1263. ;
  1264. }
  1265. }
  1266. /* Sorting name lists. */
  1267. /* Sort *singly* linked LIST of names, of given LENGTH, using COMPARE
  1268. to order names. Return the sorted list. Note that after calling
  1269. this function, the 'prev' links in list elements are messed up.
  1270. Apart from the type 'struct name' and the definition of SUCCESSOR,
  1271. this is a generic list-sorting function, but it's too painful to
  1272. make it both generic and portable
  1273. in C. */
  1274. static struct name *
  1275. merge_sort_sll (struct name *list, int length,
  1276. int (*compare) (struct name const*, struct name const*))
  1277. {
  1278. struct name *first_list;
  1279. struct name *second_list;
  1280. int first_length;
  1281. int second_length;
  1282. struct name *result;
  1283. struct name **merge_point;
  1284. struct name *cursor;
  1285. int counter;
  1286. # define SUCCESSOR(name) ((name)->next)
  1287. if (length == 1)
  1288. return list;
  1289. if (length == 2)
  1290. {
  1291. if ((*compare) (list, SUCCESSOR (list)) > 0)
  1292. {
  1293. result = SUCCESSOR (list);
  1294. SUCCESSOR (result) = list;
  1295. SUCCESSOR (list) = 0;
  1296. return result;
  1297. }
  1298. return list;
  1299. }
  1300. first_list = list;
  1301. first_length = (length + 1) / 2;
  1302. second_length = length / 2;
  1303. for (cursor = list, counter = first_length - 1;
  1304. counter;
  1305. cursor = SUCCESSOR (cursor), counter--)
  1306. continue;
  1307. second_list = SUCCESSOR (cursor);
  1308. SUCCESSOR (cursor) = 0;
  1309. first_list = merge_sort_sll (first_list, first_length, compare);
  1310. second_list = merge_sort_sll (second_list, second_length, compare);
  1311. merge_point = &result;
  1312. while (first_list && second_list)
  1313. if ((*compare) (first_list, second_list) < 0)
  1314. {
  1315. cursor = SUCCESSOR (first_list);
  1316. *merge_point = first_list;
  1317. merge_point = &SUCCESSOR (first_list);
  1318. first_list = cursor;
  1319. }
  1320. else
  1321. {
  1322. cursor = SUCCESSOR (second_list);
  1323. *merge_point = second_list;
  1324. merge_point = &SUCCESSOR (second_list);
  1325. second_list = cursor;
  1326. }
  1327. if (first_list)
  1328. *merge_point = first_list;
  1329. else
  1330. *merge_point = second_list;
  1331. return result;
  1332. #undef SUCCESSOR
  1333. }
  1334. /* Sort doubly linked LIST of names, of given LENGTH, using COMPARE
  1335. to order names. Return the sorted list. */
  1336. static struct name *
  1337. merge_sort (struct name *list, int length,
  1338. int (*compare) (struct name const*, struct name const*))
  1339. {
  1340. struct name *head, *p, *prev;
  1341. head = merge_sort_sll (list, length, compare);
  1342. /* Fixup prev pointers */
  1343. for (prev = NULL, p = head; p; prev = p, p = p->next)
  1344. p->prev = prev;
  1345. return head;
  1346. }
  1347. /* A comparison function for sorting names. Put found names last;
  1348. break ties by string comparison. */
  1349. static int
  1350. compare_names_found (struct name const *n1, struct name const *n2)
  1351. {
  1352. int found_diff = WASFOUND (n2) - WASFOUND (n1);
  1353. return found_diff ? found_diff : strcmp (n1->name, n2->name);
  1354. }
  1355. /* Simple comparison by names. */
  1356. static int
  1357. compare_names (struct name const *n1, struct name const *n2)
  1358. {
  1359. return strcmp (n1->name, n2->name);
  1360. }
  1361. /* Add all the dirs under ST to the namelist NAME, descending the
  1362. directory hierarchy recursively. */
  1363. static void
  1364. add_hierarchy_to_namelist (struct tar_stat_info *st, struct name *name)
  1365. {
  1366. const char *buffer;
  1367. name->directory = scan_directory (st);
  1368. buffer = directory_contents (name->directory);
  1369. if (buffer)
  1370. {
  1371. struct name *child_head = NULL, *child_tail = NULL;
  1372. size_t name_length = name->length;
  1373. size_t allocated_length = (name_length >= NAME_FIELD_SIZE
  1374. ? name_length + NAME_FIELD_SIZE
  1375. : NAME_FIELD_SIZE) + 2;
  1376. char *namebuf = xmalloc (allocated_length);
  1377. const char *string;
  1378. size_t string_length;
  1379. int change_dir = name->change_dir;
  1380. strcpy (namebuf, name->name);
  1381. if (! ISSLASH (namebuf[name_length - 1]))
  1382. {
  1383. namebuf[name_length++] = '/';
  1384. namebuf[name_length] = '\0';
  1385. }
  1386. for (string = buffer; *string; string += string_length + 1)
  1387. {
  1388. string_length = strlen (string);
  1389. if (*string == 'D')
  1390. {
  1391. struct name *np;
  1392. struct tar_stat_info subdir;
  1393. int subfd;
  1394. /* need to have at least string_length bytes above the
  1395. name_length, this includes the trailing null character */
  1396. while (allocated_length < name_length + string_length)
  1397. namebuf = x2realloc (namebuf, &allocated_length);
  1398. strcpy (namebuf + name_length, string + 1);
  1399. np = addname (namebuf, change_dir, false, name);
  1400. if (!child_head)
  1401. child_head = np;
  1402. else
  1403. child_tail->sibling = np;
  1404. child_tail = np;
  1405. tar_stat_init (&subdir);
  1406. subdir.parent = st;
  1407. if (st->fd < 0)
  1408. {
  1409. subfd = -1;
  1410. errno = - st->fd;
  1411. }
  1412. else
  1413. subfd = subfile_open (st, string + 1,
  1414. open_read_flags | O_DIRECTORY);
  1415. if (subfd < 0)
  1416. open_diag (namebuf);
  1417. else
  1418. {
  1419. subdir.fd = subfd;
  1420. if (fstat (subfd, &subdir.stat) != 0)
  1421. stat_diag (namebuf);
  1422. else if (! (O_DIRECTORY || S_ISDIR (subdir.stat.st_mode)))
  1423. {
  1424. errno = ENOTDIR;
  1425. open_diag (namebuf);
  1426. }
  1427. else
  1428. {
  1429. subdir.orig_file_name = xstrdup (namebuf);
  1430. add_hierarchy_to_namelist (&subdir, np);
  1431. restore_parent_fd (&subdir);
  1432. }
  1433. }
  1434. tar_stat_destroy (&subdir);
  1435. }
  1436. }
  1437. free (namebuf);
  1438. name->child = child_head;
  1439. }
  1440. }
  1441. /* Auxiliary functions for hashed table of struct name's. */
  1442. static size_t
  1443. name_hash (void const *entry, size_t n_buckets)
  1444. {
  1445. struct name const *name = entry;
  1446. return hash_string (name->caname, n_buckets);
  1447. }
  1448. /* Compare two directories for equality of their names. */
  1449. static bool
  1450. name_compare (void const *entry1, void const *entry2)
  1451. {
  1452. struct name const *name1 = entry1;
  1453. struct name const *name2 = entry2;
  1454. return strcmp (name1->caname, name2->caname) == 0;
  1455. }
  1456. /* Rebase 'name' member of CHILD and all its siblings to
  1457. the new PARENT. */
  1458. static void
  1459. rebase_child_list (struct name *child, struct name *parent)
  1460. {
  1461. size_t old_prefix_len = child->parent->length;
  1462. size_t new_prefix_len = parent->length;
  1463. char *new_prefix = parent->name;
  1464. for (; child; child = child->sibling)
  1465. {
  1466. size_t size = child->length - old_prefix_len + new_prefix_len;
  1467. char *newp = xmalloc (size + 1);
  1468. strcpy (newp, new_prefix);
  1469. strcat (newp, child->name + old_prefix_len);
  1470. free (child->name);
  1471. child->name = newp;
  1472. child->length = size;
  1473. rebase_directory (child->directory,
  1474. child->parent->name, old_prefix_len,
  1475. new_prefix, new_prefix_len);
  1476. }
  1477. }
  1478. /* Collect all the names from argv[] (or whatever), expand them into a
  1479. directory tree, and sort them. This gets only subdirectories, not
  1480. all files. */
  1481. void
  1482. collect_and_sort_names (void)
  1483. {
  1484. struct name *name;
  1485. struct name *next_name, *prev_name = NULL;
  1486. int num_names;
  1487. Hash_table *nametab;
  1488. name_gather ();
  1489. if (!namelist)
  1490. addname (".", 0, false, NULL);
  1491. if (listed_incremental_option)
  1492. {
  1493. switch (chdir_count ())
  1494. {
  1495. case 0:
  1496. break;
  1497. case 1:
  1498. if (namelist->change_dir == 0)
  1499. USAGE_ERROR ((0, 0,
  1500. _("Using -C option inside file list is not "
  1501. "allowed with --listed-incremental")));
  1502. break;
  1503. default:
  1504. USAGE_ERROR ((0, 0,
  1505. _("Only one -C option is allowed with "
  1506. "--listed-incremental")));
  1507. }
  1508. read_directory_file ();
  1509. }
  1510. num_names = 0;
  1511. for (name = namelist; name; name = name->next, num_names++)
  1512. {
  1513. struct tar_stat_info st;
  1514. if (name->found_count || name->directory)
  1515. continue;
  1516. if (name->matching_flags & EXCLUDE_WILDCARDS)
  1517. /* NOTE: EXCLUDE_ANCHORED is not relevant here */
  1518. /* FIXME: just skip regexps for now */
  1519. continue;
  1520. chdir_do (name->change_dir);
  1521. if (name->name[0] == 0)
  1522. continue;
  1523. tar_stat_init (&st);
  1524. if (deref_stat (name->name, &st.stat) != 0)
  1525. {
  1526. stat_diag (name->name);
  1527. continue;
  1528. }
  1529. if (S_ISDIR (st.stat.st_mode))
  1530. {
  1531. int dir_fd = openat (chdir_fd, name->name,
  1532. open_read_flags | O_DIRECTORY);
  1533. if (dir_fd < 0)
  1534. open_diag (name->name);
  1535. else
  1536. {
  1537. st.fd = dir_fd;
  1538. if (fstat (dir_fd, &st.stat) != 0)
  1539. stat_diag (name->name);
  1540. else if (O_DIRECTORY || S_ISDIR (st.stat.st_mode))
  1541. {
  1542. st.orig_file_name = xstrdup (name->name);
  1543. name->found_count++;
  1544. add_hierarchy_to_namelist (&st, name);
  1545. }
  1546. else
  1547. {
  1548. errno = ENOTDIR;
  1549. open_diag (name->name);
  1550. }
  1551. }
  1552. }
  1553. tar_stat_destroy (&st);
  1554. }
  1555. namelist = merge_sort (namelist, num_names, compare_names);
  1556. num_names = 0;
  1557. nametab = hash_initialize (0, 0, name_hash, name_compare, NULL);
  1558. for (name = namelist; name; name = next_name)
  1559. {
  1560. next_name = name->next;
  1561. name->caname = normalize_filename (name->change_dir, name->name);
  1562. if (prev_name)
  1563. {
  1564. struct name *p = hash_lookup (nametab, name);
  1565. if (p)
  1566. {
  1567. /* Keep the one listed in the command line */
  1568. if (!name->parent)
  1569. {
  1570. if (p->child)
  1571. rebase_child_list (p->child, name);
  1572. hash_remove (nametab, name);
  1573. /* FIXME: remove_directory (p->caname); ? */
  1574. remname (p);
  1575. free_name (p);
  1576. num_names--;
  1577. }
  1578. else
  1579. {
  1580. if (name->child)
  1581. rebase_child_list (name->child, p);
  1582. /* FIXME: remove_directory (name->caname); ? */
  1583. remname (name);
  1584. free_name (name);
  1585. continue;
  1586. }
  1587. }
  1588. }
  1589. name->found_count = 0;
  1590. if (!hash_insert (nametab, name))
  1591. xalloc_die ();
  1592. prev_name = name;
  1593. num_names++;
  1594. }
  1595. nametail = prev_name;
  1596. hash_free (nametab);
  1597. namelist = merge_sort (namelist, num_names, compare_names_found);
  1598. if (listed_incremental_option)
  1599. {
  1600. for (name = namelist; name && name->name[0] == 0; name = name->next)
  1601. ;
  1602. if (name)
  1603. append_incremental_renames (name->directory);
  1604. }
  1605. }
  1606. /* This is like name_match, except that
  1607. 1. It returns a pointer to the name it matched, and doesn't set FOUND
  1608. in structure. The caller will have to do that if it wants to.
  1609. 2. If the namelist is empty, it returns null, unlike name_match, which
  1610. returns TRUE. */
  1611. struct name *
  1612. name_scan (const char *file_name)
  1613. {
  1614. size_t length = strlen (file_name);
  1615. while (1)
  1616. {
  1617. struct name *cursor = namelist_match (file_name, length);
  1618. if (cursor)
  1619. return cursor;
  1620. /* Filename from archive not found in namelist. If we have the whole
  1621. namelist here, just return 0. Otherwise, read the next name in and
  1622. compare it. If this was the last name, namelist->found_count will
  1623. remain on. If not, we loop to compare the newly read name. */
  1624. if (same_order_option && namelist && namelist->found_count)
  1625. {
  1626. name_gather (); /* read one more */
  1627. if (namelist->found_count)
  1628. return 0;
  1629. }
  1630. else
  1631. return 0;
  1632. }
  1633. }
  1634. /* This returns a name from the namelist which doesn't have ->found
  1635. set. It sets ->found before returning, so successive calls will
  1636. find and return all the non-found names in the namelist. */
  1637. struct name *gnu_list_name;
  1638. struct name const *
  1639. name_from_list (void)
  1640. {
  1641. if (!gnu_list_name)
  1642. gnu_list_name = namelist;
  1643. while (gnu_list_name
  1644. && (gnu_list_name->found_count || gnu_list_name->name[0] == 0))
  1645. gnu_list_name = gnu_list_name->next;
  1646. if (gnu_list_name)
  1647. {
  1648. gnu_list_name->found_count++;
  1649. chdir_do (gnu_list_name->change_dir);
  1650. return gnu_list_name;
  1651. }
  1652. return NULL;
  1653. }
  1654. void
  1655. blank_name_list (void)
  1656. {
  1657. struct name *name;
  1658. gnu_list_name = 0;
  1659. for (name = namelist; name; name = name->next)
  1660. name->found_count = 0;
  1661. }
  1662. /* Yield a newly allocated file name consisting of DIR_NAME concatenated to
  1663. NAME, with an intervening slash if DIR_NAME does not already end in one. */
  1664. char *
  1665. make_file_name (const char *directory_name, const char *name)
  1666. {
  1667. size_t dirlen = strlen (directory_name);
  1668. size_t namelen = strlen (name) + 1;
  1669. int slash = dirlen && ! ISSLASH (directory_name[dirlen - 1]);
  1670. char *buffer = xmalloc (dirlen + slash + namelen);
  1671. memcpy (buffer, directory_name, dirlen);
  1672. buffer[dirlen] = '/';
  1673. memcpy (buffer + dirlen + slash, name, namelen);
  1674. return buffer;
  1675. }
  1676. /* Return the size of the prefix of FILE_NAME that is removed after
  1677. stripping NUM leading file name components. NUM must be
  1678. positive. */
  1679. size_t
  1680. stripped_prefix_len (char const *file_name, size_t num)
  1681. {
  1682. char const *p = file_name + FILE_SYSTEM_PREFIX_LEN (file_name);
  1683. while (ISSLASH (*p))
  1684. p++;
  1685. while (*p)
  1686. {
  1687. bool slash = ISSLASH (*p);
  1688. p++;
  1689. if (slash)
  1690. {
  1691. if (--num == 0)
  1692. return p - file_name;
  1693. while (ISSLASH (*p))
  1694. p++;
  1695. }
  1696. }
  1697. return -1;
  1698. }
  1699. /* Return nonzero if NAME contains ".." as a file name component. */
  1700. bool
  1701. contains_dot_dot (char const *name)
  1702. {
  1703. char const *p = name + FILE_SYSTEM_PREFIX_LEN (name);
  1704. for (;; p++)
  1705. {
  1706. if (p[0] == '.' && p[1] == '.' && (ISSLASH (p[2]) || !p[2]))
  1707. return 1;
  1708. while (! ISSLASH (*p))
  1709. {
  1710. if (! *p++)
  1711. return 0;
  1712. }
  1713. }
  1714. }