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)
26 static int find_dirs PROTO((char *dir, List * list, int checkadm,
28 static int find_rcs PROTO((char *dir, List * list));
29 static int add_subdir_proc PROTO((Node *, void *));
30 static int register_subdir_proc PROTO((Node *, void *));
33 * add the key from entry on entries list to the files list
35 static int add_entries_proc PROTO((Node *, void *));
37 add_entries_proc (node, 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 (repository, which, aflag, optentries)
71 /* make a list for the files */
74 /* look at entries (if necessary) */
77 /* parse the entries file (if it exists) */
78 entries = Entries_Open (aflag, NULL);
81 /* walk the entries file adding elements to the files list */
82 (void) walklist (entries, add_entries_proc, files);
84 /* if our caller wanted the entries list, return it; else free it */
85 if (optentries != NULL)
86 *optentries = entries;
88 Entries_Close (entries);
92 if ((which & W_REPOS) && repository && !isreadable (CVSADM_ENTSTAT))
94 /* search the repository */
95 if (find_rcs (repository, files) != 0)
97 error (0, errno, "cannot open directory %s", repository);
101 /* search the attic too */
105 dir = xmalloc (strlen (repository) + sizeof (CVSATTIC) + 10);
106 (void) sprintf (dir, "%s/%s", repository, CVSATTIC);
107 if (find_rcs (dir, files) != 0
108 && !existence_error (errno))
109 /* For now keep this a fatal error, seems less useful
110 for access control than the case above. */
111 error (1, errno, "cannot open directory %s", dir);
116 /* sort the list into alphabetical order and return it */
117 sortlist (files, fsortcmp);
125 * Add an entry from the subdirs list to the directories list. This
126 * is called via walklist.
130 add_subdir_proc (p, closure)
134 List *dirlist = closure;
135 Entnode *entnode = p->data;
138 if (entnode->type != ENT_SUBDIR)
143 dnode->key = xstrdup (entnode->user);
144 if (addnode (dirlist, dnode) != 0)
150 * Register a subdirectory. This is called via walklist.
155 register_subdir_proc (p, closure)
159 List *entries = (List *) closure;
161 Subdir_Register (entries, (char *) NULL, p->key);
166 * create a list of directories to traverse from the current directory
169 Find_Directories (repository, which, entries)
176 /* make a list for the directories */
177 dirlist = getlist ();
179 /* find the local ones */
183 struct stickydirtag *sdtp;
185 /* Look through the Entries file. */
188 tmpentries = entries;
189 else if (isfile (CVSADM_ENT))
190 tmpentries = Entries_Open (0, NULL);
194 if (tmpentries != NULL)
195 sdtp = tmpentries->list->data;
197 /* If we do have an entries list, then if sdtp is NULL, or if
198 sdtp->subdirs is nonzero, all subdirectory information is
199 recorded in the entries list. */
200 if (tmpentries != NULL && (sdtp == NULL || sdtp->subdirs))
201 walklist (tmpentries, add_subdir_proc, (void *) dirlist);
204 /* This is an old working directory, in which subdirectory
205 information is not recorded in the Entries file. Find
206 the subdirectories the hard way, and, if possible, add
207 it to the Entries file for next time. */
209 /* FIXME-maybe: find_dirs is bogus for this usage because
210 it skips CVSATTIC and CVSLCK directories--those names
211 should be special only in the repository. However, in
212 the interests of not perturbing this code, we probably
213 should leave well enough alone unless we want to write
214 a sanity.sh test case (which would operate by manually
215 hacking on the CVS/Entries file). */
217 if (find_dirs (".", dirlist, 1, tmpentries) != 0)
218 error (1, errno, "cannot open current directory");
219 if (tmpentries != NULL)
221 if (! list_isempty (dirlist))
222 walklist (dirlist, register_subdir_proc,
223 (void *) tmpentries);
225 Subdirs_Known (tmpentries);
229 if (entries == NULL && tmpentries != NULL)
230 Entries_Close (tmpentries);
233 /* look for sub-dirs in the repository */
234 if ((which & W_REPOS) && repository)
236 /* search the repository */
237 if (find_dirs (repository, dirlist, 0, entries) != 0)
238 error (1, errno, "cannot open directory %s", repository);
240 /* We don't need to look in the attic because directories
241 never go in the attic. In the future, there hopefully will
242 be a better mechanism for detecting whether a directory in
243 the repository is alive or dead; it may or may not involve
244 moving directories to the attic. */
247 /* sort the list into alphabetical order and return it */
248 sortlist (dirlist, fsortcmp);
253 * Finds all the ,v files in the argument directory, and adds them to the
254 * files list. Returns 0 for success and non-zero if the argument directory
255 * cannot be opened, in which case errno is set to indicate the error.
256 * In the error case LIST is left in some reasonable state (unchanged, or
257 * containing the files which were found before the error occurred).
268 /* set up to read the dir */
269 if ((dirp = CVS_OPENDIR (dir)) == NULL)
272 /* read the dir, grabbing the ,v files */
274 while ((dp = CVS_READDIR (dirp)) != NULL)
276 if (CVS_FNMATCH (RCSPAT, dp->d_name, 0) == 0)
280 comma = strrchr (dp->d_name, ','); /* strip the ,v */
284 p->key = xstrdup (dp->d_name);
285 if (addnode (list, p) != 0)
292 int save_errno = errno;
293 (void) CVS_CLOSEDIR (dirp);
297 (void) CVS_CLOSEDIR (dirp);
302 * Finds all the subdirectories of the argument dir and adds them to
303 * the specified list. Sub-directories without a CVS administration
304 * directory are optionally ignored. If ENTRIES is not NULL, all
305 * files on the list are ignored. Returns 0 for success or 1 on
306 * error, in which case errno is set to indicate the error.
309 find_dirs (dir, list, checkadm, entries)
320 int skip_emptydir = 0;
322 /* First figure out whether we need to skip directories named
323 Emptydir. Except in the CVSNULLREPOS case, Emptydir is just
324 a normal directory name. */
326 && strncmp (dir, current_parsed_root->directory, strlen (current_parsed_root->directory)) == 0
327 && ISDIRSEP (dir[strlen (current_parsed_root->directory)])
328 && strcmp (dir + strlen (current_parsed_root->directory) + 1, CVSROOTADM) == 0)
331 /* set up to read the dir */
332 if ((dirp = CVS_OPENDIR (dir)) == NULL)
335 /* read the dir, grabbing sub-dirs */
337 while ((dp = CVS_READDIR (dirp)) != NULL)
339 if (strcmp (dp->d_name, ".") == 0 ||
340 strcmp (dp->d_name, "..") == 0 ||
341 strcmp (dp->d_name, CVSATTIC) == 0 ||
342 strcmp (dp->d_name, CVSLCK) == 0 ||
343 strcmp (dp->d_name, CVSREP) == 0)
346 /* findnode() is going to be significantly faster than stat()
347 because it involves no system calls. That is why we bother
348 with the entries argument, and why we check this first. */
349 if (entries != NULL && findnode (entries, dp->d_name) != NULL)
353 && strcmp (dp->d_name, CVSNULLREPOS) == 0)
357 if (dp->d_type != DT_DIR)
359 if (dp->d_type != DT_UNKNOWN && dp->d_type != DT_LNK)
362 /* don't bother stating ,v files */
363 if (CVS_FNMATCH (RCSPAT, dp->d_name, 0) == 0)
368 strlen (dir) + strlen (dp->d_name) + 10);
369 sprintf (tmp, "%s/%s", dir, dp->d_name);
377 /* check for administration directories (if needed) */
380 /* blow off symbolic links to dirs in local dir */
382 if (dp->d_type != DT_DIR)
384 /* we're either unknown or a symlink at this point */
385 if (dp->d_type == DT_LNK)
388 /* Note that we only get here if we already set tmp
396 /* check for new style */
399 (strlen (dir) + strlen (dp->d_name)
400 + sizeof (CVSADM) + 10));
401 (void) sprintf (tmp, "%s/%s/%s", dir, dp->d_name, CVSADM);
406 /* put it in the list */
409 p->key = xstrdup (dp->d_name);
410 if (addnode (list, p) != 0)
418 int save_errno = errno;
419 (void) CVS_CLOSEDIR (dirp);
423 (void) CVS_CLOSEDIR (dirp);