1 /* This file is part of the Zebra server.
2 Copyright (C) 1994-2010 Index Data
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 #include <sys/types.h>
35 int zebra_file_stat(const char *file_name, struct stat *buf,
40 return lstat(file_name, buf);
42 return stat(file_name, buf);
45 struct dir_entry *dir_open(const char *rep, const char *base,
52 struct dirent dent_s, *dent = &dent_s;
53 size_t entry_max = 500;
55 struct dir_entry *entry;
57 if (base && !yaz_is_abspath(rep))
59 strcpy(full_rep, base);
60 strcat(full_rep, "/");
64 strcat(full_rep, rep);
66 yaz_log(YLOG_DEBUG, "dir_open %s", full_rep);
67 if (!(dir = opendir(full_rep)))
69 yaz_log(YLOG_WARN|YLOG_ERRNO, "opendir %s", rep);
72 entry = (struct dir_entry *) xmalloc(sizeof(*entry) * entry_max);
74 pathpos = strlen(path);
75 if (!pathpos || path[pathpos-1] != '/')
76 path[pathpos++] = '/';
77 while ( (dent = readdir(dir)) )
80 if (strcmp(dent->d_name, ".") == 0 ||
81 strcmp(dent->d_name, "..") == 0)
83 if (idx == entry_max-1)
85 struct dir_entry *entry_n;
87 entry_n = (struct dir_entry *)
88 xmalloc(sizeof(*entry) * (entry_max += 1000));
89 memcpy(entry_n, entry, idx * sizeof(*entry));
93 strcpy(path + pathpos, dent->d_name);
95 if (base && !yaz_is_abspath(path))
97 strcpy(full_rep, base);
98 strcat(full_rep, "/");
99 strcat(full_rep, path);
100 zebra_file_stat(full_rep, &finfo, follow_links);
103 zebra_file_stat(path, &finfo, follow_links);
104 switch (finfo.st_mode & S_IFMT)
107 entry[idx].kind = dirs_file;
108 entry[idx].mtime = finfo.st_mtime;
109 entry[idx].name = (char *) xmalloc(strlen(dent->d_name)+1);
110 strcpy(entry[idx].name, dent->d_name);
114 entry[idx].kind = dirs_dir;
115 entry[idx].mtime = finfo.st_mtime;
116 entry[idx].name = (char *) xmalloc(strlen(dent->d_name)+2);
117 strcpy(entry[idx].name, dent->d_name);
118 strcat(entry[idx].name, "/");
123 entry[idx].name = NULL;
125 yaz_log(YLOG_DEBUG, "dir_close");
129 static int dir_cmp(const void *p1, const void *p2)
131 return strcmp(((struct dir_entry *) p1)->name,
132 ((struct dir_entry *) p2)->name);
135 void dir_sort(struct dir_entry *e)
138 while (e[nmemb].name)
140 qsort(e, nmemb, sizeof(*e), dir_cmp);
143 void dir_free(struct dir_entry **e_p)
146 struct dir_entry *e = *e_p;
157 * c-file-style: "Stroustrup"
158 * indent-tabs-mode: nil
160 * vim: shiftwidth=4 tabstop=8 expandtab