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 3 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, see <http://www.gnu.org/licenses/>.
21 #include "libcli/raw/libcliraw.h"
22 #include "libcli/libcli.h"
23 #include "system/dir.h"
26 struct smbcli_tree
*tree
;
32 callback function for torture_deltree()
34 static void delete_fn(struct clilist_file_info
*finfo
, const char *name
, void *state
)
36 struct delete_state
*dstate
= (struct delete_state
*)state
;
38 if (ISDOT(finfo
->name
) || ISDOTDOT(finfo
->name
)) {
44 if (asprintf(&s
, "%s%s", n
, finfo
->name
) < 0) {
49 if (finfo
->attrib
& FILE_ATTRIBUTE_READONLY
) {
50 if (NT_STATUS_IS_ERR(smbcli_setatr(dstate
->tree
, s
, 0, 0))) {
51 DEBUG(2,("Failed to remove READONLY on %s - %s\n",
52 s
, smbcli_errstr(dstate
->tree
)));
56 if (finfo
->attrib
& FILE_ATTRIBUTE_DIRECTORY
) {
58 if (asprintf(&s2
, "%s\\*", s
) < 0) {
63 smbcli_unlink(dstate
->tree
, s2
);
64 smbcli_list(dstate
->tree
, s2
,
65 FILE_ATTRIBUTE_DIRECTORY
|FILE_ATTRIBUTE_HIDDEN
|FILE_ATTRIBUTE_SYSTEM
,
68 if (NT_STATUS_IS_ERR(smbcli_rmdir(dstate
->tree
, s
))) {
69 DEBUG(2,("Failed to delete %s - %s\n",
70 s
, smbcli_errstr(dstate
->tree
)));
71 dstate
->failed
= true;
73 dstate
->total_deleted
++;
75 if (NT_STATUS_IS_ERR(smbcli_unlink(dstate
->tree
, s
))) {
76 DEBUG(2,("Failed to delete %s - %s\n",
77 s
, smbcli_errstr(dstate
->tree
)));
78 dstate
->failed
= true;
80 dstate
->total_deleted
++;
87 recursively descend a tree deleting all files
88 returns the number of files deleted, or -1 on error
90 int smbcli_deltree(struct smbcli_tree
*tree
, const char *dname
)
93 struct delete_state dstate
;
97 dstate
.total_deleted
= 0;
98 dstate
.failed
= false;
100 /* it might be a file */
101 status
= smbcli_unlink(tree
, dname
);
102 if (NT_STATUS_IS_OK(status
)) {
105 if (NT_STATUS_EQUAL(smbcli_nt_error(tree
), NT_STATUS_OBJECT_NAME_NOT_FOUND
) ||
106 NT_STATUS_EQUAL(smbcli_nt_error(tree
), NT_STATUS_OBJECT_PATH_NOT_FOUND
) ||
107 NT_STATUS_EQUAL(smbcli_nt_error(tree
), NT_STATUS_NO_SUCH_FILE
) ||
108 NT_STATUS_EQUAL(smbcli_nt_error(tree
), NT_STATUS_DOS(ERRDOS
, ERRbadfile
))) {
111 if (NT_STATUS_EQUAL(status
, NT_STATUS_CANNOT_DELETE
)) {
112 /* it could be read-only */
113 smbcli_setatr(tree
, dname
, FILE_ATTRIBUTE_NORMAL
, 0);
114 if (NT_STATUS_IS_OK(smbcli_unlink(tree
, dname
))) {
119 if (asprintf(&mask
, "%s\\*", dname
) < 0) {
122 smbcli_unlink_wcard(dstate
.tree
, mask
);
123 smbcli_list(dstate
.tree
, mask
,
124 FILE_ATTRIBUTE_DIRECTORY
|FILE_ATTRIBUTE_HIDDEN
|FILE_ATTRIBUTE_SYSTEM
,
128 status
= smbcli_rmdir(dstate
.tree
, dname
);
129 if (NT_STATUS_EQUAL(status
, NT_STATUS_CANNOT_DELETE
)) {
130 /* it could be read-only */
131 smbcli_setatr(dstate
.tree
, dname
, FILE_ATTRIBUTE_NORMAL
, 0);
132 status
= smbcli_rmdir(dstate
.tree
, dname
);
134 if (NT_STATUS_IS_ERR(status
)) {
135 DEBUG(2,("Failed to delete %s - %s\n",
136 dname
, smbcli_errstr(dstate
.tree
)));
139 dstate
.total_deleted
++;
145 return dstate
.total_deleted
;