2 Unix SMB/CIFS implementation.
4 POSIX NTVFS backend - directory search functions
6 Copyright (C) Andrew Tridgell 2004
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "vfs_posix.h"
25 #include "system/time.h"
26 #include "system/filesys.h"
29 /* the state of a search started with pvfs_search_first() */
30 struct pvfs_search_state {
31 struct pvfs_state *pvfs;
34 uint16_t search_attrib;
41 /* place a reasonable limit on old-style searches as clients tend to
42 not send search close requests */
43 #define MAX_OLD_SEARCHES 2000
46 destroy an open search
48 static int pvfs_search_destructor(void *ptr)
50 struct pvfs_search_state *search = ptr;
51 idr_remove(search->pvfs->idtree_search, search->handle);
56 fill in a single search result for a given info level
58 static NTSTATUS fill_search_info(struct pvfs_state *pvfs,
59 enum smb_search_level level,
60 const char *unix_path,
62 struct pvfs_search_state *search,
64 union smb_search_data *file)
66 struct pvfs_filename *name;
68 const char *shortname;
70 status = pvfs_resolve_partial(pvfs, file, unix_path, fname, &name);
71 if (!NT_STATUS_IS_OK(status)) {
75 status = pvfs_match_attrib(pvfs, name, search->search_attrib, search->must_attrib);
76 if (!NT_STATUS_IS_OK(status)) {
81 case RAW_SEARCH_SEARCH:
82 case RAW_SEARCH_FFIRST:
83 case RAW_SEARCH_FUNIQUE:
84 shortname = pvfs_short_name(pvfs, name, name);
85 file->search.attrib = name->dos.attrib;
86 file->search.write_time = nt_time_to_unix(name->dos.write_time);
87 file->search.size = name->st.st_size;
88 file->search.name = shortname;
89 file->search.id.reserved = search->handle >> 8;
90 memset(file->search.id.name, ' ', sizeof(file->search.id.name));
91 memcpy(file->search.id.name, shortname,
92 MIN(strlen(shortname)+1, sizeof(file->search.id.name)));
93 file->search.id.handle = search->handle & 0xFF;
94 file->search.id.server_cookie = dir_index;
95 file->search.id.client_cookie = 0;
98 case RAW_SEARCH_STANDARD:
99 file->standard.resume_key = dir_index;
100 file->standard.create_time = nt_time_to_unix(name->dos.create_time);
101 file->standard.access_time = nt_time_to_unix(name->dos.access_time);
102 file->standard.write_time = nt_time_to_unix(name->dos.write_time);
103 file->standard.size = name->st.st_size;
104 file->standard.alloc_size = name->dos.alloc_size;
105 file->standard.attrib = name->dos.attrib;
106 file->standard.name.s = fname;
109 case RAW_SEARCH_EA_SIZE:
110 file->ea_size.resume_key = dir_index;
111 file->ea_size.create_time = nt_time_to_unix(name->dos.create_time);
112 file->ea_size.access_time = nt_time_to_unix(name->dos.access_time);
113 file->ea_size.write_time = nt_time_to_unix(name->dos.write_time);
114 file->ea_size.size = name->st.st_size;
115 file->ea_size.alloc_size = name->dos.alloc_size;
116 file->ea_size.attrib = name->dos.attrib;
117 file->ea_size.ea_size = name->dos.ea_size;
118 file->ea_size.name.s = fname;
121 case RAW_SEARCH_DIRECTORY_INFO:
122 file->directory_info.file_index = dir_index;
123 file->directory_info.create_time = name->dos.create_time;
124 file->directory_info.access_time = name->dos.access_time;
125 file->directory_info.write_time = name->dos.write_time;
126 file->directory_info.change_time = name->dos.change_time;
127 file->directory_info.size = name->st.st_size;
128 file->directory_info.alloc_size = name->dos.alloc_size;
129 file->directory_info.attrib = name->dos.attrib;
130 file->directory_info.name.s = fname;
133 case RAW_SEARCH_FULL_DIRECTORY_INFO:
134 file->full_directory_info.file_index = dir_index;
135 file->full_directory_info.create_time = name->dos.create_time;
136 file->full_directory_info.access_time = name->dos.access_time;
137 file->full_directory_info.write_time = name->dos.write_time;
138 file->full_directory_info.change_time = name->dos.change_time;
139 file->full_directory_info.size = name->st.st_size;
140 file->full_directory_info.alloc_size = name->dos.alloc_size;
141 file->full_directory_info.attrib = name->dos.attrib;
142 file->full_directory_info.ea_size = name->dos.ea_size;
143 file->full_directory_info.name.s = fname;
146 case RAW_SEARCH_NAME_INFO:
147 file->name_info.file_index = dir_index;
148 file->name_info.name.s = fname;
151 case RAW_SEARCH_BOTH_DIRECTORY_INFO:
152 file->both_directory_info.file_index = dir_index;
153 file->both_directory_info.create_time = name->dos.create_time;
154 file->both_directory_info.access_time = name->dos.access_time;
155 file->both_directory_info.write_time = name->dos.write_time;
156 file->both_directory_info.change_time = name->dos.change_time;
157 file->both_directory_info.size = name->st.st_size;
158 file->both_directory_info.alloc_size = name->dos.alloc_size;
159 file->both_directory_info.attrib = name->dos.attrib;
160 file->both_directory_info.ea_size = name->dos.ea_size;
161 file->both_directory_info.short_name.s = pvfs_short_name(pvfs, file, name);
162 file->both_directory_info.name.s = fname;
165 case RAW_SEARCH_ID_FULL_DIRECTORY_INFO:
166 file->id_full_directory_info.file_index = dir_index;
167 file->id_full_directory_info.create_time = name->dos.create_time;
168 file->id_full_directory_info.access_time = name->dos.access_time;
169 file->id_full_directory_info.write_time = name->dos.write_time;
170 file->id_full_directory_info.change_time = name->dos.change_time;
171 file->id_full_directory_info.size = name->st.st_size;
172 file->id_full_directory_info.alloc_size = name->dos.alloc_size;
173 file->id_full_directory_info.attrib = name->dos.attrib;
174 file->id_full_directory_info.ea_size = name->dos.ea_size;
175 file->id_full_directory_info.file_id = name->dos.file_id;
176 file->id_full_directory_info.name.s = fname;
179 case RAW_SEARCH_ID_BOTH_DIRECTORY_INFO:
180 file->id_both_directory_info.file_index = dir_index;
181 file->id_both_directory_info.create_time = name->dos.create_time;
182 file->id_both_directory_info.access_time = name->dos.access_time;
183 file->id_both_directory_info.write_time = name->dos.write_time;
184 file->id_both_directory_info.change_time = name->dos.change_time;
185 file->id_both_directory_info.size = name->st.st_size;
186 file->id_both_directory_info.alloc_size = name->dos.alloc_size;
187 file->id_both_directory_info.attrib = name->dos.attrib;
188 file->id_both_directory_info.ea_size = name->dos.ea_size;
189 file->id_both_directory_info.file_id = name->dos.file_id;
190 file->id_both_directory_info.short_name.s = pvfs_short_name(pvfs, file, name);
191 file->id_both_directory_info.name.s = fname;
194 case RAW_SEARCH_GENERIC:
198 return NT_STATUS_INVALID_LEVEL;
205 static NTSTATUS pvfs_search_fill(struct pvfs_state *pvfs, TALLOC_CTX *mem_ctx,
207 struct pvfs_search_state *search,
208 enum smb_search_level level,
210 void *search_private,
211 BOOL (*callback)(void *, union smb_search_data *))
213 struct pvfs_dir *dir = search->dir;
218 if (max_count == 0) {
222 while ((*reply_count) < max_count) {
223 union smb_search_data *file;
225 uint_t ofs = search->current_index;
227 name = pvfs_list_next(dir, &search->current_index);
228 if (name == NULL) break;
230 file = talloc_p(mem_ctx, union smb_search_data);
232 return NT_STATUS_NO_MEMORY;
235 status = fill_search_info(pvfs, level,
236 pvfs_list_unix_path(dir), name,
237 search, search->current_index, file);
238 if (!NT_STATUS_IS_OK(status)) {
243 if (!callback(search_private, file)) {
245 search->current_index = ofs;
253 pvfs_list_hibernate(dir);
259 we've run out of search handles - cleanup those that the client forgot
262 static void pvfs_search_cleanup(struct pvfs_state *pvfs)
265 time_t t = time(NULL);
267 for (i=0;i<MAX_OLD_SEARCHES;i++) {
268 struct pvfs_search_state *search = idr_find(pvfs->idtree_search, i);
269 if (search == NULL) return;
270 if (pvfs_list_eos(search->dir, search->current_index) &&
271 search->last_used != 0 &&
272 t > search->last_used + 30) {
273 /* its almost certainly been forgotten
282 list files in a directory matching a wildcard pattern - old SMBsearch interface
284 static NTSTATUS pvfs_search_first_old(struct ntvfs_module_context *ntvfs,
285 struct smbsrv_request *req, union smb_search_first *io,
286 void *search_private,
287 BOOL (*callback)(void *, union smb_search_data *))
289 struct pvfs_dir *dir;
290 struct pvfs_state *pvfs = ntvfs->private_data;
291 struct pvfs_search_state *search;
293 uint16_t search_attrib;
296 struct pvfs_filename *name;
299 search_attrib = io->search_first.in.search_attrib;
300 pattern = io->search_first.in.pattern;
302 /* resolve the cifs name to a posix name */
303 status = pvfs_resolve_name(pvfs, req, pattern, PVFS_RESOLVE_WILDCARD, &name);
304 if (!NT_STATUS_IS_OK(status)) {
308 if (!name->has_wildcard && !name->exists) {
309 return STATUS_NO_MORE_FILES;
312 /* we initially make search a child of the request, then if we
313 need to keep it long term we steal it for the private
315 search = talloc_p(req, struct pvfs_search_state);
317 return NT_STATUS_NO_MEMORY;
320 /* do the actual directory listing */
321 status = pvfs_list_start(pvfs, name, search, &dir);
322 if (!NT_STATUS_IS_OK(status)) {
326 /* we need to give a handle back to the client so it
327 can continue a search */
328 id = idr_get_new(pvfs->idtree_search, search, MAX_OLD_SEARCHES);
330 pvfs_search_cleanup(pvfs);
331 id = idr_get_new(pvfs->idtree_search, search, MAX_OLD_SEARCHES);
334 return NT_STATUS_INSUFFICIENT_RESOURCES;
340 search->current_index = 0;
341 search->search_attrib = search_attrib & 0xFF;
342 search->must_attrib = (search_attrib>>8) & 0xFF;
343 search->last_used = time(NULL);
345 talloc_set_destructor(search, pvfs_search_destructor);
347 status = pvfs_search_fill(pvfs, req, io->search_first.in.max_count, search, io->generic.level,
348 &reply_count, search_private, callback);
349 if (!NT_STATUS_IS_OK(status)) {
353 io->search_first.out.count = reply_count;
355 /* not matching any entries is an error */
356 if (reply_count == 0) {
357 return STATUS_NO_MORE_FILES;
360 talloc_steal(pvfs, search);
365 /* continue a old style search */
366 static NTSTATUS pvfs_search_next_old(struct ntvfs_module_context *ntvfs,
367 struct smbsrv_request *req, union smb_search_next *io,
368 void *search_private,
369 BOOL (*callback)(void *, union smb_search_data *))
371 struct pvfs_state *pvfs = ntvfs->private_data;
372 struct pvfs_search_state *search;
373 struct pvfs_dir *dir;
374 uint_t reply_count, max_count;
378 handle = io->search_next.in.id.handle | (io->search_next.in.id.reserved<<8);
379 max_count = io->search_next.in.max_count;
381 search = idr_find(pvfs->idtree_search, handle);
382 if (search == NULL) {
383 /* we didn't find the search handle */
384 return NT_STATUS_INVALID_HANDLE;
387 search->current_index = io->search_next.in.id.server_cookie;
388 search->last_used = time(NULL);
391 status = pvfs_list_wakeup(dir, &search->current_index);
392 if (!NT_STATUS_IS_OK(status)) {
396 status = pvfs_search_fill(pvfs, req, max_count, search, io->generic.level,
397 &reply_count, search_private, callback);
398 if (!NT_STATUS_IS_OK(status)) {
402 io->search_next.out.count = reply_count;
404 /* not matching any entries means end of search */
405 if (reply_count == 0) {
413 list files in a directory matching a wildcard pattern
415 NTSTATUS pvfs_search_first(struct ntvfs_module_context *ntvfs,
416 struct smbsrv_request *req, union smb_search_first *io,
417 void *search_private,
418 BOOL (*callback)(void *, union smb_search_data *))
420 struct pvfs_dir *dir;
421 struct pvfs_state *pvfs = ntvfs->private_data;
422 struct pvfs_search_state *search;
424 uint16_t search_attrib, max_count;
427 struct pvfs_filename *name;
430 if (io->generic.level >= RAW_SEARCH_SEARCH) {
431 return pvfs_search_first_old(ntvfs, req, io, search_private, callback);
434 search_attrib = io->t2ffirst.in.search_attrib;
435 pattern = io->t2ffirst.in.pattern;
436 max_count = io->t2ffirst.in.max_count;
438 /* resolve the cifs name to a posix name */
439 status = pvfs_resolve_name(pvfs, req, pattern, PVFS_RESOLVE_WILDCARD, &name);
440 if (!NT_STATUS_IS_OK(status)) {
444 if (!name->has_wildcard && !name->exists) {
445 return NT_STATUS_NO_SUCH_FILE;
448 /* we initially make search a child of the request, then if we
449 need to keep it long term we steal it for the private
451 search = talloc_p(req, struct pvfs_search_state);
453 return NT_STATUS_NO_MEMORY;
456 /* do the actual directory listing */
457 status = pvfs_list_start(pvfs, name, search, &dir);
458 if (!NT_STATUS_IS_OK(status)) {
462 id = idr_get_new(pvfs->idtree_search, search, UINT16_MAX);
464 return NT_STATUS_INSUFFICIENT_RESOURCES;
470 search->current_index = 0;
471 search->search_attrib = search_attrib;
472 search->must_attrib = 0;
473 search->last_used = 0;
475 talloc_set_destructor(search, pvfs_search_destructor);
477 status = pvfs_search_fill(pvfs, req, max_count, search, io->generic.level,
478 &reply_count, search_private, callback);
479 if (!NT_STATUS_IS_OK(status)) {
483 /* not matching any entries is an error */
484 if (reply_count == 0) {
485 return NT_STATUS_NO_SUCH_FILE;
488 io->t2ffirst.out.count = reply_count;
489 io->t2ffirst.out.handle = search->handle;
490 io->t2ffirst.out.end_of_search = pvfs_list_eos(dir, search->current_index) ? 1 : 0;
492 /* work out if we are going to keep the search state
493 and allow for a search continue */
494 if ((io->t2ffirst.in.flags & FLAG_TRANS2_FIND_CLOSE) ||
495 ((io->t2ffirst.in.flags & FLAG_TRANS2_FIND_CLOSE_IF_END) &&
496 io->t2ffirst.out.end_of_search)) {
499 talloc_steal(pvfs, search);
505 /* continue a search */
506 NTSTATUS pvfs_search_next(struct ntvfs_module_context *ntvfs,
507 struct smbsrv_request *req, union smb_search_next *io,
508 void *search_private,
509 BOOL (*callback)(void *, union smb_search_data *))
511 struct pvfs_state *pvfs = ntvfs->private_data;
512 struct pvfs_search_state *search;
513 struct pvfs_dir *dir;
518 if (io->generic.level >= RAW_SEARCH_SEARCH) {
519 return pvfs_search_next_old(ntvfs, req, io, search_private, callback);
522 handle = io->t2fnext.in.handle;
524 search = idr_find(pvfs->idtree_search, handle);
525 if (search == NULL) {
526 /* we didn't find the search handle */
527 return NT_STATUS_INVALID_HANDLE;
532 /* work out what type of continuation is being used */
533 if (io->t2fnext.in.last_name && *io->t2fnext.in.last_name) {
534 status = pvfs_list_seek(dir, io->t2fnext.in.last_name, &search->current_index);
535 if (!NT_STATUS_IS_OK(status)) {
536 if (io->t2fnext.in.resume_key) {
537 search->current_index = io->t2fnext.in.resume_key;
542 } else if (io->t2fnext.in.flags & FLAG_TRANS2_FIND_CONTINUE) {
543 /* plain continue - nothing to do */
545 search->current_index = io->t2fnext.in.resume_key;
548 status = pvfs_list_wakeup(dir, &search->current_index);
549 if (!NT_STATUS_IS_OK(status)) {
553 status = pvfs_search_fill(pvfs, req, io->t2fnext.in.max_count, search, io->generic.level,
554 &reply_count, search_private, callback);
555 if (!NT_STATUS_IS_OK(status)) {
559 io->t2fnext.out.count = reply_count;
560 io->t2fnext.out.end_of_search = pvfs_list_eos(dir, search->current_index) ? 1 : 0;
562 /* work out if we are going to keep the search state */
563 if ((io->t2fnext.in.flags & FLAG_TRANS2_FIND_CLOSE) ||
564 ((io->t2fnext.in.flags & FLAG_TRANS2_FIND_CLOSE_IF_END) &&
565 io->t2fnext.out.end_of_search)) {
573 NTSTATUS pvfs_search_close(struct ntvfs_module_context *ntvfs,
574 struct smbsrv_request *req, union smb_search_close *io)
576 struct pvfs_state *pvfs = ntvfs->private_data;
577 struct pvfs_search_state *search;
580 if (io->generic.level == RAW_FINDCLOSE_FCLOSE) {
581 handle = io->fclose.in.id.handle;
583 handle = io->findclose.in.handle;
586 search = idr_find(pvfs->idtree_search, handle);
587 if (search == NULL) {
588 /* we didn't find the search handle */
589 return NT_STATUS_INVALID_HANDLE;