r62: Fix the build
[samba.git] / source4 / ntvfs / simple / vfs_simple.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    simple NTVFS filesystem backend
5
6    Copyright (C) Andrew Tridgell 2003
7
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.
12    
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.
17    
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.
21 */
22 /*
23   this implements a very simple NTVFS filesystem backend. 
24   
25   this backend largely ignores the POSIX -> CIFS mappings, just doing absolutely
26   minimal work to give a working backend.
27 */
28
29 #include "includes.h"
30 #include "svfs.h"
31
32 #ifndef O_DIRECTORY
33 #define O_DIRECTORY 0
34 #endif
35
36 /*
37   connect to a share - used when a tree_connect operation comes
38   in. For a disk based backend we needs to ensure that the base
39   directory exists (tho it doesn't need to be accessible by the user,
40   that comes later)
41 */
42 static NTSTATUS svfs_connect(struct request_context *req, const char *sharename)
43 {
44         struct stat st;
45         struct tcon_context *conn = req->conn;
46         struct svfs_private *private;
47
48         conn->ntvfs_private = talloc(conn->mem_ctx, sizeof(struct svfs_private));
49
50         private = conn->ntvfs_private;
51
52         private->next_search_handle = 0;
53         private->connectpath = talloc_strdup(conn->mem_ctx, lp_pathname(conn->service));
54
55         /* the directory must exist */
56         if (stat(private->connectpath, &st) != 0 || !S_ISDIR(st.st_mode)) {
57                 DEBUG(0,("'%s' is not a directory, when connecting to [%s]\n", 
58                          private->connectpath, sharename));
59                 return NT_STATUS_BAD_NETWORK_NAME;
60         }
61
62         conn->fs_type = talloc_strdup(conn->mem_ctx, "NTFS");
63         conn->dev_type = talloc_strdup(conn->mem_ctx, "A:");
64
65         return NT_STATUS_OK;
66 }
67
68 /*
69   disconnect from a share
70 */
71 static NTSTATUS svfs_disconnect(struct tcon_context *req)
72 {
73         return NT_STATUS_OK;
74 }
75
76 /*
77   delete a file - the dirtype specifies the file types to include in the search. 
78   The name can contain CIFS wildcards, but rarely does (except with OS/2 clients)
79 */
80 static NTSTATUS svfs_unlink(struct request_context *req, struct smb_unlink *unl)
81 {
82         char *unix_path;
83
84         unix_path = svfs_unix_path(req, unl->in.pattern);
85
86         /* ignoring wildcards ... */
87         if (unlink(unix_path) == -1) {
88                 return map_nt_error_from_unix(errno);
89         }
90
91         return NT_STATUS_OK;
92 }
93
94
95 /*
96   ioctl interface - we don't do any
97 */
98 static NTSTATUS svfs_ioctl(struct request_context *req, union smb_ioctl *io)
99 {
100         return NT_STATUS_INVALID_PARAMETER;
101 }
102
103 /*
104   check if a directory exists
105 */
106 static NTSTATUS svfs_chkpath(struct request_context *req, struct smb_chkpath *cp)
107 {
108         char *unix_path;
109         struct stat st;
110
111         unix_path = svfs_unix_path(req, cp->in.path);
112
113         if (stat(unix_path, &st) == -1) {
114                 return map_nt_error_from_unix(errno);
115         }
116
117         if (!S_ISDIR(st.st_mode)) {
118                 return NT_STATUS_NOT_A_DIRECTORY;
119         }
120
121         return NT_STATUS_OK;
122 }
123
124 /*
125   approximately map a struct stat to a generic fileinfo struct
126 */
127 static NTSTATUS svfs_map_fileinfo(struct request_context *req, union smb_fileinfo *info, struct stat *st)
128 {
129         unix_to_nt_time(&info->generic.out.create_time, st->st_ctime);
130         unix_to_nt_time(&info->generic.out.access_time, st->st_atime);
131         unix_to_nt_time(&info->generic.out.write_time,  st->st_mtime);
132         unix_to_nt_time(&info->generic.out.change_time, st->st_mtime);
133         info->generic.out.alloc_size = st->st_size;
134         info->generic.out.size = st->st_size;
135         info->generic.out.attrib = svfs_unix_to_dos_attrib(st->st_mode);
136         info->generic.out.alloc_size = st->st_blksize * st->st_blocks;
137         info->generic.out.nlink = st->st_nlink;
138         info->generic.out.directory = S_ISDIR(st->st_mode) ? 1 : 0;
139         info->generic.out.file_id = svfs_file_id(st);
140         /* REWRITE: TODO stuff in here */
141         info->generic.out.delete_pending = 0;
142         info->generic.out.ea_size = 0;
143         info->generic.out.num_eas = 0;
144         info->generic.out.fname.s = talloc_strdup(req->mem_ctx, "TODO - STORE FILENAME");
145         info->generic.out.alt_fname.s = talloc_strdup(req->mem_ctx, "TODO - STORE ALT_FN");
146         info->generic.out.ex_attrib = 0;
147         info->generic.out.compressed_size = 0;
148         info->generic.out.format = 0;
149         info->generic.out.unit_shift = 0;
150         info->generic.out.chunk_shift = 0;
151         info->generic.out.cluster_shift = 0;
152         
153         info->generic.out.access_flags = 0;
154         info->generic.out.position = 0;
155         info->generic.out.mode = 0;
156         info->generic.out.alignment_requirement = 0;
157         info->generic.out.reparse_tag = 0;
158         info->generic.out.num_streams = 0;
159         /* setup a single data stream */
160         info->generic.out.num_streams = 1;
161         info->generic.out.streams = talloc(req->mem_ctx, sizeof(info->stream_info.out.streams[0]));
162         if (!info->generic.out.streams) {
163                 return NT_STATUS_NO_MEMORY;
164         }
165         info->generic.out.streams[0].size = st->st_size;
166         info->generic.out.streams[0].alloc_size = st->st_size;
167         info->generic.out.streams[0].stream_name.s = talloc_strdup(req->mem_ctx,"::$DATA");
168         /* REWRITE: end */
169
170         return NT_STATUS_OK;
171 }
172
173 /*
174   return info on a pathname
175 */
176 static NTSTATUS svfs_qpathinfo(struct request_context *req, union smb_fileinfo *info)
177 {
178         char *unix_path;
179         struct stat st;
180
181         DEBUG(19,("svfs_qpathinfo: file %s level 0x%x\n", info->generic.in.fname, info->generic.level));
182         if (info->generic.level != RAW_FILEINFO_GENERIC) {
183                 return ntvfs_map_qpathinfo(req, info);
184         }
185         
186         unix_path = svfs_unix_path(req, info->generic.in.fname);
187         DEBUG(19,("svfs_qpathinfo: file %s\n", unix_path));
188         if (stat(unix_path, &st) == -1) {
189                 DEBUG(19,("svfs_qpathinfo: file %s errno=%d\n", unix_path, errno));
190                 if (errno == 0)
191                         errno = ENOENT;
192                 return map_nt_error_from_unix(errno);
193         }
194         DEBUG(19,("svfs_qpathinfo: file %s, stat done\n", unix_path));
195         return svfs_map_fileinfo(req, info, &st);
196 }
197
198 /*
199   query info on a open file
200 */
201 static NTSTATUS svfs_qfileinfo(struct request_context *req, union smb_fileinfo *info)
202 {
203         struct stat st;
204
205         if (info->generic.level != RAW_FILEINFO_GENERIC) {
206                 return ntvfs_map_qfileinfo(req, info);
207         }
208         
209         if (fstat(info->generic.in.fnum, &st) == -1) {
210                 if (errno == 0)
211                         errno = ENOENT;
212                 return map_nt_error_from_unix(errno);
213         }
214
215         return svfs_map_fileinfo(req, info, &st);
216 }
217
218
219 /*
220   open a file
221 */
222 static NTSTATUS svfs_open(struct request_context *req, union smb_open *io)
223 {
224         char *unix_path;
225         struct stat st;
226         int fd, flags;
227         
228         if (io->generic.level != RAW_OPEN_GENERIC) {
229                 return ntvfs_map_open(req, io);
230         }
231
232         unix_path = svfs_unix_path(req, io->ntcreatex.in.fname);
233
234         switch (io->generic.in.open_disposition) {
235         case NTCREATEX_DISP_SUPERSEDE:
236                 flags = O_RDWR | O_CREAT | O_TRUNC;
237                 break;
238         case NTCREATEX_DISP_OPEN:
239                 flags = O_RDWR;
240                 break;
241         case NTCREATEX_DISP_CREATE:
242                 flags = O_RDWR | O_CREAT | O_EXCL;
243                 break;
244         case NTCREATEX_DISP_OPEN_IF:
245                 flags = O_RDWR | O_CREAT;
246                 break;
247         case NTCREATEX_DISP_OVERWRITE:
248                 flags = O_RDWR;
249                 break;
250         case NTCREATEX_DISP_OVERWRITE_IF:
251                 flags = O_RDWR | O_CREAT | O_TRUNC;
252                 break;
253         default:
254                 flags = O_RDWR;
255                 break;
256         }
257
258         if (io->generic.in.create_options & NTCREATEX_OPTIONS_DIRECTORY) {
259                 flags = O_RDONLY | O_DIRECTORY;
260                 switch (io->generic.in.open_disposition) {
261                 case NTCREATEX_DISP_CREATE:
262                         if (mkdir(unix_path, 0755) == -1) {
263                                 DEBUG(9,("svfs_open: mkdir %s errno=%d\n", unix_path, errno));
264                                 return map_nt_error_from_unix(errno);
265                         }
266                         break;
267                 case NTCREATEX_DISP_OPEN_IF:
268                         if (mkdir(unix_path, 0755) == -1 && errno != EEXIST) {
269                                 DEBUG(9,("svfs_open: mkdir %s errno=%d\n", unix_path, errno));
270                                 return map_nt_error_from_unix(errno);
271                         }
272                         break;
273                 }
274         }
275         DEBUG(9,("svfs_open: file %s flags=0x%x\n", unix_path, flags));
276         fd = open(unix_path, flags, 0644);
277         DEBUG(9,("svfs_open: fd=%d errno=%d\n", fd, errno));
278         if (fd == -1) {
279                 if (errno == 0)
280                         errno = ENOENT;
281                 return map_nt_error_from_unix(errno);
282         }
283
284         if (fstat(fd, &st) == -1) {
285                 DEBUG(9,("svfs_open: fstat errno=%d\n", errno));
286                 if (errno == 0)
287                         errno = ENOENT;
288                 close(fd);
289                 return map_nt_error_from_unix(errno);
290         }
291
292         ZERO_STRUCT(io->generic.out);
293         
294         unix_to_nt_time(&io->generic.out.create_time, st.st_ctime);
295         unix_to_nt_time(&io->generic.out.access_time, st.st_atime);
296         unix_to_nt_time(&io->generic.out.write_time,  st.st_mtime);
297         unix_to_nt_time(&io->generic.out.change_time, st.st_mtime);
298         io->generic.out.fnum = fd;
299         io->generic.out.alloc_size = st.st_size;
300         io->generic.out.size = st.st_size;
301         io->generic.out.attrib = svfs_unix_to_dos_attrib(st.st_mode);
302         io->generic.out.is_directory = S_ISDIR(st.st_mode) ? 1 : 0;
303
304         return NT_STATUS_OK;
305 }
306
307 /*
308   create a directory
309 */
310 static NTSTATUS svfs_mkdir(struct request_context *req, union smb_mkdir *md)
311 {
312         char *unix_path;
313
314         if (md->generic.level != RAW_MKDIR_MKDIR) {
315                 return NT_STATUS_INVALID_LEVEL;
316         }
317
318         unix_path = svfs_unix_path(req, md->mkdir.in.path);
319
320         if (mkdir(unix_path, 0777) == -1) {
321                 return map_nt_error_from_unix(errno);
322         }
323
324         return NT_STATUS_OK;
325 }
326
327 /*
328   remove a directory
329 */
330 static NTSTATUS svfs_rmdir(struct request_context *req, struct smb_rmdir *rd)
331 {
332         char *unix_path;
333
334         unix_path = svfs_unix_path(req, rd->in.path);
335
336         if (rmdir(unix_path) == -1) {
337                 return map_nt_error_from_unix(errno);
338         }
339
340         return NT_STATUS_OK;
341 }
342
343 /*
344   rename a set of files
345 */
346 static NTSTATUS svfs_rename(struct request_context *req, union smb_rename *ren)
347 {
348         char *unix_path1, *unix_path2;
349
350         if (ren->generic.level != RAW_RENAME_RENAME) {
351                 return NT_STATUS_INVALID_LEVEL;
352         }
353
354         unix_path1 = svfs_unix_path(req, ren->rename.in.pattern1);
355         unix_path2 = svfs_unix_path(req, ren->rename.in.pattern2);
356
357         if (rename(unix_path1, unix_path2) != 0) {
358                 return map_nt_error_from_unix(errno);
359         }
360         
361         return NT_STATUS_OK;
362 }
363
364 /*
365   copy a set of files
366 */
367 static NTSTATUS svfs_copy(struct request_context *req, struct smb_copy *cp)
368 {
369         return NT_STATUS_NOT_SUPPORTED;
370 }
371
372 /*
373   read from a file
374 */
375 static NTSTATUS svfs_read(struct request_context *req, union smb_read *rd)
376 {
377         ssize_t ret;
378
379         if (rd->generic.level != RAW_READ_READX) {
380                 return NT_STATUS_NOT_SUPPORTED;
381         }
382
383         ret = pread(rd->readx.in.fnum, 
384                     rd->readx.out.data, 
385                     rd->readx.in.maxcnt,
386                     rd->readx.in.offset);
387         if (ret == -1) {
388                 return map_nt_error_from_unix(errno);
389         }
390
391         rd->readx.out.nread = ret;
392         rd->readx.out.remaining = 0; /* should fill this in? */
393         rd->readx.out.compaction_mode = 0; 
394
395         return NT_STATUS_OK;
396 }
397
398 /*
399   write to a file
400 */
401 static NTSTATUS svfs_write(struct request_context *req, union smb_write *wr)
402 {
403         ssize_t ret;
404
405         switch (wr->generic.level) {
406         case RAW_WRITE_WRITEX:
407                 ret = pwrite(wr->writex.in.fnum, 
408                              wr->writex.in.data, 
409                              wr->writex.in.count,
410                              wr->writex.in.offset);
411                 if (ret == -1) {
412                         return map_nt_error_from_unix(errno);
413                 }
414                 
415                 wr->writex.out.nwritten = ret;
416                 wr->writex.out.remaining = 0; /* should fill this in? */
417
418                 return NT_STATUS_OK;
419
420         case RAW_WRITE_WRITE:
421                 if (wr->write.in.count == 0) {
422                         /* a truncate! */
423                         ret = ftruncate(wr->write.in.fnum, wr->write.in.offset);
424                 } else {
425                         ret = pwrite(wr->write.in.fnum, 
426                                      wr->write.in.data, 
427                                      wr->write.in.count,
428                                      wr->write.in.offset);
429                 }
430                 if (ret == -1) {
431                         return map_nt_error_from_unix(errno);
432                 }
433                 
434                 wr->write.out.nwritten = ret;
435
436                 return NT_STATUS_OK;
437         }
438
439         return NT_STATUS_NOT_SUPPORTED;
440 }
441
442 /*
443   seek in a file
444 */
445 static NTSTATUS svfs_seek(struct request_context *req, struct smb_seek *io)
446 {
447         return NT_STATUS_NOT_SUPPORTED;
448 }
449
450 /*
451   flush a file
452 */
453 static NTSTATUS svfs_flush(struct request_context *req, struct smb_flush *io)
454 {
455         fsync(io->in.fnum);
456         return NT_STATUS_OK;
457 }
458
459 /*
460   close a file
461 */
462 static NTSTATUS svfs_close(struct request_context *req, union smb_close *io)
463 {
464         if (io->generic.level != RAW_CLOSE_CLOSE) {
465                 /* we need a mapping function */
466                 return NT_STATUS_INVALID_LEVEL;
467         }
468
469         if (close(io->close.in.fnum) != 0) {
470                 return map_nt_error_from_unix(errno);
471         }
472
473         return NT_STATUS_OK;
474 }
475
476 /*
477   exit - closing files?
478 */
479 static NTSTATUS svfs_exit(struct request_context *req)
480 {
481         return NT_STATUS_NOT_SUPPORTED;
482 }
483
484 /*
485   lock a byte range
486 */
487 static NTSTATUS svfs_lock(struct request_context *req, union smb_lock *lck)
488 {
489         DEBUG(0,("REWRITE: not doing byte range locking!\n"));
490         return NT_STATUS_OK;
491 }
492
493 /*
494   set info on a pathname
495 */
496 static NTSTATUS svfs_setpathinfo(struct request_context *req, union smb_setfileinfo *st)
497 {
498         return NT_STATUS_NOT_SUPPORTED;
499 }
500
501 /*
502   set info on a open file
503 */
504 static NTSTATUS svfs_setfileinfo(struct request_context *req, 
505                                  union smb_setfileinfo *info)
506 {
507         struct utimbuf unix_times;
508         int fd;
509                 
510         switch (info->generic.level) {
511         case RAW_SFILEINFO_END_OF_FILE_INFO:
512         case RAW_SFILEINFO_END_OF_FILE_INFORMATION:
513                 if (ftruncate(info->end_of_file_info.file.fnum, 
514                               info->end_of_file_info.in.size) != 0) {
515                         return map_nt_error_from_unix(errno);
516                 }
517                 break;
518         case RAW_SFILEINFO_SETATTRE:
519                 unix_times.actime = info->setattre.in.access_time;
520                 unix_times.modtime = info->setattre.in.write_time;
521                 fd = info->setattre.file.fnum;
522         
523                 if (unix_times.actime == 0 && unix_times.modtime == 0) {
524                         break;
525                 } 
526
527                 /* set modify time = to access time if modify time was 0 */
528                 if (unix_times.actime != 0 && unix_times.modtime == 0) {
529                         unix_times.modtime = unix_times.actime;
530                 }
531
532                 /* Set the date on this file */
533                 if (svfs_file_utime(fd, &unix_times) != 0) {
534                         return NT_STATUS_ACCESS_DENIED;
535                 }
536                 break;
537         }
538         return NT_STATUS_OK;
539 }
540
541
542 /*
543   return filesystem space info
544 */
545 static NTSTATUS svfs_fsinfo(struct request_context *req, union smb_fsinfo *fs)
546 {
547         struct svfs_private *private = req->conn->ntvfs_private;
548         struct stat st;
549
550         if (fs->generic.level != RAW_QFS_GENERIC) {
551                 return ntvfs_map_fsinfo(req, fs);
552         }
553
554         if (sys_fsusage(private->connectpath, 
555                         &fs->generic.out.blocks_free, 
556                         &fs->generic.out.blocks_total) == -1) {
557                 return map_nt_error_from_unix(errno);
558         }
559
560         fs->generic.out.block_size = 512;
561
562         if (stat(private->connectpath, &st) != 0) {
563                 return NT_STATUS_DISK_CORRUPT_ERROR;
564         }
565         
566         fs->generic.out.fs_id = st.st_ino;
567         unix_to_nt_time(&fs->generic.out.create_time, st.st_ctime);
568         fs->generic.out.serial_number = st.st_ino;
569         fs->generic.out.fs_attr = 0;
570         fs->generic.out.max_file_component_length = 255;
571         fs->generic.out.device_type = 0;
572         fs->generic.out.device_characteristics = 0;
573         fs->generic.out.quota_soft = 0;
574         fs->generic.out.quota_hard = 0;
575         fs->generic.out.quota_flags = 0;
576         fs->generic.out.volume_name = talloc_strdup(req->mem_ctx, lp_servicename(req->conn->service));
577         fs->generic.out.fs_type = req->conn->fs_type;
578
579         return NT_STATUS_OK;
580 }
581
582 #if 0
583 /*
584   return filesystem attribute info
585 */
586 static NTSTATUS svfs_fsattr(struct request_context *req, union smb_fsattr *fs)
587 {
588         struct stat st;
589         struct svfs_private *private = req->conn->ntvfs_private;
590
591         if (fs->generic.level != RAW_FSATTR_GENERIC) {
592                 return ntvfs_map_fsattr(req, fs);
593         }
594
595         if (stat(private->connectpath, &st) != 0) {
596                 return map_nt_error_from_unix(errno);
597         }
598
599         unix_to_nt_time(&fs->generic.out.create_time, st.st_ctime);
600         fs->generic.out.fs_attr = 
601                 FILE_CASE_PRESERVED_NAMES | 
602                 FILE_CASE_SENSITIVE_SEARCH | 
603                 FILE_PERSISTENT_ACLS;
604         fs->generic.out.max_file_component_length = 255;
605         fs->generic.out.serial_number = 1;
606         fs->generic.out.fs_type = talloc_strdup(req->mem_ctx, "NTFS");
607         fs->generic.out.volume_name = talloc_strdup(req->mem_ctx, 
608                                                     lp_servicename(req->conn->service));
609
610         return NT_STATUS_OK;
611 }
612 #endif
613
614 /*
615   return print queue info
616 */
617 static NTSTATUS svfs_lpq(struct request_context *req, union smb_lpq *lpq)
618 {
619         return NT_STATUS_NOT_SUPPORTED;
620 }
621
622 /* 
623    list files in a directory matching a wildcard pattern
624 */
625 static NTSTATUS svfs_search_first(struct request_context *req, union smb_search_first *io, 
626                                   void *search_private, 
627                                   BOOL (*callback)(void *, union smb_search_data *))
628 {
629         struct svfs_dir *dir;
630         int i;
631         struct svfs_private *private = req->conn->ntvfs_private;
632         struct search_state *search;
633         union smb_search_data file;
634         TALLOC_CTX *mem_ctx;
635         uint_t max_count;
636
637         if (io->generic.level != RAW_SEARCH_BOTH_DIRECTORY_INFO) {
638                 return NT_STATUS_NOT_SUPPORTED;
639         }
640
641         mem_ctx = talloc_init("svfs_search");
642
643         search = talloc_zero(mem_ctx, sizeof(struct search_state));
644         if (!search) {
645                 return NT_STATUS_NO_MEMORY;
646         }
647
648         max_count = io->t2ffirst.in.max_count;
649
650         dir = svfs_list(mem_ctx, req, io->t2ffirst.in.pattern);
651         if (!dir) {
652                 talloc_destroy_pool(mem_ctx);
653                 return NT_STATUS_FOOBAR;
654         }
655
656         search->mem_ctx = mem_ctx;
657         search->handle = private->next_search_handle;
658         search->dir = dir;
659
660         if (dir->count < max_count) {
661                 max_count = dir->count;
662         }
663
664         for (i=0; i < max_count;i++) {
665                 ZERO_STRUCT(file);
666                 unix_to_nt_time(&file.both_directory_info.create_time, dir->files[i].st.st_ctime);
667                 unix_to_nt_time(&file.both_directory_info.access_time, dir->files[i].st.st_atime);
668                 unix_to_nt_time(&file.both_directory_info.write_time,  dir->files[i].st.st_mtime);
669                 unix_to_nt_time(&file.both_directory_info.change_time, dir->files[i].st.st_mtime);
670                 file.both_directory_info.name.s = dir->files[i].name;
671                 file.both_directory_info.short_name.s = dir->files[i].name;
672                 file.both_directory_info.size = dir->files[i].st.st_size;
673                 file.both_directory_info.attrib = svfs_unix_to_dos_attrib(dir->files[i].st.st_mode);
674
675                 if (!callback(search_private, &file)) {
676                         break;
677                 }
678         }
679
680         search->current_index = i;
681
682         io->t2ffirst.out.count = i;
683         io->t2ffirst.out.handle = search->handle;
684         io->t2ffirst.out.end_of_search = (i == dir->count) ? 1 : 0;
685
686         /* work out if we are going to keep the search state */
687         if ((io->t2ffirst.in.flags & FLAG_TRANS2_FIND_CLOSE) ||
688             ((io->t2ffirst.in.flags & FLAG_TRANS2_FIND_CLOSE_IF_END) && (i == dir->count))) {
689                 talloc_destroy(search->mem_ctx);
690         } else {
691                 private->next_search_handle++;
692                 DLIST_ADD(private->search, search);
693         }
694
695         return NT_STATUS_OK;
696 }
697
698 /* continue a search */
699 static NTSTATUS svfs_search_next(struct request_context *req, union smb_search_next *io, 
700                                  void *search_private, 
701                                  BOOL (*callback)(void *, union smb_search_data *))
702 {
703         struct svfs_dir *dir;
704         int i;
705         struct svfs_private *private = req->conn->ntvfs_private;
706         struct search_state *search;
707         union smb_search_data file;
708         uint_t max_count;
709
710         if (io->generic.level != RAW_SEARCH_BOTH_DIRECTORY_INFO) {
711                 return NT_STATUS_NOT_SUPPORTED;
712         }
713
714         for (search=private->search; search; search = search->next) {
715                 if (search->handle == io->t2fnext.in.handle) break;
716         }
717         
718         if (!search) {
719                 /* we didn't find the search handle */
720                 return NT_STATUS_FOOBAR;
721         }
722
723         dir = search->dir;
724
725         /* the client might be asking for something other than just continuing
726            with the search */
727         if (!(io->t2fnext.in.flags & FLAG_TRANS2_FIND_CONTINUE) &&
728             (io->t2fnext.in.flags & FLAG_TRANS2_FIND_REQUIRE_RESUME) &&
729             io->t2fnext.in.last_name && *io->t2fnext.in.last_name) {
730                 /* look backwards first */
731                 for (i=search->current_index; i > 0; i--) {
732                         if (strcmp(io->t2fnext.in.last_name, dir->files[i-1].name) == 0) {
733                                 search->current_index = i;
734                                 goto found;
735                         }
736                 }
737
738                 /* then look forwards */
739                 for (i=search->current_index+1; i <= dir->count; i++) {
740                         if (strcmp(io->t2fnext.in.last_name, dir->files[i-1].name) == 0) {
741                                 search->current_index = i;
742                                 goto found;
743                         }
744                 }
745         }
746
747 found:  
748         max_count = search->current_index + io->t2fnext.in.max_count;
749
750         if (max_count > dir->count) {
751                 max_count = dir->count;
752         }
753
754         for (i = search->current_index; i < max_count;i++) {
755                 ZERO_STRUCT(file);
756                 unix_to_nt_time(&file.both_directory_info.create_time, dir->files[i].st.st_ctime);
757                 unix_to_nt_time(&file.both_directory_info.access_time, dir->files[i].st.st_atime);
758                 unix_to_nt_time(&file.both_directory_info.write_time,  dir->files[i].st.st_mtime);
759                 unix_to_nt_time(&file.both_directory_info.change_time, dir->files[i].st.st_mtime);
760                 file.both_directory_info.name.s = dir->files[i].name;
761                 file.both_directory_info.short_name.s = dir->files[i].name;
762                 file.both_directory_info.size = dir->files[i].st.st_size;
763                 file.both_directory_info.attrib = svfs_unix_to_dos_attrib(dir->files[i].st.st_mode);
764
765                 if (!callback(search_private, &file)) {
766                         break;
767                 }
768         }
769
770         io->t2fnext.out.count = i - search->current_index;
771         io->t2fnext.out.end_of_search = (i == dir->count) ? 1 : 0;
772
773         search->current_index = i;
774
775         /* work out if we are going to keep the search state */
776         if ((io->t2fnext.in.flags & FLAG_TRANS2_FIND_CLOSE) ||
777             ((io->t2fnext.in.flags & FLAG_TRANS2_FIND_CLOSE_IF_END) && (i == dir->count))) {
778                 DLIST_REMOVE(private->search, search);
779                 talloc_destroy(search->mem_ctx);
780         }
781
782         return NT_STATUS_OK;
783 }
784
785 /* close a search */
786 static NTSTATUS svfs_search_close(struct request_context *req, union smb_search_close *io)
787 {
788         struct svfs_private *private = req->conn->ntvfs_private;
789         struct search_state *search;
790
791         for (search=private->search; search; search = search->next) {
792                 if (search->handle == io->findclose.in.handle) break;
793         }
794         
795         if (!search) {
796                 /* we didn't find the search handle */
797                 return NT_STATUS_FOOBAR;
798         }
799
800         DLIST_REMOVE(private->search, search);
801         talloc_destroy(search->mem_ctx);
802
803         return NT_STATUS_OK;
804 }
805
806 /* SMBtrans - not used on file shares */
807 static NTSTATUS svfs_trans(struct request_context *req, struct smb_trans2 *trans2)
808 {
809         return NT_STATUS_ACCESS_DENIED;
810 }
811
812
813 /*
814   initialialise the POSIX disk backend, registering ourselves with the ntvfs subsystem
815  */
816 NTSTATUS ntvfs_simple_init(void)
817 {
818         NTSTATUS ret;
819         struct ntvfs_ops ops;
820
821         ZERO_STRUCT(ops);
822
823         /* fill in the name and type */
824         ops.name = "simple";
825         ops.type = NTVFS_DISK;
826
827         /* fill in all the operations */
828         ops.connect = svfs_connect;
829         ops.disconnect = svfs_disconnect;
830         ops.unlink = svfs_unlink;
831         ops.chkpath = svfs_chkpath;
832         ops.qpathinfo = svfs_qpathinfo;
833         ops.setpathinfo = svfs_setpathinfo;
834         ops.open = svfs_open;
835         ops.mkdir = svfs_mkdir;
836         ops.rmdir = svfs_rmdir;
837         ops.rename = svfs_rename;
838         ops.copy = svfs_copy;
839         ops.ioctl = svfs_ioctl;
840         ops.read = svfs_read;
841         ops.write = svfs_write;
842         ops.seek = svfs_seek;
843         ops.flush = svfs_flush; 
844         ops.close = svfs_close;
845         ops.exit = svfs_exit;
846         ops.lock = svfs_lock;
847         ops.setfileinfo = svfs_setfileinfo;
848         ops.qfileinfo = svfs_qfileinfo;
849         ops.fsinfo = svfs_fsinfo;
850         ops.lpq = svfs_lpq;
851         ops.search_first = svfs_search_first;
852         ops.search_next = svfs_search_next;
853         ops.search_close = svfs_search_close;
854         ops.trans = svfs_trans;
855
856         /* register ourselves with the NTVFS subsystem. We register under the name 'default'
857            as we wish to be the default backend */
858         ret = register_backend("ntvfs", &ops);
859
860         if (!NT_STATUS_IS_OK(ret)) {
861                 DEBUG(0,("Failed to register POSIX backend!\n"));
862         }
863
864         return ret;
865 }