Happy new year.
[idzebra-moved-to-github.git] / index / dir.c
1 /* This file is part of the Zebra server.
2    Copyright (C) 1994-2011 Index Data
3
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
7 version.
8
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
12 for more details.
13
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
17
18 */
19
20
21 #include <stdio.h>
22 #include <string.h>
23 #include <assert.h>
24 #if HAVE_UNISTD_H
25 #include <unistd.h>
26 #endif
27 #include <direntz.h>
28 #include <sys/types.h>
29 #include <errno.h>
30 #include <fcntl.h>
31
32 #include "index.h"
33
34
35 int zebra_file_stat(const char *file_name, struct stat *buf,
36                      int follow_links)
37 {
38 #ifndef WIN32
39     if (!follow_links)
40         return lstat(file_name, buf);
41 #endif
42     return stat(file_name, buf);
43 }
44
45 struct dir_entry *dir_open(const char *rep, const char *base,
46                             int follow_links)
47 {
48     DIR *dir;
49     char path[1024];
50     char full_rep[1024];
51     size_t pathpos;
52     struct dirent dent_s, *dent = &dent_s;
53     size_t entry_max = 500;
54     size_t idx = 0;
55     struct dir_entry *entry;
56
57     if (base && !yaz_is_abspath(rep))
58     {
59         strcpy(full_rep, base);
60         strcat(full_rep, "/");
61     }
62     else
63         *full_rep = '\0';
64     strcat(full_rep, rep);
65
66     yaz_log(YLOG_DEBUG, "dir_open %s", full_rep);
67     if (!(dir = opendir(full_rep)))
68     {
69         yaz_log(YLOG_WARN|YLOG_ERRNO, "opendir %s", rep);
70         return NULL;
71     }
72     entry = (struct dir_entry *) xmalloc(sizeof(*entry) * entry_max);
73     strcpy(path, rep);
74     pathpos = strlen(path);
75     if (!pathpos || path[pathpos-1] != '/')
76         path[pathpos++] = '/';
77     while ( (dent = readdir(dir)) )
78     {
79         struct stat finfo;
80         if (strcmp(dent->d_name, ".") == 0 ||
81             strcmp(dent->d_name, "..") == 0)
82             continue;
83         if (idx == entry_max-1)
84         {
85             struct dir_entry *entry_n;
86
87             entry_n = (struct dir_entry *)
88                 xmalloc(sizeof(*entry) * (entry_max += 1000));
89             memcpy(entry_n, entry, idx * sizeof(*entry));
90             xfree(entry);
91             entry = entry_n;
92         }
93         strcpy(path + pathpos, dent->d_name);
94
95         if (base && !yaz_is_abspath(path))
96         {
97             strcpy(full_rep, base);
98             strcat(full_rep, "/");
99             strcat(full_rep, path);
100             zebra_file_stat(full_rep, &finfo, follow_links);
101         }
102         else
103             zebra_file_stat(path, &finfo, follow_links);
104         switch (finfo.st_mode & S_IFMT)
105         {
106         case S_IFREG:
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);
111             idx++;
112             break;
113         case S_IFDIR:
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, "/");
119             idx++;
120             break;
121         }
122     }
123     entry[idx].name = NULL;
124     closedir(dir);
125     yaz_log(YLOG_DEBUG, "dir_close");
126     return entry;
127 }
128
129 static int dir_cmp(const void *p1, const void *p2)
130 {
131     return strcmp(((struct dir_entry *) p1)->name,
132                   ((struct dir_entry *) p2)->name);
133 }
134
135 void dir_sort(struct dir_entry *e)
136 {
137     size_t nmemb = 0;
138     while (e[nmemb].name)
139         nmemb++;
140     qsort(e, nmemb, sizeof(*e), dir_cmp); 
141 }
142
143 void dir_free(struct dir_entry **e_p)
144 {
145     size_t i = 0;
146     struct dir_entry *e = *e_p;
147
148     assert(e);
149     while (e[i].name)
150         xfree(e[i++].name);
151     xfree(e);
152     *e_p = NULL;
153 }
154 /*
155  * Local variables:
156  * c-basic-offset: 4
157  * c-file-style: "Stroustrup"
158  * indent-tabs-mode: nil
159  * End:
160  * vim: shiftwidth=4 tabstop=8 expandtab
161  */
162