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