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