r4013: got rid of a bunch of unused or unmaintained code
[samba.git] / source / 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 "clilist.h"
23
24 struct delete_state {
25         struct smbcli_tree *tree;
26         int total_deleted;
27         BOOL failed;
28 };
29
30 /* 
31    callback function for torture_deltree() 
32 */
33 static void delete_fn(struct clilist_file_info *finfo, const char *name, void *state)
34 {
35         struct delete_state *dstate = state;
36         char *s, *n;
37         if (strcmp(finfo->name, ".") == 0 ||
38             strcmp(finfo->name, "..") == 0) return;
39
40         n = strdup(name);
41         n[strlen(n)-1] = 0;
42         asprintf(&s, "%s%s", n, finfo->name);
43
44         if (finfo->attrib & FILE_ATTRIBUTE_READONLY) {
45                 if (NT_STATUS_IS_ERR(smbcli_setatr(dstate->tree, s, 0, 0))) {
46                         DEBUG(2,("Failed to remove READONLY on %s - %s\n",
47                                  s, smbcli_errstr(dstate->tree)));                      
48                 }
49         }
50
51         if (finfo->attrib & FILE_ATTRIBUTE_DIRECTORY) {
52                 char *s2;
53                 asprintf(&s2, "%s\\*", s);
54                 smbcli_unlink(dstate->tree, s2);
55                 smbcli_list(dstate->tree, s2, 
56                          FILE_ATTRIBUTE_DIRECTORY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM, 
57                          delete_fn, state);
58                 free(s2);
59                 if (NT_STATUS_IS_ERR(smbcli_rmdir(dstate->tree, s))) {
60                         DEBUG(2,("Failed to delete %s - %s\n", 
61                                  s, smbcli_errstr(dstate->tree)));
62                         dstate->failed = True;
63                 }
64                 dstate->total_deleted++;
65         } else {
66                 if (NT_STATUS_IS_ERR(smbcli_unlink(dstate->tree, s))) {
67                         DEBUG(2,("Failed to delete %s - %s\n", 
68                                  s, smbcli_errstr(dstate->tree)));
69                         dstate->failed = True;
70                 }
71                 dstate->total_deleted++;
72         }
73         free(s);
74         free(n);
75 }
76
77 /* 
78    recursively descend a tree deleting all files
79    returns the number of files deleted, or -1 on error
80 */
81 int smbcli_deltree(struct smbcli_tree *tree, const char *dname)
82 {
83         char *mask;
84         struct delete_state dstate;
85
86         dstate.tree = tree;
87         dstate.total_deleted = 0;
88         dstate.failed = False;
89
90         /* it might be a file */
91         if (NT_STATUS_IS_OK(smbcli_unlink(tree, dname))) {
92                 return 1;
93         }
94         if (NT_STATUS_EQUAL(smbcli_nt_error(tree), NT_STATUS_OBJECT_NAME_NOT_FOUND) ||
95             NT_STATUS_EQUAL(smbcli_nt_error(tree), NT_STATUS_OBJECT_PATH_NOT_FOUND) ||
96             NT_STATUS_EQUAL(smbcli_nt_error(tree), NT_STATUS_NO_SUCH_FILE)) {
97                 return 0;
98         }
99
100         asprintf(&mask, "%s\\*", dname);
101         smbcli_unlink(dstate.tree, mask);
102         smbcli_list(dstate.tree, mask, 
103                  FILE_ATTRIBUTE_DIRECTORY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM, 
104                  delete_fn, &dstate);
105         free(mask);
106         if (NT_STATUS_IS_ERR(smbcli_rmdir(dstate.tree, dname))) {
107                 DEBUG(2,("Failed to delete %s - %s\n", 
108                          dname, smbcli_errstr(dstate.tree)));
109                 return -1;
110         }
111         dstate.total_deleted++;
112
113         if (dstate.failed) {
114                 return -1;
115         }
116
117         return dstate.total_deleted;
118 }