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