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