/contrib/cvs/src/recurse.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 1299 lines · 729 code · 148 blank · 422 comment · 207 complexity · 8df86747dec896956368a0387c3ec009 MD5 · raw file

  1. /*
  2. * Copyright (C) 1986-2008 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. * General recursion handler
  14. *
  15. */
  16. #include "cvs.h"
  17. #include "savecwd.h"
  18. #include "fileattr.h"
  19. #include "edit.h"
  20. #include <assert.h>
  21. static int do_dir_proc PROTO((Node * p, void *closure));
  22. static int do_file_proc PROTO((Node * p, void *closure));
  23. static void addlist PROTO((List ** listp, char *key));
  24. static int unroll_files_proc PROTO((Node *p, void *closure));
  25. static void addfile PROTO((List **listp, char *dir, char *file));
  26. static char *update_dir;
  27. static char *repository = NULL;
  28. static List *filelist = NULL; /* holds list of files on which to operate */
  29. static List *dirlist = NULL; /* holds list of directories on which to operate */
  30. struct recursion_frame {
  31. FILEPROC fileproc;
  32. FILESDONEPROC filesdoneproc;
  33. DIRENTPROC direntproc;
  34. DIRLEAVEPROC dirleaveproc;
  35. void *callerdat;
  36. Dtype flags;
  37. int which;
  38. int aflag;
  39. int locktype;
  40. int dosrcs;
  41. char *repository; /* Keep track of repository for rtag */
  42. };
  43. static int do_recursion PROTO ((struct recursion_frame *frame));
  44. /* I am half tempted to shove a struct file_info * into the struct
  45. recursion_frame (but then we would need to modify or create a
  46. recursion_frame for each file), or shove a struct recursion_frame *
  47. into the struct file_info (more tempting, although it isn't completely
  48. clear that the struct file_info should contain info about recursion
  49. processor internals). So instead use this struct. */
  50. struct frame_and_file {
  51. struct recursion_frame *frame;
  52. struct file_info *finfo;
  53. };
  54. /* Similarly, we need to pass the entries list to do_dir_proc. */
  55. struct frame_and_entries {
  56. struct recursion_frame *frame;
  57. List *entries;
  58. };
  59. /* Start a recursive command.
  60. Command line arguments (ARGC, ARGV) dictate the directories and
  61. files on which we operate. In the special case of no arguments, we
  62. default to ".". */
  63. int
  64. start_recursion (fileproc, filesdoneproc, direntproc, dirleaveproc, callerdat,
  65. argc, argv, local, which, aflag, locktype,
  66. update_preload, dosrcs, repository_in)
  67. FILEPROC fileproc;
  68. FILESDONEPROC filesdoneproc;
  69. DIRENTPROC direntproc;
  70. DIRLEAVEPROC dirleaveproc;
  71. void *callerdat;
  72. int argc;
  73. char **argv;
  74. int local;
  75. /* This specifies the kind of recursion. There are several cases:
  76. 1. W_LOCAL is not set but W_REPOS or W_ATTIC is. The current
  77. directory when we are called must be the repository and
  78. recursion proceeds according to what exists in the repository.
  79. 2a. W_LOCAL is set but W_REPOS and W_ATTIC are not. The
  80. current directory when we are called must be the working
  81. directory. Recursion proceeds according to what exists in the
  82. working directory, never (I think) consulting any part of the
  83. repository which does not correspond to the working directory
  84. ("correspond" == Name_Repository).
  85. 2b. W_LOCAL is set and so is W_REPOS or W_ATTIC. This is the
  86. weird one. The current directory when we are called must be
  87. the working directory. We recurse through working directories,
  88. but we recurse into a directory if it is exists in the working
  89. directory *or* it exists in the repository. If a directory
  90. does not exist in the working directory, the direntproc must
  91. either tell us to skip it (R_SKIP_ALL), or must create it (I
  92. think those are the only two cases). */
  93. int which;
  94. int aflag;
  95. int locktype;
  96. char *update_preload;
  97. int dosrcs;
  98. /* Keep track of the repository string. This is only for the remote mode,
  99. * specifically, r* commands (rtag, rdiff, co, ...) where xgetwd() was
  100. * used to locate the repository. Things would break when xgetwd() was
  101. * used with a symlinked repository because xgetwd() would return the true
  102. * path and in some cases this would cause the path to be printed as other
  103. * than the user specified in error messages and in other cases some of
  104. * CVS's security assertions would fail.
  105. */
  106. char *repository_in;
  107. {
  108. int i, err = 0;
  109. #ifdef CLIENT_SUPPORT
  110. List *args_to_send_when_finished = NULL;
  111. #endif
  112. List *files_by_dir = NULL;
  113. struct recursion_frame frame;
  114. frame.fileproc = fileproc;
  115. frame.filesdoneproc = filesdoneproc;
  116. frame.direntproc = direntproc;
  117. frame.dirleaveproc = dirleaveproc;
  118. frame.callerdat = callerdat;
  119. frame.flags = local ? R_SKIP_DIRS : R_PROCESS;
  120. frame.which = which;
  121. frame.aflag = aflag;
  122. frame.locktype = locktype;
  123. frame.dosrcs = dosrcs;
  124. /* If our repository_in has a trailing "/.", remove it before storing it
  125. * for do_recursion().
  126. *
  127. * FIXME: This is somewhat of a hack in the sense that many of our callers
  128. * painstakingly compute and add the trailing '.' we now remove.
  129. */
  130. while (repository_in && strlen (repository_in) >= 2
  131. && repository_in[strlen (repository_in) - 2] == '/'
  132. && repository_in[strlen (repository_in) - 1] == '.')
  133. {
  134. /* Beware the case where the string is exactly "/." or "//.".
  135. * Paths with a leading "//" are special on some early UNIXes.
  136. */
  137. if (strlen (repository_in) == 2 || strlen (repository_in) == 3)
  138. repository_in[strlen (repository_in) - 1] = '\0';
  139. else
  140. repository_in[strlen (repository_in) - 2] = '\0';
  141. }
  142. frame.repository = repository_in;
  143. expand_wild (argc, argv, &argc, &argv);
  144. if (update_preload == NULL)
  145. update_dir = xstrdup ("");
  146. else
  147. update_dir = xstrdup (update_preload);
  148. /* clean up from any previous calls to start_recursion */
  149. if (repository)
  150. {
  151. free (repository);
  152. repository = (char *) NULL;
  153. }
  154. if (filelist)
  155. dellist (&filelist); /* FIXME-krp: no longer correct. */
  156. if (dirlist)
  157. dellist (&dirlist);
  158. #ifdef SERVER_SUPPORT
  159. if (server_active)
  160. {
  161. for (i = 0; i < argc; ++i)
  162. server_pathname_check (argv[i]);
  163. }
  164. #endif
  165. if (argc == 0)
  166. {
  167. int just_subdirs = (which & W_LOCAL) && !isdir (CVSADM);
  168. #ifdef CLIENT_SUPPORT
  169. if (!just_subdirs
  170. && CVSroot_cmdline == NULL
  171. && current_parsed_root->isremote)
  172. {
  173. cvsroot_t *root = Name_Root (NULL, update_dir);
  174. if (root)
  175. {
  176. if (strcmp (root->original, current_parsed_root->original))
  177. /* We're skipping this directory because it is for
  178. * a different root. Therefore, we just want to
  179. * do the subdirectories only. Processing files would
  180. * cause a working directory from one repository to be
  181. * processed against a different repository, which could
  182. * cause all kinds of spurious conflicts and such.
  183. *
  184. * Question: what about the case of "cvs update foo"
  185. * where we process foo/bar and not foo itself? That
  186. * seems to be handled somewhere (else) but why should
  187. * it be a separate case? Needs investigation... */
  188. just_subdirs = 1;
  189. free_cvsroot_t (root);
  190. }
  191. }
  192. #endif
  193. /*
  194. * There were no arguments, so we'll probably just recurse. The
  195. * exception to the rule is when we are called from a directory
  196. * without any CVS administration files. That has always meant to
  197. * process each of the sub-directories, so we pretend like we were
  198. * called with the list of sub-dirs of the current dir as args
  199. */
  200. if (just_subdirs)
  201. {
  202. dirlist = Find_Directories ((char *) NULL, W_LOCAL, (List *) NULL);
  203. /* If there are no sub-directories, there is a certain logic in
  204. favor of doing nothing, but in fact probably the user is just
  205. confused about what directory they are in, or whether they
  206. cvs add'd a new directory. In the case of at least one
  207. sub-directory, at least when we recurse into them we
  208. notice (hopefully) whether they are under CVS control. */
  209. if (list_isempty (dirlist))
  210. {
  211. if (update_dir[0] == '\0')
  212. error (0, 0, "in directory .:");
  213. else
  214. error (0, 0, "in directory %s:", update_dir);
  215. error (1, 0,
  216. "there is no version here; run '%s checkout' first",
  217. program_name);
  218. }
  219. #ifdef CLIENT_SUPPORT
  220. else if (current_parsed_root->isremote && server_started)
  221. {
  222. /* In the the case "cvs update foo bar baz", a call to
  223. send_file_names in update.c will have sent the
  224. appropriate "Argument" commands to the server. In
  225. this case, that won't have happened, so we need to
  226. do it here. While this example uses "update", this
  227. generalizes to other commands. */
  228. /* This is the same call to Find_Directories as above.
  229. FIXME: perhaps it would be better to write a
  230. function that duplicates a list. */
  231. args_to_send_when_finished = Find_Directories ((char *) NULL,
  232. W_LOCAL,
  233. (List *) NULL);
  234. }
  235. #endif
  236. }
  237. else
  238. addlist (&dirlist, ".");
  239. goto do_the_work;
  240. }
  241. /*
  242. * There were arguments, so we have to handle them by hand. To do
  243. * that, we set up the filelist and dirlist with the arguments and
  244. * call do_recursion. do_recursion recognizes the fact that the
  245. * lists are non-null when it starts and doesn't update them.
  246. *
  247. * explicitly named directories are stored in dirlist.
  248. * explicitly named files are stored in filelist.
  249. * other possibility is named entities whicha are not currently in
  250. * the working directory.
  251. */
  252. for (i = 0; i < argc; i++)
  253. {
  254. /* if this argument is a directory, then add it to the list of
  255. directories. */
  256. if (!wrap_name_has (argv[i], WRAP_TOCVS) && isdir (argv[i]))
  257. {
  258. strip_trailing_slashes (argv[i]);
  259. addlist (&dirlist, argv[i]);
  260. }
  261. else
  262. {
  263. /* otherwise, split argument into directory and component names. */
  264. char *dir;
  265. char *comp;
  266. char *file_to_try;
  267. /* Now break out argv[i] into directory part (DIR) and file part (COMP).
  268. DIR and COMP will each point to a newly malloc'd string. */
  269. dir = xstrdup (argv[i]);
  270. /* Its okay to discard the const below - we know we just allocated
  271. * dir ourselves.
  272. */
  273. comp = (char *)last_component (dir);
  274. if (comp == dir)
  275. {
  276. /* no dir component. What we have is an implied "./" */
  277. dir = xstrdup(".");
  278. }
  279. else
  280. {
  281. char *p = comp;
  282. p[-1] = '\0';
  283. comp = xstrdup (p);
  284. }
  285. /* if this argument exists as a file in the current
  286. working directory tree, then add it to the files list. */
  287. if (!(which & W_LOCAL))
  288. {
  289. /* If doing rtag, we've done a chdir to the repository. */
  290. file_to_try = xmalloc (strlen (argv[i]) + sizeof (RCSEXT) + 5);
  291. sprintf (file_to_try, "%s%s", argv[i], RCSEXT);
  292. }
  293. else
  294. file_to_try = xstrdup (argv[i]);
  295. if (isfile (file_to_try))
  296. addfile (&files_by_dir, dir, comp);
  297. else if (isdir (dir))
  298. {
  299. if ((which & W_LOCAL) && isdir (CVSADM) &&
  300. !current_parsed_root->isremote)
  301. {
  302. /* otherwise, look for it in the repository. */
  303. char *tmp_update_dir;
  304. char *repos;
  305. char *reposfile;
  306. tmp_update_dir = xmalloc (strlen (update_dir)
  307. + strlen (dir)
  308. + 5);
  309. strcpy (tmp_update_dir, update_dir);
  310. if (*tmp_update_dir != '\0')
  311. (void) strcat (tmp_update_dir, "/");
  312. (void) strcat (tmp_update_dir, dir);
  313. /* look for it in the repository. */
  314. repos = Name_Repository (dir, tmp_update_dir);
  315. reposfile = xmalloc (strlen (repos)
  316. + strlen (comp)
  317. + 5);
  318. (void) sprintf (reposfile, "%s/%s", repos, comp);
  319. free (repos);
  320. if (!wrap_name_has (comp, WRAP_TOCVS) && isdir (reposfile))
  321. addlist (&dirlist, argv[i]);
  322. else
  323. addfile (&files_by_dir, dir, comp);
  324. free (tmp_update_dir);
  325. free (reposfile);
  326. }
  327. else
  328. addfile (&files_by_dir, dir, comp);
  329. }
  330. else
  331. error (1, 0, "no such directory `%s'", dir);
  332. free (file_to_try);
  333. free (dir);
  334. free (comp);
  335. }
  336. }
  337. /* At this point we have looped over all named arguments and built
  338. a coupla lists. Now we unroll the lists, setting up and
  339. calling do_recursion. */
  340. err += walklist (files_by_dir, unroll_files_proc, (void *) &frame);
  341. dellist(&files_by_dir);
  342. /* then do_recursion on the dirlist. */
  343. if (dirlist != NULL)
  344. {
  345. do_the_work:
  346. err += do_recursion (&frame);
  347. }
  348. /* Free the data which expand_wild allocated. */
  349. free_names (&argc, argv);
  350. free (update_dir);
  351. update_dir = NULL;
  352. #ifdef CLIENT_SUPPORT
  353. if (args_to_send_when_finished != NULL)
  354. {
  355. /* FIXME (njc): in the multiroot case, we don't want to send
  356. argument commands for those top-level directories which do
  357. not contain any subdirectories which have files checked out
  358. from current_parsed_root->original. If we do, and two repositories
  359. have a module with the same name, nasty things could happen.
  360. This is hard. Perhaps we should send the Argument commands
  361. later in this procedure, after we've had a chance to notice
  362. which directores we're using (after do_recursion has been
  363. called once). This means a _lot_ of rewriting, however.
  364. What we need to do for that to happen is descend the tree
  365. and construct a list of directories which are checked out
  366. from current_cvsroot. Now, we eliminate from the list all
  367. of those directories which are immediate subdirectories of
  368. another directory in the list. To say that the opposite
  369. way, we keep the directories which are not immediate
  370. subdirectories of any other in the list. Here's a picture:
  371. a
  372. / \
  373. B C
  374. / \
  375. D e
  376. / \
  377. F G
  378. / \
  379. H I
  380. The node in capitals are those directories which are
  381. checked out from current_cvsroot. We want the list to
  382. contain B, C, F, and G. D, H, and I are not included,
  383. because their parents are also checked out from
  384. current_cvsroot.
  385. The algorithm should be:
  386. 1) construct a tree of all directory names where each
  387. element contains a directory name and a flag which notes if
  388. that directory is checked out from current_cvsroot
  389. a0
  390. / \
  391. B1 C1
  392. / \
  393. D1 e0
  394. / \
  395. F1 G1
  396. / \
  397. H1 I1
  398. 2) Recursively descend the tree. For each node, recurse
  399. before processing the node. If the flag is zero, do
  400. nothing. If the flag is 1, check the node's parent. If
  401. the parent's flag is one, change the current entry's flag
  402. to zero.
  403. a0
  404. / \
  405. B1 C1
  406. / \
  407. D0 e0
  408. / \
  409. F1 G1
  410. / \
  411. H0 I0
  412. 3) Walk the tree and spit out "Argument" commands to tell
  413. the server which directories to munge.
  414. Yuck. It's not clear this is worth spending time on, since
  415. we might want to disable cvs commands entirely from
  416. directories that do not have CVSADM files...
  417. Anyways, the solution as it stands has modified server.c
  418. (dirswitch) to create admin files [via server.c
  419. (create_adm_p)] in all path elements for a client's
  420. "Directory xxx" command, which forces the server to descend
  421. and serve the files there. client.c (send_file_names) has
  422. also been modified to send only those arguments which are
  423. appropriate to current_parsed_root->original.
  424. */
  425. /* Construct a fake argc/argv pair. */
  426. int our_argc = 0, i;
  427. char **our_argv = NULL;
  428. if (! list_isempty (args_to_send_when_finished))
  429. {
  430. Node *head, *p;
  431. head = args_to_send_when_finished->list;
  432. /* count the number of nodes */
  433. i = 0;
  434. for (p = head->next; p != head; p = p->next)
  435. i++;
  436. our_argc = i;
  437. /* create the argument vector */
  438. our_argv = (char **) xmalloc (sizeof (char *) * our_argc);
  439. /* populate it */
  440. i = 0;
  441. for (p = head->next; p != head; p = p->next)
  442. our_argv[i++] = xstrdup (p->key);
  443. }
  444. /* We don't want to expand widcards, since we've just created
  445. a list of directories directly from the filesystem. */
  446. send_file_names (our_argc, our_argv, 0);
  447. /* Free our argc/argv. */
  448. if (our_argv != NULL)
  449. {
  450. for (i = 0; i < our_argc; i++)
  451. free (our_argv[i]);
  452. free (our_argv);
  453. }
  454. dellist (&args_to_send_when_finished);
  455. }
  456. #endif
  457. return (err);
  458. }
  459. /*
  460. * Implement the recursive policies on the local directory. This may be
  461. * called directly, or may be called by start_recursion
  462. */
  463. static int
  464. do_recursion (frame)
  465. struct recursion_frame *frame;
  466. {
  467. int err = 0;
  468. int dodoneproc = 1;
  469. char *srepository = NULL;
  470. List *entries = NULL;
  471. int locktype;
  472. int process_this_directory = 1;
  473. /* do nothing if told */
  474. if (frame->flags == R_SKIP_ALL)
  475. return (0);
  476. locktype = noexec ? CVS_LOCK_NONE : frame->locktype;
  477. /* The fact that locks are not active here is what makes us fail to have
  478. the
  479. If someone commits some changes in one cvs command,
  480. then an update by someone else will either get all the
  481. changes, or none of them.
  482. property (see node Concurrency in cvs.texinfo).
  483. The most straightforward fix would just to readlock the whole
  484. tree before starting an update, but that means that if a commit
  485. gets blocked on a big update, it might need to wait a *long*
  486. time.
  487. A more adequate fix would be a two-pass design for update,
  488. checkout, etc. The first pass would go through the repository,
  489. with the whole tree readlocked, noting what versions of each
  490. file we want to get. The second pass would release all locks
  491. (except perhaps short-term locks on one file at a
  492. time--although I think RCS already deals with this) and
  493. actually get the files, specifying the particular versions it wants.
  494. This could be sped up by separating out the data needed for the
  495. first pass into a separate file(s)--for example a file
  496. attribute for each file whose value contains the head revision
  497. for each branch. The structure should be designed so that
  498. commit can relatively quickly update the information for a
  499. single file or a handful of files (file attributes, as
  500. implemented in Jan 96, are probably acceptable; improvements
  501. would be possible such as branch attributes which are in
  502. separate files for each branch). */
  503. #if defined(SERVER_SUPPORT) && defined(SERVER_FLOWCONTROL)
  504. /*
  505. * Now would be a good time to check to see if we need to stop
  506. * generating data, to give the buffers a chance to drain to the
  507. * remote client. We should not have locks active at this point,
  508. * but if there are writelocks around, we cannot pause here. */
  509. if (server_active && locktype != CVS_LOCK_WRITE)
  510. server_pause_check();
  511. #endif
  512. /* Check the value in CVSADM_ROOT and see if it's in the list. If
  513. not, add it to our lists of CVS/Root directories and do not
  514. process the files in this directory. Otherwise, continue as
  515. usual. THIS_ROOT might be NULL if we're doing an initial
  516. checkout -- check before using it. The default should be that
  517. we process a directory's contents and only skip those contents
  518. if a CVS/Root file exists.
  519. If we're running the server, we want to process all
  520. directories, since we're guaranteed to have only one CVSROOT --
  521. our own. */
  522. /* If -d was specified, it should override CVS/Root.
  523. In the single-repository case, it is long-standing CVS behavior
  524. and makes sense - the user might want another access method,
  525. another server (which mounts the same repository), &c.
  526. In the multiple-repository case, -d overrides all CVS/Root
  527. files. That is the only plausible generalization I can
  528. think of. */
  529. if (CVSroot_cmdline == NULL && !server_active)
  530. {
  531. cvsroot_t *this_root = Name_Root ((char *) NULL, update_dir);
  532. if (this_root != NULL)
  533. {
  534. if (findnode (root_directories, this_root->original))
  535. {
  536. process_this_directory = !strcmp (current_parsed_root->original,
  537. this_root->original);
  538. free_cvsroot_t (this_root);
  539. }
  540. else
  541. {
  542. /* Add it to our list. */
  543. Node *n = getnode ();
  544. n->type = NT_UNKNOWN;
  545. n->key = xstrdup (this_root->original);
  546. n->data = this_root;
  547. if (addnode (root_directories, n))
  548. error (1, 0, "cannot add new CVSROOT %s",
  549. this_root->original);
  550. process_this_directory = 0;
  551. }
  552. }
  553. }
  554. /*
  555. * Fill in repository with the current repository
  556. */
  557. if (frame->which & W_LOCAL)
  558. {
  559. if (isdir (CVSADM))
  560. {
  561. repository = Name_Repository ((char *) NULL, update_dir);
  562. srepository = repository; /* remember what to free */
  563. }
  564. else
  565. repository = NULL;
  566. }
  567. else
  568. {
  569. repository = frame->repository;
  570. assert (repository != NULL);
  571. }
  572. fileattr_startdir (repository);
  573. /*
  574. * The filesdoneproc needs to be called for each directory where files
  575. * processed, or each directory that is processed by a call where no
  576. * directories were passed in. In fact, the only time we don't want to
  577. * call back the filesdoneproc is when we are processing directories that
  578. * were passed in on the command line (or in the special case of `.' when
  579. * we were called with no args
  580. */
  581. if (dirlist != NULL && filelist == NULL)
  582. dodoneproc = 0;
  583. /*
  584. * If filelist or dirlist is already set, we don't look again. Otherwise,
  585. * find the files and directories
  586. */
  587. if (filelist == NULL && dirlist == NULL)
  588. {
  589. /* both lists were NULL, so start from scratch */
  590. if (frame->fileproc != NULL && frame->flags != R_SKIP_FILES)
  591. {
  592. int lwhich = frame->which;
  593. /* be sure to look in the attic if we have sticky tags/date */
  594. if ((lwhich & W_ATTIC) == 0)
  595. if (isreadable (CVSADM_TAG))
  596. lwhich |= W_ATTIC;
  597. /* In the !(which & W_LOCAL) case, we filled in repository
  598. earlier in the function. In the (which & W_LOCAL) case,
  599. the Find_Names function is going to look through the
  600. Entries file. If we do not have a repository, that
  601. does not make sense, so we insist upon having a
  602. repository at this point. Name_Repository will give a
  603. reasonable error message. */
  604. if (repository == NULL)
  605. {
  606. Name_Repository ((char *) NULL, update_dir);
  607. assert (!"Not reached. Please report this problem to <"
  608. PACKAGE_BUGREPORT ">");
  609. }
  610. /* find the files and fill in entries if appropriate */
  611. if (process_this_directory)
  612. {
  613. filelist = Find_Names (repository, lwhich, frame->aflag,
  614. &entries);
  615. if (filelist == NULL)
  616. {
  617. error (0, 0, "skipping directory %s", update_dir);
  618. /* Note that Find_Directories and the filesdoneproc
  619. in particular would do bad things ("? foo.c" in
  620. the case of some filesdoneproc's). */
  621. goto skip_directory;
  622. }
  623. }
  624. }
  625. /* find sub-directories if we will recurse */
  626. if (frame->flags != R_SKIP_DIRS)
  627. dirlist = Find_Directories (
  628. process_this_directory ? repository : NULL,
  629. frame->which, entries);
  630. }
  631. else
  632. {
  633. /* something was passed on the command line */
  634. if (filelist != NULL && frame->fileproc != NULL)
  635. {
  636. /* we will process files, so pre-parse entries */
  637. if (frame->which & W_LOCAL)
  638. entries = Entries_Open (frame->aflag, NULL);
  639. }
  640. }
  641. /* process the files (if any) */
  642. if (process_this_directory && filelist != NULL && frame->fileproc)
  643. {
  644. struct file_info finfo_struct;
  645. struct frame_and_file frfile;
  646. /* read lock it if necessary */
  647. if (repository)
  648. {
  649. if (locktype == CVS_LOCK_READ)
  650. {
  651. if (Reader_Lock (repository) != 0)
  652. error (1, 0, "read lock failed - giving up");
  653. }
  654. else if (locktype == CVS_LOCK_WRITE)
  655. lock_dir_for_write (repository);
  656. }
  657. #ifdef CLIENT_SUPPORT
  658. /* For the server, we handle notifications in a completely different
  659. place (server_notify). For local, we can't do them here--we don't
  660. have writelocks in place, and there is no way to get writelocks
  661. here. */
  662. if (current_parsed_root->isremote)
  663. cvs_notify_check (repository, update_dir);
  664. #endif /* CLIENT_SUPPORT */
  665. finfo_struct.repository = repository;
  666. finfo_struct.update_dir = update_dir;
  667. finfo_struct.entries = entries;
  668. /* do_file_proc will fill in finfo_struct.file. */
  669. frfile.finfo = &finfo_struct;
  670. frfile.frame = frame;
  671. /* process the files */
  672. err += walklist (filelist, do_file_proc, &frfile);
  673. /* unlock it */
  674. if (/* We only lock the repository above when repository is set */
  675. repository
  676. /* and when asked for a read or write lock. */
  677. && locktype != CVS_LOCK_NONE)
  678. Lock_Cleanup ();
  679. /* clean up */
  680. dellist (&filelist);
  681. }
  682. /* call-back files done proc (if any) */
  683. if (process_this_directory && dodoneproc && frame->filesdoneproc != NULL)
  684. err = frame->filesdoneproc (frame->callerdat, err, repository,
  685. update_dir[0] ? update_dir : ".",
  686. entries);
  687. skip_directory:
  688. fileattr_write ();
  689. fileattr_free ();
  690. /* process the directories (if necessary) */
  691. if (dirlist != NULL)
  692. {
  693. struct frame_and_entries frent;
  694. frent.frame = frame;
  695. frent.entries = entries;
  696. err += walklist (dirlist, do_dir_proc, (void *) &frent);
  697. }
  698. #if 0
  699. else if (frame->dirleaveproc != NULL)
  700. err += frame->dirleaveproc (frame->callerdat, ".", err, ".");
  701. #endif
  702. dellist (&dirlist);
  703. if (entries)
  704. {
  705. Entries_Close (entries);
  706. entries = NULL;
  707. }
  708. /* free the saved copy of the pointer if necessary */
  709. if (srepository)
  710. {
  711. free (srepository);
  712. }
  713. repository = (char *) NULL;
  714. return err;
  715. }
  716. /*
  717. * Process each of the files in the list with the callback proc
  718. */
  719. static int
  720. do_file_proc (p, closure)
  721. Node *p;
  722. void *closure;
  723. {
  724. struct frame_and_file *frfile = (struct frame_and_file *)closure;
  725. struct file_info *finfo = frfile->finfo;
  726. int ret;
  727. char *tmp;
  728. finfo->file = p->key;
  729. tmp = xmalloc (strlen (finfo->file)
  730. + strlen (finfo->update_dir)
  731. + 2);
  732. tmp[0] = '\0';
  733. if (finfo->update_dir[0] != '\0')
  734. {
  735. strcat (tmp, finfo->update_dir);
  736. strcat (tmp, "/");
  737. }
  738. strcat (tmp, finfo->file);
  739. if (frfile->frame->dosrcs && repository)
  740. {
  741. finfo->rcs = RCS_parse (finfo->file, repository);
  742. /* OK, without W_LOCAL the error handling becomes relatively
  743. simple. The file names came from readdir() on the
  744. repository and so we know any ENOENT is an error
  745. (e.g. symlink pointing to nothing). Now, the logic could
  746. be simpler - since we got the name from readdir, we could
  747. just be calling RCS_parsercsfile. */
  748. if (finfo->rcs == NULL
  749. && !(frfile->frame->which & W_LOCAL))
  750. {
  751. error (0, 0, "could not read RCS file for %s", tmp);
  752. free (tmp);
  753. cvs_flushout ();
  754. return 0;
  755. }
  756. }
  757. else
  758. finfo->rcs = (RCSNode *) NULL;
  759. finfo->fullname = tmp;
  760. ret = frfile->frame->fileproc (frfile->frame->callerdat, finfo);
  761. freercsnode(&finfo->rcs);
  762. free (tmp);
  763. /* Allow the user to monitor progress with tail -f. Doing this once
  764. per file should be no big deal, but we don't want the performance
  765. hit of flushing on every line like previous versions of CVS. */
  766. cvs_flushout ();
  767. return ret;
  768. }
  769. /*
  770. * Process each of the directories in the list (recursing as we go)
  771. */
  772. static int
  773. do_dir_proc (p, closure)
  774. Node *p;
  775. void *closure;
  776. {
  777. struct frame_and_entries *frent = (struct frame_and_entries *) closure;
  778. struct recursion_frame *frame = frent->frame;
  779. struct recursion_frame xframe;
  780. char *dir = p->key;
  781. char *newrepos;
  782. List *sdirlist;
  783. char *srepository;
  784. Dtype dir_return = R_PROCESS;
  785. int stripped_dot = 0;
  786. int err = 0;
  787. struct saved_cwd cwd;
  788. char *saved_update_dir;
  789. int process_this_directory = 1;
  790. if (fncmp (dir, CVSADM) == 0)
  791. {
  792. /* This seems to most often happen when users (beginning users,
  793. generally), try "cvs ci *" or something similar. On that
  794. theory, it is possible that we should just silently skip the
  795. CVSADM directories, but on the other hand, using a wildcard
  796. like this isn't necessarily a practice to encourage (it operates
  797. only on files which exist in the working directory, unlike
  798. regular CVS recursion). */
  799. /* FIXME-reentrancy: printed_cvs_msg should be in a "command
  800. struct" or some such, so that it gets cleared for each new
  801. command (this is possible using the remote protocol and a
  802. custom-written client). The struct recursion_frame is not
  803. far back enough though, some commands (commit at least)
  804. will call start_recursion several times. An alternate solution
  805. would be to take this whole check and move it to a new function
  806. validate_arguments or some such that all the commands call
  807. and which snips the offending directory from the argc,argv
  808. vector. */
  809. static int printed_cvs_msg = 0;
  810. if (!printed_cvs_msg)
  811. {
  812. error (0, 0, "warning: directory %s specified in argument",
  813. dir);
  814. error (0, 0, "\
  815. but CVS uses %s for its own purposes; skipping %s directory",
  816. CVSADM, dir);
  817. printed_cvs_msg = 1;
  818. }
  819. return 0;
  820. }
  821. saved_update_dir = update_dir;
  822. update_dir = xmalloc (strlen (saved_update_dir)
  823. + strlen (dir)
  824. + 5);
  825. strcpy (update_dir, saved_update_dir);
  826. /* set up update_dir - skip dots if not at start */
  827. if (strcmp (dir, ".") != 0)
  828. {
  829. if (update_dir[0] != '\0')
  830. {
  831. (void) strcat (update_dir, "/");
  832. (void) strcat (update_dir, dir);
  833. }
  834. else
  835. (void) strcpy (update_dir, dir);
  836. /*
  837. * Here we need a plausible repository name for the sub-directory. We
  838. * create one by concatenating the new directory name onto the
  839. * previous repository name. The only case where the name should be
  840. * used is in the case where we are creating a new sub-directory for
  841. * update -d and in that case the generated name will be correct.
  842. */
  843. if (repository == NULL)
  844. newrepos = xstrdup ("");
  845. else
  846. {
  847. newrepos = xmalloc (strlen (repository) + strlen (dir) + 5);
  848. sprintf (newrepos, "%s/%s", repository, dir);
  849. }
  850. }
  851. else
  852. {
  853. if (update_dir[0] == '\0')
  854. (void) strcpy (update_dir, dir);
  855. if (repository == NULL)
  856. newrepos = xstrdup ("");
  857. else
  858. newrepos = xstrdup (repository);
  859. }
  860. /* Check to see that the CVSADM directory, if it exists, seems to be
  861. well-formed. It can be missing files if the user hit ^C in the
  862. middle of a previous run. We want to (a) make this a nonfatal
  863. error, and (b) make sure we print which directory has the
  864. problem.
  865. Do this before the direntproc, so that (1) the direntproc
  866. doesn't have to guess/deduce whether we will skip the directory
  867. (e.g. send_dirent_proc and whether to send the directory), and
  868. (2) so that the warm fuzzy doesn't get printed if we skip the
  869. directory. */
  870. if (frame->which & W_LOCAL)
  871. {
  872. char *cvsadmdir;
  873. cvsadmdir = xmalloc (strlen (dir)
  874. + sizeof (CVSADM_REP)
  875. + sizeof (CVSADM_ENT)
  876. + 80);
  877. strcpy (cvsadmdir, dir);
  878. strcat (cvsadmdir, "/");
  879. strcat (cvsadmdir, CVSADM);
  880. if (isdir (cvsadmdir))
  881. {
  882. strcpy (cvsadmdir, dir);
  883. strcat (cvsadmdir, "/");
  884. strcat (cvsadmdir, CVSADM_REP);
  885. if (!isfile (cvsadmdir))
  886. {
  887. /* Some commands like update may have printed "? foo" but
  888. if we were planning to recurse, and don't on account of
  889. CVS/Repository, we want to say why. */
  890. error (0, 0, "ignoring %s (%s missing)", update_dir,
  891. CVSADM_REP);
  892. dir_return = R_SKIP_ALL;
  893. }
  894. /* Likewise for CVS/Entries. */
  895. if (dir_return != R_SKIP_ALL)
  896. {
  897. strcpy (cvsadmdir, dir);
  898. strcat (cvsadmdir, "/");
  899. strcat (cvsadmdir, CVSADM_ENT);
  900. if (!isfile (cvsadmdir))
  901. {
  902. /* Some commands like update may have printed "? foo" but
  903. if we were planning to recurse, and don't on account of
  904. CVS/Repository, we want to say why. */
  905. error (0, 0, "ignoring %s (%s missing)", update_dir,
  906. CVSADM_ENT);
  907. dir_return = R_SKIP_ALL;
  908. }
  909. }
  910. }
  911. free (cvsadmdir);
  912. }
  913. /* Only process this directory if the root matches. This nearly
  914. duplicates code in do_recursion. */
  915. /* If -d was specified, it should override CVS/Root.
  916. In the single-repository case, it is long-standing CVS behavior
  917. and makes sense - the user might want another access method,
  918. another server (which mounts the same repository), &c.
  919. In the multiple-repository case, -d overrides all CVS/Root
  920. files. That is the only plausible generalization I can
  921. think of. */
  922. if (CVSroot_cmdline == NULL && !server_active)
  923. {
  924. cvsroot_t *this_root = Name_Root (dir, update_dir);
  925. if (this_root != NULL)
  926. {
  927. if (findnode (root_directories, this_root->original))
  928. {
  929. process_this_directory = !strcmp (current_parsed_root->original,
  930. this_root->original);
  931. free_cvsroot_t (this_root);
  932. }
  933. else
  934. {
  935. /* Add it to our list. */
  936. Node *n = getnode ();
  937. n->type = NT_UNKNOWN;
  938. n->key = xstrdup (this_root->original);
  939. n->data = this_root;
  940. if (addnode (root_directories, n))
  941. error (1, 0, "cannot add new CVSROOT %s",
  942. this_root->original);
  943. process_this_directory = 0;
  944. }
  945. }
  946. }
  947. /* call-back dir entry proc (if any) */
  948. if (dir_return == R_SKIP_ALL)
  949. ;
  950. else if (frame->direntproc != NULL)
  951. {
  952. /* If we're doing the actual processing, call direntproc.
  953. Otherwise, assume that we need to process this directory
  954. and recurse. FIXME. */
  955. if (process_this_directory)
  956. dir_return = frame->direntproc (frame->callerdat, dir, newrepos,
  957. update_dir, frent->entries);
  958. else
  959. dir_return = R_PROCESS;
  960. }
  961. else
  962. {
  963. /* Generic behavior. I don't see a reason to make the caller specify
  964. a direntproc just to get this. */
  965. if ((frame->which & W_LOCAL) && !isdir (dir))
  966. dir_return = R_SKIP_ALL;
  967. }
  968. free (newrepos);
  969. /* only process the dir if the return code was 0 */
  970. if (dir_return != R_SKIP_ALL)
  971. {
  972. /* save our current directory and static vars */
  973. if (save_cwd (&cwd))
  974. error_exit ();
  975. sdirlist = dirlist;
  976. srepository = repository;
  977. dirlist = NULL;
  978. /* cd to the sub-directory */
  979. if (CVS_CHDIR (dir) < 0)
  980. error (1, errno, "could not chdir to %s", dir);
  981. /* honor the global SKIP_DIRS (a.k.a. local) */
  982. if (frame->flags == R_SKIP_DIRS)
  983. dir_return = R_SKIP_DIRS;
  984. /* remember if the `.' will be stripped for subsequent dirs */
  985. if (strcmp (update_dir, ".") == 0)
  986. {
  987. update_dir[0] = '\0';
  988. stripped_dot = 1;
  989. }
  990. /* make the recursive call */
  991. xframe = *frame;
  992. xframe.flags = dir_return;
  993. /* Keep track of repository, really just for r* commands (rtag, rdiff,
  994. * co, ...) to tag_check_valid, since all the other commands use
  995. * CVS/Repository to figure it out per directory.
  996. */
  997. if (repository)
  998. {
  999. if (strcmp (dir, ".") == 0)
  1000. xframe.repository = xstrdup (repository);
  1001. else
  1002. {
  1003. xframe.repository = xmalloc (strlen (repository)
  1004. + strlen (dir)
  1005. + 2);
  1006. sprintf (xframe.repository, "%s/%s", repository, dir);
  1007. }
  1008. }
  1009. else
  1010. xframe.repository = NULL;
  1011. err += do_recursion (&xframe);
  1012. if (xframe.repository)
  1013. {
  1014. free (xframe.repository);
  1015. xframe.repository = NULL;
  1016. }
  1017. /* put the `.' back if necessary */
  1018. if (stripped_dot)
  1019. (void) strcpy (update_dir, ".");
  1020. /* call-back dir leave proc (if any) */
  1021. if (process_this_directory && frame->dirleaveproc != NULL)
  1022. err = frame->dirleaveproc (frame->callerdat, dir, err, update_dir,
  1023. frent->entries);
  1024. /* get back to where we started and restore state vars */
  1025. if (restore_cwd (&cwd, NULL))
  1026. error_exit ();
  1027. free_cwd (&cwd);
  1028. dirlist = sdirlist;
  1029. repository = srepository;
  1030. }
  1031. free (update_dir);
  1032. update_dir = saved_update_dir;
  1033. return err;
  1034. }
  1035. /*
  1036. * Add a node to a list allocating the list if necessary.
  1037. */
  1038. static void
  1039. addlist (listp, key)
  1040. List **listp;
  1041. char *key;
  1042. {
  1043. Node *p;
  1044. if (*listp == NULL)
  1045. *listp = getlist ();
  1046. p = getnode ();
  1047. p->type = FILES;
  1048. p->key = xstrdup (key);
  1049. if (addnode (*listp, p) != 0)
  1050. freenode (p);
  1051. }
  1052. static void
  1053. addfile (listp, dir, file)
  1054. List **listp;
  1055. char *dir;
  1056. char *file;
  1057. {
  1058. Node *n;
  1059. List *fl;
  1060. /* add this dir. */
  1061. addlist (listp, dir);
  1062. n = findnode (*listp, dir);
  1063. if (n == NULL)
  1064. {
  1065. error (1, 0, "can't find recently added dir node `%s' in start_recursion.",
  1066. dir);
  1067. }
  1068. n->type = DIRS;
  1069. fl = n->data;
  1070. addlist (&fl, file);
  1071. n->data = fl;
  1072. return;
  1073. }
  1074. static int
  1075. unroll_files_proc (p, closure)
  1076. Node *p;
  1077. void *closure;
  1078. {
  1079. Node *n;
  1080. struct recursion_frame *frame = (struct recursion_frame *) closure;
  1081. int err = 0;
  1082. List *save_dirlist;
  1083. char *save_update_dir = NULL;
  1084. struct saved_cwd cwd;
  1085. /* if this dir was also an explicitly named argument, then skip
  1086. it. We'll catch it later when we do dirs. */
  1087. n = findnode (dirlist, p->key);
  1088. if (n != NULL)
  1089. return (0);
  1090. /* otherwise, call dorecusion for this list of files. */
  1091. filelist = p->data;
  1092. p->data = NULL;
  1093. save_dirlist = dirlist;
  1094. dirlist = NULL;
  1095. if (strcmp(p->key, ".") != 0)
  1096. {
  1097. if (save_cwd (&cwd))
  1098. error_exit ();
  1099. if ( CVS_CHDIR (p->key) < 0)
  1100. error (1, errno, "could not chdir to %s", p->key);
  1101. save_update_dir = update_dir;
  1102. update_dir = xmalloc (strlen (save_update_dir)
  1103. + strlen (p->key)
  1104. + 5);
  1105. strcpy (update_dir, save_update_dir);
  1106. if (*update_dir != '\0')
  1107. (void) strcat (update_dir, "/");
  1108. (void) strcat (update_dir, p->key);
  1109. }
  1110. err += do_recursion (frame);
  1111. if (save_update_dir != NULL)
  1112. {
  1113. free (update_dir);
  1114. update_dir = save_update_dir;
  1115. if (restore_cwd (&cwd, NULL))
  1116. error_exit ();
  1117. free_cwd (&cwd);
  1118. }
  1119. dirlist = save_dirlist;
  1120. if (filelist)
  1121. dellist (&filelist);
  1122. return(err);
  1123. }