r3174: added pvfs_is_open() to allow us to check for open files on unlink. We
[gd/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 "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         if (!pvfs_match_attrib(pvfs, name, attrib)) {
46                 talloc_free(name);
47                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
48         }
49
50         /* finally try the actual unlink */
51         if (unlink(name->full_name) == -1) {
52                 status = pvfs_map_errno(pvfs, errno);
53         }
54
55         talloc_free(name);
56
57         return status;
58 }
59
60 /*
61   delete a file - the dirtype specifies the file types to include in the search. 
62   The name can contain CIFS wildcards, but rarely does (except with OS/2 clients)
63 */
64 NTSTATUS pvfs_unlink(struct ntvfs_module_context *ntvfs,
65                      struct smbsrv_request *req, struct smb_unlink *unl)
66 {
67         struct pvfs_state *pvfs = ntvfs->private_data;
68         struct pvfs_dir *dir;
69         NTSTATUS status;
70         uint32_t i, total_deleted=0;
71         struct pvfs_filename *name;
72
73         /* resolve the cifs name to a posix name */
74         status = pvfs_resolve_name(pvfs, req, unl->in.pattern, 0, &name);
75         if (!NT_STATUS_IS_OK(status)) {
76                 return status;
77         }
78
79         if (!name->exists && !name->has_wildcard) {
80                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
81         }
82
83         if (pvfs_is_open(pvfs, name)) {
84                 return NT_STATUS_SHARING_VIOLATION;
85         }
86
87         dir = talloc_p(req, struct pvfs_dir);
88         if (dir == NULL) {
89                 return NT_STATUS_NO_MEMORY;
90         }
91
92         /* get list of matching files */
93         status = pvfs_list(pvfs, name, dir);
94         if (!NT_STATUS_IS_OK(status)) {
95                 return status;
96         }
97
98         if (dir->count == 0) {
99                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
100         }
101
102         for (i=0;i<dir->count;i++) {
103                 status = pvfs_unlink_one(pvfs, req, dir->unix_path, dir->names[i], unl->in.attrib);
104                 if (NT_STATUS_IS_OK(status)) {
105                         total_deleted++;
106                 }
107         }
108
109         if (total_deleted == 0) {
110                 return status;
111         }
112
113         return NT_STATUS_OK;
114 }
115
116