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