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