1 /* $Id: dir.c,v 1.33 2006-05-10 08:13:20 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);
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 yaz_log (YLOG_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;
160 * indent-tabs-mode: nil
162 * vim: shiftwidth=4 tabstop=8 expandtab