r3747: - added some of the infrastructure needed for streams support in pvfs
[kai/samba-autobuild/.git] / source4 / 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 one file
29 */
30 static NTSTATUS pvfs_unlink_one(struct pvfs_state *pvfs, TALLOC_CTX *mem_ctx,
31                                 const char *unix_path, 
32                                 const char *fname, uint32_t attrib)
33 {
34         struct pvfs_filename *name;
35         NTSTATUS status;
36
37         /* get a pvfs_filename object */
38         status = pvfs_resolve_partial(pvfs, mem_ctx, 
39                                       unix_path, fname, &name);
40         if (!NT_STATUS_IS_OK(status)) {
41                 return status;
42         }
43
44         /* make sure its matches the given attributes */
45         status = pvfs_match_attrib(pvfs, name, attrib, 0);
46         if (!NT_STATUS_IS_OK(status)) {
47                 talloc_free(name);
48                 return status;
49         }
50
51         status = pvfs_can_delete(pvfs, name);
52         if (!NT_STATUS_IS_OK(status)) {
53                 talloc_free(name);
54                 return status;
55         }
56
57         if (name->dos.attrib & FILE_ATTRIBUTE_DIRECTORY) {
58                 talloc_free(name);
59                 return NT_STATUS_FILE_IS_A_DIRECTORY;
60         }
61
62         /* finally try the actual unlink */
63         if (unlink(name->full_name) == -1) {
64                 status = pvfs_map_errno(pvfs, errno);
65         }
66
67         talloc_free(name);
68
69         return status;
70 }
71
72 /*
73   delete a file - the dirtype specifies the file types to include in the search. 
74   The name can contain CIFS wildcards, but rarely does (except with OS/2 clients)
75 */
76 NTSTATUS pvfs_unlink(struct ntvfs_module_context *ntvfs,
77                      struct smbsrv_request *req, struct smb_unlink *unl)
78 {
79         struct pvfs_state *pvfs = ntvfs->private_data;
80         struct pvfs_dir *dir;
81         NTSTATUS status;
82         uint32_t total_deleted=0;
83         struct pvfs_filename *name;
84         const char *fname;
85         uint_t ofs;
86
87         /* resolve the cifs name to a posix name */
88         status = pvfs_resolve_name(pvfs, req, unl->in.pattern, 
89                                    PVFS_RESOLVE_WILDCARD, &name);
90         if (!NT_STATUS_IS_OK(status)) {
91                 return status;
92         }
93
94         if (!name->exists && !name->has_wildcard) {
95                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
96         }
97
98         if (name->exists && 
99             (name->dos.attrib & FILE_ATTRIBUTE_DIRECTORY)) {
100                 return NT_STATUS_FILE_IS_A_DIRECTORY;
101         }
102
103         /* get list of matching files */
104         status = pvfs_list_start(pvfs, name, req, &dir);
105         if (!NT_STATUS_IS_OK(status)) {
106                 return status;
107         }
108
109         status = NT_STATUS_NO_SUCH_FILE;
110
111         ofs = 0;
112
113         while ((fname = pvfs_list_next(dir, &ofs))) {
114                 /* this seems to be a special case */
115                 if ((unl->in.attrib & FILE_ATTRIBUTE_DIRECTORY) &&
116                     (strcmp(fname, ".") == 0 ||
117                      strcmp(fname, "..") == 0)) {
118                         return NT_STATUS_OBJECT_NAME_INVALID;
119                 }
120
121                 status = pvfs_unlink_one(pvfs, req, pvfs_list_unix_path(dir), fname, unl->in.attrib);
122                 if (NT_STATUS_IS_OK(status)) {
123                         total_deleted++;
124                 }
125         }
126
127         if (total_deleted > 0) {
128                 status = NT_STATUS_OK;
129         }
130
131         return status;
132 }
133
134