Use readdir_r on threaded systems
[idzebra-moved-to-github.git] / index / dir.c
1 /* $Id: dir.c,v 1.27 2002-10-30 12:58:21 adam Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
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     logf (LOG_DEBUG, "dir_open %s", full_rep);
70     if (!(dir = opendir(full_rep)))
71     {
72         logf (LOG_WARN|LOG_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 (
83 #if _REENTRANT
84                     (readdir_r (dir, &dent_s, &dent) == 0 && dent)
85 #else
86                     (dent = readdir (dir))
87 #endif
88                     )
89     {
90         struct stat finfo;
91         if (strcmp (dent->d_name, ".") == 0 ||
92             strcmp (dent->d_name, "..") == 0)
93             continue;
94         if (idx == entry_max-1)
95         {
96             struct dir_entry *entry_n;
97
98             entry_n = (struct dir_entry *)
99                 xmalloc (sizeof(*entry) * (entry_max += 1000));
100             memcpy (entry_n, entry, idx * sizeof(*entry));
101             xfree (entry);
102             entry = entry_n;
103         }
104         strcpy (path + pathpos, dent->d_name);
105
106         if (base && !yaz_is_abspath (path))
107         {
108             strcpy (full_rep, base);
109             strcat (full_rep, "/");
110             strcat (full_rep, path);
111             zebra_file_stat (full_rep, &finfo, follow_links);
112         }
113         else
114             zebra_file_stat (path, &finfo, follow_links);
115         switch (finfo.st_mode & S_IFMT)
116         {
117         case S_IFREG:
118             entry[idx].kind = dirs_file;
119             entry[idx].mtime = finfo.st_mtime;
120             entry[idx].name = (char *) xmalloc (strlen(dent->d_name)+1);
121             strcpy (entry[idx].name, dent->d_name);
122             idx++;
123             break;
124         case S_IFDIR:
125             entry[idx].kind = dirs_dir;
126             entry[idx].mtime = finfo.st_mtime;
127             entry[idx].name = (char *) xmalloc (strlen(dent->d_name)+2);
128             strcpy (entry[idx].name, dent->d_name);
129             strcat (entry[idx].name, "/");
130             idx++;
131             break;
132         }
133     }
134     entry[idx].name = NULL;
135     closedir (dir);
136     logf (LOG_DEBUG, "dir_close");
137     return entry;
138 }
139
140 static int dir_cmp (const void *p1, const void *p2)
141 {
142     return strcmp (((struct dir_entry *) p1)->name,
143                    ((struct dir_entry *) p2)->name);
144 }
145
146 void dir_sort (struct dir_entry *e)
147 {
148     size_t nmemb = 0;
149     while (e[nmemb].name)
150         nmemb++;
151     qsort (e, nmemb, sizeof(*e), dir_cmp); 
152 }
153
154 void dir_free (struct dir_entry **e_p)
155 {
156     size_t i = 0;
157     struct dir_entry *e = *e_p;
158
159     assert (e);
160     while (e[i].name)
161         xfree (e[i++].name);
162     xfree (e);
163     *e_p = NULL;
164 }