getfilelist.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /***********************************************************************
  42. ** NAME
  43. ** getfilelist.c
  44. **
  45. ** DESCRIPTION
  46. **
  47. **
  48. ** AUTHOR
  49. ** Rich Megginson <[email protected]>
  50. **
  51. ***********************************************************************/
  52. /***********************************************************************
  53. ** Includes
  54. ***********************************************************************/
  55. #include "prio.h"
  56. #include "slap.h"
  57. #include "avl.h"
  58. struct data_wrapper {
  59. char **list;
  60. int n;
  61. int max;
  62. const char *dirname;
  63. };
  64. static int
  65. add_file_to_list(caddr_t data, caddr_t arg)
  66. {
  67. struct data_wrapper *dw = (struct data_wrapper *)arg;
  68. if (dw) {
  69. /* max is number of entries; the range of n is 0 - max-1 */
  70. PR_ASSERT(dw->n <= dw->max);
  71. PR_ASSERT(dw->list);
  72. PR_ASSERT(data);
  73. /* this strdup is free'd by free_filelist */
  74. dw->list[dw->n++] = slapi_ch_smprintf("%s/%s", dw->dirname, data);
  75. return 0;
  76. }
  77. return -1;
  78. }
  79. static void
  80. free_string(caddr_t data)
  81. {
  82. slapi_ch_free((void **)&data);
  83. }
  84. static int
  85. file_is_type_x(const char *dirname, const char *filename, PRFileType x)
  86. {
  87. struct PRFileInfo inf;
  88. int status = 0;
  89. char *fullpath = slapi_ch_smprintf("%s/%s", dirname, filename);
  90. if (PR_SUCCESS == PR_GetFileInfo(fullpath, &inf) &&
  91. inf.type == x)
  92. status = 1;
  93. slapi_ch_free((void **)&fullpath);
  94. return status;
  95. }
  96. /* return true if the given path and file corresponds to a directory */
  97. static int
  98. is_a_dir(const char *dirname, const char *filename)
  99. {
  100. return file_is_type_x(dirname, filename, PR_FILE_DIRECTORY);
  101. }
  102. /* return true if the given path and file corresponds to a regular file */
  103. static int
  104. is_a_file(const char *dirname, const char *filename)
  105. {
  106. return file_is_type_x(dirname, filename, PR_FILE_FILE);
  107. }
  108. static int
  109. matches(const char *filename, const char *pattern)
  110. {
  111. Slapi_Regex *re = NULL;
  112. int match = 0;
  113. char *error = NULL;
  114. if (!pattern)
  115. return 1; /* null pattern matches everything */
  116. /* Compile the pattern */
  117. re = slapi_re_comp( pattern, &error );
  118. if (re) {
  119. /* Matches the compiled pattern against the filename */
  120. match = slapi_re_exec( re, filename, -1 /* no time limit */ );
  121. slapi_re_free( re );
  122. }
  123. return match;
  124. }
  125. /**
  126. * getfilelist will return a list of all files and directories in the
  127. * given directory matching the given pattern. If dirname is NULL, the
  128. * current directory "." will be used. If the pattern is NULL, all files
  129. * and directories will be returned. The additional integer arguments
  130. * control which files and directories are selected. The default value
  131. * for all of them is 0, which will not return hidden files (e.g. files
  132. * beginning with . on unix), but will return both files and directories
  133. * If nofiles is non-zero, only directory names will be returned. If
  134. * nodirs is non-zero, only filenames will be returned.
  135. * The pattern is a grep style regular expression, not a shell or command
  136. * interpreter style regular expression. For example, to get all files ending
  137. * in .ldif, use ".*\\.ldif" instead of "*.ldif"
  138. * The return value is a NULL terminated array of names.
  139. */
  140. char **
  141. get_filelist(
  142. const char *dirname, /* directory path; if NULL, uses "." */
  143. const char *pattern, /* grep (not shell!) file pattern regex */
  144. int hiddenfiles, /* if true, return hidden files and directories too */
  145. int nofiles, /* if true, do not return files */
  146. int nodirs /* if true, do not return directories */
  147. )
  148. {
  149. Avlnode *filetree = 0;
  150. PRDir *dirptr = 0;
  151. PRDirEntry *dirent = 0;
  152. PRDirFlags dirflags = PR_SKIP_BOTH & PR_SKIP_HIDDEN;
  153. char **retval = 0;
  154. int num = 0;
  155. struct data_wrapper dw;
  156. if (!dirname)
  157. dirname = ".";
  158. if (hiddenfiles)
  159. dirflags = PR_SKIP_BOTH;
  160. if (!(dirptr = PR_OpenDir(dirname))) {
  161. return NULL;
  162. }
  163. /* read the directory entries into an ascii sorted avl tree */
  164. for (dirent = PR_ReadDir(dirptr, dirflags); dirent ;
  165. dirent = PR_ReadDir(dirptr, dirflags)) {
  166. if (nofiles && is_a_file(dirname, dirent->name))
  167. continue;
  168. if (nodirs && is_a_dir(dirname, dirent->name))
  169. continue;
  170. if (1 == matches(dirent->name, pattern)) {
  171. /* this strdup is free'd by free_string */
  172. char *newone = slapi_ch_strdup(dirent->name);
  173. avl_insert(&filetree, newone, strcmp, 0);
  174. num++;
  175. }
  176. }
  177. PR_CloseDir(dirptr);
  178. /* allocate space for the list */
  179. retval = (char **)slapi_ch_calloc(num+1, sizeof(char *));
  180. /* traverse the avl tree and copy the filenames into the list */
  181. dw.list = retval;
  182. dw.n = 0;
  183. dw.max = num;
  184. dw.dirname = dirname;
  185. (void)avl_apply(filetree, add_file_to_list, &dw, -1, AVL_INORDER);
  186. retval[num] = 0; /* set last entry to null */
  187. /* delete the avl tree and all its data */
  188. avl_free(filetree, free_string);
  189. return retval;
  190. }
  191. void
  192. free_filelist(char **filelist)
  193. {
  194. int ii;
  195. for (ii = 0; filelist && filelist[ii]; ++ii)
  196. slapi_ch_free((void **)&filelist[ii]);
  197. slapi_ch_free((void **)&filelist);
  198. }
  199. /**
  200. * Returns a list of files in order of "priority" where priority is defined
  201. * as:
  202. * The first two characters in the filename are digits representing a
  203. * number from 00 to 99. The lower the number the higher the
  204. * priority. For example, 00 is in the list before 01, and 99 is the
  205. * last item in the list. The ordering of files with the same
  206. * priority cannot be guaranteed. The pattern is the grep style
  207. * regular expression of filenames to match which is applied to the
  208. * end of the string. If you are a Solaris person, you may recognize
  209. * this as the rules for init level initialization using shell scripts
  210. * under /etc/rcX.d/
  211. */
  212. char **
  213. get_priority_filelist(const char *directory, const char *pattern)
  214. {
  215. char *basepattern = "^[0-9][0-9]";
  216. char *genericpattern = ".*"; /* used if pattern is null */
  217. char *bigpattern = 0;
  218. char **retval = 0;
  219. if (!pattern)
  220. pattern = genericpattern;
  221. bigpattern = slapi_ch_smprintf("%s%s", basepattern, pattern);
  222. retval = get_filelist(directory, bigpattern, 0, 0, 1);
  223. slapi_ch_free((void **)&bigpattern);
  224. return retval;
  225. }