1 /* $Id: dir.c,v 1.31 2005-06-14 20:28:53 adam Exp $
2 Copyright (C) 1995-2005
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,
43 return lstat(file_name, buf);
45 return stat(file_name, buf);
48 struct dir_entry *dir_open (const char *rep, const char *base,
55 struct dirent dent_s, *dent = &dent_s;
56 size_t entry_max = 500;
58 struct dir_entry *entry;
60 if (base && !yaz_is_abspath(rep))
62 strcpy (full_rep, base);
63 strcat (full_rep, "/");
67 strcat (full_rep, rep);
69 yaz_log (YLOG_DEBUG, "dir_open %s", full_rep);
70 if (!(dir = opendir(full_rep)))
72 yaz_log (YLOG_WARN|YLOG_ERRNO, "opendir %s", rep);
73 if (errno != ENOENT && errno != EACCES)
77 entry = (struct dir_entry *) xmalloc (sizeof(*entry) * entry_max);
79 pathpos = strlen(path);
80 if (!pathpos || path[pathpos-1] != '/')
81 path[pathpos++] = '/';
82 while ( (dent = readdir (dir)) )
85 if (strcmp (dent->d_name, ".") == 0 ||
86 strcmp (dent->d_name, "..") == 0)
88 if (idx == entry_max-1)
90 struct dir_entry *entry_n;
92 entry_n = (struct dir_entry *)
93 xmalloc (sizeof(*entry) * (entry_max += 1000));
94 memcpy (entry_n, entry, idx * sizeof(*entry));
98 strcpy (path + pathpos, dent->d_name);
100 if (base && !yaz_is_abspath (path))
102 strcpy (full_rep, base);
103 strcat (full_rep, "/");
104 strcat (full_rep, path);
105 zebra_file_stat (full_rep, &finfo, follow_links);
108 zebra_file_stat (path, &finfo, follow_links);
109 switch (finfo.st_mode & S_IFMT)
112 entry[idx].kind = dirs_file;
113 entry[idx].mtime = finfo.st_mtime;
114 entry[idx].name = (char *) xmalloc (strlen(dent->d_name)+1);
115 strcpy (entry[idx].name, dent->d_name);
119 entry[idx].kind = dirs_dir;
120 entry[idx].mtime = finfo.st_mtime;
121 entry[idx].name = (char *) xmalloc (strlen(dent->d_name)+2);
122 strcpy (entry[idx].name, dent->d_name);
123 strcat (entry[idx].name, "/");
128 entry[idx].name = NULL;
130 yaz_log (YLOG_DEBUG, "dir_close");
134 static int dir_cmp (const void *p1, const void *p2)
136 return strcmp (((struct dir_entry *) p1)->name,
137 ((struct dir_entry *) p2)->name);
140 void dir_sort (struct dir_entry *e)
143 while (e[nmemb].name)
145 qsort (e, nmemb, sizeof(*e), dir_cmp);
148 void dir_free (struct dir_entry **e_p)
151 struct dir_entry *e = *e_p;