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