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