r12694: Move some headers to the directory of the subsystem they belong to.
[samba.git] / source4 / libcli / clideltree.c
1 /* 
2    Unix SMB/CIFS implementation.
3    useful function for deleting a whole directory tree
4    Copyright (C) Andrew Tridgell 2003
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22 #include "libcli/raw/libcliraw.h"
23 #include "libcli/libcli.h"
24
25 struct delete_state {
26         struct smbcli_tree *tree;
27         int total_deleted;
28         BOOL failed;
29 };
30
31 /* 
32    callback function for torture_deltree() 
33 */
34 static void delete_fn(struct clilist_file_info *finfo, const char *name, void *state)
35 {
36         struct delete_state *dstate = state;
37         char *s, *n;
38         if (strcmp(finfo->name, ".") == 0 ||
39             strcmp(finfo->name, "..") == 0) return;
40
41         n = strdup(name);
42         n[strlen(n)-1] = 0;
43         asprintf(&s, "%s%s", n, finfo->name);
44
45         if (finfo->attrib & FILE_ATTRIBUTE_READONLY) {
46                 if (NT_STATUS_IS_ERR(smbcli_setatr(dstate->tree, s, 0, 0))) {
47                         DEBUG(2,("Failed to remove READONLY on %s - %s\n",
48                                  s, smbcli_errstr(dstate->tree)));                      
49                 }
50         }
51
52         if (finfo->attrib & FILE_ATTRIBUTE_DIRECTORY) {
53                 char *s2;
54                 asprintf(&s2, "%s\\*", s);
55                 smbcli_unlink(dstate->tree, s2);
56                 smbcli_list(dstate->tree, s2, 
57                          FILE_ATTRIBUTE_DIRECTORY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM, 
58                          delete_fn, state);
59                 free(s2);
60                 if (NT_STATUS_IS_ERR(smbcli_rmdir(dstate->tree, s))) {
61                         DEBUG(2,("Failed to delete %s - %s\n", 
62                                  s, smbcli_errstr(dstate->tree)));
63                         dstate->failed = True;
64                 }
65                 dstate->total_deleted++;
66         } else {
67                 if (NT_STATUS_IS_ERR(smbcli_unlink(dstate->tree, s))) {
68                         DEBUG(2,("Failed to delete %s - %s\n", 
69                                  s, smbcli_errstr(dstate->tree)));
70                         dstate->failed = True;
71                 }
72                 dstate->total_deleted++;
73         }
74         free(s);
75         free(n);
76 }
77
78 /* 
79    recursively descend a tree deleting all files
80    returns the number of files deleted, or -1 on error
81 */
82 int smbcli_deltree(struct smbcli_tree *tree, const char *dname)
83 {
84         char *mask;
85         struct delete_state dstate;
86
87         dstate.tree = tree;
88         dstate.total_deleted = 0;
89         dstate.failed = False;
90
91         /* it might be a file */
92         if (NT_STATUS_IS_OK(smbcli_unlink(tree, dname))) {
93                 return 1;
94         }
95         if (NT_STATUS_EQUAL(smbcli_nt_error(tree), NT_STATUS_OBJECT_NAME_NOT_FOUND) ||
96             NT_STATUS_EQUAL(smbcli_nt_error(tree), NT_STATUS_OBJECT_PATH_NOT_FOUND) ||
97             NT_STATUS_EQUAL(smbcli_nt_error(tree), NT_STATUS_NO_SUCH_FILE)) {
98                 return 0;
99         }
100
101         asprintf(&mask, "%s\\*", dname);
102         smbcli_unlink(dstate.tree, mask);
103         smbcli_list(dstate.tree, mask, 
104                  FILE_ATTRIBUTE_DIRECTORY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM, 
105                  delete_fn, &dstate);
106         free(mask);
107         if (NT_STATUS_IS_ERR(smbcli_rmdir(dstate.tree, dname))) {
108                 DEBUG(2,("Failed to delete %s - %s\n", 
109                          dname, smbcli_errstr(dstate.tree)));
110                 return -1;
111         }
112         dstate.total_deleted++;
113
114         if (dstate.failed) {
115                 return -1;
116         }
117
118         return dstate.total_deleted;
119 }