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