b43e67faed13e2837bb4766c66f4c009055d9f74
[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 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 "includes.h"
24 #include "vfs_posix.h"
25
26 /*
27   create a directory with EAs
28 */
29 static NTSTATUS pvfs_t2mkdir(struct pvfs_state *pvfs,
30                              struct smbsrv_request *req, union smb_mkdir *md)
31 {
32         NTSTATUS status;
33         struct pvfs_filename *name;
34         mode_t mode;
35         int i;
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         mode = pvfs_fileperms(pvfs, FILE_ATTRIBUTE_DIRECTORY);
48
49         if (mkdir(name->full_name, mode) == -1) {
50                 return pvfs_map_errno(pvfs, errno);
51         }
52
53         status = pvfs_resolve_name(pvfs, req, md->t2mkdir.in.path, 0, &name);
54         if (!NT_STATUS_IS_OK(status)) {
55                 return status;
56         }
57         if (!name->exists ||
58             !(name->dos.attrib & FILE_ATTRIBUTE_DIRECTORY)) {
59                 return NT_STATUS_INTERNAL_ERROR;
60         }
61
62         /* setup any EAs that were asked for */
63         for (i=0;i<md->t2mkdir.in.num_eas;i++) {
64                 status = pvfs_setfileinfo_ea_set(pvfs, name, -1, &md->t2mkdir.in.eas[i]);
65                 if (!NT_STATUS_IS_OK(status)) {
66                         rmdir(name->full_name);
67                         return status;
68                 }
69         }
70
71         return NT_STATUS_OK;
72 }
73
74 /*
75   create a directory
76 */
77 NTSTATUS pvfs_mkdir(struct ntvfs_module_context *ntvfs,
78                     struct smbsrv_request *req, union smb_mkdir *md)
79 {
80         struct pvfs_state *pvfs = ntvfs->private_data;
81         NTSTATUS status;
82         struct pvfs_filename *name;
83         mode_t mode;
84
85         if (md->generic.level == RAW_MKDIR_T2MKDIR) {
86                 return pvfs_t2mkdir(pvfs, req, md);
87         }
88
89         if (md->generic.level != RAW_MKDIR_MKDIR) {
90                 return NT_STATUS_INVALID_LEVEL;
91         }
92
93         /* resolve the cifs name to a posix name */
94         status = pvfs_resolve_name(pvfs, req, md->mkdir.in.path, 0, &name);
95         if (!NT_STATUS_IS_OK(status)) {
96                 return status;
97         }
98
99         if (name->exists) {
100                 return NT_STATUS_OBJECT_NAME_COLLISION;
101         }
102
103         mode = pvfs_fileperms(pvfs, FILE_ATTRIBUTE_DIRECTORY);
104
105         if (mkdir(name->full_name, mode) == -1) {
106                 return pvfs_map_errno(pvfs, errno);
107         }
108
109         return NT_STATUS_OK;
110 }
111
112 /*
113   remove a directory
114 */
115 NTSTATUS pvfs_rmdir(struct ntvfs_module_context *ntvfs,
116                     struct smbsrv_request *req, struct smb_rmdir *rd)
117 {
118         struct pvfs_state *pvfs = ntvfs->private_data;
119         NTSTATUS status;
120         struct pvfs_filename *name;
121
122         /* resolve the cifs name to a posix name */
123         status = pvfs_resolve_name(pvfs, req, rd->in.path, 0, &name);
124         if (!NT_STATUS_IS_OK(status)) {
125                 return status;
126         }
127
128         if (!name->exists) {
129                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
130         }
131
132         if (rmdir(name->full_name) == -1) {
133                 return pvfs_map_errno(pvfs, errno);
134         }
135
136         return NT_STATUS_OK;
137 }