names.c 44 KB

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