r5298: - got rid of pstring.h from includes.h. This at least makes it a bit
[jra/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 "system/filesys.h"
31 #include "svfs.h"
32 #include "system/time.h"
33 #include "dlinklist.h"
34 #include "smb_server/smb_server.h"
35
36 #ifndef O_DIRECTORY
37 #define O_DIRECTORY 0
38 #endif
39
40 #define CHECK_READ_ONLY(req) do { if (lp_readonly(req->tcon->service)) return NT_STATUS_ACCESS_DENIED; } while (0)
41
42 #ifndef HAVE_PREAD
43 static ssize_t pread(int __fd, void *__buf, size_t __nbytes, off_t __offset)
44 {
45         if (lseek(__fd, __offset, SEEK_SET) != __offset) {
46                 return -1;
47         }
48         return read(__fd, __buf, __nbytes);
49 }
50 #endif
51
52 #ifndef HAVE_PWRITE
53 static ssize_t pwrite(int __fd, const void *__buf, size_t __nbytes, off_t __offset)
54 {
55         if (lseek(__fd, __offset, SEEK_SET) != __offset) {
56                 return -1;
57         }
58         return write(__fd, __buf, __nbytes);
59 }
60 #endif
61
62 /*
63   connect to a share - used when a tree_connect operation comes
64   in. For a disk based backend we needs to ensure that the base
65   directory exists (tho it doesn't need to be accessible by the user,
66   that comes later)
67 */
68 static NTSTATUS svfs_connect(struct ntvfs_module_context *ntvfs,
69                              struct smbsrv_request *req, const char *sharename)
70 {
71         struct stat st;
72         struct smbsrv_tcon *tcon = req->tcon;
73         struct svfs_private *private;
74
75         private = talloc(tcon, struct svfs_private);
76
77         private->next_search_handle = 0;
78         private->connectpath = talloc_strdup(tcon, lp_pathname(tcon->service));
79         private->open_files = NULL;
80         private->search = NULL;
81
82         /* the directory must exist */
83         if (stat(private->connectpath, &st) != 0 || !S_ISDIR(st.st_mode)) {
84                 DEBUG(0,("'%s' is not a directory, when connecting to [%s]\n", 
85                          private->connectpath, sharename));
86                 return NT_STATUS_BAD_NETWORK_NAME;
87         }
88
89         tcon->fs_type = talloc_strdup(tcon, "NTFS");
90         tcon->dev_type = talloc_strdup(tcon, "A:");
91
92         ntvfs->private_data = private;
93
94         DEBUG(0,("WARNING: ntvfs simple: connect to share [%s] with ROOT privileges!!!\n",sharename));
95
96         return NT_STATUS_OK;
97 }
98
99 /*
100   disconnect from a share
101 */
102 static NTSTATUS svfs_disconnect(struct ntvfs_module_context *ntvfs,
103                                 struct smbsrv_tcon *tcon)
104 {
105         return NT_STATUS_OK;
106 }
107
108 /*
109   find open file handle given fd
110 */
111 static struct svfs_file *find_fd(struct svfs_private *private, int fd)
112 {
113         struct svfs_file *f;
114         for (f=private->open_files;f;f=f->next) {
115                 if (f->fd == fd) {
116                         return f;
117                 }
118         }
119         return NULL;
120 }
121
122 /*
123   delete a file - the dirtype specifies the file types to include in the search. 
124   The name can contain CIFS wildcards, but rarely does (except with OS/2 clients)
125 */
126 static NTSTATUS svfs_unlink(struct ntvfs_module_context *ntvfs,
127                             struct smbsrv_request *req, struct smb_unlink *unl)
128 {
129         char *unix_path;
130
131         CHECK_READ_ONLY(req);
132
133         unix_path = svfs_unix_path(ntvfs, req, unl->in.pattern);
134
135         /* ignoring wildcards ... */
136         if (unlink(unix_path) == -1) {
137                 return map_nt_error_from_unix(errno);
138         }
139
140         return NT_STATUS_OK;
141 }
142
143
144 /*
145   ioctl interface - we don't do any
146 */
147 static NTSTATUS svfs_ioctl(struct ntvfs_module_context *ntvfs,
148                            struct smbsrv_request *req, union smb_ioctl *io)
149 {
150         return NT_STATUS_INVALID_PARAMETER;
151 }
152
153 /*
154   check if a directory exists
155 */
156 static NTSTATUS svfs_chkpath(struct ntvfs_module_context *ntvfs,
157                              struct smbsrv_request *req, struct smb_chkpath *cp)
158 {
159         char *unix_path;
160         struct stat st;
161
162         unix_path = svfs_unix_path(ntvfs, req, cp->in.path);
163
164         if (stat(unix_path, &st) == -1) {
165                 return map_nt_error_from_unix(errno);
166         }
167
168         if (!S_ISDIR(st.st_mode)) {
169                 return NT_STATUS_NOT_A_DIRECTORY;
170         }
171
172         return NT_STATUS_OK;
173 }
174
175 /*
176   approximately map a struct stat to a generic fileinfo struct
177 */
178 static NTSTATUS svfs_map_fileinfo(struct ntvfs_module_context *ntvfs,
179                                   struct smbsrv_request *req, union smb_fileinfo *info, 
180                                   struct stat *st, const char *unix_path)
181 {
182         struct svfs_dir *dir = NULL;
183         char *pattern = NULL;
184         int i;
185         const char *s, *short_name;
186
187         s = strrchr(unix_path, '/');
188         if (s) {
189                 short_name = s+1;
190         } else {
191                 short_name = "";
192         }
193
194         asprintf(&pattern, "%s:*", unix_path);
195         
196         if (pattern) {
197                 dir = svfs_list_unix(req, req, pattern);
198         }
199
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 = svfs_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 = svfs_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;
222         
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;
236         }
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");
240
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;
246         }
247
248         return NT_STATUS_OK;
249 }
250
251 /*
252   return info on a pathname
253 */
254 static NTSTATUS svfs_qpathinfo(struct ntvfs_module_context *ntvfs,
255                                struct smbsrv_request *req, union smb_fileinfo *info)
256 {
257         char *unix_path;
258         struct stat st;
259
260         DEBUG(19,("svfs_qpathinfo: file %s level 0x%x\n", info->generic.in.fname, info->generic.level));
261         if (info->generic.level != RAW_FILEINFO_GENERIC) {
262                 return ntvfs_map_qpathinfo(req, info, ntvfs);
263         }
264         
265         unix_path = svfs_unix_path(ntvfs, req, info->generic.in.fname);
266         DEBUG(19,("svfs_qpathinfo: file %s\n", unix_path));
267         if (stat(unix_path, &st) == -1) {
268                 DEBUG(19,("svfs_qpathinfo: file %s errno=%d\n", unix_path, errno));
269                 return map_nt_error_from_unix(errno);
270         }
271         DEBUG(19,("svfs_qpathinfo: file %s, stat done\n", unix_path));
272         return svfs_map_fileinfo(ntvfs, req, info, &st, unix_path);
273 }
274
275 /*
276   query info on a open file
277 */
278 static NTSTATUS svfs_qfileinfo(struct ntvfs_module_context *ntvfs,
279                                struct smbsrv_request *req, union smb_fileinfo *info)
280 {
281         struct svfs_private *private = ntvfs->private_data;
282         struct svfs_file *f;
283         struct stat st;
284
285         if (info->generic.level != RAW_FILEINFO_GENERIC) {
286                 return ntvfs_map_qfileinfo(req, info, ntvfs);
287         }
288
289         f = find_fd(private, info->generic.in.fnum);
290         if (!f) {
291                 return NT_STATUS_INVALID_HANDLE;
292         }
293         
294         if (fstat(info->generic.in.fnum, &st) == -1) {
295                 return map_nt_error_from_unix(errno);
296         }
297
298         return svfs_map_fileinfo(ntvfs, req,info, &st, f->name);
299 }
300
301
302 /*
303   open a file
304 */
305 static NTSTATUS svfs_open(struct ntvfs_module_context *ntvfs,
306                           struct smbsrv_request *req, union smb_open *io)
307 {
308         struct svfs_private *private = ntvfs->private_data;
309         char *unix_path;
310         struct stat st;
311         int fd, flags;
312         struct svfs_file *f;
313         int create_flags, rdwr_flags;
314         
315         if (io->generic.level != RAW_OPEN_GENERIC) {
316                 return ntvfs_map_open(req, io, ntvfs);
317         }
318
319         if (lp_readonly(req->tcon->service)) {
320                 create_flags = 0;
321                 rdwr_flags = O_RDONLY;
322         } else {
323                 create_flags = O_CREAT;
324                 rdwr_flags = O_RDWR;
325         }
326
327         unix_path = svfs_unix_path(ntvfs, req, io->ntcreatex.in.fname);
328
329         switch (io->generic.in.open_disposition) {
330         case NTCREATEX_DISP_SUPERSEDE:
331         case NTCREATEX_DISP_OVERWRITE_IF:
332                 flags = create_flags | O_TRUNC;
333                 break;
334         case NTCREATEX_DISP_OPEN:
335         case NTCREATEX_DISP_OVERWRITE:
336                 flags = 0;
337                 break;
338         case NTCREATEX_DISP_CREATE:
339                 flags = create_flags | O_EXCL;
340                 break;
341         case NTCREATEX_DISP_OPEN_IF:
342                 flags = create_flags;
343                 break;
344         default:
345                 flags = 0;
346                 break;
347         }
348         
349         flags |= rdwr_flags;
350
351         if (io->generic.in.create_options & NTCREATEX_OPTIONS_DIRECTORY) {
352                 flags = O_RDONLY | O_DIRECTORY;
353                 if (lp_readonly(req->tcon->service)) {
354                         goto do_open;
355                 }
356                 switch (io->generic.in.open_disposition) {
357                 case NTCREATEX_DISP_CREATE:
358                         if (mkdir(unix_path, 0755) == -1) {
359                                 DEBUG(9,("svfs_open: mkdir %s errno=%d\n", unix_path, errno));
360                                 return map_nt_error_from_unix(errno);
361                         }
362                         break;
363                 case NTCREATEX_DISP_OPEN_IF:
364                         if (mkdir(unix_path, 0755) == -1 && errno != EEXIST) {
365                                 DEBUG(9,("svfs_open: mkdir %s errno=%d\n", unix_path, errno));
366                                 return map_nt_error_from_unix(errno);
367                         }
368                         break;
369                 }
370         }
371
372 do_open:
373         fd = open(unix_path, flags, 0644);
374         if (fd == -1) {
375                 return map_nt_error_from_unix(errno);
376         }
377
378         if (fstat(fd, &st) == -1) {
379                 DEBUG(9,("svfs_open: fstat errno=%d\n", errno));
380                 close(fd);
381                 return map_nt_error_from_unix(errno);
382         }
383
384         f = talloc(req->tcon, struct svfs_file);
385         f->fd = fd;
386         f->name = talloc_strdup(req->tcon, unix_path);
387
388         DLIST_ADD(private->open_files, f);
389
390         ZERO_STRUCT(io->generic.out);
391         
392         unix_to_nt_time(&io->generic.out.create_time, st.st_ctime);
393         unix_to_nt_time(&io->generic.out.access_time, st.st_atime);
394         unix_to_nt_time(&io->generic.out.write_time,  st.st_mtime);
395         unix_to_nt_time(&io->generic.out.change_time, st.st_mtime);
396         io->generic.out.fnum = fd;
397         io->generic.out.alloc_size = st.st_size;
398         io->generic.out.size = st.st_size;
399         io->generic.out.attrib = svfs_unix_to_dos_attrib(st.st_mode);
400         io->generic.out.is_directory = S_ISDIR(st.st_mode) ? 1 : 0;
401
402         return NT_STATUS_OK;
403 }
404
405 /*
406   create a directory
407 */
408 static NTSTATUS svfs_mkdir(struct ntvfs_module_context *ntvfs,
409                            struct smbsrv_request *req, union smb_mkdir *md)
410 {
411         char *unix_path;
412
413         CHECK_READ_ONLY(req);
414
415         if (md->generic.level != RAW_MKDIR_MKDIR) {
416                 return NT_STATUS_INVALID_LEVEL;
417         }
418
419         unix_path = svfs_unix_path(ntvfs, req, md->mkdir.in.path);
420
421         if (mkdir(unix_path, 0777) == -1) {
422                 return map_nt_error_from_unix(errno);
423         }
424
425         return NT_STATUS_OK;
426 }
427
428 /*
429   remove a directory
430 */
431 static NTSTATUS svfs_rmdir(struct ntvfs_module_context *ntvfs,
432                            struct smbsrv_request *req, struct smb_rmdir *rd)
433 {
434         char *unix_path;
435
436         CHECK_READ_ONLY(req);
437
438         unix_path = svfs_unix_path(ntvfs, req, rd->in.path);
439
440         if (rmdir(unix_path) == -1) {
441                 return map_nt_error_from_unix(errno);
442         }
443
444         return NT_STATUS_OK;
445 }
446
447 /*
448   rename a set of files
449 */
450 static NTSTATUS svfs_rename(struct ntvfs_module_context *ntvfs,
451                             struct smbsrv_request *req, union smb_rename *ren)
452 {
453         char *unix_path1, *unix_path2;
454
455         CHECK_READ_ONLY(req);
456
457         if (ren->generic.level != RAW_RENAME_RENAME) {
458                 return NT_STATUS_INVALID_LEVEL;
459         }
460
461         unix_path1 = svfs_unix_path(ntvfs, req, ren->rename.in.pattern1);
462         unix_path2 = svfs_unix_path(ntvfs, req, ren->rename.in.pattern2);
463
464         if (rename(unix_path1, unix_path2) == -1) {
465                 return map_nt_error_from_unix(errno);
466         }
467         
468         return NT_STATUS_OK;
469 }
470
471 /*
472   copy a set of files
473 */
474 static NTSTATUS svfs_copy(struct ntvfs_module_context *ntvfs,
475                           struct smbsrv_request *req, struct smb_copy *cp)
476 {
477         return NT_STATUS_NOT_SUPPORTED;
478 }
479
480 /*
481   read from a file
482 */
483 static NTSTATUS svfs_read(struct ntvfs_module_context *ntvfs,
484                           struct smbsrv_request *req, union smb_read *rd)
485 {
486         ssize_t ret;
487
488         if (rd->generic.level != RAW_READ_READX) {
489                 return NT_STATUS_NOT_SUPPORTED;
490         }
491
492         ret = pread(rd->readx.in.fnum, 
493                     rd->readx.out.data, 
494                     rd->readx.in.maxcnt,
495                     rd->readx.in.offset);
496         if (ret == -1) {
497                 return map_nt_error_from_unix(errno);
498         }
499
500         rd->readx.out.nread = ret;
501         rd->readx.out.remaining = 0; /* should fill this in? */
502         rd->readx.out.compaction_mode = 0; 
503
504         return NT_STATUS_OK;
505 }
506
507 /*
508   write to a file
509 */
510 static NTSTATUS svfs_write(struct ntvfs_module_context *ntvfs,
511                            struct smbsrv_request *req, union smb_write *wr)
512 {
513         ssize_t ret;
514
515         if (wr->generic.level != RAW_WRITE_WRITEX) {
516                 return ntvfs_map_write(req, wr, ntvfs);
517         }
518
519         CHECK_READ_ONLY(req);
520
521         ret = pwrite(wr->writex.in.fnum, 
522                      wr->writex.in.data, 
523                      wr->writex.in.count,
524                      wr->writex.in.offset);
525         if (ret == -1) {
526                 return map_nt_error_from_unix(errno);
527         }
528                 
529         wr->writex.out.nwritten = ret;
530         wr->writex.out.remaining = 0; /* should fill this in? */
531         
532         return NT_STATUS_OK;
533 }
534
535 /*
536   seek in a file
537 */
538 static NTSTATUS svfs_seek(struct ntvfs_module_context *ntvfs,
539                           struct smbsrv_request *req, struct smb_seek *io)
540 {
541         return NT_STATUS_NOT_SUPPORTED;
542 }
543
544 /*
545   flush a file
546 */
547 static NTSTATUS svfs_flush(struct ntvfs_module_context *ntvfs,
548                            struct smbsrv_request *req, struct smb_flush *io)
549 {
550         fsync(io->in.fnum);
551         return NT_STATUS_OK;
552 }
553
554 /*
555   close a file
556 */
557 static NTSTATUS svfs_close(struct ntvfs_module_context *ntvfs,
558                            struct smbsrv_request *req, union smb_close *io)
559 {
560         struct svfs_private *private = ntvfs->private_data;
561         struct svfs_file *f;
562
563         if (io->generic.level != RAW_CLOSE_CLOSE) {
564                 /* we need a mapping function */
565                 return NT_STATUS_INVALID_LEVEL;
566         }
567
568         f = find_fd(private, io->close.in.fnum);
569         if (!f) {
570                 return NT_STATUS_INVALID_HANDLE;
571         }
572
573         if (close(io->close.in.fnum) == -1) {
574                 return map_nt_error_from_unix(errno);
575         }
576
577         DLIST_REMOVE(private->open_files, f);
578         talloc_free(f->name);
579         talloc_free(f);
580
581         return NT_STATUS_OK;
582 }
583
584 /*
585   exit - closing files
586 */
587 static NTSTATUS svfs_exit(struct ntvfs_module_context *ntvfs,
588                           struct smbsrv_request *req)
589 {
590         return NT_STATUS_NOT_SUPPORTED;
591 }
592
593 /*
594   logoff - closing files
595 */
596 static NTSTATUS svfs_logoff(struct ntvfs_module_context *ntvfs,
597                             struct smbsrv_request *req)
598 {
599         return NT_STATUS_NOT_SUPPORTED;
600 }
601
602 /*
603   setup for an async call
604 */
605 static NTSTATUS svfs_async_setup(struct ntvfs_module_context *ntvfs,
606                                  struct smbsrv_request *req, 
607                                  void *private)
608 {
609         return NT_STATUS_OK;
610 }
611
612 /*
613   cancel an async call
614 */
615 static NTSTATUS svfs_cancel(struct ntvfs_module_context *ntvfs, struct smbsrv_request *req)
616 {
617         return NT_STATUS_UNSUCCESSFUL;
618 }
619
620 /*
621   lock a byte range
622 */
623 static NTSTATUS svfs_lock(struct ntvfs_module_context *ntvfs,
624                           struct smbsrv_request *req, union smb_lock *lck)
625 {
626         DEBUG(0,("REWRITE: not doing byte range locking!\n"));
627         return NT_STATUS_OK;
628 }
629
630 /*
631   set info on a pathname
632 */
633 static NTSTATUS svfs_setpathinfo(struct ntvfs_module_context *ntvfs,
634                                  struct smbsrv_request *req, union smb_setfileinfo *st)
635 {
636         CHECK_READ_ONLY(req);
637
638         return NT_STATUS_NOT_SUPPORTED;
639 }
640
641 /*
642   set info on a open file
643 */
644 static NTSTATUS svfs_setfileinfo(struct ntvfs_module_context *ntvfs,
645                                  struct smbsrv_request *req, 
646                                  union smb_setfileinfo *info)
647 {
648         struct utimbuf unix_times;
649         int fd;
650
651         CHECK_READ_ONLY(req);
652                 
653         switch (info->generic.level) {
654         case RAW_SFILEINFO_END_OF_FILE_INFO:
655         case RAW_SFILEINFO_END_OF_FILE_INFORMATION:
656                 if (ftruncate(info->end_of_file_info.file.fnum, 
657                               info->end_of_file_info.in.size) == -1) {
658                         return map_nt_error_from_unix(errno);
659                 }
660                 break;
661         case RAW_SFILEINFO_SETATTRE:
662                 unix_times.actime = info->setattre.in.access_time;
663                 unix_times.modtime = info->setattre.in.write_time;
664                 fd = info->setattre.file.fnum;
665         
666                 if (unix_times.actime == 0 && unix_times.modtime == 0) {
667                         break;
668                 } 
669
670                 /* set modify time = to access time if modify time was 0 */
671                 if (unix_times.actime != 0 && unix_times.modtime == 0) {
672                         unix_times.modtime = unix_times.actime;
673                 }
674
675                 /* Set the date on this file */
676                 if (svfs_file_utime(fd, &unix_times) != 0) {
677                         return NT_STATUS_ACCESS_DENIED;
678                 }
679                 break;
680         default:
681                 DEBUG(2,("svfs_setfileinfo: level %d not implemented\n", 
682                          info->generic.level));
683                 return NT_STATUS_NOT_IMPLEMENTED;
684         }
685         return NT_STATUS_OK;
686 }
687
688
689 /*
690   return filesystem space info
691 */
692 static NTSTATUS svfs_fsinfo(struct ntvfs_module_context *ntvfs,
693                             struct smbsrv_request *req, union smb_fsinfo *fs)
694 {
695         struct svfs_private *private = ntvfs->private_data;
696         struct stat st;
697
698         if (fs->generic.level != RAW_QFS_GENERIC) {
699                 return ntvfs_map_fsinfo(req, fs, ntvfs);
700         }
701
702         if (sys_fsusage(private->connectpath, 
703                         &fs->generic.out.blocks_free, 
704                         &fs->generic.out.blocks_total) == -1) {
705                 return map_nt_error_from_unix(errno);
706         }
707
708         fs->generic.out.block_size = 512;
709
710         if (stat(private->connectpath, &st) != 0) {
711                 return NT_STATUS_DISK_CORRUPT_ERROR;
712         }
713         
714         fs->generic.out.fs_id = st.st_ino;
715         unix_to_nt_time(&fs->generic.out.create_time, st.st_ctime);
716         fs->generic.out.serial_number = st.st_ino;
717         fs->generic.out.fs_attr = 0;
718         fs->generic.out.max_file_component_length = 255;
719         fs->generic.out.device_type = 0;
720         fs->generic.out.device_characteristics = 0;
721         fs->generic.out.quota_soft = 0;
722         fs->generic.out.quota_hard = 0;
723         fs->generic.out.quota_flags = 0;
724         fs->generic.out.volume_name = talloc_strdup(req, lp_servicename(req->tcon->service));
725         fs->generic.out.fs_type = req->tcon->fs_type;
726
727         return NT_STATUS_OK;
728 }
729
730 #if 0
731 /*
732   return filesystem attribute info
733 */
734 static NTSTATUS svfs_fsattr(struct ntvfs_module_context *ntvfs,
735                             struct smbsrv_request *req, union smb_fsattr *fs)
736 {
737         struct stat st;
738         struct svfs_private *private = ntvfs->private_data;
739
740         if (fs->generic.level != RAW_FSATTR_GENERIC) {
741                 return ntvfs_map_fsattr(req, fs, ntvfs);
742         }
743
744         if (stat(private->connectpath, &st) == -1) {
745                 return map_nt_error_from_unix(errno);
746         }
747
748         unix_to_nt_time(&fs->generic.out.create_time, st.st_ctime);
749         fs->generic.out.fs_attr = 
750                 FILE_CASE_PRESERVED_NAMES | 
751                 FILE_CASE_SENSITIVE_SEARCH | 
752                 FILE_PERSISTENT_ACLS;
753         fs->generic.out.max_file_component_length = 255;
754         fs->generic.out.serial_number = 1;
755         fs->generic.out.fs_type = talloc_strdup(req, "NTFS");
756         fs->generic.out.volume_name = talloc_strdup(req, 
757                                                     lp_servicename(req->tcon->service));
758
759         return NT_STATUS_OK;
760 }
761 #endif
762
763 /*
764   return print queue info
765 */
766 static NTSTATUS svfs_lpq(struct ntvfs_module_context *ntvfs,
767                          struct smbsrv_request *req, union smb_lpq *lpq)
768 {
769         return NT_STATUS_NOT_SUPPORTED;
770 }
771
772 /* 
773    list files in a directory matching a wildcard pattern
774 */
775 static NTSTATUS svfs_search_first(struct ntvfs_module_context *ntvfs,
776                                   struct smbsrv_request *req, union smb_search_first *io, 
777                                   void *search_private, 
778                                   BOOL (*callback)(void *, union smb_search_data *))
779 {
780         struct svfs_dir *dir;
781         int i;
782         struct svfs_private *private = ntvfs->private_data;
783         struct search_state *search;
784         union smb_search_data file;
785         uint_t max_count;
786
787         if (io->generic.level != RAW_SEARCH_BOTH_DIRECTORY_INFO) {
788                 return NT_STATUS_NOT_SUPPORTED;
789         }
790
791         search = talloc_zero(private, struct search_state);
792         if (!search) {
793                 return NT_STATUS_NO_MEMORY;
794         }
795
796         max_count = io->t2ffirst.in.max_count;
797
798         dir = svfs_list(ntvfs, req, io->t2ffirst.in.pattern);
799         if (!dir) {
800                 return NT_STATUS_FOOBAR;
801         }
802
803         search->handle = private->next_search_handle;
804         search->dir = dir;
805
806         if (dir->count < max_count) {
807                 max_count = dir->count;
808         }
809
810         for (i=0; i < max_count;i++) {
811                 ZERO_STRUCT(file);
812                 unix_to_nt_time(&file.both_directory_info.create_time, dir->files[i].st.st_ctime);
813                 unix_to_nt_time(&file.both_directory_info.access_time, dir->files[i].st.st_atime);
814                 unix_to_nt_time(&file.both_directory_info.write_time,  dir->files[i].st.st_mtime);
815                 unix_to_nt_time(&file.both_directory_info.change_time, dir->files[i].st.st_mtime);
816                 file.both_directory_info.name.s = dir->files[i].name;
817                 file.both_directory_info.short_name.s = dir->files[i].name;
818                 file.both_directory_info.size = dir->files[i].st.st_size;
819                 file.both_directory_info.attrib = svfs_unix_to_dos_attrib(dir->files[i].st.st_mode);
820
821                 if (!callback(search_private, &file)) {
822                         break;
823                 }
824         }
825
826         search->current_index = i;
827
828         io->t2ffirst.out.count = i;
829         io->t2ffirst.out.handle = search->handle;
830         io->t2ffirst.out.end_of_search = (i == dir->count) ? 1 : 0;
831
832         /* work out if we are going to keep the search state */
833         if ((io->t2ffirst.in.flags & FLAG_TRANS2_FIND_CLOSE) ||
834             ((io->t2ffirst.in.flags & FLAG_TRANS2_FIND_CLOSE_IF_END) && (i == dir->count))) {
835                 talloc_free(search);
836         } else {
837                 private->next_search_handle++;
838                 DLIST_ADD(private->search, search);
839         }
840
841         return NT_STATUS_OK;
842 }
843
844 /* continue a search */
845 static NTSTATUS svfs_search_next(struct ntvfs_module_context *ntvfs,
846                                  struct smbsrv_request *req, union smb_search_next *io, 
847                                  void *search_private, 
848                                  BOOL (*callback)(void *, union smb_search_data *))
849 {
850         struct svfs_dir *dir;
851         int i;
852         struct svfs_private *private = ntvfs->private_data;
853         struct search_state *search;
854         union smb_search_data file;
855         uint_t max_count;
856
857         if (io->generic.level != RAW_SEARCH_BOTH_DIRECTORY_INFO) {
858                 return NT_STATUS_NOT_SUPPORTED;
859         }
860
861         for (search=private->search; search; search = search->next) {
862                 if (search->handle == io->t2fnext.in.handle) break;
863         }
864         
865         if (!search) {
866                 /* we didn't find the search handle */
867                 return NT_STATUS_FOOBAR;
868         }
869
870         dir = search->dir;
871
872         /* the client might be asking for something other than just continuing
873            with the search */
874         if (!(io->t2fnext.in.flags & FLAG_TRANS2_FIND_CONTINUE) &&
875             (io->t2fnext.in.flags & FLAG_TRANS2_FIND_REQUIRE_RESUME) &&
876             io->t2fnext.in.last_name && *io->t2fnext.in.last_name) {
877                 /* look backwards first */
878                 for (i=search->current_index; i > 0; i--) {
879                         if (strcmp(io->t2fnext.in.last_name, dir->files[i-1].name) == 0) {
880                                 search->current_index = i;
881                                 goto found;
882                         }
883                 }
884
885                 /* then look forwards */
886                 for (i=search->current_index+1; i <= dir->count; i++) {
887                         if (strcmp(io->t2fnext.in.last_name, dir->files[i-1].name) == 0) {
888                                 search->current_index = i;
889                                 goto found;
890                         }
891                 }
892         }
893
894 found:  
895         max_count = search->current_index + io->t2fnext.in.max_count;
896
897         if (max_count > dir->count) {
898                 max_count = dir->count;
899         }
900
901         for (i = search->current_index; i < max_count;i++) {
902                 ZERO_STRUCT(file);
903                 unix_to_nt_time(&file.both_directory_info.create_time, dir->files[i].st.st_ctime);
904                 unix_to_nt_time(&file.both_directory_info.access_time, dir->files[i].st.st_atime);
905                 unix_to_nt_time(&file.both_directory_info.write_time,  dir->files[i].st.st_mtime);
906                 unix_to_nt_time(&file.both_directory_info.change_time, dir->files[i].st.st_mtime);
907                 file.both_directory_info.name.s = dir->files[i].name;
908                 file.both_directory_info.short_name.s = dir->files[i].name;
909                 file.both_directory_info.size = dir->files[i].st.st_size;
910                 file.both_directory_info.attrib = svfs_unix_to_dos_attrib(dir->files[i].st.st_mode);
911
912                 if (!callback(search_private, &file)) {
913                         break;
914                 }
915         }
916
917         io->t2fnext.out.count = i - search->current_index;
918         io->t2fnext.out.end_of_search = (i == dir->count) ? 1 : 0;
919
920         search->current_index = i;
921
922         /* work out if we are going to keep the search state */
923         if ((io->t2fnext.in.flags & FLAG_TRANS2_FIND_CLOSE) ||
924             ((io->t2fnext.in.flags & FLAG_TRANS2_FIND_CLOSE_IF_END) && (i == dir->count))) {
925                 DLIST_REMOVE(private->search, search);
926                 talloc_free(search);
927         }
928
929         return NT_STATUS_OK;
930 }
931
932 /* close a search */
933 static NTSTATUS svfs_search_close(struct ntvfs_module_context *ntvfs,
934                                   struct smbsrv_request *req, union smb_search_close *io)
935 {
936         struct svfs_private *private = ntvfs->private_data;
937         struct search_state *search;
938
939         for (search=private->search; search; search = search->next) {
940                 if (search->handle == io->findclose.in.handle) break;
941         }
942         
943         if (!search) {
944                 /* we didn't find the search handle */
945                 return NT_STATUS_FOOBAR;
946         }
947
948         DLIST_REMOVE(private->search, search);
949         talloc_free(search);
950
951         return NT_STATUS_OK;
952 }
953
954 /* SMBtrans - not used on file shares */
955 static NTSTATUS svfs_trans(struct ntvfs_module_context *ntvfs,
956                            struct smbsrv_request *req, struct smb_trans2 *trans2)
957 {
958         return NT_STATUS_ACCESS_DENIED;
959 }
960
961
962 /*
963   initialialise the POSIX disk backend, registering ourselves with the ntvfs subsystem
964  */
965 NTSTATUS ntvfs_simple_init(void)
966 {
967         NTSTATUS ret;
968         struct ntvfs_ops ops;
969
970         ZERO_STRUCT(ops);
971
972         /* fill in all the operations */
973         ops.connect = svfs_connect;
974         ops.disconnect = svfs_disconnect;
975         ops.unlink = svfs_unlink;
976         ops.chkpath = svfs_chkpath;
977         ops.qpathinfo = svfs_qpathinfo;
978         ops.setpathinfo = svfs_setpathinfo;
979         ops.openfile = svfs_open;
980         ops.mkdir = svfs_mkdir;
981         ops.rmdir = svfs_rmdir;
982         ops.rename = svfs_rename;
983         ops.copy = svfs_copy;
984         ops.ioctl = svfs_ioctl;
985         ops.read = svfs_read;
986         ops.write = svfs_write;
987         ops.seek = svfs_seek;
988         ops.flush = svfs_flush; 
989         ops.close = svfs_close;
990         ops.exit = svfs_exit;
991         ops.lock = svfs_lock;
992         ops.setfileinfo = svfs_setfileinfo;
993         ops.qfileinfo = svfs_qfileinfo;
994         ops.fsinfo = svfs_fsinfo;
995         ops.lpq = svfs_lpq;
996         ops.search_first = svfs_search_first;
997         ops.search_next = svfs_search_next;
998         ops.search_close = svfs_search_close;
999         ops.trans = svfs_trans;
1000         ops.logoff = svfs_logoff;
1001         ops.async_setup = svfs_async_setup;
1002         ops.cancel = svfs_cancel;
1003
1004         /* register ourselves with the NTVFS subsystem. We register
1005            under names 'simple'
1006         */
1007
1008         ops.type = NTVFS_DISK;
1009         ops.name = "simple";
1010         ret = ntvfs_register(&ops);
1011
1012         if (!NT_STATUS_IS_OK(ret)) {
1013                 DEBUG(0,("Failed to register simple backend with name: %s!\n",
1014                          ops.name));
1015         }
1016
1017         return ret;
1018 }