r2561: completely redid the ntvfs module chaining code, You can now do something...
[samba.git] / source / ntvfs / posix / pvfs_open.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    POSIX NTVFS backend - open and close
5
6    Copyright (C) Andrew Tridgell 2004
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 #include "include/includes.h"
24 #include "vfs_posix.h"
25
26
27 /*
28   find open file handle given fnum
29 */
30 struct pvfs_file *pvfs_find_fd(struct pvfs_state *pvfs, uint16_t fnum)
31 {
32         struct pvfs_file *f;
33         for (f=pvfs->open_files;f;f=f->next) {
34                 if (f->fnum == fnum) {
35                         return f;
36                 }
37         }
38         return NULL;
39 }
40
41 /*
42   open a file
43   TODO: this is a temporary implementation derived from the simple backend
44   its purpose is to allow other tests to run 
45 */
46 NTSTATUS pvfs_open(struct smbsrv_request *req, union smb_open *io)
47 {
48         NTVFS_GET_PRIVATE(pvfs_state, pvfs, req);
49         int fd, flags;
50         struct pvfs_filename *name;
51         struct pvfs_file *f;
52         NTSTATUS status;
53
54         if (io->generic.level != RAW_OPEN_GENERIC) {
55                 return ntvfs_map_open(req, io, pvfs->ops);
56         }
57
58         /* resolve the cifs name to a posix name */
59         status = pvfs_resolve_name(pvfs, req, io->ntcreatex.in.fname,
60                                    PVFS_RESOLVE_NO_WILDCARD, &name);
61         if (!NT_STATUS_IS_OK(status)) {
62                 return status;
63         }
64
65         switch (io->generic.in.open_disposition) {
66         case NTCREATEX_DISP_SUPERSEDE:
67         case NTCREATEX_DISP_OVERWRITE_IF:
68                 flags = O_CREAT | O_TRUNC;
69                 break;
70         case NTCREATEX_DISP_OPEN:
71         case NTCREATEX_DISP_OVERWRITE:
72                 flags = 0;
73                 break;
74         case NTCREATEX_DISP_CREATE:
75                 flags = O_CREAT | O_EXCL;
76                 break;
77         case NTCREATEX_DISP_OPEN_IF:
78                 flags = O_CREAT;
79                 break;
80         default:
81                 flags = 0;
82                 break;
83         }
84         
85         flags |= O_RDWR;
86
87 /* we need to do this differently to support systems without O_DIRECTORY */
88 #ifndef O_DIRECTORY
89 #define O_DIRECTORY 0
90 #endif
91
92         if (io->generic.in.create_options & NTCREATEX_OPTIONS_DIRECTORY) {
93                 flags = O_RDONLY | O_DIRECTORY;
94                 if (pvfs->flags & PVFS_FLAG_READONLY) {
95                         goto do_open;
96                 }
97                 switch (io->generic.in.open_disposition) {
98                 case NTCREATEX_DISP_CREATE:
99                         if (mkdir(name->full_name, 0755) == -1) {
100                                 return pvfs_map_errno(pvfs,errno);
101                         }
102                         break;
103                 case NTCREATEX_DISP_OPEN_IF:
104                         if (mkdir(name->full_name, 0755) == -1 && errno != EEXIST) {
105                                 return pvfs_map_errno(pvfs,errno);
106                         }
107                         break;
108                 }
109         }
110
111 do_open:
112         fd = open(name->full_name, flags, 0644);
113         if (fd == -1) {
114                 if (errno == 0)
115                         errno = ENOENT;
116                 return pvfs_map_errno(pvfs,errno);
117         }
118
119         f = talloc_p(pvfs, struct pvfs_file);
120         if (f == NULL) {
121                 close(fd);
122                 return NT_STATUS_NO_MEMORY;
123         }
124
125         /* re-resolve the open fd */
126         status = pvfs_resolve_name_fd(pvfs, fd, name);
127         if (!NT_STATUS_IS_OK(status)) {
128                 return status;
129         }
130
131         f->fnum = fd;
132         f->fd = fd;
133         f->name = talloc_steal(f, name);
134
135         DLIST_ADD(pvfs->open_files, f);
136
137         ZERO_STRUCT(io->generic.out);
138         
139         io->generic.out.create_time = name->dos.create_time;
140         io->generic.out.access_time = name->dos.access_time;
141         io->generic.out.write_time = name->dos.write_time;
142         io->generic.out.change_time = name->dos.change_time;
143         io->generic.out.fnum = f->fnum;
144         io->generic.out.alloc_size = name->dos.alloc_size;
145         io->generic.out.size = name->st.st_size;
146         io->generic.out.attrib = name->dos.attrib;
147         io->generic.out.is_directory = (name->dos.attrib & FILE_ATTRIBUTE_DIRECTORY)?1:0;
148
149         return NT_STATUS_OK;
150 }
151
152
153 /*
154   close a file
155 */
156 NTSTATUS pvfs_close(struct smbsrv_request *req, union smb_close *io)
157 {
158         NTVFS_GET_PRIVATE(pvfs_state, pvfs, req);
159         struct pvfs_file *f;
160
161         if (io->generic.level != RAW_CLOSE_CLOSE) {
162                 /* we need a mapping function */
163                 return NT_STATUS_INVALID_LEVEL;
164         }
165
166         f = pvfs_find_fd(pvfs, io->close.in.fnum);
167         if (!f) {
168                 return NT_STATUS_INVALID_HANDLE;
169         }
170
171         if (close(f->fd) != 0) {
172                 return pvfs_map_errno(pvfs, errno);
173         }
174
175         DLIST_REMOVE(pvfs->open_files, f);
176         talloc_free(f);
177
178         return NT_STATUS_OK;
179 }
180