2 * Copyright (C) 1986-2005 The Free Software Foundation, Inc.
4 * Portions Copyright (C) 1998-2005 Derek Price, Ximbiot <http://ximbiot.com>,
7 * Portions Copyright (C) 1992, Brian Berliner and Jeff Polk
8 * Portions Copyright (C) 1989-1992, Brian Berliner
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.
15 * Finds all the pertinent file names, both from the administration and from the
20 * Finds all pertinent sub-directories of the checked out instantiation and the
21 * repository (and optionally the attic)
28 static int find_dirs (char *dir, List * list, int checkadm,
30 static int find_rcs (const char *dir, List * list);
31 static int add_subdir_proc (Node *, void *);
32 static int register_subdir_proc (Node *, void *);
35 * add the key from entry on entries list to the files list
37 static int add_entries_proc (Node *, void *);
39 add_entries_proc (Node *node, void *closure)
42 List *filelist = closure;
43 Entnode *entnode = node->data;
45 if (entnode->type != ENT_FILE)
50 fnode->key = xstrdup (node->key);
51 if (addnode (filelist, fnode) != 0)
56 /* Find files in the repository and/or working directory. On error,
57 may either print a nonfatal error and return NULL, or just give
58 a fatal error. On success, return non-NULL (even if it is an empty
62 Find_Names (char *repository, int which, int aflag, List **optentries)
67 /* make a list for the files */
70 /* look at entries (if necessary) */
73 /* parse the entries file (if it exists) */
74 entries = Entries_Open (aflag, NULL);
77 /* walk the entries file adding elements to the files list */
78 (void) walklist (entries, add_entries_proc, files);
80 /* if our caller wanted the entries list, return it; else free it */
81 if (optentries != NULL)
82 *optentries = entries;
84 Entries_Close (entries);
88 if ((which & W_REPOS) && repository && !isreadable (CVSADM_ENTSTAT))
90 /* search the repository */
91 if (find_rcs (repository, files) != 0)
93 error (0, errno, "cannot open directory %s",
94 primary_root_inverse_translate (repository));
98 /* search the attic too */
101 char *dir = Xasprintf ("%s/%s", repository, CVSATTIC);
102 if (find_rcs (dir, files) != 0
103 && !existence_error (errno))
104 /* For now keep this a fatal error, seems less useful
105 for access control than the case above. */
106 error (1, errno, "cannot open directory %s",
107 primary_root_inverse_translate (dir));
112 /* sort the list into alphabetical order and return it */
113 sortlist (files, fsortcmp);
121 * Add an entry from the subdirs list to the directories list. This
122 * is called via walklist.
126 add_subdir_proc (Node *p, void *closure)
128 List *dirlist = closure;
129 Entnode *entnode = p->data;
132 if (entnode->type != ENT_SUBDIR)
137 dnode->key = xstrdup (entnode->user);
138 if (addnode (dirlist, dnode) != 0)
144 * Register a subdirectory. This is called via walklist.
149 register_subdir_proc (Node *p, void *closure)
151 List *entries = (List *) closure;
153 Subdir_Register (entries, NULL, p->key);
158 * create a list of directories to traverse from the current directory
161 Find_Directories (char *repository, int which, List *entries)
165 /* make a list for the directories */
166 dirlist = getlist ();
168 /* find the local ones */
172 struct stickydirtag *sdtp;
174 /* Look through the Entries file. */
177 tmpentries = entries;
178 else if (isfile (CVSADM_ENT))
179 tmpentries = Entries_Open (0, NULL);
183 if (tmpentries != NULL)
184 sdtp = tmpentries->list->data;
186 /* If we do have an entries list, then if sdtp is NULL, or if
187 sdtp->subdirs is nonzero, all subdirectory information is
188 recorded in the entries list. */
189 if (tmpentries != NULL && (sdtp == NULL || sdtp->subdirs))
190 walklist (tmpentries, add_subdir_proc, (void *) dirlist);
193 /* This is an old working directory, in which subdirectory
194 information is not recorded in the Entries file. Find
195 the subdirectories the hard way, and, if possible, add
196 it to the Entries file for next time. */
198 /* FIXME-maybe: find_dirs is bogus for this usage because
199 it skips CVSATTIC and CVSLCK directories--those names
200 should be special only in the repository. However, in
201 the interests of not perturbing this code, we probably
202 should leave well enough alone unless we want to write
203 a sanity.sh test case (which would operate by manually
204 hacking on the CVS/Entries file). */
206 if (find_dirs (".", dirlist, 1, tmpentries) != 0)
207 error (1, errno, "cannot open current directory");
208 if (tmpentries != NULL)
210 if (! list_isempty (dirlist))
211 walklist (dirlist, register_subdir_proc,
212 (void *) tmpentries);
214 Subdirs_Known (tmpentries);
218 if (entries == NULL && tmpentries != NULL)
219 Entries_Close (tmpentries);
222 /* look for sub-dirs in the repository */
223 if ((which & W_REPOS) && repository)
225 /* search the repository */
226 if (find_dirs (repository, dirlist, 0, entries) != 0)
227 error (1, errno, "cannot open directory %s", repository);
229 /* We don't need to look in the attic because directories
230 never go in the attic. In the future, there hopefully will
231 be a better mechanism for detecting whether a directory in
232 the repository is alive or dead; it may or may not involve
233 moving directories to the attic. */
236 /* sort the list into alphabetical order and return it */
237 sortlist (dirlist, fsortcmp);
243 /* Finds all the files matching PAT. If DIR is NULL, PAT will be interpreted
244 * as either absolute or relative to the PWD and read errors, e.g. failure to
245 * open a directory, will be ignored. If DIR is not NULL, PAT is
246 * always interpreted as relative to DIR. Adds all matching files and
247 * directories to a new List. Returns the new List for success and NULL in
248 * case of error, in which case ERRNO will also be set.
251 * When DIR is NULL, this is really just a thinly veiled wrapper for glob().
253 * Much of the cruft in this function could be avoided if DIR was eliminated.
256 * dir The directory to match relative to.
257 * pat The pattern to match against, via glob().
260 * errno Set on error.
261 * really_quiet Used to decide whether to print warnings.
264 * A pointer to a List of matching file and directory names, on success.
268 * Error returns can be caused if glob() returns an error. ERRNO will be
269 * set. When !REALLY_QUIET and the failure was not a read error, a warning
270 * message will be printed via error (0, errno, ...).
273 find_files (const char *dir, const char *pat)
279 bool dirslash = false;
283 size_t catpatlen = 0;
285 if (glob_pattern_p (dir, false))
287 /* Escape special characters in DIR. */
299 expand_string (&catpat, &catpatlen, len + 1);
300 catpat[len++] = '\\';
302 expand_string (&catpat, &catpatlen, len + 1);
303 catpat[len++] = *p++;
311 xrealloc_and_strcat (&catpat, &catpatlen, dir);
312 p = dir + strlen (dir);
315 dirslash = *p - 1 == '/';
317 xrealloc_and_strcat (&catpat, &catpatlen, "/");
319 xrealloc_and_strcat (&catpat, &catpatlen, pat);
323 err = glob (pat, GLOB_PERIOD | (dir ? GLOB_ERR : 0), NULL, &glist);
324 if (err && err != GLOB_NOMATCH)
326 if (err == GLOB_ABORTED)
327 /* Let our caller handle the problem. */
329 if (err == GLOB_NOSPACE) errno = ENOMEM;
331 error (0, errno, "glob failed");
332 if (catpat) free (catpat);
336 /* Copy what glob() returned into a List for our caller. */
338 for (i = 0; i < glist.gl_pathc; i++)
343 /* Ignore `.' && `..'. */
344 tmp = last_component (glist.gl_pathv[i]);
345 if (!strcmp (tmp, ".") || !strcmp (tmp, ".."))
350 p->key = xstrdup (glist.gl_pathv[i]
351 + (dir ? strlen (dir) + !dirslash : 0));
352 if (addnode (retval, p)) freenode (p);
355 if (catpat) free (catpat);
362 /* walklist() proc which strips a trailing RCSEXT from node keys.
365 strip_rcsext (Node *p, void *closure)
367 char *s = p->key + strlen (p->key) - strlen (RCSEXT);
368 assert (!strcmp (s, RCSEXT));
369 *s = '\0'; /* strip the ,v */
376 * Finds all the ,v files in the directory DIR, and adds them to the LIST.
377 * Returns 0 for success and non-zero if DIR cannot be opened, in which case
378 * ERRNO is set to indicate the error. In the error case, LIST is left in some
379 * reasonable state (unchanged, or containing the files which were found before
380 * the error occurred).
383 * dir The directory to open for read.
386 * list Where to store matching file entries.
389 * errno Set on error.
401 if (!(newlist = find_files (dir, RCSPAT)))
403 walklist (newlist, strip_rcsext, NULL);
404 mergelists (list, &newlist);
411 * Finds all the subdirectories of the argument dir and adds them to
412 * the specified list. Sub-directories without a CVS administration
413 * directory are optionally ignored. If ENTRIES is not NULL, all
414 * files on the list are ignored. Returns 0 for success or 1 on
415 * error, in which case errno is set to indicate the error.
418 find_dirs (char *dir, List *list, int checkadm, List *entries)
425 int skip_emptydir = 0;
427 /* First figure out whether we need to skip directories named
428 Emptydir. Except in the CVSNULLREPOS case, Emptydir is just
429 a normal directory name. */
431 && strncmp (dir, current_parsed_root->directory, strlen (current_parsed_root->directory)) == 0
432 && ISSLASH (dir[strlen (current_parsed_root->directory)])
433 && strcmp (dir + strlen (current_parsed_root->directory) + 1, CVSROOTADM) == 0)
436 /* set up to read the dir */
437 if ((dirp = CVS_OPENDIR (dir)) == NULL)
440 /* read the dir, grabbing sub-dirs */
442 while ((dp = CVS_READDIR (dirp)) != NULL)
444 if (strcmp (dp->d_name, ".") == 0 ||
445 strcmp (dp->d_name, "..") == 0 ||
446 strcmp (dp->d_name, CVSATTIC) == 0 ||
447 strcmp (dp->d_name, CVSLCK) == 0 ||
448 strcmp (dp->d_name, CVSREP) == 0)
451 /* findnode() is going to be significantly faster than stat()
452 because it involves no system calls. That is why we bother
453 with the entries argument, and why we check this first. */
454 if (entries != NULL && findnode (entries, dp->d_name) != NULL)
458 && strcmp (dp->d_name, CVSNULLREPOS) == 0)
462 if (dp->d_type != DT_DIR)
464 if (dp->d_type != DT_UNKNOWN && dp->d_type != DT_LNK)
467 /* don't bother stating ,v files */
468 if (CVS_FNMATCH (RCSPAT, dp->d_name, 0) == 0)
473 strlen (dir) + strlen (dp->d_name) + 10);
474 sprintf (tmp, "%s/%s", dir, dp->d_name);
482 /* check for administration directories (if needed) */
485 /* blow off symbolic links to dirs in local dir */
487 if (dp->d_type != DT_DIR)
489 /* we're either unknown or a symlink at this point */
490 if (dp->d_type == DT_LNK)
493 /* Note that we only get here if we already set tmp
501 /* check for new style */
504 (strlen (dir) + strlen (dp->d_name)
505 + sizeof (CVSADM) + 10));
506 (void) sprintf (tmp, "%s/%s/%s", dir, dp->d_name, CVSADM);
511 /* put it in the list */
514 p->key = xstrdup (dp->d_name);
515 if (addnode (list, p) != 0)
523 int save_errno = errno;
524 (void) CVS_CLOSEDIR (dirp);
528 (void) CVS_CLOSEDIR (dirp);