Towards GPL
[idzebra-moved-to-github.git] / index / dir.c
1 /* $Id: dir.c,v 1.23 2002-08-02 19:26:55 adam Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
3    Index Data Aps
4
5 This file is part of the Zebra server.
6
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
21 */
22
23
24 #include <stdio.h>
25 #include <string.h>
26 #include <assert.h>
27 #ifndef WIN32
28 #include <unistd.h>
29 #endif
30 #include <direntz.h>
31 #include <sys/stat.h>
32 #include <sys/types.h>
33 #include <errno.h>
34 #include <fcntl.h>
35
36 #include "index.h"
37
38 struct dir_entry *dir_open (const char *rep, const char *base)
39 {
40     DIR *dir;
41     char path[1024];
42     char full_rep[1024];
43     size_t pathpos;
44     struct dirent *dent;
45     size_t entry_max = 500;
46     size_t idx = 0;
47     struct dir_entry *entry;
48
49     if (base && !yaz_is_abspath(rep))
50     {
51         strcpy (full_rep, base);
52         strcat (full_rep, "/");
53     }
54     else
55         *full_rep = '\0';
56     strcat (full_rep, rep);
57
58     logf (LOG_LOG, "dir_open %s", full_rep);
59     if (!(dir = opendir(full_rep)))
60     {
61         logf (LOG_WARN|LOG_ERRNO, "opendir %s", rep);
62         if (errno != ENOENT && errno != EACCES)
63             exit (1);
64         return NULL;
65     }
66     entry = (struct dir_entry *) xmalloc (sizeof(*entry) * entry_max);
67     strcpy (path, rep);
68     pathpos = strlen(path);
69     if (!pathpos || path[pathpos-1] != '/')
70         path[pathpos++] = '/';
71     while ((dent = readdir (dir)))
72     {
73         struct stat finfo;
74         if (strcmp (dent->d_name, ".") == 0 ||
75             strcmp (dent->d_name, "..") == 0)
76             continue;
77         if (idx == entry_max-1)
78         {
79             struct dir_entry *entry_n;
80
81             entry_n = (struct dir_entry *)
82                 xmalloc (sizeof(*entry) * (entry_max += 1000));
83             memcpy (entry_n, entry, idx * sizeof(*entry));
84             xfree (entry);
85             entry = entry_n;
86         }
87         strcpy (path + pathpos, dent->d_name);
88
89         if (base && !yaz_is_abspath (path))
90         {
91             strcpy (full_rep, base);
92             strcat (full_rep, "/");
93             strcat (full_rep, path);
94             stat (full_rep, &finfo);
95         }
96         else
97             stat (path, &finfo);
98         switch (finfo.st_mode & S_IFMT)
99         {
100         case S_IFREG:
101             entry[idx].kind = dirs_file;
102             entry[idx].mtime = finfo.st_mtime;
103             entry[idx].name = (char *) xmalloc (strlen(dent->d_name)+1);
104             strcpy (entry[idx].name, dent->d_name);
105             idx++;
106             break;
107         case S_IFDIR:
108             entry[idx].kind = dirs_dir;
109             entry[idx].mtime = finfo.st_mtime;
110             entry[idx].name = (char *) xmalloc (strlen(dent->d_name)+2);
111             strcpy (entry[idx].name, dent->d_name);
112             strcat (entry[idx].name, "/");
113             idx++;
114             break;
115         }
116     }
117     entry[idx].name = NULL;
118     closedir (dir);
119     logf (LOG_DEBUG, "dir_close");
120     return entry;
121 }
122
123 static int dir_cmp (const void *p1, const void *p2)
124 {
125     return strcmp (((struct dir_entry *) p1)->name,
126                    ((struct dir_entry *) p2)->name);
127 }
128
129 void dir_sort (struct dir_entry *e)
130 {
131     size_t nmemb = 0;
132     while (e[nmemb].name)
133         nmemb++;
134     qsort (e, nmemb, sizeof(*e), dir_cmp); 
135 }
136
137 void dir_free (struct dir_entry **e_p)
138 {
139     size_t i = 0;
140     struct dir_entry *e = *e_p;
141
142     assert (e);
143     while (e[i].name)
144         xfree (e[i++].name);
145     xfree (e);
146     *e_p = NULL;
147 }