Partial port to WIN95/NT.
[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.17  1997-09-09 13:38:06  adam
8  * Partial port to WIN95/NT.
9  *
10  * Revision 1.16  1996/10/29 14:06:45  adam
11  * Include zebrautl.h instead of alexutil.h.
12  *
13  * Revision 1.15  1996/06/04 10:18:58  adam
14  * Minor changes - removed include of ctype.h.
15  *
16  * Revision 1.14  1996/04/09  06:49:18  adam
17  * Traversal of directories doesn't follow symbolic links.
18  *
19  * Revision 1.13  1996/03/21 14:50:08  adam
20  * File update uses modify-time instead of change-time.
21  *
22  * Revision 1.12  1996/02/05  12:29:55  adam
23  * Logging reduced a bit.
24  * The remaining running time is estimated during register merge.
25  *
26  * Revision 1.11  1995/11/20  16:59:44  adam
27  * New update method: the 'old' keys are saved for each records.
28  *
29  * Revision 1.10  1995/11/20  11:56:22  adam
30  * Work on new traversal.
31  *
32  * Revision 1.9  1995/10/30  13:42:12  adam
33  * Added errno.h
34  *
35  * Revision 1.8  1995/10/10  13:59:23  adam
36  * Function rset_open changed its wflag parameter to general flags.
37  *
38  * Revision 1.7  1995/09/28  09:19:40  adam
39  * xfree/xmalloc used everywhere.
40  * Extract/retrieve method seems to work for text records.
41  *
42  * Revision 1.6  1995/09/08  14:52:26  adam
43  * Minor changes. Dictionary is lower case now.
44  *
45  * Revision 1.5  1995/09/06  16:11:16  adam
46  * Option: only one word key per file.
47  *
48  * Revision 1.4  1995/09/04  12:33:41  adam
49  * Various cleanup. YAZ util used instead.
50  *
51  * Revision 1.3  1995/09/01  14:06:35  adam
52  * Split of work into more files.
53  *
54  * Revision 1.2  1995/09/01  10:57:07  adam
55  * Minor changes.
56  *
57  * Revision 1.1  1995/09/01  10:34:51  adam
58  * Added dir.c
59  *
60  */
61 #include <stdio.h>
62 #include <string.h>
63 #include <assert.h>
64 #ifndef WINDOWS
65 #include <unistd.h>
66 #endif
67 #include <direntz.h>
68 #include <sys/stat.h>
69 #include <sys/types.h>
70 #include <errno.h>
71 #include <fcntl.h>
72
73 #include "index.h"
74
75 struct dir_entry *dir_open (const char *rep)
76 {
77     DIR *dir;
78     char path[256];
79     size_t pathpos;
80     struct dirent *dent;
81     size_t entry_max = 500;
82     size_t idx = 0;
83     struct dir_entry *entry;
84
85     logf (LOG_DEBUG, "dir_open %s", rep);
86     if (!(dir = opendir(rep)))
87     {
88         logf (LOG_WARN|LOG_ERRNO, "opendir %s", rep);
89         if (errno != ENOENT && errno != EACCES)
90             exit (1);
91         return NULL;
92     }
93     entry = xmalloc (sizeof(*entry) * entry_max);
94     strcpy (path, rep);
95     pathpos = strlen(path);
96     if (!pathpos || path[pathpos-1] != '/')
97         path[pathpos++] = '/';
98     while ((dent = readdir (dir)))
99     {
100         struct stat finfo;
101         if (strcmp (dent->d_name, ".") == 0 ||
102             strcmp (dent->d_name, "..") == 0)
103             continue;
104         if (idx == entry_max-1)
105         {
106             struct dir_entry *entry_n;
107
108             entry_n = xmalloc (sizeof(*entry) * (entry_max += 1000));
109             memcpy (entry_n, entry, idx * sizeof(*entry));
110             xfree (entry);
111             entry = entry_n;
112         }
113         strcpy (path + pathpos, dent->d_name);
114         lstat (path, &finfo);
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 = 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 = 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 }