2 Unix SMB/CIFS implementation.
4 simpler Samba VFS filesystem backend for clients which support the
5 CIFS Unix Extensions or newer CIFS POSIX protocol extensions
7 Copyright (C) Andrew Tridgell 2003
8 Copyright (C) Steve French 2006
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 this implements a very simple NTVFS filesystem backend.
25 this backend largely ignores the POSIX -> CIFS mappings, just doing absolutely
26 minimal work to give a working backend.
30 #include "system/dir.h"
31 #include "system/filesys.h"
32 #include "cifsposix.h"
33 #include "system/time.h"
34 #include "lib/util/dlinklist.h"
35 #include "ntvfs/ntvfs.h"
36 #include "ntvfs/cifs_posix_cli/proto.h"
42 #define CHECK_READ_ONLY(req) do { if (share_bool_option(ntvfs->ctx->config, SHARE_READONLY, True)) return NT_STATUS_ACCESS_DENIED; } while (0)
45 connect to a share - used when a tree_connect operation comes
46 in. For a disk based backend we needs to ensure that the base
47 directory exists (tho it doesn't need to be accessible by the user,
50 static NTSTATUS cifspsx_connect(struct ntvfs_module_context *ntvfs,
51 struct ntvfs_request *req, const char *sharename)
54 struct cifspsx_private *private;
55 struct share_config *scfg = ntvfs->ctx->config;
57 private = talloc(ntvfs, struct cifspsx_private);
58 NT_STATUS_HAVE_NO_MEMORY(private);
59 private->ntvfs = ntvfs;
60 private->next_search_handle = 0;
61 private->connectpath = talloc_strdup(private, share_string_option(scfg, SHARE_PATH, ""));
62 private->open_files = NULL;
63 private->search = NULL;
65 /* the directory must exist */
66 if (stat(private->connectpath, &st) != 0 || !S_ISDIR(st.st_mode)) {
67 DEBUG(0,("'%s' is not a directory, when connecting to [%s]\n",
68 private->connectpath, sharename));
69 return NT_STATUS_BAD_NETWORK_NAME;
72 ntvfs->ctx->fs_type = talloc_strdup(ntvfs->ctx, "NTFS");
73 NT_STATUS_HAVE_NO_MEMORY(ntvfs->ctx->fs_type);
74 ntvfs->ctx->dev_type = talloc_strdup(ntvfs->ctx, "A:");
75 NT_STATUS_HAVE_NO_MEMORY(ntvfs->ctx->dev_type);
77 ntvfs->private_data = private;
79 DEBUG(0,("WARNING: ntvfs cifs posix: connect to share [%s] with ROOT privileges!!!\n",sharename));
85 disconnect from a share
87 static NTSTATUS cifspsx_disconnect(struct ntvfs_module_context *ntvfs)
93 find open file handle given fd
95 static struct cifspsx_file *find_fd(struct cifspsx_private *private, struct ntvfs_handle *handle)
97 struct cifspsx_file *f;
100 p = ntvfs_handle_get_backend_data(handle, private->ntvfs);
103 f = talloc_get_type(p, struct cifspsx_file);
110 delete a file - the dirtype specifies the file types to include in the search.
111 The name can contain CIFS wildcards, but rarely does (except with OS/2 clients)
113 static NTSTATUS cifspsx_unlink(struct ntvfs_module_context *ntvfs,
114 struct ntvfs_request *req,
115 union smb_unlink *unl)
119 CHECK_READ_ONLY(req);
121 unix_path = cifspsx_unix_path(ntvfs, req, unl->unlink.in.pattern);
123 /* ignoring wildcards ... */
124 if (unlink(unix_path) == -1) {
125 return map_nt_error_from_unix(errno);
133 ioctl interface - we don't do any
135 static NTSTATUS cifspsx_ioctl(struct ntvfs_module_context *ntvfs,
136 struct ntvfs_request *req, union smb_ioctl *io)
138 return NT_STATUS_INVALID_PARAMETER;
142 check if a directory exists
144 static NTSTATUS cifspsx_chkpath(struct ntvfs_module_context *ntvfs,
145 struct ntvfs_request *req,
146 union smb_chkpath *cp)
151 unix_path = cifspsx_unix_path(ntvfs, req, cp->chkpath.in.path);
153 if (stat(unix_path, &st) == -1) {
154 return map_nt_error_from_unix(errno);
157 if (!S_ISDIR(st.st_mode)) {
158 return NT_STATUS_NOT_A_DIRECTORY;
165 build a file_id from a stat struct
167 static uint64_t cifspsx_file_id(struct stat *st)
169 uint64_t ret = st->st_ino;
176 approximately map a struct stat to a generic fileinfo struct
178 static NTSTATUS cifspsx_map_fileinfo(struct ntvfs_module_context *ntvfs,
179 struct ntvfs_request *req, union smb_fileinfo *info,
180 struct stat *st, const char *unix_path)
182 struct cifspsx_dir *dir = NULL;
183 char *pattern = NULL;
185 const char *s, *short_name;
187 s = strrchr(unix_path, '/');
194 asprintf(&pattern, "%s:*", unix_path);
197 dir = cifspsx_list_unix(req, req, pattern);
200 unix_to_nt_time(&info->generic.out.create_time, st->st_ctime);
201 unix_to_nt_time(&info->generic.out.access_time, st->st_atime);
202 unix_to_nt_time(&info->generic.out.write_time, st->st_mtime);
203 unix_to_nt_time(&info->generic.out.change_time, st->st_mtime);
204 info->generic.out.alloc_size = st->st_size;
205 info->generic.out.size = st->st_size;
206 info->generic.out.attrib = cifspsx_unix_to_dos_attrib(st->st_mode);
207 info->generic.out.alloc_size = st->st_blksize * st->st_blocks;
208 info->generic.out.nlink = st->st_nlink;
209 info->generic.out.directory = S_ISDIR(st->st_mode) ? 1 : 0;
210 info->generic.out.file_id = cifspsx_file_id(st);
211 /* REWRITE: TODO stuff in here */
212 info->generic.out.delete_pending = 0;
213 info->generic.out.ea_size = 0;
214 info->generic.out.num_eas = 0;
215 info->generic.out.fname.s = talloc_strdup(req, short_name);
216 info->generic.out.alt_fname.s = talloc_strdup(req, short_name);
217 info->generic.out.compressed_size = 0;
218 info->generic.out.format = 0;
219 info->generic.out.unit_shift = 0;
220 info->generic.out.chunk_shift = 0;
221 info->generic.out.cluster_shift = 0;
223 info->generic.out.access_flags = 0;
224 info->generic.out.position = 0;
225 info->generic.out.mode = 0;
226 info->generic.out.alignment_requirement = 0;
227 info->generic.out.reparse_tag = 0;
228 info->generic.out.num_streams = 0;
229 /* setup a single data stream */
230 info->generic.out.num_streams = 1 + (dir?dir->count:0);
231 info->generic.out.streams = talloc_array(req,
232 struct stream_struct,
233 info->generic.out.num_streams);
234 if (!info->generic.out.streams) {
235 return NT_STATUS_NO_MEMORY;
237 info->generic.out.streams[0].size = st->st_size;
238 info->generic.out.streams[0].alloc_size = st->st_size;
239 info->generic.out.streams[0].stream_name.s = talloc_strdup(req,"::$DATA");
241 for (i=0;dir && i<dir->count;i++) {
242 s = strchr(dir->files[i].name, ':');
243 info->generic.out.streams[1+i].size = dir->files[i].st.st_size;
244 info->generic.out.streams[1+i].alloc_size = dir->files[i].st.st_size;
245 info->generic.out.streams[1+i].stream_name.s = s?s:dir->files[i].name;
252 return info on a pathname
254 static NTSTATUS cifspsx_qpathinfo(struct ntvfs_module_context *ntvfs,
255 struct ntvfs_request *req, union smb_fileinfo *info)
260 DEBUG(19,("cifspsx_qpathinfo: file %s level 0x%x\n", info->generic.in.file.path, info->generic.level));
261 if (info->generic.level != RAW_FILEINFO_GENERIC) {
262 return ntvfs_map_qpathinfo(ntvfs, req, info);
265 unix_path = cifspsx_unix_path(ntvfs, req, info->generic.in.file.path);
266 DEBUG(19,("cifspsx_qpathinfo: file %s\n", unix_path));
267 if (stat(unix_path, &st) == -1) {
268 DEBUG(19,("cifspsx_qpathinfo: file %s errno=%d\n", unix_path, errno));
269 return map_nt_error_from_unix(errno);
271 DEBUG(19,("cifspsx_qpathinfo: file %s, stat done\n", unix_path));
272 return cifspsx_map_fileinfo(ntvfs, req, info, &st, unix_path);
276 query info on a open file
278 static NTSTATUS cifspsx_qfileinfo(struct ntvfs_module_context *ntvfs,
279 struct ntvfs_request *req, union smb_fileinfo *info)
281 struct cifspsx_private *private = ntvfs->private_data;
282 struct cifspsx_file *f;
285 if (info->generic.level != RAW_FILEINFO_GENERIC) {
286 return ntvfs_map_qfileinfo(ntvfs, req, info);
289 f = find_fd(private, info->generic.in.file.ntvfs);
291 return NT_STATUS_INVALID_HANDLE;
294 if (fstat(f->fd, &st) == -1) {
295 return map_nt_error_from_unix(errno);
298 return cifspsx_map_fileinfo(ntvfs, req,info, &st, f->name);
305 static NTSTATUS cifspsx_open(struct ntvfs_module_context *ntvfs,
306 struct ntvfs_request *req, union smb_open *io)
308 struct cifspsx_private *private = ntvfs->private_data;
312 struct cifspsx_file *f;
313 int create_flags, rdwr_flags;
316 struct ntvfs_handle *handle;
318 if (io->generic.level != RAW_OPEN_GENERIC) {
319 return ntvfs_map_open(ntvfs, req, io);
322 readonly = share_bool_option(ntvfs->ctx->config, SHARE_READONLY, SHARE_READONLY_DEFAULT);
325 rdwr_flags = O_RDONLY;
327 create_flags = O_CREAT;
331 unix_path = cifspsx_unix_path(ntvfs, req, io->ntcreatex.in.fname);
333 switch (io->generic.in.open_disposition) {
334 case NTCREATEX_DISP_SUPERSEDE:
335 case NTCREATEX_DISP_OVERWRITE_IF:
336 flags = create_flags | O_TRUNC;
338 case NTCREATEX_DISP_OPEN:
339 case NTCREATEX_DISP_OVERWRITE:
342 case NTCREATEX_DISP_CREATE:
343 flags = create_flags | O_EXCL;
345 case NTCREATEX_DISP_OPEN_IF:
346 flags = create_flags;
355 if (io->generic.in.create_options & NTCREATEX_OPTIONS_DIRECTORY) {
356 flags = O_RDONLY | O_DIRECTORY;
360 switch (io->generic.in.open_disposition) {
361 case NTCREATEX_DISP_CREATE:
362 if (mkdir(unix_path, 0755) == -1) {
363 DEBUG(9,("cifspsx_open: mkdir %s errno=%d\n", unix_path, errno));
364 return map_nt_error_from_unix(errno);
367 case NTCREATEX_DISP_OPEN_IF:
368 if (mkdir(unix_path, 0755) == -1 && errno != EEXIST) {
369 DEBUG(9,("cifspsx_open: mkdir %s errno=%d\n", unix_path, errno));
370 return map_nt_error_from_unix(errno);
377 fd = open(unix_path, flags, 0644);
379 return map_nt_error_from_unix(errno);
382 if (fstat(fd, &st) == -1) {
383 DEBUG(9,("cifspsx_open: fstat errno=%d\n", errno));
385 return map_nt_error_from_unix(errno);
388 status = ntvfs_handle_new(ntvfs, req, &handle);
389 NT_STATUS_NOT_OK_RETURN(status);
391 f = talloc(handle, struct cifspsx_file);
392 NT_STATUS_HAVE_NO_MEMORY(f);
394 f->name = talloc_strdup(f, unix_path);
395 NT_STATUS_HAVE_NO_MEMORY(f->name);
397 DLIST_ADD(private->open_files, f);
399 status = ntvfs_handle_set_backend_data(handle, ntvfs, f);
400 NT_STATUS_NOT_OK_RETURN(status);
402 ZERO_STRUCT(io->generic.out);
404 unix_to_nt_time(&io->generic.out.create_time, st.st_ctime);
405 unix_to_nt_time(&io->generic.out.access_time, st.st_atime);
406 unix_to_nt_time(&io->generic.out.write_time, st.st_mtime);
407 unix_to_nt_time(&io->generic.out.change_time, st.st_mtime);
408 io->generic.out.file.ntvfs = handle;
409 io->generic.out.alloc_size = st.st_size;
410 io->generic.out.size = st.st_size;
411 io->generic.out.attrib = cifspsx_unix_to_dos_attrib(st.st_mode);
412 io->generic.out.is_directory = S_ISDIR(st.st_mode) ? 1 : 0;
420 static NTSTATUS cifspsx_mkdir(struct ntvfs_module_context *ntvfs,
421 struct ntvfs_request *req, union smb_mkdir *md)
425 CHECK_READ_ONLY(req);
427 if (md->generic.level != RAW_MKDIR_MKDIR) {
428 return NT_STATUS_INVALID_LEVEL;
431 unix_path = cifspsx_unix_path(ntvfs, req, md->mkdir.in.path);
433 if (mkdir(unix_path, 0777) == -1) {
434 return map_nt_error_from_unix(errno);
443 static NTSTATUS cifspsx_rmdir(struct ntvfs_module_context *ntvfs,
444 struct ntvfs_request *req, struct smb_rmdir *rd)
448 CHECK_READ_ONLY(req);
450 unix_path = cifspsx_unix_path(ntvfs, req, rd->in.path);
452 if (rmdir(unix_path) == -1) {
453 return map_nt_error_from_unix(errno);
460 rename a set of files
462 static NTSTATUS cifspsx_rename(struct ntvfs_module_context *ntvfs,
463 struct ntvfs_request *req, union smb_rename *ren)
465 char *unix_path1, *unix_path2;
467 CHECK_READ_ONLY(req);
469 if (ren->generic.level != RAW_RENAME_RENAME) {
470 return NT_STATUS_INVALID_LEVEL;
473 unix_path1 = cifspsx_unix_path(ntvfs, req, ren->rename.in.pattern1);
474 unix_path2 = cifspsx_unix_path(ntvfs, req, ren->rename.in.pattern2);
476 if (rename(unix_path1, unix_path2) == -1) {
477 return map_nt_error_from_unix(errno);
486 static NTSTATUS cifspsx_copy(struct ntvfs_module_context *ntvfs,
487 struct ntvfs_request *req, struct smb_copy *cp)
489 return NT_STATUS_NOT_SUPPORTED;
495 static NTSTATUS cifspsx_read(struct ntvfs_module_context *ntvfs,
496 struct ntvfs_request *req, union smb_read *rd)
498 struct cifspsx_private *private = ntvfs->private_data;
499 struct cifspsx_file *f;
502 if (rd->generic.level != RAW_READ_READX) {
503 return NT_STATUS_NOT_SUPPORTED;
506 f = find_fd(private, rd->readx.in.file.ntvfs);
508 return NT_STATUS_INVALID_HANDLE;
514 rd->readx.in.offset);
516 return map_nt_error_from_unix(errno);
519 rd->readx.out.nread = ret;
520 rd->readx.out.remaining = 0; /* should fill this in? */
521 rd->readx.out.compaction_mode = 0;
529 static NTSTATUS cifspsx_write(struct ntvfs_module_context *ntvfs,
530 struct ntvfs_request *req, union smb_write *wr)
532 struct cifspsx_private *private = ntvfs->private_data;
533 struct cifspsx_file *f;
536 if (wr->generic.level != RAW_WRITE_WRITEX) {
537 return ntvfs_map_write(ntvfs, req, wr);
540 CHECK_READ_ONLY(req);
542 f = find_fd(private, wr->writex.in.file.ntvfs);
544 return NT_STATUS_INVALID_HANDLE;
550 wr->writex.in.offset);
552 return map_nt_error_from_unix(errno);
555 wr->writex.out.nwritten = ret;
556 wr->writex.out.remaining = 0; /* should fill this in? */
564 static NTSTATUS cifspsx_seek(struct ntvfs_module_context *ntvfs,
565 struct ntvfs_request *req,
568 return NT_STATUS_NOT_SUPPORTED;
574 static NTSTATUS cifspsx_flush(struct ntvfs_module_context *ntvfs,
575 struct ntvfs_request *req,
578 struct cifspsx_private *private = ntvfs->private_data;
579 struct cifspsx_file *f;
581 switch (io->generic.level) {
582 case RAW_FLUSH_FLUSH:
584 /* ignore the additional unknown option in SMB2 */
585 f = find_fd(private, io->generic.in.file.ntvfs);
587 return NT_STATUS_INVALID_HANDLE;
593 for (f=private->open_files;f;f=f->next) {
599 return NT_STATUS_INVALID_LEVEL;
605 static NTSTATUS cifspsx_close(struct ntvfs_module_context *ntvfs,
606 struct ntvfs_request *req,
609 struct cifspsx_private *private = ntvfs->private_data;
610 struct cifspsx_file *f;
612 if (io->generic.level != RAW_CLOSE_CLOSE) {
613 /* we need a mapping function */
614 return NT_STATUS_INVALID_LEVEL;
617 f = find_fd(private, io->close.in.file.ntvfs);
619 return NT_STATUS_INVALID_HANDLE;
622 if (close(f->fd) == -1) {
623 return map_nt_error_from_unix(errno);
626 DLIST_REMOVE(private->open_files, f);
627 talloc_free(f->name);
636 static NTSTATUS cifspsx_exit(struct ntvfs_module_context *ntvfs,
637 struct ntvfs_request *req)
639 return NT_STATUS_NOT_SUPPORTED;
643 logoff - closing files
645 static NTSTATUS cifspsx_logoff(struct ntvfs_module_context *ntvfs,
646 struct ntvfs_request *req)
648 return NT_STATUS_NOT_SUPPORTED;
652 setup for an async call
654 static NTSTATUS cifspsx_async_setup(struct ntvfs_module_context *ntvfs,
655 struct ntvfs_request *req,
664 static NTSTATUS cifspsx_cancel(struct ntvfs_module_context *ntvfs, struct ntvfs_request *req)
666 return NT_STATUS_UNSUCCESSFUL;
672 static NTSTATUS cifspsx_lock(struct ntvfs_module_context *ntvfs,
673 struct ntvfs_request *req, union smb_lock *lck)
675 DEBUG(0,("REWRITE: not doing byte range locking!\n"));
680 set info on a pathname
682 static NTSTATUS cifspsx_setpathinfo(struct ntvfs_module_context *ntvfs,
683 struct ntvfs_request *req, union smb_setfileinfo *st)
685 CHECK_READ_ONLY(req);
687 return NT_STATUS_NOT_SUPPORTED;
691 set info on a open file
693 static NTSTATUS cifspsx_setfileinfo(struct ntvfs_module_context *ntvfs,
694 struct ntvfs_request *req,
695 union smb_setfileinfo *info)
697 struct cifspsx_private *private = ntvfs->private_data;
698 struct cifspsx_file *f;
699 struct utimbuf unix_times;
701 CHECK_READ_ONLY(req);
703 f = find_fd(private, info->generic.in.file.ntvfs);
705 return NT_STATUS_INVALID_HANDLE;
708 switch (info->generic.level) {
709 case RAW_SFILEINFO_END_OF_FILE_INFO:
710 case RAW_SFILEINFO_END_OF_FILE_INFORMATION:
712 info->end_of_file_info.in.size) == -1) {
713 return map_nt_error_from_unix(errno);
716 case RAW_SFILEINFO_SETATTRE:
717 unix_times.actime = info->setattre.in.access_time;
718 unix_times.modtime = info->setattre.in.write_time;
720 if (unix_times.actime == 0 && unix_times.modtime == 0) {
724 /* set modify time = to access time if modify time was 0 */
725 if (unix_times.actime != 0 && unix_times.modtime == 0) {
726 unix_times.modtime = unix_times.actime;
729 /* Set the date on this file */
730 if (cifspsx_file_utime(f->fd, &unix_times) != 0) {
731 return NT_STATUS_ACCESS_DENIED;
735 DEBUG(2,("cifspsx_setfileinfo: level %d not implemented\n",
736 info->generic.level));
737 return NT_STATUS_NOT_IMPLEMENTED;
744 return filesystem space info
746 static NTSTATUS cifspsx_fsinfo(struct ntvfs_module_context *ntvfs,
747 struct ntvfs_request *req, union smb_fsinfo *fs)
749 struct cifspsx_private *private = ntvfs->private_data;
752 if (fs->generic.level != RAW_QFS_GENERIC) {
753 return ntvfs_map_fsinfo(ntvfs, req, fs);
756 if (sys_fsusage(private->connectpath,
757 &fs->generic.out.blocks_free,
758 &fs->generic.out.blocks_total) == -1) {
759 return map_nt_error_from_unix(errno);
762 fs->generic.out.block_size = 512;
764 if (stat(private->connectpath, &st) != 0) {
765 return NT_STATUS_DISK_CORRUPT_ERROR;
768 fs->generic.out.fs_id = st.st_ino;
769 unix_to_nt_time(&fs->generic.out.create_time, st.st_ctime);
770 fs->generic.out.serial_number = st.st_ino;
771 fs->generic.out.fs_attr = 0;
772 fs->generic.out.max_file_component_length = 255;
773 fs->generic.out.device_type = 0;
774 fs->generic.out.device_characteristics = 0;
775 fs->generic.out.quota_soft = 0;
776 fs->generic.out.quota_hard = 0;
777 fs->generic.out.quota_flags = 0;
778 fs->generic.out.volume_name = talloc_strdup(req, ntvfs->ctx->config->name);
779 fs->generic.out.fs_type = ntvfs->ctx->fs_type;
786 return filesystem attribute info
788 static NTSTATUS cifspsx_fsattr(struct ntvfs_module_context *ntvfs,
789 struct ntvfs_request *req, union smb_fsattr *fs)
792 struct cifspsx_private *private = ntvfs->private_data;
794 if (fs->generic.level != RAW_FSATTR_GENERIC) {
795 return ntvfs_map_fsattr(ntvfs, req, fs);
798 if (stat(private->connectpath, &st) == -1) {
799 return map_nt_error_from_unix(errno);
802 unix_to_nt_time(&fs->generic.out.create_time, st.st_ctime);
803 fs->generic.out.fs_attr =
804 FILE_CASE_PRESERVED_NAMES |
805 FILE_CASE_SENSITIVE_SEARCH |
806 FILE_PERSISTENT_ACLS;
807 fs->generic.out.max_file_component_length = 255;
808 fs->generic.out.serial_number = 1;
809 fs->generic.out.fs_type = talloc_strdup(req, "NTFS");
810 fs->generic.out.volume_name = talloc_strdup(req,
811 lp_servicename(req->tcon->service));
818 return print queue info
820 static NTSTATUS cifspsx_lpq(struct ntvfs_module_context *ntvfs,
821 struct ntvfs_request *req, union smb_lpq *lpq)
823 return NT_STATUS_NOT_SUPPORTED;
827 list files in a directory matching a wildcard pattern
829 static NTSTATUS cifspsx_search_first(struct ntvfs_module_context *ntvfs,
830 struct ntvfs_request *req, union smb_search_first *io,
831 void *search_private,
832 BOOL (*callback)(void *, const union smb_search_data *))
834 struct cifspsx_dir *dir;
836 struct cifspsx_private *private = ntvfs->private_data;
837 struct search_state *search;
838 union smb_search_data file;
841 if (io->generic.level != RAW_SEARCH_TRANS2) {
842 return NT_STATUS_NOT_SUPPORTED;
845 if (io->generic.data_level != RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO) {
846 return NT_STATUS_NOT_SUPPORTED;
849 search = talloc_zero(private, struct search_state);
851 return NT_STATUS_NO_MEMORY;
854 max_count = io->t2ffirst.in.max_count;
856 dir = cifspsx_list(ntvfs, req, io->t2ffirst.in.pattern);
858 return NT_STATUS_FOOBAR;
861 search->handle = private->next_search_handle;
864 if (dir->count < max_count) {
865 max_count = dir->count;
868 for (i=0; i < max_count;i++) {
870 unix_to_nt_time(&file.both_directory_info.create_time, dir->files[i].st.st_ctime);
871 unix_to_nt_time(&file.both_directory_info.access_time, dir->files[i].st.st_atime);
872 unix_to_nt_time(&file.both_directory_info.write_time, dir->files[i].st.st_mtime);
873 unix_to_nt_time(&file.both_directory_info.change_time, dir->files[i].st.st_mtime);
874 file.both_directory_info.name.s = dir->files[i].name;
875 file.both_directory_info.short_name.s = dir->files[i].name;
876 file.both_directory_info.size = dir->files[i].st.st_size;
877 file.both_directory_info.attrib = cifspsx_unix_to_dos_attrib(dir->files[i].st.st_mode);
879 if (!callback(search_private, &file)) {
884 search->current_index = i;
886 io->t2ffirst.out.count = i;
887 io->t2ffirst.out.handle = search->handle;
888 io->t2ffirst.out.end_of_search = (i == dir->count) ? 1 : 0;
890 /* work out if we are going to keep the search state */
891 if ((io->t2ffirst.in.flags & FLAG_TRANS2_FIND_CLOSE) ||
892 ((io->t2ffirst.in.flags & FLAG_TRANS2_FIND_CLOSE_IF_END) && (i == dir->count))) {
895 private->next_search_handle++;
896 DLIST_ADD(private->search, search);
902 /* continue a search */
903 static NTSTATUS cifspsx_search_next(struct ntvfs_module_context *ntvfs,
904 struct ntvfs_request *req, union smb_search_next *io,
905 void *search_private,
906 BOOL (*callback)(void *, const union smb_search_data *))
908 struct cifspsx_dir *dir;
910 struct cifspsx_private *private = ntvfs->private_data;
911 struct search_state *search;
912 union smb_search_data file;
915 if (io->generic.level != RAW_SEARCH_TRANS2) {
916 return NT_STATUS_NOT_SUPPORTED;
919 if (io->generic.data_level != RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO) {
920 return NT_STATUS_NOT_SUPPORTED;
923 for (search=private->search; search; search = search->next) {
924 if (search->handle == io->t2fnext.in.handle) break;
928 /* we didn't find the search handle */
929 return NT_STATUS_FOOBAR;
934 /* the client might be asking for something other than just continuing
936 if (!(io->t2fnext.in.flags & FLAG_TRANS2_FIND_CONTINUE) &&
937 (io->t2fnext.in.flags & FLAG_TRANS2_FIND_REQUIRE_RESUME) &&
938 io->t2fnext.in.last_name && *io->t2fnext.in.last_name) {
939 /* look backwards first */
940 for (i=search->current_index; i > 0; i--) {
941 if (strcmp(io->t2fnext.in.last_name, dir->files[i-1].name) == 0) {
942 search->current_index = i;
947 /* then look forwards */
948 for (i=search->current_index+1; i <= dir->count; i++) {
949 if (strcmp(io->t2fnext.in.last_name, dir->files[i-1].name) == 0) {
950 search->current_index = i;
957 max_count = search->current_index + io->t2fnext.in.max_count;
959 if (max_count > dir->count) {
960 max_count = dir->count;
963 for (i = search->current_index; i < max_count;i++) {
965 unix_to_nt_time(&file.both_directory_info.create_time, dir->files[i].st.st_ctime);
966 unix_to_nt_time(&file.both_directory_info.access_time, dir->files[i].st.st_atime);
967 unix_to_nt_time(&file.both_directory_info.write_time, dir->files[i].st.st_mtime);
968 unix_to_nt_time(&file.both_directory_info.change_time, dir->files[i].st.st_mtime);
969 file.both_directory_info.name.s = dir->files[i].name;
970 file.both_directory_info.short_name.s = dir->files[i].name;
971 file.both_directory_info.size = dir->files[i].st.st_size;
972 file.both_directory_info.attrib = cifspsx_unix_to_dos_attrib(dir->files[i].st.st_mode);
974 if (!callback(search_private, &file)) {
979 io->t2fnext.out.count = i - search->current_index;
980 io->t2fnext.out.end_of_search = (i == dir->count) ? 1 : 0;
982 search->current_index = i;
984 /* work out if we are going to keep the search state */
985 if ((io->t2fnext.in.flags & FLAG_TRANS2_FIND_CLOSE) ||
986 ((io->t2fnext.in.flags & FLAG_TRANS2_FIND_CLOSE_IF_END) && (i == dir->count))) {
987 DLIST_REMOVE(private->search, search);
995 static NTSTATUS cifspsx_search_close(struct ntvfs_module_context *ntvfs,
996 struct ntvfs_request *req, union smb_search_close *io)
998 struct cifspsx_private *private = ntvfs->private_data;
999 struct search_state *search;
1001 for (search=private->search; search; search = search->next) {
1002 if (search->handle == io->findclose.in.handle) break;
1006 /* we didn't find the search handle */
1007 return NT_STATUS_FOOBAR;
1010 DLIST_REMOVE(private->search, search);
1011 talloc_free(search);
1013 return NT_STATUS_OK;
1016 /* SMBtrans - not used on file shares */
1017 static NTSTATUS cifspsx_trans(struct ntvfs_module_context *ntvfs,
1018 struct ntvfs_request *req, struct smb_trans2 *trans2)
1020 return NT_STATUS_ACCESS_DENIED;
1025 initialialise the POSIX disk backend, registering ourselves with the ntvfs subsystem
1027 NTSTATUS ntvfs_cifs_posix_init(void)
1030 struct ntvfs_ops ops;
1031 NTVFS_CURRENT_CRITICAL_SIZES(vers);
1035 /* fill in all the operations */
1036 ops.connect = cifspsx_connect;
1037 ops.disconnect = cifspsx_disconnect;
1038 ops.unlink = cifspsx_unlink;
1039 ops.chkpath = cifspsx_chkpath;
1040 ops.qpathinfo = cifspsx_qpathinfo;
1041 ops.setpathinfo = cifspsx_setpathinfo;
1042 ops.open = cifspsx_open;
1043 ops.mkdir = cifspsx_mkdir;
1044 ops.rmdir = cifspsx_rmdir;
1045 ops.rename = cifspsx_rename;
1046 ops.copy = cifspsx_copy;
1047 ops.ioctl = cifspsx_ioctl;
1048 ops.read = cifspsx_read;
1049 ops.write = cifspsx_write;
1050 ops.seek = cifspsx_seek;
1051 ops.flush = cifspsx_flush;
1052 ops.close = cifspsx_close;
1053 ops.exit = cifspsx_exit;
1054 ops.lock = cifspsx_lock;
1055 ops.setfileinfo = cifspsx_setfileinfo;
1056 ops.qfileinfo = cifspsx_qfileinfo;
1057 ops.fsinfo = cifspsx_fsinfo;
1058 ops.lpq = cifspsx_lpq;
1059 ops.search_first = cifspsx_search_first;
1060 ops.search_next = cifspsx_search_next;
1061 ops.search_close = cifspsx_search_close;
1062 ops.trans = cifspsx_trans;
1063 ops.logoff = cifspsx_logoff;
1064 ops.async_setup = cifspsx_async_setup;
1065 ops.cancel = cifspsx_cancel;
1067 /* register ourselves with the NTVFS subsystem. We register
1068 under names 'cifsposix'
1071 ops.type = NTVFS_DISK;
1072 ops.name = "cifsposix";
1073 ret = ntvfs_register(&ops, &vers);
1075 if (!NT_STATUS_IS_OK(ret)) {
1076 DEBUG(0,("Failed to register cifs posix backend with name: %s!\n",