bda4014beec16c8a701347d0e6d8a36188350627
[jelmer/samba4-debian.git] / source / ntvfs / posix / pvfs_unlink.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    POSIX NTVFS backend - unlink
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 "vfs_posix.h"
24 #include "system/dir.h"
25
26
27 /*
28   unlink a file
29 */
30 static NTSTATUS pvfs_unlink_file(struct pvfs_state *pvfs,
31                                  struct pvfs_filename *name)
32 {
33         NTSTATUS status;
34
35         if (name->dos.attrib & FILE_ATTRIBUTE_DIRECTORY) {
36                 return NT_STATUS_FILE_IS_A_DIRECTORY;
37         }
38
39         if (name->st.st_nlink == 1) {
40                 status = pvfs_xattr_unlink_hook(pvfs, name->full_name);
41                 if (!NT_STATUS_IS_OK(status)) {
42                         return status;
43                 }
44         }
45
46         /* finally try the actual unlink */
47         if (unlink(name->full_name) == -1) {
48                 status = pvfs_map_errno(pvfs, errno);
49         }
50
51         if (NT_STATUS_IS_OK(status)) {
52                 notify_trigger(pvfs->notify_context, 
53                                NOTIFY_ACTION_REMOVED, 
54                                FILE_NOTIFY_CHANGE_FILE_NAME,
55                                name->full_name);
56         }
57
58         return status;
59 }
60
61 /*
62   unlink one file
63 */
64 static NTSTATUS pvfs_unlink_one(struct pvfs_state *pvfs,
65                                 struct ntvfs_request *req,
66                                 union smb_unlink *unl,
67                                 struct pvfs_filename *name)
68 {
69         NTSTATUS status;
70
71         /* make sure its matches the given attributes */
72         status = pvfs_match_attrib(pvfs, name,
73                                    unl->unlink.in.attrib, 0);
74         if (!NT_STATUS_IS_OK(status)) {
75                 return status;
76         }
77
78         status = pvfs_can_delete(pvfs, req, name, NULL);
79         if (!NT_STATUS_IS_OK(status)) {
80                 return status;
81         }
82
83         if (name->stream_name) {
84                 if (!name->stream_exists) {
85                         return NT_STATUS_OBJECT_NAME_NOT_FOUND;
86                 }
87
88                 return pvfs_stream_delete(pvfs, name, -1);
89         }
90
91         return pvfs_unlink_file(pvfs, name);
92 }
93
94 /*
95   delete a file - the dirtype specifies the file types to include in the search. 
96   The name can contain CIFS wildcards, but rarely does (except with OS/2 clients)
97 */
98 NTSTATUS pvfs_unlink(struct ntvfs_module_context *ntvfs,
99                      struct ntvfs_request *req,
100                      union smb_unlink *unl)
101 {
102         struct pvfs_state *pvfs = ntvfs->private_data;
103         struct pvfs_dir *dir;
104         NTSTATUS status;
105         uint32_t total_deleted=0;
106         struct pvfs_filename *name;
107         const char *fname;
108         off_t ofs;
109
110         /* resolve the cifs name to a posix name */
111         status = pvfs_resolve_name(pvfs, req, unl->unlink.in.pattern, 
112                                    PVFS_RESOLVE_WILDCARD | PVFS_RESOLVE_STREAMS, &name);
113         if (!NT_STATUS_IS_OK(status)) {
114                 return status;
115         }
116
117         if (!name->exists && !name->has_wildcard) {
118                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
119         }
120
121         if (name->exists && 
122             (name->dos.attrib & FILE_ATTRIBUTE_DIRECTORY)) {
123                 return NT_STATUS_FILE_IS_A_DIRECTORY;
124         }
125
126         if (!name->has_wildcard) {
127                 return pvfs_unlink_one(pvfs, req, unl, name);
128         }
129
130         /* get list of matching files */
131         status = pvfs_list_start(pvfs, name, req, &dir);
132         if (!NT_STATUS_IS_OK(status)) {
133                 return status;
134         }
135
136         status = NT_STATUS_NO_SUCH_FILE;
137         talloc_free(name);
138
139         ofs = 0;
140
141         while ((fname = pvfs_list_next(dir, &ofs))) {
142                 /* this seems to be a special case */
143                 if ((unl->unlink.in.attrib & FILE_ATTRIBUTE_DIRECTORY) &&
144                     (ISDOT(fname) || ISDOTDOT(fname))) {
145                         return NT_STATUS_OBJECT_NAME_INVALID;
146                 }
147
148                 /* get a pvfs_filename object */
149                 status = pvfs_resolve_partial(pvfs, req,
150                                               pvfs_list_unix_path(dir),
151                                               fname, &name);
152                 if (!NT_STATUS_IS_OK(status)) {
153                         return status;
154                 }
155
156                 status = pvfs_unlink_one(pvfs, req, unl, name);
157                 if (NT_STATUS_IS_OK(status)) {
158                         total_deleted++;
159                 }
160
161                 talloc_free(name);
162         }
163
164         if (total_deleted > 0) {
165                 status = NT_STATUS_OK;
166         }
167
168         return status;
169 }
170
171