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