home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / cvs-1.8.7-src.tgz / tar.out / fsf / cvs / src / find_names.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  9KB  |  369 lines

  1. /*
  2.  * Copyright (c) 1992, Brian Berliner and Jeff Polk
  3.  * Copyright (c) 1989-1992, Brian Berliner
  4.  * 
  5.  * You may distribute under the terms of the GNU General Public License as
  6.  * specified in the README file that comes with the CVS 1.4 kit.
  7.  * 
  8.  * Find Names
  9.  * 
  10.  * Finds all the pertinent file names, both from the administration and from the
  11.  * repository
  12.  * 
  13.  * Find Dirs
  14.  * 
  15.  * Finds all pertinent sub-directories of the checked out instantiation and the
  16.  * repository (and optionally the attic)
  17.  */
  18.  
  19. #include "cvs.h"
  20.  
  21. static int find_dirs PROTO((char *dir, List * list, int checkadm,
  22.                 List *entries));
  23. static int find_rcs PROTO((char *dir, List * list));
  24. static int add_subdir_proc PROTO((Node *, void *));
  25. static int register_subdir_proc PROTO((Node *, void *));
  26.  
  27. static List *filelist;
  28.  
  29. /*
  30.  * add the key from entry on entries list to the files list
  31.  */
  32. static int add_entries_proc PROTO((Node *, void *));
  33. static int
  34. add_entries_proc (node, closure)
  35.      Node *node;
  36.      void *closure;
  37. {
  38.     Entnode *entnode;
  39.     Node *fnode;
  40.  
  41.     entnode = (Entnode *) node->data;
  42.     if (entnode->type != ENT_FILE)
  43.     return (0);
  44.  
  45.     fnode = getnode ();
  46.     fnode->type = FILES;
  47.     fnode->key = xstrdup (node->key);
  48.     if (addnode (filelist, fnode) != 0)
  49.     freenode (fnode);
  50.     return (0);
  51. }
  52.  
  53. /*
  54.  * compare two files list node (for sort)
  55.  */
  56. static int fsortcmp PROTO ((const Node *, const Node *));
  57. static int
  58. fsortcmp (p, q)
  59.     const Node *p;
  60.     const Node *q;
  61. {
  62.     return (strcmp (p->key, q->key));
  63. }
  64.  
  65. List *
  66. Find_Names (repository, which, aflag, optentries)
  67.     char *repository;
  68.     int which;
  69.     int aflag;
  70.     List **optentries;
  71. {
  72.     List *entries;
  73.     List *files;
  74.     char dir[PATH_MAX];
  75.  
  76.     /* make a list for the files */
  77.     files = filelist = getlist ();
  78.  
  79.     /* look at entries (if necessary) */
  80.     if (which & W_LOCAL)
  81.     {
  82.     /* parse the entries file (if it exists) */
  83.     entries = Entries_Open (aflag);
  84.     if (entries != NULL)
  85.     {
  86.         /* walk the entries file adding elements to the files list */
  87.         (void) walklist (entries, add_entries_proc, NULL);
  88.  
  89.         /* if our caller wanted the entries list, return it; else free it */
  90.         if (optentries != NULL)
  91.         *optentries = entries;
  92.         else
  93.         Entries_Close (entries);
  94.     }
  95.     }
  96.  
  97.     if ((which & W_REPOS) && repository && !isreadable (CVSADM_ENTSTAT))
  98.     {
  99.     /* search the repository */
  100.     if (find_rcs (repository, files) != 0)
  101.         error (1, errno, "cannot open directory %s", repository);
  102.  
  103.     /* search the attic too */
  104.     if (which & W_ATTIC)
  105.     {
  106.         (void) sprintf (dir, "%s/%s", repository, CVSATTIC);
  107.         (void) find_rcs (dir, files);
  108.     }
  109.     }
  110.  
  111.     /* sort the list into alphabetical order and return it */
  112.     sortlist (files, fsortcmp);
  113.     return (files);
  114. }
  115.  
  116. /*
  117.  * Add an entry from the subdirs list to the directories list.  This
  118.  * is called via walklist.
  119.  */
  120.  
  121. static int
  122. add_subdir_proc (p, closure)
  123.      Node *p;
  124.      void *closure;
  125. {
  126.     List *dirlist = (List *) closure;
  127.     Entnode *entnode;
  128.     Node *dnode;
  129.  
  130.     entnode = (Entnode *) p->data;
  131.     if (entnode->type != ENT_SUBDIR)
  132.     return 0;
  133.  
  134.     dnode = getnode ();
  135.     dnode->type = DIRS;
  136.     dnode->key = xstrdup (entnode->user);
  137.     if (addnode (dirlist, dnode) != 0)
  138.     freenode (dnode);
  139.     return 0;
  140. }
  141.  
  142. /*
  143.  * Register a subdirectory.  This is called via walklist.
  144.  */
  145.  
  146. /*ARGSUSED*/
  147. static int
  148. register_subdir_proc (p, closure)
  149.      Node *p;
  150.      void *closure;
  151. {
  152.     List *entries = (List *) closure;
  153.  
  154.     Subdir_Register (entries, (char *) NULL, p->key);
  155.     return 0;
  156. }
  157.  
  158. /*
  159.  * create a list of directories to traverse from the current directory
  160.  */
  161. List *
  162. Find_Directories (repository, which, entries)
  163.     char *repository;
  164.     int which;
  165.     List *entries;
  166. {
  167.     List *dirlist;
  168.  
  169.     /* make a list for the directories */
  170.     dirlist = getlist ();
  171.  
  172.     /* find the local ones */
  173.     if (which & W_LOCAL)
  174.     {
  175.     List *tmpentries;
  176.     struct stickydirtag *sdtp;
  177.  
  178.     /* Look through the Entries file.  */
  179.  
  180.     if (entries != NULL)
  181.         tmpentries = entries;
  182.     else if (isfile (CVSADM_ENT))
  183.         tmpentries = Entries_Open (0);
  184.     else
  185.         tmpentries = NULL;
  186.  
  187.     if (tmpentries != NULL)
  188.         sdtp = (struct stickydirtag *) tmpentries->list->data;
  189.  
  190.     /* If we do have an entries list, then if sdtp is NULL, or if
  191.            sdtp->subdirs is nonzero, all subdirectory information is
  192.            recorded in the entries list.  */
  193.     if (tmpentries != NULL && (sdtp == NULL || sdtp->subdirs))
  194.         walklist (tmpentries, add_subdir_proc, (void *) dirlist);
  195.     else
  196.     {
  197.         /* This is an old working directory, in which subdirectory
  198.                information is not recorded in the Entries file.  Find
  199.                the subdirectories the hard way, and, if possible, add
  200.                it to the Entries file for next time.  */
  201.         if (find_dirs (".", dirlist, 1, tmpentries) != 0)
  202.         error (1, errno, "cannot open current directory");
  203.         if (tmpentries != NULL)
  204.         {
  205.         if (! list_isempty (dirlist))
  206.             walklist (dirlist, register_subdir_proc,
  207.                   (void *) tmpentries);
  208.         else
  209.             Subdirs_Known (tmpentries);
  210.         }
  211.     }
  212.  
  213.     if (entries == NULL && tmpentries != NULL)
  214.         Entries_Close (tmpentries);
  215.     }
  216.  
  217.     /* look for sub-dirs in the repository */
  218.     if ((which & W_REPOS) && repository)
  219.     {
  220.     /* search the repository */
  221.     if (find_dirs (repository, dirlist, 0, entries) != 0)
  222.         error (1, errno, "cannot open directory %s", repository);
  223.  
  224. #ifdef ATTIC_DIR_SUPPORT        /* XXX - FIXME */
  225.     /* search the attic too */
  226.     if (which & W_ATTIC)
  227.     {
  228.         char dir[PATH_MAX];
  229.  
  230.         (void) sprintf (dir, "%s/%s", repository, CVSATTIC);
  231.         (void) find_dirs (dir, dirlist, 0, entries);
  232.     }
  233. #endif
  234.     }
  235.  
  236.     /* sort the list into alphabetical order and return it */
  237.     sortlist (dirlist, fsortcmp);
  238.     return (dirlist);
  239. }
  240.  
  241. /*
  242.  * Finds all the ,v files in the argument directory, and adds them to the
  243.  * files list.  Returns 0 for success and non-zero if the argument directory
  244.  * cannot be opened.
  245.  */
  246. static int
  247. find_rcs (dir, list)
  248.     char *dir;
  249.     List *list;
  250. {
  251.     Node *p;
  252.     struct dirent *dp;
  253.     DIR *dirp;
  254.  
  255.     /* set up to read the dir */
  256.     if ((dirp = CVS_OPENDIR (dir)) == NULL)
  257.     return (1);
  258.  
  259.     /* read the dir, grabbing the ,v files */
  260.     while ((dp = readdir (dirp)) != NULL)
  261.     {
  262.     if (fnmatch (RCSPAT, dp->d_name, 0) == 0) 
  263.     {
  264.         char *comma;
  265.  
  266.         comma = strrchr (dp->d_name, ',');    /* strip the ,v */
  267.         *comma = '\0';
  268.         p = getnode ();
  269.         p->type = FILES;
  270.         p->key = xstrdup (dp->d_name);
  271.         if (addnode (list, p) != 0)
  272.         freenode (p);
  273.     }
  274.     }
  275.     (void) closedir (dirp);
  276.     return (0);
  277. }
  278.  
  279. /*
  280.  * Finds all the subdirectories of the argument dir and adds them to
  281.  * the specified list.  Sub-directories without a CVS administration
  282.  * directory are optionally ignored.  If ENTRIES is not NULL, all
  283.  * files on the list are ignored.  Returns 0 for success or 1 on
  284.  * error.
  285.  */
  286. static int
  287. find_dirs (dir, list, checkadm, entries)
  288.     char *dir;
  289.     List *list;
  290.     int checkadm;
  291.     List *entries;
  292. {
  293.     Node *p;
  294.     char tmp[PATH_MAX];
  295.     struct dirent *dp;
  296.     DIR *dirp;
  297.  
  298.     /* set up to read the dir */
  299.     if ((dirp = CVS_OPENDIR (dir)) == NULL)
  300.     return (1);
  301.  
  302.     /* read the dir, grabbing sub-dirs */
  303.     while ((dp = readdir (dirp)) != NULL)
  304.     {
  305.     if (strcmp (dp->d_name, ".") == 0 ||
  306.         strcmp (dp->d_name, "..") == 0 ||
  307.         strcmp (dp->d_name, CVSATTIC) == 0 ||
  308.         strcmp (dp->d_name, CVSLCK) == 0 ||
  309.         strcmp (dp->d_name, CVSREP) == 0)
  310.         continue;
  311.  
  312.     /* findnode() is going to be significantly faster than stat()
  313.        because it involves no system calls.  That is why we bother
  314.        with the entries argument, and why we check this first.  */
  315.     if (entries != NULL && findnode (entries, dp->d_name) != NULL)
  316.         continue;
  317.  
  318. #ifdef DT_DIR
  319.     if (dp->d_type != DT_DIR) 
  320.     {
  321.         if (dp->d_type != DT_UNKNOWN && dp->d_type != DT_LNK)
  322.         continue;
  323. #endif
  324.         /* don't bother stating ,v files */
  325.         if (fnmatch (RCSPAT, dp->d_name, 0) == 0)
  326.         continue;
  327.  
  328.         sprintf (tmp, "%s/%s", dir, dp->d_name);
  329.         if (!isdir (tmp))
  330.         continue;
  331.  
  332. #ifdef DT_DIR
  333.     }
  334. #endif
  335.  
  336.     /* check for administration directories (if needed) */
  337.     if (checkadm)
  338.     {
  339.         /* blow off symbolic links to dirs in local dir */
  340. #ifdef DT_DIR
  341.         if (dp->d_type != DT_DIR)
  342.         {
  343.         /* we're either unknown or a symlink at this point */
  344.         if (dp->d_type == DT_LNK)
  345.             continue;
  346. #endif
  347.         if (islink (tmp))
  348.             continue;
  349. #ifdef DT_DIR
  350.         }
  351. #endif
  352.  
  353.         /* check for new style */
  354.         (void) sprintf (tmp, "%s/%s/%s", dir, dp->d_name, CVSADM);
  355.         if (!isdir (tmp))
  356.         continue;
  357.     }
  358.  
  359.     /* put it in the list */
  360.     p = getnode ();
  361.     p->type = DIRS;
  362.     p->key = xstrdup (dp->d_name);
  363.     if (addnode (list, p) != 0)
  364.         freenode (p);
  365.     }
  366.     (void) closedir (dirp);
  367.     return (0);
  368. }
  369.