1 /* $Id: dir.c,v 1.24 2002-09-03 11:44:54 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
31 #include <sys/types.h>
38 int zebra_file_stat (const char *file_name, struct stat *buf,
42 return stat(file_name, buf);
43 return lstat(file_name, buf);
46 struct dir_entry *dir_open (const char *rep, const char *base,
54 size_t entry_max = 500;
56 struct dir_entry *entry;
58 if (base && !yaz_is_abspath(rep))
60 strcpy (full_rep, base);
61 strcat (full_rep, "/");
65 strcat (full_rep, rep);
67 logf (LOG_LOG, "dir_open %s", full_rep);
68 if (!(dir = opendir(full_rep)))
70 logf (LOG_WARN|LOG_ERRNO, "opendir %s", rep);
71 if (errno != ENOENT && errno != EACCES)
75 entry = (struct dir_entry *) xmalloc (sizeof(*entry) * entry_max);
77 pathpos = strlen(path);
78 if (!pathpos || path[pathpos-1] != '/')
79 path[pathpos++] = '/';
80 while ((dent = readdir (dir)))
83 if (strcmp (dent->d_name, ".") == 0 ||
84 strcmp (dent->d_name, "..") == 0)
86 if (idx == entry_max-1)
88 struct dir_entry *entry_n;
90 entry_n = (struct dir_entry *)
91 xmalloc (sizeof(*entry) * (entry_max += 1000));
92 memcpy (entry_n, entry, idx * sizeof(*entry));
96 strcpy (path + pathpos, dent->d_name);
98 if (base && !yaz_is_abspath (path))
100 strcpy (full_rep, base);
101 strcat (full_rep, "/");
102 strcat (full_rep, path);
103 zebra_file_stat (full_rep, &finfo, follow_links);
106 zebra_file_stat (path, &finfo, follow_links);
107 switch (finfo.st_mode & S_IFMT)
110 entry[idx].kind = dirs_file;
111 entry[idx].mtime = finfo.st_mtime;
112 entry[idx].name = (char *) xmalloc (strlen(dent->d_name)+1);
113 strcpy (entry[idx].name, dent->d_name);
117 entry[idx].kind = dirs_dir;
118 entry[idx].mtime = finfo.st_mtime;
119 entry[idx].name = (char *) xmalloc (strlen(dent->d_name)+2);
120 strcpy (entry[idx].name, dent->d_name);
121 strcat (entry[idx].name, "/");
126 entry[idx].name = NULL;
128 logf (LOG_DEBUG, "dir_close");
132 static int dir_cmp (const void *p1, const void *p2)
134 return strcmp (((struct dir_entry *) p1)->name,
135 ((struct dir_entry *) p2)->name);
138 void dir_sort (struct dir_entry *e)
141 while (e[nmemb].name)
143 qsort (e, nmemb, sizeof(*e), dir_cmp);
146 void dir_free (struct dir_entry **e_p)
149 struct dir_entry *e = *e_p;