/contrib/cvs/src/find_names.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 427 lines · 270 code · 50 blank · 107 comment · 102 complexity · ccfba2b7489f69d06ae1d83586fb62bf MD5 · raw file

  1. /*
  2. * Copyright (C) 1986-2005 The Free Software Foundation, Inc.
  3. *
  4. * Portions Copyright (C) 1998-2005 Derek Price, Ximbiot <http://ximbiot.com>,
  5. * and others.
  6. *
  7. * Portions Copyright (C) 1992, Brian Berliner and Jeff Polk
  8. * Portions Copyright (C) 1989-1992, Brian Berliner
  9. *
  10. * You may distribute under the terms of the GNU General Public License as
  11. * specified in the README file that comes with the CVS source distribution.
  12. *
  13. * Find Names
  14. *
  15. * Finds all the pertinent file names, both from the administration and from the
  16. * repository
  17. *
  18. * Find Dirs
  19. *
  20. * Finds all pertinent sub-directories of the checked out instantiation and the
  21. * repository (and optionally the attic)
  22. */
  23. #include "cvs.h"
  24. static int find_dirs PROTO((char *dir, List * list, int checkadm,
  25. List *entries));
  26. static int find_rcs PROTO((char *dir, List * list));
  27. static int add_subdir_proc PROTO((Node *, void *));
  28. static int register_subdir_proc PROTO((Node *, void *));
  29. /*
  30. * add the key from entry on entries list to the files list
  31. */
  32. static int add_entries_proc PROTO((Node *, void *));
  33. static int
  34. add_entries_proc (node, closure)
  35. Node *node;
  36. void *closure;
  37. {
  38. Node *fnode;
  39. List *filelist = closure;
  40. Entnode *entnode = node->data;
  41. if (entnode->type != ENT_FILE)
  42. return (0);
  43. fnode = getnode ();
  44. fnode->type = FILES;
  45. fnode->key = xstrdup (node->key);
  46. if (addnode (filelist, fnode) != 0)
  47. freenode (fnode);
  48. return (0);
  49. }
  50. /* Find files in the repository and/or working directory. On error,
  51. may either print a nonfatal error and return NULL, or just give
  52. a fatal error. On success, return non-NULL (even if it is an empty
  53. list). */
  54. List *
  55. Find_Names (repository, which, aflag, optentries)
  56. char *repository;
  57. int which;
  58. int aflag;
  59. List **optentries;
  60. {
  61. List *entries;
  62. List *files;
  63. /* make a list for the files */
  64. files = getlist ();
  65. /* look at entries (if necessary) */
  66. if (which & W_LOCAL)
  67. {
  68. /* parse the entries file (if it exists) */
  69. entries = Entries_Open (aflag, NULL);
  70. if (entries != NULL)
  71. {
  72. /* walk the entries file adding elements to the files list */
  73. (void) walklist (entries, add_entries_proc, files);
  74. /* if our caller wanted the entries list, return it; else free it */
  75. if (optentries != NULL)
  76. *optentries = entries;
  77. else
  78. Entries_Close (entries);
  79. }
  80. }
  81. if ((which & W_REPOS) && repository && !isreadable (CVSADM_ENTSTAT))
  82. {
  83. /* search the repository */
  84. if (find_rcs (repository, files) != 0)
  85. {
  86. error (0, errno, "cannot open directory %s", repository);
  87. goto error_exit;
  88. }
  89. /* search the attic too */
  90. if (which & W_ATTIC)
  91. {
  92. char *dir;
  93. dir = xmalloc (strlen (repository) + sizeof (CVSATTIC) + 10);
  94. (void) sprintf (dir, "%s/%s", repository, CVSATTIC);
  95. if (find_rcs (dir, files) != 0
  96. && !existence_error (errno))
  97. /* For now keep this a fatal error, seems less useful
  98. for access control than the case above. */
  99. error (1, errno, "cannot open directory %s", dir);
  100. free (dir);
  101. }
  102. }
  103. /* sort the list into alphabetical order and return it */
  104. sortlist (files, fsortcmp);
  105. return (files);
  106. error_exit:
  107. dellist (&files);
  108. return NULL;
  109. }
  110. /*
  111. * Add an entry from the subdirs list to the directories list. This
  112. * is called via walklist.
  113. */
  114. static int
  115. add_subdir_proc (p, closure)
  116. Node *p;
  117. void *closure;
  118. {
  119. List *dirlist = closure;
  120. Entnode *entnode = p->data;
  121. Node *dnode;
  122. if (entnode->type != ENT_SUBDIR)
  123. return 0;
  124. dnode = getnode ();
  125. dnode->type = DIRS;
  126. dnode->key = xstrdup (entnode->user);
  127. if (addnode (dirlist, dnode) != 0)
  128. freenode (dnode);
  129. return 0;
  130. }
  131. /*
  132. * Register a subdirectory. This is called via walklist.
  133. */
  134. /*ARGSUSED*/
  135. static int
  136. register_subdir_proc (p, closure)
  137. Node *p;
  138. void *closure;
  139. {
  140. List *entries = (List *) closure;
  141. Subdir_Register (entries, (char *) NULL, p->key);
  142. return 0;
  143. }
  144. /*
  145. * create a list of directories to traverse from the current directory
  146. */
  147. List *
  148. Find_Directories (repository, which, entries)
  149. char *repository;
  150. int which;
  151. List *entries;
  152. {
  153. List *dirlist;
  154. /* make a list for the directories */
  155. dirlist = getlist ();
  156. /* find the local ones */
  157. if (which & W_LOCAL)
  158. {
  159. List *tmpentries;
  160. struct stickydirtag *sdtp;
  161. /* Look through the Entries file. */
  162. if (entries != NULL)
  163. tmpentries = entries;
  164. else if (isfile (CVSADM_ENT))
  165. tmpentries = Entries_Open (0, NULL);
  166. else
  167. tmpentries = NULL;
  168. if (tmpentries != NULL)
  169. sdtp = tmpentries->list->data;
  170. /* If we do have an entries list, then if sdtp is NULL, or if
  171. sdtp->subdirs is nonzero, all subdirectory information is
  172. recorded in the entries list. */
  173. if (tmpentries != NULL && (sdtp == NULL || sdtp->subdirs))
  174. walklist (tmpentries, add_subdir_proc, (void *) dirlist);
  175. else
  176. {
  177. /* This is an old working directory, in which subdirectory
  178. information is not recorded in the Entries file. Find
  179. the subdirectories the hard way, and, if possible, add
  180. it to the Entries file for next time. */
  181. /* FIXME-maybe: find_dirs is bogus for this usage because
  182. it skips CVSATTIC and CVSLCK directories--those names
  183. should be special only in the repository. However, in
  184. the interests of not perturbing this code, we probably
  185. should leave well enough alone unless we want to write
  186. a sanity.sh test case (which would operate by manually
  187. hacking on the CVS/Entries file). */
  188. if (find_dirs (".", dirlist, 1, tmpentries) != 0)
  189. error (1, errno, "cannot open current directory");
  190. if (tmpentries != NULL)
  191. {
  192. if (! list_isempty (dirlist))
  193. walklist (dirlist, register_subdir_proc,
  194. (void *) tmpentries);
  195. else
  196. Subdirs_Known (tmpentries);
  197. }
  198. }
  199. if (entries == NULL && tmpentries != NULL)
  200. Entries_Close (tmpentries);
  201. }
  202. /* look for sub-dirs in the repository */
  203. if ((which & W_REPOS) && repository)
  204. {
  205. /* search the repository */
  206. if (find_dirs (repository, dirlist, 0, entries) != 0)
  207. error (1, errno, "cannot open directory %s", repository);
  208. /* We don't need to look in the attic because directories
  209. never go in the attic. In the future, there hopefully will
  210. be a better mechanism for detecting whether a directory in
  211. the repository is alive or dead; it may or may not involve
  212. moving directories to the attic. */
  213. }
  214. /* sort the list into alphabetical order and return it */
  215. sortlist (dirlist, fsortcmp);
  216. return (dirlist);
  217. }
  218. /*
  219. * Finds all the ,v files in the argument directory, and adds them to the
  220. * files list. Returns 0 for success and non-zero if the argument directory
  221. * cannot be opened, in which case errno is set to indicate the error.
  222. * In the error case LIST is left in some reasonable state (unchanged, or
  223. * containing the files which were found before the error occurred).
  224. */
  225. static int
  226. find_rcs (dir, list)
  227. char *dir;
  228. List *list;
  229. {
  230. Node *p;
  231. struct dirent *dp;
  232. DIR *dirp;
  233. /* set up to read the dir */
  234. if ((dirp = CVS_OPENDIR (dir)) == NULL)
  235. return (1);
  236. /* read the dir, grabbing the ,v files */
  237. errno = 0;
  238. while ((dp = CVS_READDIR (dirp)) != NULL)
  239. {
  240. if (CVS_FNMATCH (RCSPAT, dp->d_name, 0) == 0)
  241. {
  242. char *comma;
  243. comma = strrchr (dp->d_name, ','); /* strip the ,v */
  244. *comma = '\0';
  245. p = getnode ();
  246. p->type = FILES;
  247. p->key = xstrdup (dp->d_name);
  248. if (addnode (list, p) != 0)
  249. freenode (p);
  250. }
  251. errno = 0;
  252. }
  253. if (errno != 0)
  254. {
  255. int save_errno = errno;
  256. (void) CVS_CLOSEDIR (dirp);
  257. errno = save_errno;
  258. return 1;
  259. }
  260. (void) CVS_CLOSEDIR (dirp);
  261. return (0);
  262. }
  263. /*
  264. * Finds all the subdirectories of the argument dir and adds them to
  265. * the specified list. Sub-directories without a CVS administration
  266. * directory are optionally ignored. If ENTRIES is not NULL, all
  267. * files on the list are ignored. Returns 0 for success or 1 on
  268. * error, in which case errno is set to indicate the error.
  269. */
  270. static int
  271. find_dirs (dir, list, checkadm, entries)
  272. char *dir;
  273. List *list;
  274. int checkadm;
  275. List *entries;
  276. {
  277. Node *p;
  278. char *tmp = NULL;
  279. size_t tmp_size = 0;
  280. struct dirent *dp;
  281. DIR *dirp;
  282. int skip_emptydir = 0;
  283. /* First figure out whether we need to skip directories named
  284. Emptydir. Except in the CVSNULLREPOS case, Emptydir is just
  285. a normal directory name. */
  286. if (isabsolute (dir)
  287. && strncmp (dir, current_parsed_root->directory, strlen (current_parsed_root->directory)) == 0
  288. && ISDIRSEP (dir[strlen (current_parsed_root->directory)])
  289. && strcmp (dir + strlen (current_parsed_root->directory) + 1, CVSROOTADM) == 0)
  290. skip_emptydir = 1;
  291. /* set up to read the dir */
  292. if ((dirp = CVS_OPENDIR (dir)) == NULL)
  293. return (1);
  294. /* read the dir, grabbing sub-dirs */
  295. errno = 0;
  296. while ((dp = CVS_READDIR (dirp)) != NULL)
  297. {
  298. if (strcmp (dp->d_name, ".") == 0 ||
  299. strcmp (dp->d_name, "..") == 0 ||
  300. strcmp (dp->d_name, CVSATTIC) == 0 ||
  301. strcmp (dp->d_name, CVSLCK) == 0 ||
  302. strcmp (dp->d_name, CVSREP) == 0)
  303. goto do_it_again;
  304. /* findnode() is going to be significantly faster than stat()
  305. because it involves no system calls. That is why we bother
  306. with the entries argument, and why we check this first. */
  307. if (entries != NULL && findnode (entries, dp->d_name) != NULL)
  308. goto do_it_again;
  309. if (skip_emptydir
  310. && strcmp (dp->d_name, CVSNULLREPOS) == 0)
  311. goto do_it_again;
  312. #ifdef DT_DIR
  313. if (dp->d_type != DT_DIR)
  314. {
  315. if (dp->d_type != DT_UNKNOWN && dp->d_type != DT_LNK)
  316. goto do_it_again;
  317. #endif
  318. /* don't bother stating ,v files */
  319. if (CVS_FNMATCH (RCSPAT, dp->d_name, 0) == 0)
  320. goto do_it_again;
  321. expand_string (&tmp,
  322. &tmp_size,
  323. strlen (dir) + strlen (dp->d_name) + 10);
  324. sprintf (tmp, "%s/%s", dir, dp->d_name);
  325. if (!isdir (tmp))
  326. goto do_it_again;
  327. #ifdef DT_DIR
  328. }
  329. #endif
  330. /* check for administration directories (if needed) */
  331. if (checkadm)
  332. {
  333. /* blow off symbolic links to dirs in local dir */
  334. #ifdef DT_DIR
  335. if (dp->d_type != DT_DIR)
  336. {
  337. /* we're either unknown or a symlink at this point */
  338. if (dp->d_type == DT_LNK)
  339. goto do_it_again;
  340. #endif
  341. /* Note that we only get here if we already set tmp
  342. above. */
  343. if (islink (tmp))
  344. goto do_it_again;
  345. #ifdef DT_DIR
  346. }
  347. #endif
  348. /* check for new style */
  349. expand_string (&tmp,
  350. &tmp_size,
  351. (strlen (dir) + strlen (dp->d_name)
  352. + sizeof (CVSADM) + 10));
  353. (void) sprintf (tmp, "%s/%s/%s", dir, dp->d_name, CVSADM);
  354. if (!isdir (tmp))
  355. goto do_it_again;
  356. }
  357. /* put it in the list */
  358. p = getnode ();
  359. p->type = DIRS;
  360. p->key = xstrdup (dp->d_name);
  361. if (addnode (list, p) != 0)
  362. freenode (p);
  363. do_it_again:
  364. errno = 0;
  365. }
  366. if (errno != 0)
  367. {
  368. int save_errno = errno;
  369. (void) CVS_CLOSEDIR (dirp);
  370. errno = save_errno;
  371. return 1;
  372. }
  373. (void) CVS_CLOSEDIR (dirp);
  374. if (tmp != NULL)
  375. free (tmp);
  376. return (0);
  377. }