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