tools: add shared path directory helper

Move the common path-is-directory check into tools.c and declare it in net.h.

Replace the local NDIR and Trustee helper implementations with
tool_path_is_dir(), and update NDIR, REMOVE and REVOKE callers to use the
shared helper directly. The helper preserves the existing behavior of treating
empty and current-directory paths as directories and otherwise falling back to
stat().

No behavior change.
This commit is contained in:
Mario Fetka
2026-05-29 13:09:05 +02:00
parent e73eb89f8c
commit 686b27a668
7 changed files with 32 additions and 52 deletions

16
ndir.c
View File

@@ -229,20 +229,6 @@ static int ndir_is_accepted_stub(char *s)
return(0);
}
static int ndir_path_is_dir(char *path)
{
struct stat st;
if (!path || !*path || tool_is_current_path(path))
return(1);
if (stat(path, &st) == 0) {
if (st.st_mode & S_IFDIR)
return(1);
}
return(0);
}
static void ndir_split_spec(char *spec, char *dir, char *pat)
{
@@ -252,7 +238,7 @@ static void ndir_split_spec(char *spec, char *dir, char *pat)
return;
}
if (!tool_has_wildcards(spec) && ndir_path_is_dir(spec)) {
if (!tool_has_wildcards(spec) && tool_path_is_dir(spec)) {
strmaxcpy(dir, spec, 259);
strmaxcpy(pat, "*.*", 259);
return;

1
net.h
View File

@@ -160,6 +160,7 @@ extern int tool_current_dhandle(uint8 *connid, uint8 *dhandle);
extern int tool_current_dhandle_only(uint8 *dhandle);
extern int tool_current_prefix(char *out, int max);
extern int tool_is_current_path(char *path);
extern int tool_path_is_dir(char *path);
extern void tool_upcopy(char *dst, char *src, int max);
extern void tool_basename(char *dst, char *src, int max);
extern void tool_header_path(char *out, char *path, int max);

View File

@@ -148,7 +148,7 @@ static int remove_files(char *path, uint16 dhandle, uint32 object_id,
char target[260];
int rc = 0;
if (trustee_path_is_dir(path)) {
if (tool_path_is_dir(path)) {
strmaxcpy(dir, path, sizeof(dir) - 1);
strmaxcpy(pat, "*.*", sizeof(pat) - 1);
} else if (tool_has_wildcards(path)) {

View File

@@ -187,7 +187,7 @@ static int revoke_files(char *path, uint16 dhandle, uint32 object_id,
char target[260];
int rc = 0;
if (trustee_path_is_dir(path)) {
if (tool_path_is_dir(path)) {
strmaxcpy(dir, path, sizeof(dir) - 1);
strmaxcpy(pat, "*.*", sizeof(pat) - 1);
} else if (tool_has_wildcards(path)) {

28
tools.c
View File

@@ -854,6 +854,34 @@ int tool_is_current_path(char *path)
return(0);
}
/*
* tool_path_is_dir
*
* Purpose:
* Tests whether a path should be treated as a directory.
*
* Parameters:
* path - DOS/NetWare path to test.
*
* Returns:
* Non-zero when the path is empty/current-directory syntax or stat() reports
* a directory, otherwise zero.
*/
int tool_path_is_dir(char *path)
{
struct stat st;
if (tool_is_current_path(path))
return(1);
if (stat(path, &st) == 0) {
if (st.st_mode & S_IFDIR)
return(1);
}
return(0);
}
/*
* tool_upcopy
*

View File

@@ -212,38 +212,4 @@ uint32 trustee_lookup_object(char *name, uint16 *objtype, int objtype_given)
return(object_id);
}
/*
* trustee_path_is_dir
*
* Purpose:
* Tests whether a path should be treated as a directory by the trustee
* commands.
*
* Parameters:
* path - DOS/NetWare path to test.
*
* Returns:
* Non-zero when the path is empty/current-directory syntax or stat() reports
* a directory, otherwise zero.
*
* Notes:
* This remains in trustee.c because the empty/current-directory handling is
* part of the GRANT/REMOVE/REVOKE trustee command semantics rather than a
* completely generic path predicate.
*/
int trustee_path_is_dir(char *path)
{
struct stat st;
if (!path || !*path || tool_strsame(path, ".") || tool_strsame(path, ".\\") ||
tool_strsame(path, "./"))
return(1);
if (stat(path, &st) == 0) {
if (st.st_mode & S_IFDIR)
return(1);
}
return(0);
}

View File

@@ -47,6 +47,5 @@ int trustee_parse_right_word(char *s, uint16 *rights);
void trustee_rights_bracket(uint16 rights, char *out);
void trustee_header_path(char *out, char *path, int max);
uint32 trustee_lookup_object(char *name, uint16 *objtype, int objtype_given);
int trustee_path_is_dir(char *path);
#endif