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