41b7f1b0668f74fa193dd012169ce00728cd250a
[idzebra-moved-to-github.git] / index / dir.c
1 /* $Id: dir.c,v 1.30 2005-01-15 19:38:24 adam Exp $
2    Copyright (C) 1995-2005
3    Index Data ApS
4
5 This file is part of the Zebra server.
6
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
10 version.
11
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
15 for more details.
16
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
20 02111-1307, USA.
21 */
22
23
24 #include <stdio.h>
25 #include <string.h>
26 #include <assert.h>
27 #ifndef WIN32
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         if (errno != ENOENT && errno != EACCES)
74             exit (1);
75         return NULL;
76     }
77     entry = (struct dir_entry *) xmalloc (sizeof(*entry) * entry_max);
78     strcpy (path, rep);
79     pathpos = strlen(path);
80     if (!pathpos || path[pathpos-1] != '/')
81         path[pathpos++] = '/';
82     while ( (dent = readdir (dir)) )
83     {
84         struct stat finfo;
85         if (strcmp (dent->d_name, ".") == 0 ||
86             strcmp (dent->d_name, "..") == 0)
87             continue;
88         if (idx == entry_max-1)
89         {
90             struct dir_entry *entry_n;
91
92             entry_n = (struct dir_entry *)
93                 xmalloc (sizeof(*entry) * (entry_max += 1000));
94             memcpy (entry_n, entry, idx * sizeof(*entry));
95             xfree (entry);
96             entry = entry_n;
97         }
98         strcpy (path + pathpos, dent->d_name);
99
100         if (base && !yaz_is_abspath (path))
101         {
102             strcpy (full_rep, base);
103             strcat (full_rep, "/");
104             strcat (full_rep, path);
105             zebra_file_stat (full_rep, &finfo, follow_links);
106         }
107         else
108             zebra_file_stat (path, &finfo, follow_links);
109         switch (finfo.st_mode & S_IFMT)
110         {
111         case S_IFREG:
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);
116             idx++;
117             break;
118         case S_IFDIR:
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, "/");
124             idx++;
125             break;
126         }
127     }
128     entry[idx].name = NULL;
129     closedir (dir);
130     yaz_log (YLOG_DEBUG, "dir_close");
131     return entry;
132 }
133
134 static int dir_cmp (const void *p1, const void *p2)
135 {
136     return strcmp (((struct dir_entry *) p1)->name,
137                    ((struct dir_entry *) p2)->name);
138 }
139
140 void dir_sort (struct dir_entry *e)
141 {
142     size_t nmemb = 0;
143     while (e[nmemb].name)
144         nmemb++;
145     qsort (e, nmemb, sizeof(*e), dir_cmp); 
146 }
147
148 void dir_free (struct dir_entry **e_p)
149 {
150     size_t i = 0;
151     struct dir_entry *e = *e_p;
152
153     assert (e);
154     while (e[i].name)
155         xfree (e[i++].name);
156     xfree (e);
157     *e_p = NULL;
158 }