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