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