r22637: Install tdbbackup and tdbdump again.
[Samba.git] / source / libcli / clideltree.c
blob51b4e6568d35bdbff2e905827047aee681b50620
1 /*
2 Unix SMB/CIFS implementation.
3 useful function for deleting a whole directory tree
4 Copyright (C) Andrew Tridgell 2003
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.
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.
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.
21 #include "includes.h"
22 #include "libcli/raw/libcliraw.h"
23 #include "libcli/libcli.h"
24 #include "system/dir.h"
26 struct delete_state {
27 struct smbcli_tree *tree;
28 int total_deleted;
29 BOOL failed;
32 /*
33 callback function for torture_deltree()
35 static void delete_fn(struct clilist_file_info *finfo, const char *name, void *state)
37 struct delete_state *dstate = state;
38 char *s, *n;
39 if (ISDOT(finfo->name) || ISDOTDOT(finfo->name)) {
40 return;
43 n = strdup(name);
44 n[strlen(n)-1] = 0;
45 asprintf(&s, "%s%s", n, finfo->name);
47 if (finfo->attrib & FILE_ATTRIBUTE_READONLY) {
48 if (NT_STATUS_IS_ERR(smbcli_setatr(dstate->tree, s, 0, 0))) {
49 DEBUG(2,("Failed to remove READONLY on %s - %s\n",
50 s, smbcli_errstr(dstate->tree)));
54 if (finfo->attrib & FILE_ATTRIBUTE_DIRECTORY) {
55 char *s2;
56 asprintf(&s2, "%s\\*", s);
57 smbcli_unlink(dstate->tree, s2);
58 smbcli_list(dstate->tree, s2,
59 FILE_ATTRIBUTE_DIRECTORY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM,
60 delete_fn, state);
61 free(s2);
62 if (NT_STATUS_IS_ERR(smbcli_rmdir(dstate->tree, s))) {
63 DEBUG(2,("Failed to delete %s - %s\n",
64 s, smbcli_errstr(dstate->tree)));
65 dstate->failed = True;
67 dstate->total_deleted++;
68 } else {
69 if (NT_STATUS_IS_ERR(smbcli_unlink(dstate->tree, s))) {
70 DEBUG(2,("Failed to delete %s - %s\n",
71 s, smbcli_errstr(dstate->tree)));
72 dstate->failed = True;
74 dstate->total_deleted++;
76 free(s);
77 free(n);
80 /*
81 recursively descend a tree deleting all files
82 returns the number of files deleted, or -1 on error
84 int smbcli_deltree(struct smbcli_tree *tree, const char *dname)
86 char *mask;
87 struct delete_state dstate;
89 dstate.tree = tree;
90 dstate.total_deleted = 0;
91 dstate.failed = False;
93 /* it might be a file */
94 if (NT_STATUS_IS_OK(smbcli_unlink(tree, dname))) {
95 return 1;
97 if (NT_STATUS_EQUAL(smbcli_nt_error(tree), NT_STATUS_OBJECT_NAME_NOT_FOUND) ||
98 NT_STATUS_EQUAL(smbcli_nt_error(tree), NT_STATUS_OBJECT_PATH_NOT_FOUND) ||
99 NT_STATUS_EQUAL(smbcli_nt_error(tree), NT_STATUS_NO_SUCH_FILE)) {
100 return 0;
103 asprintf(&mask, "%s\\*", dname);
104 smbcli_unlink(dstate.tree, mask);
105 smbcli_list(dstate.tree, mask,
106 FILE_ATTRIBUTE_DIRECTORY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM,
107 delete_fn, &dstate);
108 free(mask);
109 if (NT_STATUS_IS_ERR(smbcli_rmdir(dstate.tree, dname))) {
110 DEBUG(2,("Failed to delete %s - %s\n",
111 dname, smbcli_errstr(dstate.tree)));
112 return -1;
114 dstate.total_deleted++;
116 if (dstate.failed) {
117 return -1;
120 return dstate.total_deleted;