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.
24 struct cli_tree *tree;
30 callback function for torture_deltree()
32 static void delete_fn(file_info *finfo, const char *name, void *state)
34 struct delete_state *dstate = state;
36 if (strcmp(finfo->name, ".") == 0 ||
37 strcmp(finfo->name, "..") == 0) return;
41 asprintf(&s, "%s%s", n, finfo->name);
43 if (finfo->mode & FILE_ATTRIBUTE_READONLY) {
44 if (NT_STATUS_IS_ERR(cli_setatr(dstate->tree, s, 0, 0))) {
45 DEBUG(2,("Failed to remove READONLY on %s - %s\n",
46 s, cli_errstr(dstate->tree)));
50 if (finfo->mode & FILE_ATTRIBUTE_DIRECTORY) {
52 asprintf(&s2, "%s\\*", s);
53 cli_unlink(dstate->tree, s2);
54 cli_list(dstate->tree, s2,
55 FILE_ATTRIBUTE_DIRECTORY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM,
58 if (NT_STATUS_IS_ERR(cli_rmdir(dstate->tree, s))) {
59 DEBUG(2,("Failed to delete %s - %s\n",
60 s, cli_errstr(dstate->tree)));
61 dstate->failed = True;
63 dstate->total_deleted++;
65 if (NT_STATUS_IS_ERR(cli_unlink(dstate->tree, s))) {
66 DEBUG(2,("Failed to delete %s - %s\n",
67 s, cli_errstr(dstate->tree)));
68 dstate->failed = True;
70 dstate->total_deleted++;
77 recursively descend a tree deleting all files
78 returns the number of files deleted, or -1 on error
80 int cli_deltree(struct cli_tree *tree, const char *dname)
83 struct delete_state dstate;
86 dstate.total_deleted = 0;
87 dstate.failed = False;
89 /* it might be a file */
90 if (NT_STATUS_IS_OK(cli_unlink(tree, dname))) {
93 if (NT_STATUS_EQUAL(cli_nt_error(tree), NT_STATUS_OBJECT_NAME_NOT_FOUND) ||
94 NT_STATUS_EQUAL(cli_nt_error(tree), NT_STATUS_OBJECT_PATH_NOT_FOUND) ||
95 NT_STATUS_EQUAL(cli_nt_error(tree), NT_STATUS_NO_SUCH_FILE)) {
99 asprintf(&mask, "%s\\*", dname);
100 cli_unlink(dstate.tree, mask);
101 cli_list(dstate.tree, mask,
102 FILE_ATTRIBUTE_DIRECTORY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM,
105 if (NT_STATUS_IS_ERR(cli_rmdir(dstate.tree, dname))) {
106 DEBUG(2,("Failed to delete %s - %s\n",
107 dname, cli_errstr(dstate.tree)));
110 dstate.total_deleted++;
116 return dstate.total_deleted;