]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
ctdb-common: Add tunable directory loading
authorMartin Schwenke <mschwenke@ddn.com>
Fri, 27 Jun 2025 05:11:59 +0000 (15:11 +1000)
committerMartin Schwenke <martins@samba.org>
Tue, 22 Jul 2025 23:02:34 +0000 (23:02 +0000)
This is simpler than include files and more general than just allowing
a 2nd file to be loaded.

Signed-off-by: Martin Schwenke <mschwenke@ddn.com>
Reviewed-by: Amitay Isaacs <amitay@gmail.com>
ctdb/common/tunable.c
ctdb/common/tunable.h

index 0641fde706616cdf269e475ffd42c6b5a242d850..e7525f586b691d59b91d527f2b9efe60e48e6896 100644 (file)
@@ -18,7 +18,9 @@
 */
 
 #include "replace.h"
+#include "system/dir.h"
 #include "system/filesys.h"
+#include "system/glob.h"
 #include "system/locale.h"
 #include "system/network.h"
 
@@ -400,3 +402,76 @@ bool ctdb_tunable_load_file(TALLOC_CTX *mem_ctx,
 
        return status && state.status;
 }
+
+static int tunables_filter(const struct dirent *de)
+{
+       int ret;
+
+       /* Match a script pattern */
+       ret = fnmatch("*.tunables", de->d_name, 0);
+       if (ret == 0) {
+               return 1;
+       }
+
+       return 0;
+}
+
+bool ctdb_tunable_load_directory(TALLOC_CTX *mem_ctx,
+                                struct ctdb_tunable_list *tun_list,
+                                const char *dir)
+{
+       struct dirent **namelist = NULL;
+       int count = 0;
+       bool status = true;
+       int i = 0;
+
+       count = scandir(dir, &namelist, tunables_filter, alphasort);
+       if (count == -1) {
+               switch (errno) {
+               case ENOENT:
+                       D_INFO("Optional tunables directory %s not found\n",
+                              dir);
+                       break;
+               default:
+                       DBG_ERR("Failed to open directory %s (err=%d)\n",
+                               dir,
+                               errno);
+                       status = false;
+               }
+               goto done;
+       }
+
+       if (count == 0) {
+               goto done;
+       }
+
+       for (i = 0; i < count; i++) {
+               char *file = NULL;
+               bool file_status = false;
+
+               file = talloc_asprintf(mem_ctx,
+                                      "%s/%s",
+                                      dir,
+                                      namelist[i]->d_name);
+               if (file == NULL) {
+                       DBG_ERR("Memory allocation error\n");
+                       goto done;
+               }
+
+               file_status = ctdb_tunable_load_file(mem_ctx, tun_list, file);
+               if (!file_status) {
+                       status = false;
+               }
+               TALLOC_FREE(file);
+       }
+
+done:
+       if (namelist != NULL && count != -1) {
+               for (i = 0; i < count; i++) {
+                       free(namelist[i]);
+               }
+               free(namelist);
+       }
+
+       return status;
+}
index b7cd62c4ba50abd8f1f89f1684c9837242524afb..bd8ee45bf1ab4656cd57f96a5d659b8407dc01f0 100644 (file)
@@ -38,5 +38,8 @@ char *ctdb_tunable_names_to_string(TALLOC_CTX *mem_ctx);
 bool ctdb_tunable_load_file(TALLOC_CTX *mem_ctx,
                            struct ctdb_tunable_list *tun_list,
                            const char *file);
+bool ctdb_tunable_load_directory(TALLOC_CTX *mem_ctx,
+                                struct ctdb_tunable_list *tun_list,
+                                const char *dir);
 
 #endif /* __CTDB_TUNABLE_H__ */