2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 2004
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 directory listing functions for posix backend
25 #include "vfs_posix.h"
26 #include "system/dir.h"
28 #define NAME_CACHE_SIZE 100
30 struct name_cache_entry {
36 struct pvfs_state *pvfs;
42 const char *unix_path;
44 struct name_cache_entry *name_cache;
45 uint32_t name_cache_index;
48 /* these three numbers are chosen to minimise the chances of a bad
49 interaction with the OS value for 'end of directory'. On IRIX
50 telldir() returns 0xFFFFFFFF at the end of a directory, and that
51 caused an infinite loop with the original values of 0,1,2
53 On XFS on linux telldir returns 0x7FFFFFFF at the end of a
54 directory. Thus the change from 0x80000002, as otherwise
55 0x7FFFFFFF+0x80000002==1==DIR_OFFSET_DOTDOT
57 #define DIR_OFFSET_DOT 0
58 #define DIR_OFFSET_DOTDOT 1
59 #define DIR_OFFSET_BASE 0x80000022
62 a special directory listing case where the pattern has no wildcard. We can just do a single stat()
63 thus avoiding the more expensive directory scan
65 static NTSTATUS pvfs_list_no_wildcard(struct pvfs_state *pvfs, struct pvfs_filename *name,
66 const char *pattern, struct pvfs_dir *dir)
69 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
73 dir->no_wildcard = True;
74 dir->end_of_search = False;
75 dir->unix_path = talloc_strdup(dir, name->full_name);
76 if (!dir->unix_path) {
77 return NT_STATUS_NO_MEMORY;
80 dir->single_name = talloc_strdup(dir, pattern);
81 if (!dir->single_name) {
82 return NT_STATUS_NO_MEMORY;
93 destroy an open search
95 static int pvfs_dirlist_destructor(struct pvfs_dir *dir)
97 if (dir->dir) closedir(dir->dir);
102 start to read a directory
104 if the pattern matches no files then we return NT_STATUS_OK, with dir->count = 0
106 NTSTATUS pvfs_list_start(struct pvfs_state *pvfs, struct pvfs_filename *name,
107 TALLOC_CTX *mem_ctx, struct pvfs_dir **dirp)
110 struct pvfs_dir *dir;
112 (*dirp) = talloc_zero(mem_ctx, struct pvfs_dir);
114 return NT_STATUS_NO_MEMORY;
119 /* split the unix path into a directory + pattern */
120 pattern = strrchr(name->full_name, '/');
122 /* this should not happen, as pvfs_unix_path is supposed to
123 return an absolute path */
124 return NT_STATUS_UNSUCCESSFUL;
129 if (!name->has_wildcard) {
130 return pvfs_list_no_wildcard(pvfs, name, pattern, dir);
133 dir->unix_path = talloc_strdup(dir, name->full_name);
134 if (!dir->unix_path) {
135 return NT_STATUS_NO_MEMORY;
138 dir->pattern = talloc_strdup(dir, pattern);
139 if (dir->pattern == NULL) {
140 return NT_STATUS_NO_MEMORY;
143 dir->dir = opendir(name->full_name);
145 return pvfs_map_errno(pvfs, errno);
149 dir->no_wildcard = False;
150 dir->end_of_search = False;
151 dir->offset = DIR_OFFSET_DOT;
152 dir->name_cache = talloc_zero_array(dir,
153 struct name_cache_entry,
155 if (dir->name_cache == NULL) {
157 return NT_STATUS_NO_MEMORY;
160 talloc_set_destructor(dir, pvfs_dirlist_destructor);
166 add an entry to the local cache
168 static void dcache_add(struct pvfs_dir *dir, const char *name)
170 struct name_cache_entry *e;
172 dir->name_cache_index = (dir->name_cache_index+1) % NAME_CACHE_SIZE;
173 e = &dir->name_cache[dir->name_cache_index];
175 if (e->name) talloc_free(e->name);
177 e->name = talloc_strdup(dir->name_cache, name);
178 e->offset = dir->offset;
182 return the next entry
184 const char *pvfs_list_next(struct pvfs_dir *dir, off_t *ofs)
187 enum protocol_types protocol = dir->pvfs->ntvfs->ctx->protocol;
189 /* non-wildcard searches are easy */
190 if (dir->no_wildcard) {
191 dir->end_of_search = True;
192 if (*ofs != 0) return NULL;
194 return dir->single_name;
197 /* . and .. are handled separately as some unix systems will
198 not return them first in a directory, but windows client
199 may assume that these entries always appear first */
200 if (*ofs == DIR_OFFSET_DOT) {
201 (*ofs) = DIR_OFFSET_DOTDOT;
203 if (ms_fnmatch(dir->pattern, ".", protocol) == 0) {
204 dcache_add(dir, ".");
209 if (*ofs == DIR_OFFSET_DOTDOT) {
210 (*ofs) = DIR_OFFSET_BASE;
212 if (ms_fnmatch(dir->pattern, "..", protocol) == 0) {
213 dcache_add(dir, "..");
218 if (*ofs == DIR_OFFSET_BASE) {
220 } else if (*ofs != dir->offset) {
221 seekdir(dir->dir, (*ofs) - DIR_OFFSET_BASE);
225 while ((de = readdir(dir->dir))) {
226 const char *dname = de->d_name;
228 if (ISDOT(dname) || ISDOTDOT(dname)) {
232 if (ms_fnmatch(dir->pattern, dname, protocol) != 0) {
233 char *short_name = pvfs_short_name_component(dir->pvfs, dname);
234 if (short_name == NULL ||
235 ms_fnmatch(dir->pattern, short_name, protocol) != 0) {
236 talloc_free(short_name);
239 talloc_free(short_name);
242 dir->offset = telldir(dir->dir) + DIR_OFFSET_BASE;
243 (*ofs) = dir->offset;
245 dcache_add(dir, dname);
250 dir->end_of_search = True;
255 return unix directory of an open search
257 const char *pvfs_list_unix_path(struct pvfs_dir *dir)
259 return dir->unix_path;
263 return True if end of search has been reached
265 BOOL pvfs_list_eos(struct pvfs_dir *dir, off_t ofs)
267 return dir->end_of_search;
271 seek to the given name
273 NTSTATUS pvfs_list_seek(struct pvfs_dir *dir, const char *name, off_t *ofs)
278 dir->end_of_search = False;
281 dir->offset = DIR_OFFSET_DOTDOT;
286 if (ISDOTDOT(name)) {
287 dir->offset = DIR_OFFSET_BASE;
292 for (i=dir->name_cache_index;i>=0;i--) {
293 struct name_cache_entry *e = &dir->name_cache[i];
294 if (e->name && strcasecmp_m(name, e->name) == 0) {
299 for (i=NAME_CACHE_SIZE-1;i>dir->name_cache_index;i--) {
300 struct name_cache_entry *e = &dir->name_cache[i];
301 if (e->name && strcasecmp_m(name, e->name) == 0) {
309 while ((de = readdir(dir->dir))) {
310 if (strcasecmp_m(name, de->d_name) == 0) {
311 dir->offset = telldir(dir->dir) + DIR_OFFSET_BASE;
317 dir->end_of_search = True;
319 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
323 seek to the given offset
325 NTSTATUS pvfs_list_seek_ofs(struct pvfs_dir *dir, uint32_t resume_key, off_t *ofs)
330 dir->end_of_search = False;
332 if (resume_key == DIR_OFFSET_DOT) {
333 *ofs = DIR_OFFSET_DOTDOT;
337 if (resume_key == DIR_OFFSET_DOTDOT) {
338 *ofs = DIR_OFFSET_BASE;
342 if (resume_key == DIR_OFFSET_BASE) {
344 if ((de=readdir(dir->dir)) == NULL) {
345 dir->end_of_search = True;
346 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
348 *ofs = telldir(dir->dir) + DIR_OFFSET_BASE;
353 for (i=dir->name_cache_index;i>=0;i--) {
354 struct name_cache_entry *e = &dir->name_cache[i];
355 if (resume_key == (uint32_t)e->offset) {
360 for (i=NAME_CACHE_SIZE-1;i>dir->name_cache_index;i--) {
361 struct name_cache_entry *e = &dir->name_cache[i];
362 if (resume_key == (uint32_t)e->offset) {
370 while ((de = readdir(dir->dir))) {
371 dir->offset = telldir(dir->dir) + DIR_OFFSET_BASE;
372 if (resume_key == (uint32_t)dir->offset) {
378 dir->end_of_search = True;
380 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
385 see if a directory is empty
387 BOOL pvfs_directory_empty(struct pvfs_state *pvfs, struct pvfs_filename *name)
390 DIR *dir = opendir(name->full_name);
395 while ((de = readdir(dir))) {
396 if (!ISDOT(de->d_name) && !ISDOTDOT(de->d_name)) {