Skip to content

Commit

Permalink
s3/lib: modernize set_namearray()
Browse files Browse the repository at this point in the history
No change in behaviour.

Signed-off-by: Ralph Boehme <[email protected]>
Reviewed-by: Stefan Metzmacher <[email protected]>
  • Loading branch information
slowfranklin authored and metze-samba committed Jul 26, 2024
1 parent f564fcb commit 7fc74c7
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 141 deletions.
6 changes: 3 additions & 3 deletions source3/include/proto.h
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,9 @@ const char *readdirname(DIR *p);
bool is_in_path(const char *name,
struct name_compare_entry *namelist,
bool case_sensitive);
void set_namearray(struct name_compare_entry **ppname_array,
const char *namelist);
void free_namearray(struct name_compare_entry *name_array);
void set_namearray(TALLOC_CTX *mem_ctx,
const char *namelist,
struct name_compare_entry **_name_array);
bool fcntl_lock(int fd, int op, off_t offset, off_t count, int type);
bool fcntl_getlock(int fd, int op, off_t *poffset, off_t *pcount, int *ptype, pid_t *ppid);
int map_process_lock_to_ofd_lock(int op);
Expand Down
2 changes: 1 addition & 1 deletion source3/include/vfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ struct vuid_cache {
};

struct name_compare_entry {
char *name;
const char *name;
bool is_wild;
};

Expand Down
39 changes: 0 additions & 39 deletions source3/lib/namearray.c

This file was deleted.

110 changes: 30 additions & 80 deletions source3/lib/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include "lib/dbwrap/dbwrap_ctdb.h"
#include "lib/gencache.h"
#include "lib/util/string_wrappers.h"
#include "lib/util/strv.h"

#ifdef HAVE_SYS_PRCTL_H
#include <sys/prctl.h>
Expand Down Expand Up @@ -802,106 +803,55 @@ bool is_in_path(const char *name,
if possible.
********************************************************************/

void set_namearray(struct name_compare_entry **ppname_array, const char *namelist_in)
void set_namearray(TALLOC_CTX *mem_ctx,
const char *namelist_in,
struct name_compare_entry **_name_array)
{
char *name_end;
char *namelist;
char *namelist_end;
char *nameptr;
int num_entries = 0;
int i;
struct name_compare_entry *name_array = NULL;
struct name_compare_entry *e = NULL;
char *namelist = NULL;
const char *p = NULL;
size_t num_entries;

(*ppname_array) = NULL;
*_name_array = NULL;

if((namelist_in == NULL ) || ((namelist_in != NULL) && (*namelist_in == '\0')))
if ((namelist_in == NULL) || (namelist_in[0] == '\0')) {
return;
}

namelist = talloc_strdup(talloc_tos(), namelist_in);
namelist = path_to_strv(mem_ctx, namelist_in);
if (namelist == NULL) {
DEBUG(0,("set_namearray: talloc fail\n"));
DBG_ERR("path_to_strv failed\n");
return;
}
nameptr = namelist;

namelist_end = &namelist[strlen(namelist)];

/* We need to make two passes over the string. The
first to count the number of elements, the second
to split it.
*/

while(nameptr <= namelist_end) {
if ( *nameptr == '/' ) {
/* cope with multiple (useless) /s) */
nameptr++;
continue;
}
/* anything left? */
if ( *nameptr == '\0' )
break;

/* find the next '/' or consume remaining */
name_end = strchr_m(nameptr, '/');
if (name_end == NULL) {
/* Point nameptr at the terminating '\0' */
nameptr += strlen(nameptr);
} else {
/* next segment please */
nameptr = name_end + 1;
}
num_entries++;
}
num_entries = strv_count(namelist);

if(num_entries == 0) {
talloc_free(namelist);
name_array = talloc_zero_array(mem_ctx,
struct name_compare_entry,
num_entries + 1);
if (name_array == NULL) {
DBG_ERR("talloc failed\n");
TALLOC_FREE(namelist);
return;
}

if(( (*ppname_array) = SMB_MALLOC_ARRAY(struct name_compare_entry, num_entries + 1)) == NULL) {
DEBUG(0,("set_namearray: malloc fail\n"));
talloc_free(namelist);
return;
}
namelist = talloc_reparent(mem_ctx, name_array, namelist);

/* Now copy out the names */
nameptr = namelist;
i = 0;
while(nameptr <= namelist_end) {
if ( *nameptr == '/' ) {
e = &name_array[0];

while ((p = strv_next(namelist, p)) != NULL) {
if (*p == '\0') {
/* cope with multiple (useless) /s) */
nameptr++;
continue;
}
/* anything left? */
if ( *nameptr == '\0' )
break;

/* find the next '/' or consume remaining */
name_end = strchr_m(nameptr, '/');
if (name_end != NULL) {
*name_end = '\0';
}

(*ppname_array)[i].is_wild = ms_has_wild(nameptr);
if(((*ppname_array)[i].name = SMB_STRDUP(nameptr)) == NULL) {
DEBUG(0,("set_namearray: malloc fail (1)\n"));
talloc_free(namelist);
return;
}

if (name_end == NULL) {
/* Point nameptr at the terminating '\0' */
nameptr += strlen(nameptr);
} else {
/* next segment please */
nameptr = name_end + 1;
}
i++;
e->name = p;
e->is_wild = ms_has_wild(e->name);
e++;
}

(*ppname_array)[i].name = NULL;

talloc_free(namelist);
*_name_array = name_array;
return;
}

Expand Down
9 changes: 6 additions & 3 deletions source3/modules/vfs_virusfilter.c
Original file line number Diff line number Diff line change
Expand Up @@ -255,13 +255,17 @@ static int virusfilter_vfs_connect(
exclude_files = lp_parm_const_string(
snum, "virusfilter", "exclude files", NULL);
if (exclude_files != NULL) {
set_namearray(&config->exclude_files, exclude_files);
set_namearray(config,
exclude_files,
&config->exclude_files);
}

infected_files = lp_parm_const_string(
snum, "virusfilter", "infected files", NULL);
if (infected_files != NULL) {
set_namearray(&config->infected_files, infected_files);
set_namearray(config,
infected_files,
&config->infected_files);
}

config->cache_entry_limit = lp_parm_int(
Expand Down Expand Up @@ -579,7 +583,6 @@ static void virusfilter_vfs_disconnect(struct vfs_handle_struct *handle)
config->backend->fns->disconnect(handle);
}

free_namearray(config->exclude_files);
virusfilter_io_disconnect(config->io_h);

SMB_VFS_NEXT_DISCONNECT(handle);
Expand Down
5 changes: 0 additions & 5 deletions source3/smbd/conn.c
Original file line number Diff line number Diff line change
Expand Up @@ -250,11 +250,6 @@ static void conn_free_internal(connection_struct *conn)
SAFE_FREE(state->data);
}

free_namearray(conn->veto_list);
free_namearray(conn->hide_list);
free_namearray(conn->veto_oplock_list);
free_namearray(conn->aio_write_behind_list);

ZERO_STRUCTP(conn);
}

Expand Down
20 changes: 12 additions & 8 deletions source3/smbd/smb2_service.c
Original file line number Diff line number Diff line change
Expand Up @@ -753,14 +753,18 @@ NTSTATUS make_connection_snum(struct smbXsrv_connection *xconn,

/* Add veto/hide lists */
if (!IS_IPC(conn) && !IS_PRINT(conn)) {
set_namearray( &conn->veto_list,
lp_veto_files(talloc_tos(), lp_sub, snum));
set_namearray( &conn->hide_list,
lp_hide_files(talloc_tos(), lp_sub, snum));
set_namearray( &conn->veto_oplock_list,
lp_veto_oplock_files(talloc_tos(), lp_sub, snum));
set_namearray( &conn->aio_write_behind_list,
lp_aio_write_behind(talloc_tos(), lp_sub, snum));
set_namearray(conn,
lp_veto_oplock_files(talloc_tos(), lp_sub, snum),
&conn->veto_oplock_list);
set_namearray(conn,
lp_aio_write_behind(talloc_tos(), lp_sub, snum),
&conn->aio_write_behind_list);
set_namearray(conn,
lp_veto_oplock_files(talloc_tos(), lp_sub, snum),
&conn->veto_oplock_list);
set_namearray(conn,
lp_aio_write_behind(talloc_tos(), lp_sub, snum),
&conn->aio_write_behind_list);
}
smb_fname_cpath = synthetic_smb_fname(talloc_tos(),
conn->connectpath,
Expand Down
2 changes: 1 addition & 1 deletion source3/torture/test_matching.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ bool run_str_match_mswild(int dummy)

d_fprintf(stderr, "namelist: %s\n", namelist);

set_namearray(&name_entries, namelist);
set_namearray(talloc_tos(), namelist, &name_entries);
SMB_ASSERT(name_entries != NULL);

status = samba_path_matching_mswild_create(talloc_tos(),
Expand Down
1 change: 0 additions & 1 deletion source3/wscript_build
Original file line number Diff line number Diff line change
Expand Up @@ -990,7 +990,6 @@ bld.SAMBA3_LIBRARY('samba3-util',
lib/util_str.c
lib/adt_tree.c
lib/util_malloc.c
lib/namearray.c
lib/file_id.c
lib/cbuf.c
lib/per_thread_cwd.c
Expand Down

0 comments on commit 7fc74c7

Please sign in to comment.