Uses stat and not lstat so that file traversal follows symbolic links.
[idzebra-moved-to-github.git] / index / dir.c
1 /*
2  * Copyright (C) 1994-1998, Index Data
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: dir.c,v $
7  * Revision 1.19  1998-11-03 10:16:11  adam
8  * Uses stat and not lstat so that file traversal follows symbolic links.
9  *
10  * Revision 1.18  1997/09/25 14:55:33  adam
11  * Windows port uses stat and not lstat.
12  *
13  * Revision 1.17  1997/09/09 13:38:06  adam
14  * Partial port to WIN95/NT.
15  *
16  * Revision 1.16  1996/10/29 14:06:45  adam
17  * Include zebrautl.h instead of alexutil.h.
18  *
19  * Revision 1.15  1996/06/04 10:18:58  adam
20  * Minor changes - removed include of ctype.h.
21  *
22  * Revision 1.14  1996/04/09  06:49:18  adam
23  * Traversal of directories doesn't follow symbolic links.
24  *
25  * Revision 1.13  1996/03/21 14:50:08  adam
26  * File update uses modify-time instead of change-time.
27  *
28  * Revision 1.12  1996/02/05  12:29:55  adam
29  * Logging reduced a bit.
30  * The remaining running time is estimated during register merge.
31  *
32  * Revision 1.11  1995/11/20  16:59:44  adam
33  * New update method: the 'old' keys are saved for each records.
34  *
35  * Revision 1.10  1995/11/20  11:56:22  adam
36  * Work on new traversal.
37  *
38  * Revision 1.9  1995/10/30  13:42:12  adam
39  * Added errno.h
40  *
41  * Revision 1.8  1995/10/10  13:59:23  adam
42  * Function rset_open changed its wflag parameter to general flags.
43  *
44  * Revision 1.7  1995/09/28  09:19:40  adam
45  * xfree/xmalloc used everywhere.
46  * Extract/retrieve method seems to work for text records.
47  *
48  * Revision 1.6  1995/09/08  14:52:26  adam
49  * Minor changes. Dictionary is lower case now.
50  *
51  * Revision 1.5  1995/09/06  16:11:16  adam
52  * Option: only one word key per file.
53  *
54  * Revision 1.4  1995/09/04  12:33:41  adam
55  * Various cleanup. YAZ util used instead.
56  *
57  * Revision 1.3  1995/09/01  14:06:35  adam
58  * Split of work into more files.
59  *
60  * Revision 1.2  1995/09/01  10:57:07  adam
61  * Minor changes.
62  *
63  * Revision 1.1  1995/09/01  10:34:51  adam
64  * Added dir.c
65  *
66  */
67 #include <stdio.h>
68 #include <string.h>
69 #include <assert.h>
70 #ifndef WINDOWS
71 #include <unistd.h>
72 #endif
73 #include <direntz.h>
74 #include <sys/stat.h>
75 #include <sys/types.h>
76 #include <errno.h>
77 #include <fcntl.h>
78
79 #include "index.h"
80
81 struct dir_entry *dir_open (const char *rep)
82 {
83     DIR *dir;
84     char path[256];
85     size_t pathpos;
86     struct dirent *dent;
87     size_t entry_max = 500;
88     size_t idx = 0;
89     struct dir_entry *entry;
90
91     logf (LOG_DEBUG, "dir_open %s", rep);
92     if (!(dir = opendir(rep)))
93     {
94         logf (LOG_WARN|LOG_ERRNO, "opendir %s", rep);
95         if (errno != ENOENT && errno != EACCES)
96             exit (1);
97         return NULL;
98     }
99     entry = xmalloc (sizeof(*entry) * entry_max);
100     strcpy (path, rep);
101     pathpos = strlen(path);
102     if (!pathpos || path[pathpos-1] != '/')
103         path[pathpos++] = '/';
104     while ((dent = readdir (dir)))
105     {
106         struct stat finfo;
107         if (strcmp (dent->d_name, ".") == 0 ||
108             strcmp (dent->d_name, "..") == 0)
109             continue;
110         if (idx == entry_max-1)
111         {
112             struct dir_entry *entry_n;
113
114             entry_n = xmalloc (sizeof(*entry) * (entry_max += 1000));
115             memcpy (entry_n, entry, idx * sizeof(*entry));
116             xfree (entry);
117             entry = entry_n;
118         }
119         strcpy (path + pathpos, dent->d_name);
120         stat (path, &finfo);
121         switch (finfo.st_mode & S_IFMT)
122         {
123         case S_IFREG:
124             entry[idx].kind = dirs_file;
125             entry[idx].mtime = finfo.st_mtime;
126             entry[idx].name = xmalloc (strlen(dent->d_name)+1);
127             strcpy (entry[idx].name, dent->d_name);
128             idx++;
129             break;
130         case S_IFDIR:
131             entry[idx].kind = dirs_dir;
132             entry[idx].mtime = finfo.st_mtime;
133             entry[idx].name = xmalloc (strlen(dent->d_name)+2);
134             strcpy (entry[idx].name, dent->d_name);
135             strcat (entry[idx].name, "/");
136             idx++;
137             break;
138         }
139     }
140     entry[idx].name = NULL;
141     closedir (dir);
142     logf (LOG_DEBUG, "dir_close");
143     return entry;
144 }
145
146 static int dir_cmp (const void *p1, const void *p2)
147 {
148     return strcmp (((struct dir_entry *) p1)->name,
149                    ((struct dir_entry *) p2)->name);
150 }
151
152 void dir_sort (struct dir_entry *e)
153 {
154     size_t nmemb = 0;
155     while (e[nmemb].name)
156         nmemb++;
157     qsort (e, nmemb, sizeof(*e), dir_cmp); 
158 }
159
160 void dir_free (struct dir_entry **e_p)
161 {
162     size_t i = 0;
163     struct dir_entry *e = *e_p;
164
165     assert (e);
166     while (e[i].name)
167         xfree (e[i++].name);
168     xfree (e);
169     *e_p = NULL;
170 }