summaryrefslogtreecommitdiff
path: root/file_io/unix/dir.c
blob: ac992d7836a4e92263d958e8cb25abde5902ac5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/* ====================================================================
 * Copyright (c) 1999 The Apache Group.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer. 
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. All advertising materials mentioning features or use of this
 *    software must display the following acknowledgment:
 *    "This product includes software developed by the Apache Group
 *    for use in the Apache HTTP server project (http://www.apache.org/)."
 *
 * 4. The names "Apache Server" and "Apache Group" must not be used to
 *    endorse or promote products derived from this software without
 *    prior written permission. For written permission, please contact
 *    apache@apache.org.
 *
 * 5. Products derived from this software may not be called "Apache"
 *    nor may "Apache" appear in their names without prior written
 *    permission of the Apache Group.
 *
 * 6. Redistributions of any form whatsoever must retain the following
 *    acknowledgment:
 *    "This product includes software developed by the Apache Group
 *    for use in the Apache HTTP server project (http://www.apache.org/)."
 *
 * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE APACHE GROUP OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Group.
 * For more information on the Apache Group and the Apache HTTP server
 * project, please see <http://www.apache.org/>.
 *
 */

#include <sys/types.h>
#include <errno.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
#include "fileio.h"
#include "apr_file_io.h"
#include "apr_lib.h"
#include "apr_portable.h"

ap_status_t dir_cleanup(void *thedir)
{
    struct dir_t *dir = thedir;
    if (closedir(dir->dirstruct) == 0) {
        return APR_SUCCESS;
    }
    else {
        return errno;
    }
} 

/* ***APRDOC********************************************************
 * ap_status_t ap_opendir(ap_context_t *, char *, ap_dir_t **)
 *    Open the specified directory. 
 * arg 1) The context to use.
 * arg 2) The full path to the directory (use / on all systems)
 * arg 3) The opened directory descriptor.
 */                        
ap_status_t ap_opendir(ap_context_t *cont, const char *dirname, struct dir_t **new)
{
    (*new) = (struct dir_t *)ap_palloc(cont, sizeof(struct dir_t));

    (*new)->cntxt = cont;
    (*new)->dirname = strdup(dirname);
    (*new)->dirstruct = opendir(dirname);
    (*new)->entry = NULL;

    if ((*new)->dirstruct == NULL) {
        (*new)->dirstruct = NULL;
        return errno;
    }    
    else {
        ap_register_cleanup((*new)->cntxt, (void *)(*new), dir_cleanup, NULL);
        return APR_SUCCESS;
    }
}

/* ***APRDOC********************************************************
 * ap_status_t ap_closedir(ap_dir_t *)
 *    close the specified directory. 
 * arg 1) the directory descriptor to close.
 */                        
ap_status_t ap_closedir(struct dir_t *thedir)
{
    ap_status_t rv;

    if ((rv = dir_cleanup(thedir)) == APR_SUCCESS) {
        ap_kill_cleanup(thedir->cntxt, thedir, dir_cleanup);
        return APR_SUCCESS;
    }
    return rv;
}

/* ***APRDOC********************************************************
 * ap_status_t ap_readdir(ap_dir_t *)
 *    Read the next entry from the specified directory. 
 * arg 1) the directory descriptor to read from, and fill out.
 * NOTE: All systems return . and .. as the first two files.
 */                        
ap_status_t ap_readdir(struct dir_t *thedir)
{
    thedir->entry = readdir(thedir->dirstruct);
    if (thedir->entry == NULL) {
        return errno;
    }    
    return APR_SUCCESS;
}

/* ***APRDOC********************************************************
 * ap_status_t ap_readdir(ap_dir_t *)
 *    Rewind the directory to the first entry. 
 * arg 1) the directory descriptor to rewind.
 */                        
ap_status_t ap_rewinddir(struct dir_t *thedir)
{
    rewinddir(thedir->dirstruct);
    return APR_SUCCESS;
}

/* ***APRDOC********************************************************
 * ap_status_t ap_make_dir(ap_context_t *, const char *, ap_fileperms_t)
 *    Create a new directory on the file system. 
 * arg 1) the context to use.
 * arg 2) the path for the directory to be created.  (use / on all systems)
 * arg 3) Permissions for the new direcoty.
 */                        
ap_status_t ap_make_dir(ap_context_t *cont, const char *path, ap_fileperms_t perm)
{
    mode_t mode = get_fileperms(perm);
    if (mkdir(path, mode) == 0) {
        return APR_SUCCESS;
    }
    else {
        return errno;
    }
}

/* ***APRDOC********************************************************
 * ap_status_t ap_remove_dir(ap_context_t *, const char *)
 *    Remove directory from the file system. 
 * arg 1) the context to use.
 * arg 2) the path for the directory to be removed.  (use / on all systems)
 */                        
ap_status_t ap_remove_dir(ap_context_t *cont, const char *path)
{
    if (rmdir(path) == 0) {
        return APR_SUCCESS;
    }
    else {
        return errno;
    }
}

/* ***APRDOC********************************************************
 * ap_status_t ap_dir_entry_size(ap_dir_t *, ap_ssize_t *)
 *    Get the size of the current directory entry. 
 * arg 1) the currently open directory.
 * arg 2) the size of the directory entry. 
 */                        
ap_status_t ap_dir_entry_size(struct dir_t *thedir, ap_ssize_t *size)
{
    struct stat filestat;
    char *fname = NULL;    

    if (thedir->entry == NULL) {
        *size = -1;
        return APR_ENOFILE;
    }
    fname = ap_pstrcat(thedir->cntxt, thedir->dirname, "/", 
                       thedir->entry->d_name, NULL);
    if (stat(fname, &filestat) == -1) {
        *size = -1;
        return APR_ENOSTAT;
    }
    
    *size = filestat.st_size;
    return APR_SUCCESS;
}

/* ***APRDOC********************************************************
 * ap_status_t ap_dir_entry_mtime(ap_dir_t *, time_t *)
 *    Get the last modified time of the current directory entry. 
 * arg 1) the currently open directory.
 * arg 2) the last modified time of the directory entry. 
 */                        
ap_status_t ap_dir_entry_mtime(struct dir_t *thedir, time_t *time)
{
    struct stat filestat;
    char *fname = NULL;

    if (thedir->entry == NULL) {
        *time = -1;
        return APR_ENOFILE;
    }

    fname = ap_pstrcat(thedir->cntxt, thedir->dirname, "/", 
                       thedir->entry->d_name, NULL);
    if (stat(fname, &filestat) == -1) {
        *time = -1;
        return APR_ENOSTAT;
    }
    
    *time = filestat.st_mtime;
    return APR_SUCCESS;
}
 
/* ***APRDOC********************************************************
 * ap_status_t ap_dir_entry_ftype(ap_dir_t *, ap_filetype_e *)
 *    Get the file type of the current directory entry. 
 * arg 1) the currently open directory.
 * arg 2) the file type of the directory entry. 
 */                        
ap_status_t ap_dir_entry_ftype(struct dir_t *thedir, ap_filetype_e *type)
{
    struct stat filestat;
    char *fname = NULL;

    if (thedir->entry == NULL) {
        *type = APR_REG;
        return APR_ENOFILE;
    }

    fname = ap_pstrcat(thedir->cntxt, thedir->dirname, "/", 
                       thedir->entry->d_name, NULL);
    if (stat(fname, &filestat) == -1) {
        *type = APR_REG;
        return APR_ENOSTAT;
    }

    if (S_ISREG(filestat.st_mode))
        *type = APR_REG;    
    if (S_ISDIR(filestat.st_mode))
        *type = APR_DIR;    
    if (S_ISCHR(filestat.st_mode))
        *type = APR_CHR;    
    if (S_ISBLK(filestat.st_mode))
        *type = APR_BLK;    
    if (S_ISFIFO(filestat.st_mode))
        *type = APR_PIPE;    
    if (S_ISLNK(filestat.st_mode))
        *type = APR_LNK;    
    if (S_ISSOCK(filestat.st_mode))
        *type = APR_SOCK;    
    return APR_SUCCESS;
}

/* ***APRDOC********************************************************
 * ap_status_t ap_dir_entry_filename(ap_dir_t *, char **) 
 *    Get the file name of the current directory entry. 
 * arg 1) the currently open directory.
 * arg 2) the file name of the directory entry. 
 */                        
ap_status_t ap_get_dir_filename(struct dir_t *thedir, char **new)
{
    (*new) = ap_pstrdup(thedir->cntxt, thedir->entry->d_name);
    return APR_SUCCESS;
}

/* ***APRDOC********************************************************
 * ap_status_t ap_get_os_dir(ap_dir_t *, ap_os_dir_t *)
 *    convert the dir from apr type to os specific type.
 * arg 1) The apr dir to convert.
 * arg 2) The os specific dir we are converting to
 */   
ap_status_t ap_get_os_dir(struct dir_t *dir, ap_os_dir_t *thedir)
{
    if (dir == NULL) {
        return APR_ENODIR;
    }
    thedir = dir->dirstruct;
    return APR_SUCCESS;
}

/* ***APRDOC********************************************************
 * ap_status_t ap_get_os_dir(ap_dir_t *, ap_os_dir_t *)
 *    convert the dir from os specific type to apr type.
 * arg 1) The os specific dir to convert
 * arg 2) The apr dir we are converting to.
 */
ap_status_t ap_put_os_dir(ap_context_t *cont, struct dir_t **dir,
                            ap_os_dir_t *thedir)
{
    if (cont == NULL) {
        return APR_ENOCONT;
    }
    if ((*dir) == NULL) {
        (*dir) = (struct dir_t *)ap_palloc(cont, sizeof(struct dir_t));
        (*dir)->cntxt = cont;
    }
    (*dir)->dirstruct = thedir;
    return APR_SUCCESS;
}