r2660: - converted the libcli/raw/ library to use talloc_increase_ref_count()
[bbaumbach/samba-autobuild/.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 smbsrv_request *req, uint16_t fnum)
31 {
32         NTVFS_GET_PRIVATE(pvfs_state, pvfs, req);
33         struct pvfs_file *f;
34         for (f=pvfs->open_files;f;f=f->next) {
35                 if (f->fnum == fnum) {
36                         if (req->session != f->session) {
37                                 DEBUG(2,("pvfs_find_fd: attempt to use wrong session for fnum %d\n", fnum));
38                                 return NULL;
39                         }
40                         return f;
41                 }
42         }
43         return NULL;
44 }
45
46 /*
47   by using a destructor we make sure that abnormal cleanup will not 
48   leak file descriptors (assuming at least the top level pointer is freed, which
49   will cascade down to here)
50 */
51 static int pvfs_fd_destructor(void *p)
52 {
53         struct pvfs_file *f = p;
54         if (f->fd != -1) {
55                 close(f->fd);
56                 f->fd = -1;
57         }
58         return 0;
59 }
60
61 /*
62   open a file
63   TODO: this is a temporary implementation derived from the simple backend
64   its purpose is to allow other tests to run 
65 */
66 NTSTATUS pvfs_open(struct smbsrv_request *req, union smb_open *io)
67 {
68         NTVFS_GET_PRIVATE(pvfs_state, pvfs, req);
69         int fd, flags;
70         struct pvfs_filename *name;
71         struct pvfs_file *f;
72         NTSTATUS status;
73
74         if (io->generic.level != RAW_OPEN_GENERIC) {
75                 return ntvfs_map_open(req, io, pvfs->ops);
76         }
77
78         /* resolve the cifs name to a posix name */
79         status = pvfs_resolve_name(pvfs, req, io->ntcreatex.in.fname,
80                                    PVFS_RESOLVE_NO_WILDCARD, &name);
81         if (!NT_STATUS_IS_OK(status)) {
82                 return status;
83         }
84
85         switch (io->generic.in.open_disposition) {
86         case NTCREATEX_DISP_SUPERSEDE:
87         case NTCREATEX_DISP_OVERWRITE_IF:
88                 flags = O_CREAT | O_TRUNC;
89                 break;
90         case NTCREATEX_DISP_OPEN:
91         case NTCREATEX_DISP_OVERWRITE:
92                 flags = 0;
93                 break;
94         case NTCREATEX_DISP_CREATE:
95                 flags = O_CREAT | O_EXCL;
96                 break;
97         case NTCREATEX_DISP_OPEN_IF:
98                 flags = O_CREAT;
99                 break;
100         default:
101                 flags = 0;
102                 break;
103         }
104         
105         flags |= O_RDWR;
106
107 /* we need to do this differently to support systems without O_DIRECTORY */
108 #ifndef O_DIRECTORY
109 #define O_DIRECTORY 0
110 #endif
111
112         if (io->generic.in.create_options & NTCREATEX_OPTIONS_DIRECTORY) {
113                 flags = O_RDONLY | O_DIRECTORY;
114                 if (pvfs->flags & PVFS_FLAG_READONLY) {
115                         goto do_open;
116                 }
117                 switch (io->generic.in.open_disposition) {
118                 case NTCREATEX_DISP_CREATE:
119                         if (mkdir(name->full_name, 0755) == -1) {
120                                 return pvfs_map_errno(pvfs,errno);
121                         }
122                         break;
123                 case NTCREATEX_DISP_OPEN_IF:
124                         if (mkdir(name->full_name, 0755) == -1 && errno != EEXIST) {
125                                 return pvfs_map_errno(pvfs,errno);
126                         }
127                         break;
128                 }
129         }
130
131 do_open:
132         fd = open(name->full_name, flags, 0644);
133         if (fd == -1) {
134                 if (errno == 0)
135                         errno = ENOENT;
136                 return pvfs_map_errno(pvfs,errno);
137         }
138
139         f = talloc_p(pvfs, struct pvfs_file);
140         if (f == NULL) {
141                 close(fd);
142                 return NT_STATUS_NO_MEMORY;
143         }
144
145         /* re-resolve the open fd */
146         status = pvfs_resolve_name_fd(pvfs, fd, name);
147         if (!NT_STATUS_IS_OK(status)) {
148                 return status;
149         }
150
151         f->fnum = fd;
152         f->fd = fd;
153         f->name = talloc_steal(f, name);
154         f->session = req->session;
155         f->smbpid = req->smbpid;
156
157         /* setup a destructor to avoid file descriptor leaks on
158            abnormal termination */
159         talloc_set_destructor(f, pvfs_fd_destructor);
160
161         DLIST_ADD(pvfs->open_files, f);
162
163         ZERO_STRUCT(io->generic.out);
164         
165         io->generic.out.create_time = name->dos.create_time;
166         io->generic.out.access_time = name->dos.access_time;
167         io->generic.out.write_time = name->dos.write_time;
168         io->generic.out.change_time = name->dos.change_time;
169         io->generic.out.fnum = f->fnum;
170         io->generic.out.alloc_size = name->dos.alloc_size;
171         io->generic.out.size = name->st.st_size;
172         io->generic.out.attrib = name->dos.attrib;
173         io->generic.out.is_directory = (name->dos.attrib & FILE_ATTRIBUTE_DIRECTORY)?1:0;
174
175         return NT_STATUS_OK;
176 }
177
178
179 /*
180   close a file
181 */
182 NTSTATUS pvfs_close(struct smbsrv_request *req, union smb_close *io)
183 {
184         NTVFS_GET_PRIVATE(pvfs_state, pvfs, req);
185         struct pvfs_file *f;
186         NTSTATUS status;
187
188         if (io->generic.level != RAW_CLOSE_CLOSE) {
189                 /* we need a mapping function */
190                 return NT_STATUS_INVALID_LEVEL;
191         }
192
193         f = pvfs_find_fd(req, io->close.in.fnum);
194         if (!f) {
195                 return NT_STATUS_INVALID_HANDLE;
196         }
197
198         if (close(f->fd) != 0) {
199                 status = pvfs_map_errno(pvfs, errno);
200         } else {
201                 status = NT_STATUS_OK;
202         }
203
204         talloc_set_destructor(f, NULL);
205
206         DLIST_REMOVE(pvfs->open_files, f);
207         talloc_free(f);
208
209         return status;
210 }
211
212
213 /*
214   logoff - close all file descriptors open by a vuid
215 */
216 NTSTATUS pvfs_logoff(struct smbsrv_request *req)
217 {
218         NTVFS_GET_PRIVATE(pvfs_state, pvfs, req);
219         struct pvfs_file *f, *next;
220
221         for (f=pvfs->open_files;f;f=next) {
222                 next = f->next;
223                 if (f->session == req->session) {
224                         talloc_set_destructor(f, NULL);
225                         DLIST_REMOVE(pvfs->open_files, f);
226                         talloc_free(f);
227                 }
228         }
229
230         return NT_STATUS_OK;
231 }
232
233
234 /*
235   exit - close files for the current pid
236 */
237 NTSTATUS pvfs_exit(struct smbsrv_request *req)
238 {
239         NTVFS_GET_PRIVATE(pvfs_state, pvfs, req);
240         struct pvfs_file *f, *next;
241
242         for (f=pvfs->open_files;f;f=next) {
243                 next = f->next;
244                 if (f->smbpid == req->smbpid) {
245                         talloc_set_destructor(f, NULL);
246                         DLIST_REMOVE(pvfs->open_files, f);
247                         talloc_free(f);
248                 }
249         }
250
251         return NT_STATUS_OK;
252 }
253