libsmbconf: Convert smbconf_get_share_names() to sbcErr.
[samba.git] / lib / smbconf / smbconf_txt.c
index 501382cc5fd0416ab50fe61f69ffe7bc9ba69092..444fa086633f3acadd8a7f0b6a6a677f4ed1cce3 100644 (file)
@@ -27,6 +27,7 @@
 
 #include "includes.h"
 #include "smbconf_private.h"
+#include "lib/smbconf/smbconf_txt.h"
 
 struct txt_cache {
        uint32_t current_share;
@@ -59,7 +60,7 @@ static struct txt_private_data *pd(struct smbconf_ctx *ctx)
 
 static bool smbconf_txt_do_section(const char *section, void *private_data)
 {
-       WERROR werr;
+       sbcErr err;
        uint32_t idx;
        struct txt_private_data *tpd = (struct txt_private_data *)private_data;
        struct txt_cache *cache = tpd->cache;
@@ -71,9 +72,9 @@ static bool smbconf_txt_do_section(const char *section, void *private_data)
                return true;
        }
 
-       werr = smbconf_add_string_to_array(cache, &(cache->share_names),
-                                          cache->num_shares, section);
-       if (!W_ERROR_IS_OK(werr)) {
+       err = smbconf_add_string_to_array(cache, &(cache->share_names),
+                                         cache->num_shares, section);
+       if (!SBC_ERROR_IS_OK(err)) {
                return false;
        }
        cache->current_share = cache->num_shares;
@@ -113,7 +114,7 @@ static bool smbconf_txt_do_parameter(const char *param_name,
                                     const char *param_value,
                                     void *private_data)
 {
-       WERROR werr;
+       sbcErr err;
        char **param_names, **param_values;
        uint32_t num_params;
        uint32_t idx;
@@ -145,17 +146,17 @@ static bool smbconf_txt_do_parameter(const char *param_name,
                }
                return true;
        }
-       werr = smbconf_add_string_to_array(cache,
+       err = smbconf_add_string_to_array(cache,
                                &(cache->param_names[cache->current_share]),
                                num_params, param_name);
-       if (!W_ERROR_IS_OK(werr)) {
+       if (!SBC_ERROR_IS_OK(err)) {
                return false;
        }
-       werr = smbconf_add_string_to_array(cache,
+       err = smbconf_add_string_to_array(cache,
                                &(cache->param_values[cache->current_share]),
                                num_params, param_value);
        cache->num_params[cache->current_share]++;
-       return W_ERROR_IS_OK(werr);
+       return SBC_ERROR_IS_OK(err);
 }
 
 static void smbconf_txt_flush_cache(struct smbconf_ctx *ctx)
@@ -164,7 +165,7 @@ static void smbconf_txt_flush_cache(struct smbconf_ctx *ctx)
        pd(ctx)->cache = NULL;
 }
 
-static WERROR smbconf_txt_init_cache(struct smbconf_ctx *ctx)
+static sbcErr smbconf_txt_init_cache(struct smbconf_ctx *ctx)
 {
        if (pd(ctx)->cache != NULL) {
                smbconf_txt_flush_cache(ctx);
@@ -173,40 +174,40 @@ static WERROR smbconf_txt_init_cache(struct smbconf_ctx *ctx)
        pd(ctx)->cache = talloc_zero(pd(ctx), struct txt_cache);
 
        if (pd(ctx)->cache == NULL) {
-               return WERR_NOMEM;
+               return SBC_ERR_NOMEM;
        }
 
-       return WERR_OK;
+       return SBC_ERR_OK;
 }
 
-static WERROR smbconf_txt_load_file(struct smbconf_ctx *ctx)
+static sbcErr smbconf_txt_load_file(struct smbconf_ctx *ctx)
 {
-       WERROR werr;
+       sbcErr err;
        uint64_t new_csn;
 
        if (!file_exist(ctx->path)) {
-               return WERR_BADFILE;
+               return SBC_ERR_BADFILE;
        }
 
        new_csn = (uint64_t)file_modtime(ctx->path);
        if (new_csn == pd(ctx)->csn) {
-               return WERR_OK;
+               return SBC_ERR_OK;
        }
 
-       werr = smbconf_txt_init_cache(ctx);
-       if (!W_ERROR_IS_OK(werr)) {
-               return werr;
+       err = smbconf_txt_init_cache(ctx);
+       if (!SBC_ERROR_IS_OK(err)) {
+               return err;
        }
 
        if (!pm_process(ctx->path, smbconf_txt_do_section,
                        smbconf_txt_do_parameter, pd(ctx)))
        {
-               return WERR_CAN_NOT_COMPLETE;
+               return SBC_ERR_CAN_NOT_COMPLETE;
        }
 
        pd(ctx)->csn = new_csn;
 
-       return WERR_OK;
+       return SBC_ERR_OK;
 }
 
 
@@ -219,24 +220,24 @@ static WERROR smbconf_txt_load_file(struct smbconf_ctx *ctx)
 /**
  * initialize the text based smbconf backend
  */
-static WERROR smbconf_txt_init(struct smbconf_ctx *ctx, const char *path)
+static sbcErr smbconf_txt_init(struct smbconf_ctx *ctx, const char *path)
 {
        if (path == NULL) {
-               return WERR_BADFILE;
+               return SBC_ERR_BADFILE;
        }
        ctx->path = talloc_strdup(ctx, path);
        if (ctx->path == NULL) {
-               return WERR_NOMEM;
+               return SBC_ERR_NOMEM;
        }
 
        ctx->data = talloc_zero(ctx, struct txt_private_data);
        if (ctx->data == NULL) {
-               return WERR_NOMEM;
+               return SBC_ERR_NOMEM;
        }
 
        pd(ctx)->verbatim = true;
 
-       return WERR_OK;
+       return SBC_ERR_OK;
 }
 
 static int smbconf_txt_shutdown(struct smbconf_ctx *ctx)
@@ -255,7 +256,7 @@ static bool smbconf_txt_is_writeable(struct smbconf_ctx *ctx)
        return false;
 }
 
-static WERROR smbconf_txt_open(struct smbconf_ctx *ctx)
+static sbcErr smbconf_txt_open(struct smbconf_ctx *ctx)
 {
        return smbconf_txt_load_file(ctx);
 }
@@ -284,15 +285,15 @@ static void smbconf_txt_get_csn(struct smbconf_ctx *ctx,
 /**
  * Drop the whole configuration (restarting empty)
  */
-static WERROR smbconf_txt_drop(struct smbconf_ctx *ctx)
+static sbcErr smbconf_txt_drop(struct smbconf_ctx *ctx)
 {
-       return WERR_NOT_SUPPORTED;
+       return SBC_ERR_NOT_SUPPORTED;
 }
 
 /**
  * get the list of share names defined in the configuration.
  */
-static WERROR smbconf_txt_get_share_names(struct smbconf_ctx *ctx,
+static sbcErr smbconf_txt_get_share_names(struct smbconf_ctx *ctx,
                                          TALLOC_CTX *mem_ctx,
                                          uint32_t *num_shares,
                                          char ***share_names)
@@ -300,17 +301,16 @@ static WERROR smbconf_txt_get_share_names(struct smbconf_ctx *ctx,
        uint32_t count;
        uint32_t added_count = 0;
        TALLOC_CTX *tmp_ctx = NULL;
-       WERROR werr = WERR_OK;
+       sbcErr err = SBC_ERR_OK;
        char **tmp_share_names = NULL;
 
        if ((num_shares == NULL) || (share_names == NULL)) {
-               werr = WERR_INVALID_PARAM;
-               goto done;
+               return SBC_ERR_INVALID_PARAM;
        }
 
-       werr = smbconf_txt_load_file(ctx);
-       if (!W_ERROR_IS_OK(werr)) {
-               return werr;
+       err = smbconf_txt_load_file(ctx);
+       if (!SBC_ERROR_IS_OK(err)) {
+               return err;
        }
 
        tmp_ctx = talloc_stackframe();
@@ -319,18 +319,18 @@ static WERROR smbconf_txt_get_share_names(struct smbconf_ctx *ctx,
         * possibly after NULL section */
 
        if (smbconf_share_exists(ctx, NULL)) {
-               werr = smbconf_add_string_to_array(tmp_ctx, &tmp_share_names,
-                                                  0, NULL);
-               if (!W_ERROR_IS_OK(werr)) {
+               err = smbconf_add_string_to_array(tmp_ctx, &tmp_share_names,
+                                                 0, NULL);
+               if (!SBC_ERROR_IS_OK(err)) {
                        goto done;
                }
                added_count++;
        }
 
        if (smbconf_share_exists(ctx, GLOBAL_NAME)) {
-               werr = smbconf_add_string_to_array(tmp_ctx, &tmp_share_names,
+               err = smbconf_add_string_to_array(tmp_ctx, &tmp_share_names,
                                                   added_count, GLOBAL_NAME);
-               if (!W_ERROR_IS_OK(werr)) {
+               if (!SBC_ERROR_IS_OK(err)) {
                        goto done;
                }
                added_count++;
@@ -343,10 +343,10 @@ static WERROR smbconf_txt_get_share_names(struct smbconf_ctx *ctx,
                        continue;
                }
 
-               werr = smbconf_add_string_to_array(tmp_ctx, &tmp_share_names,
+               err = smbconf_add_string_to_array(tmp_ctx, &tmp_share_names,
                                        added_count,
                                        pd(ctx)->cache->share_names[count]);
-               if (!W_ERROR_IS_OK(werr)) {
+               if (!SBC_ERROR_IS_OK(err)) {
                        goto done;
                }
                added_count++;
@@ -361,7 +361,7 @@ static WERROR smbconf_txt_get_share_names(struct smbconf_ctx *ctx,
 
 done:
        talloc_free(tmp_ctx);
-       return werr;
+       return err;
 }
 
 /**
@@ -370,10 +370,10 @@ done:
 static bool smbconf_txt_share_exists(struct smbconf_ctx *ctx,
                                     const char *servicename)
 {
-       WERROR werr;
+       sbcErr err;
 
-       werr = smbconf_txt_load_file(ctx);
-       if (!W_ERROR_IS_OK(werr)) {
+       err = smbconf_txt_load_file(ctx);
+       if (!SBC_ERROR_IS_OK(err)) {
                return false;
        }
 
@@ -400,14 +400,15 @@ static WERROR smbconf_txt_get_share(struct smbconf_ctx *ctx,
                                    struct smbconf_service **service)
 {
        WERROR werr;
+       sbcErr err;
        uint32_t sidx, count;
        bool found;
        TALLOC_CTX *tmp_ctx = NULL;
        struct smbconf_service *tmp_service = NULL;
 
-       werr = smbconf_txt_load_file(ctx);
-       if (!W_ERROR_IS_OK(werr)) {
-               return werr;
+       err = smbconf_txt_load_file(ctx);
+       if (!SBC_ERROR_IS_OK(err)) {
+               return WERR_GENERAL_FAILURE;
        }
 
        found = smbconf_find_in_array(servicename,
@@ -435,28 +436,26 @@ static WERROR smbconf_txt_get_share(struct smbconf_ctx *ctx,
        }
 
        for (count = 0; count < pd(ctx)->cache->num_params[sidx]; count++) {
-               werr = smbconf_add_string_to_array(tmp_service,
+               err = smbconf_add_string_to_array(tmp_service,
                                &(tmp_service->param_names),
                                count,
                                pd(ctx)->cache->param_names[sidx][count]);
-               if (!W_ERROR_IS_OK(werr)) {
+               if (!SBC_ERROR_IS_OK(err)) {
+                       werr = WERR_NOMEM;
                        goto done;
                }
-               werr = smbconf_add_string_to_array(tmp_service,
+               err = smbconf_add_string_to_array(tmp_service,
                                &(tmp_service->param_values),
                                count,
                                pd(ctx)->cache->param_values[sidx][count]);
-               if (!W_ERROR_IS_OK(werr)) {
+               if (!SBC_ERROR_IS_OK(err)) {
+                       werr = WERR_NOMEM;
                        goto done;
                }
        }
 
        tmp_service->num_params = count;
-       if (count > 0) {
-               *service = talloc_move(mem_ctx, &tmp_service);
-       } else {
-               *service = NULL;
-       }
+       *service = talloc_move(mem_ctx, &tmp_service);
 
 done:
        talloc_free(tmp_ctx);
@@ -492,13 +491,13 @@ static WERROR smbconf_txt_get_parameter(struct smbconf_ctx *ctx,
                                        const char *param,
                                        char **valstr)
 {
-       WERROR werr;
+       sbcErr err;
        bool found;
        uint32_t share_index, param_index;
 
-       werr = smbconf_txt_load_file(ctx);
-       if (!W_ERROR_IS_OK(werr)) {
-               return werr;
+       err = smbconf_txt_load_file(ctx);
+       if (!SBC_ERROR_IS_OK(err)) {
+               return WERR_GENERAL_FAILURE;
        }
 
        found = smbconf_find_in_array(service,
@@ -544,15 +543,16 @@ static WERROR smbconf_txt_get_includes(struct smbconf_ctx *ctx,
                                       char ***includes)
 {
        WERROR werr;
+       sbcErr err;
        bool found;
        uint32_t sidx, count;
        TALLOC_CTX *tmp_ctx = NULL;
        uint32_t tmp_num_includes = 0;
        char **tmp_includes = NULL;
 
-       werr = smbconf_txt_load_file(ctx);
-       if (!W_ERROR_IS_OK(werr)) {
-               return werr;
+       err = smbconf_txt_load_file(ctx);
+       if (!SBC_ERROR_IS_OK(err)) {
+               return WERR_GENERAL_FAILURE;
        }
 
        found = smbconf_find_in_array(service,
@@ -569,11 +569,12 @@ static WERROR smbconf_txt_get_includes(struct smbconf_ctx *ctx,
                if (strequal(pd(ctx)->cache->param_names[sidx][count],
                             "include"))
                {
-                       werr = smbconf_add_string_to_array(tmp_ctx,
+                       err = smbconf_add_string_to_array(tmp_ctx,
                                &tmp_includes,
                                tmp_num_includes,
                                pd(ctx)->cache->param_values[sidx][count]);
-                       if (!W_ERROR_IS_OK(werr)) {
+                       if (!SBC_ERROR_IS_OK(err)) {
+                               werr = WERR_NOMEM;
                                goto done;
                        }
                        tmp_num_includes++;
@@ -657,15 +658,15 @@ static struct smbconf_ops smbconf_ops_txt = {
  * initialize the smbconf text backend
  * the only function that is exported from this module
  */
-WERROR smbconf_init_txt(TALLOC_CTX *mem_ctx,
+sbcErr smbconf_init_txt(TALLOC_CTX *mem_ctx,
                        struct smbconf_ctx **conf_ctx,
                        const char *path)
 {
-       WERROR werr;
+       sbcErr err;
 
-       werr = smbconf_init_internal(mem_ctx, conf_ctx, path, &smbconf_ops_txt);
-       if (!W_ERROR_IS_OK(werr)) {
-               return werr;
+       err = smbconf_init_internal(mem_ctx, conf_ctx, path, &smbconf_ops_txt);
+       if (!SBC_ERROR_IS_OK(err)) {
+               return err;
        }
 
        return smbconf_txt_load_file(*conf_ctx);