2 Unix SMB/CIFS implementation.
4 POSIX NTVFS backend - open and close
6 Copyright (C) Andrew Tridgell 2004
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.
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.
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.
23 #include "include/includes.h"
24 #include "vfs_posix.h"
28 find open file handle given fnum
30 struct pvfs_file *pvfs_find_fd(struct pvfs_state *pvfs, uint16_t fnum)
33 for (f=pvfs->open_files;f;f=f->next) {
34 if (f->fnum == fnum) {
43 TODO: this is a temporary implementation derived from the simple backend
44 its purpose is to allow other tests to run
46 NTSTATUS pvfs_open(struct smbsrv_request *req, union smb_open *io)
48 NTVFS_GET_PRIVATE(pvfs_state, pvfs, req);
50 struct pvfs_filename *name;
54 if (io->generic.level != RAW_OPEN_GENERIC) {
55 return ntvfs_map_open(req, io, pvfs->ops);
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)) {
65 switch (io->generic.in.open_disposition) {
66 case NTCREATEX_DISP_SUPERSEDE:
67 case NTCREATEX_DISP_OVERWRITE_IF:
68 flags = O_CREAT | O_TRUNC;
70 case NTCREATEX_DISP_OPEN:
71 case NTCREATEX_DISP_OVERWRITE:
74 case NTCREATEX_DISP_CREATE:
75 flags = O_CREAT | O_EXCL;
77 case NTCREATEX_DISP_OPEN_IF:
87 /* we need to do this differently to support systems without O_DIRECTORY */
92 if (io->generic.in.create_options & NTCREATEX_OPTIONS_DIRECTORY) {
93 flags = O_RDONLY | O_DIRECTORY;
94 if (pvfs->flags & PVFS_FLAG_READONLY) {
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);
103 case NTCREATEX_DISP_OPEN_IF:
104 if (mkdir(name->full_name, 0755) == -1 && errno != EEXIST) {
105 return pvfs_map_errno(pvfs,errno);
112 fd = open(name->full_name, flags, 0644);
116 return pvfs_map_errno(pvfs,errno);
119 f = talloc_p(pvfs, struct pvfs_file);
122 return NT_STATUS_NO_MEMORY;
125 /* re-resolve the open fd */
126 status = pvfs_resolve_name_fd(pvfs, fd, name);
127 if (!NT_STATUS_IS_OK(status)) {
133 f->name = talloc_steal(f, name);
135 DLIST_ADD(pvfs->open_files, f);
137 ZERO_STRUCT(io->generic.out);
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;
156 NTSTATUS pvfs_close(struct smbsrv_request *req, union smb_close *io)
158 NTVFS_GET_PRIVATE(pvfs_state, pvfs, req);
161 if (io->generic.level != RAW_CLOSE_CLOSE) {
162 /* we need a mapping function */
163 return NT_STATUS_INVALID_LEVEL;
166 f = pvfs_find_fd(pvfs, io->close.in.fnum);
168 return NT_STATUS_INVALID_HANDLE;
171 if (close(f->fd) != 0) {
172 return pvfs_map_errno(pvfs, errno);
175 DLIST_REMOVE(pvfs->open_files, f);