1
0
mirror of https://github.com/samba-team/samba.git synced 2024-12-22 13:34:15 +03:00

s4: libcli: Add smbcli_unlink_wcard().

We will use this in place of smbcli_unlink() when we
know we are using a wildcard pattern. If can be used
to generally replace smbcli_unlink() as it calls down
to smbcli_unlink() is no wildcard is detected.

Signed-off-by: Jeremy Allison <jra@samba.org>
Reviewed-by: Ralph Boehme <slow@samba.org>
This commit is contained in:
Jeremy Allison 2021-12-02 12:05:51 -08:00 committed by Ralph Boehme
parent e2b7a2f781
commit 3d0857c9ec
2 changed files with 101 additions and 0 deletions

View File

@ -23,6 +23,7 @@
#include "system/filesys.h"
#include "libcli/raw/libcliraw.h"
#include "libcli/libcli.h"
#include "system/dir.h"
/****************************************************************************
Hard/Symlink a file (UNIX extensions).
@ -148,6 +149,101 @@ NTSTATUS smbcli_unlink(struct smbcli_tree *tree, const char *fname)
return smb_raw_unlink(tree, &parms);
}
struct wcard_delete_state {
struct smbcli_tree *tree;
NTSTATUS status;
char *error_name; /* To help debugging. */
};
static void del_fn(struct clilist_file_info *finfo,
const char *pattern,
void *priv)
{
NTSTATUS status;
union smb_unlink parms;
char *filename = NULL;
char *dirname = NULL;
char *p = NULL;
struct wcard_delete_state *state = (struct wcard_delete_state *)priv;
if (ISDOT(finfo->name) || ISDOTDOT(finfo->name)) {
return;
}
dirname = talloc_strdup(state, pattern);
if (dirname == NULL) {
TALLOC_FREE(state->error_name);
state->status = NT_STATUS_NO_MEMORY;
return;
}
p = strrchr_m(dirname, '\\');
if (p != NULL) {
/* Remove the terminating '\' */
*p = '\0';
}
if (dirname[0] != '\0') {
filename = talloc_asprintf(dirname,
"%s\\%s",
dirname,
finfo->name);
} else {
filename = talloc_asprintf(dirname,
"%s",
finfo->name);
}
if (filename == NULL) {
TALLOC_FREE(dirname);
TALLOC_FREE(state->error_name);
state->status = NT_STATUS_NO_MEMORY;
return;
}
parms.unlink.in.pattern = filename;
parms.unlink.in.attrib = FILE_ATTRIBUTE_SYSTEM |
FILE_ATTRIBUTE_HIDDEN;
status = smb_raw_unlink(state->tree, &parms);
if (NT_STATUS_IS_OK(state->status)) {
state->status = status;
if (!NT_STATUS_IS_OK(status)) {
/*
* Save off the name we failed to
* delete to help debugging.
*/
state->error_name = talloc_move(state, &filename);
}
}
TALLOC_FREE(dirname);
}
/****************************************************************************
Delete a file, possibly with a wildcard pattern.
****************************************************************************/
NTSTATUS smbcli_unlink_wcard(struct smbcli_tree *tree, const char *pattern)
{
NTSTATUS status;
int ret;
struct wcard_delete_state *state = NULL;
if (strchr(pattern, '*') == NULL) {
/* No wildcard, just call smbcli_unlink(). */
return smbcli_unlink(tree, pattern);
}
state = talloc_zero(tree, struct wcard_delete_state);
if (state == NULL) {
return NT_STATUS_NO_MEMORY;
}
state->tree = tree;
ret = smbcli_list(tree,
pattern,
FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN,
del_fn,
state);
status = state->status;
TALLOC_FREE(state);
if (ret < 0) {
return NT_STATUS_UNSUCCESSFUL;
}
return status;
}
/****************************************************************************
Create a directory.
****************************************************************************/

View File

@ -158,6 +158,11 @@ NTSTATUS smbcli_rename(struct smbcli_tree *tree, const char *fname_src,
****************************************************************************/
NTSTATUS smbcli_unlink(struct smbcli_tree *tree, const char *fname);
/****************************************************************************
Delete a wildcard pattern of files.
****************************************************************************/
NTSTATUS smbcli_unlink_wcard(struct smbcli_tree *tree, const char *fname);
/****************************************************************************
Create a directory.
****************************************************************************/