fixed error code for write on a directory
[kai/samba.git] / source4 / ntvfs / posix / pvfs_mkdir.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    POSIX NTVFS backend - mkdir and rmdir
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 3 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, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "system/dir.h"
24 #include "vfs_posix.h"
25 #include "librpc/gen_ndr/security.h"
26
27 /*
28   create a directory with EAs
29 */
30 static NTSTATUS pvfs_t2mkdir(struct pvfs_state *pvfs,
31                              struct ntvfs_request *req, union smb_mkdir *md)
32 {
33         NTSTATUS status;
34         struct pvfs_filename *name;
35         mode_t mode;
36
37         /* resolve the cifs name to a posix name */
38         status = pvfs_resolve_name(pvfs, req, md->t2mkdir.in.path, 0, &name);
39         if (!NT_STATUS_IS_OK(status)) {
40                 return status;
41         }
42
43         if (name->exists) {
44                 return NT_STATUS_OBJECT_NAME_COLLISION;
45         }
46
47         status = pvfs_access_check_parent(pvfs, req, name, SEC_DIR_ADD_FILE);
48         if (!NT_STATUS_IS_OK(status)) {
49                 return status;
50         }
51
52         mode = pvfs_fileperms(pvfs, FILE_ATTRIBUTE_DIRECTORY);
53
54         if (mkdir(name->full_name, mode) == -1) {
55                 return pvfs_map_errno(pvfs, errno);
56         }
57
58         pvfs_xattr_unlink_hook(pvfs, name->full_name);
59
60         status = pvfs_resolve_name(pvfs, req, md->t2mkdir.in.path, 0, &name);
61         if (!NT_STATUS_IS_OK(status)) {
62                 return status;
63         }
64         if (!name->exists ||
65             !(name->dos.attrib & FILE_ATTRIBUTE_DIRECTORY)) {
66                 return NT_STATUS_INTERNAL_ERROR;
67         }
68
69         /* setup an inherited acl from the parent */
70         status = pvfs_acl_inherit(pvfs, req, name, -1);
71         if (!NT_STATUS_IS_OK(status)) {
72                 rmdir(name->full_name);
73                 return status;
74         }
75
76         /* setup any EAs that were asked for */
77         status = pvfs_setfileinfo_ea_set(pvfs, name, -1, 
78                                          md->t2mkdir.in.num_eas,
79                                          md->t2mkdir.in.eas);
80         if (!NT_STATUS_IS_OK(status)) {
81                 rmdir(name->full_name);
82                 return status;
83         }
84
85         notify_trigger(pvfs->notify_context, 
86                        NOTIFY_ACTION_ADDED, 
87                        FILE_NOTIFY_CHANGE_DIR_NAME,
88                        name->full_name);
89
90         return NT_STATUS_OK;
91 }
92
93 /*
94   create a directory
95 */
96 NTSTATUS pvfs_mkdir(struct ntvfs_module_context *ntvfs,
97                     struct ntvfs_request *req, union smb_mkdir *md)
98 {
99         struct pvfs_state *pvfs = ntvfs->private_data;
100         NTSTATUS status;
101         struct pvfs_filename *name;
102         mode_t mode;
103
104         if (md->generic.level == RAW_MKDIR_T2MKDIR) {
105                 return pvfs_t2mkdir(pvfs, req, md);
106         }
107
108         if (md->generic.level != RAW_MKDIR_MKDIR) {
109                 return NT_STATUS_INVALID_LEVEL;
110         }
111
112         /* resolve the cifs name to a posix name */
113         status = pvfs_resolve_name(pvfs, req, md->mkdir.in.path, 0, &name);
114         if (!NT_STATUS_IS_OK(status)) {
115                 return status;
116         }
117
118         if (name->exists) {
119                 return NT_STATUS_OBJECT_NAME_COLLISION;
120         }
121
122         status = pvfs_access_check_parent(pvfs, req, name, SEC_DIR_ADD_FILE);
123         if (!NT_STATUS_IS_OK(status)) {
124                 return status;
125         }
126
127         mode = pvfs_fileperms(pvfs, FILE_ATTRIBUTE_DIRECTORY);
128
129         if (mkdir(name->full_name, mode) == -1) {
130                 return pvfs_map_errno(pvfs, errno);
131         }
132
133         pvfs_xattr_unlink_hook(pvfs, name->full_name);
134
135         /* setup an inherited acl from the parent */
136         status = pvfs_acl_inherit(pvfs, req, name, -1);
137         if (!NT_STATUS_IS_OK(status)) {
138                 rmdir(name->full_name);
139                 return status;
140         }
141
142         notify_trigger(pvfs->notify_context, 
143                        NOTIFY_ACTION_ADDED, 
144                        FILE_NOTIFY_CHANGE_DIR_NAME,
145                        name->full_name);
146
147         return NT_STATUS_OK;
148 }
149
150 /*
151   remove a directory
152 */
153 NTSTATUS pvfs_rmdir(struct ntvfs_module_context *ntvfs,
154                     struct ntvfs_request *req, struct smb_rmdir *rd)
155 {
156         struct pvfs_state *pvfs = ntvfs->private_data;
157         NTSTATUS status;
158         struct pvfs_filename *name;
159
160         /* resolve the cifs name to a posix name */
161         status = pvfs_resolve_name(pvfs, req, rd->in.path, 0, &name);
162         if (!NT_STATUS_IS_OK(status)) {
163                 return status;
164         }
165
166         if (!name->exists) {
167                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
168         }
169
170         status = pvfs_access_check_simple(pvfs, req, name, SEC_STD_DELETE);
171         if (!NT_STATUS_IS_OK(status)) {
172                 return status;
173         }
174
175         status = pvfs_xattr_unlink_hook(pvfs, name->full_name);
176         if (!NT_STATUS_IS_OK(status)) {
177                 return status;
178         }
179
180         if (rmdir(name->full_name) == -1) {
181                 /* some olders systems don't return ENOTEMPTY to rmdir() */
182                 if (errno == EEXIST) {
183                         return NT_STATUS_DIRECTORY_NOT_EMPTY;
184                 }
185                 return pvfs_map_errno(pvfs, errno);
186         }
187
188         notify_trigger(pvfs->notify_context, 
189                        NOTIFY_ACTION_REMOVED, 
190                        FILE_NOTIFY_CHANGE_DIR_NAME,
191                        name->full_name);
192
193         return NT_STATUS_OK;
194 }