]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
s3/lib: modernize set_namearray()
authorRalph Boehme <slow@samba.org>
Sat, 20 Jan 2024 11:58:52 +0000 (12:58 +0100)
committerStefan Metzmacher <metze@samba.org>
Fri, 26 Jul 2024 10:06:30 +0000 (10:06 +0000)
No change in behaviour.

Signed-off-by: Ralph Boehme <slow@samba.org>
Reviewed-by: Stefan Metzmacher <metze@samba.org>
source3/include/proto.h
source3/include/vfs.h
source3/lib/namearray.c [deleted file]
source3/lib/util.c
source3/modules/vfs_virusfilter.c
source3/smbd/conn.c
source3/smbd/smb2_service.c
source3/torture/test_matching.c
source3/wscript_build

index 639f93c237f21a5d85c4b643c935055caa4fa454..b25b11778410ece35062c381ad5e3b89f60c593d 100644 (file)
@@ -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);
index 56fe0a69195cf1df9a4e085d089f6401b5068e9e..a0974a49d49082aebf8c195c1fb7878a15609e3c 100644 (file)
@@ -703,7 +703,7 @@ struct vuid_cache {
 };
 
 struct name_compare_entry {
-       char *name;
+       const char *name;
        bool is_wild;
 };
 
diff --git a/source3/lib/namearray.c b/source3/lib/namearray.c
deleted file mode 100644 (file)
index f66dd0a..0000000
+++ /dev/null
@@ -1,39 +0,0 @@
-/*
-   Unix SMB/CIFS implementation.
-   Samba utility functions
-   Copyright (C) Andrew Tridgell 1992-1998
-   Copyright (C) Jeremy Allison 2001-2007
-   Copyright (C) Simo Sorce 2001
-   Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2003
-   Copyright (C) James Peach 2006
-
-   This program is free software; you can redistribute it and/or modify
-   it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 3 of the License, or
-   (at your option) any later version.
-
-   This program is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-   GNU General Public License for more details.
-
-   You should have received a copy of the GNU General Public License
-   along with this program.  If not, see <http://www.gnu.org/licenses/>.
-*/
-
-#include "includes.h"
-/****************************************************************************
- Routine to free a namearray.
-****************************************************************************/
-
-void free_namearray(struct name_compare_entry *name_array)
-{
-       int i;
-
-       if(name_array == NULL)
-               return;
-
-       for(i=0; name_array[i].name!=NULL; i++)
-               SAFE_FREE(name_array[i].name);
-       SAFE_FREE(name_array);
-}
index d7d1c48aa98213f6d7d450f84945a9e6052b2303..3246294261a5cb37b9c0afbffb894a2b197feaaf 100644 (file)
@@ -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>
@@ -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;
 }
 
index ea1886d85c839eb49107926a34b2cf0b4219a547..471eff94e89af445210b5c8a1fb9054dbd065d26 100644 (file)
@@ -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(
@@ -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);
index b7a745a951e85f2731ba25373ae4b915714b5df8..6694147c225d00cced8d248ae35b4ea079a8988b 100644 (file)
@@ -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);
 }
 
index e7f5d9ed358f508c983db22b17b8137b692a1772..f832008a698b78e9c294dfec4795c2751b606730 100644 (file)
@@ -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,
index 5c2f75bfbfe94a00aa77fc7487a642ca49a6aa2a..2b867fb37de0c1e246fe209d648039880d7b5e88 100644 (file)
@@ -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(),
index 59b65f41239f2dd71e083d0016c2285881b34604..cb68e7fffadd3cdab4a79392cb2512daea72b932 100644 (file)
@@ -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