Fix starvation of pending writes in CTDB queues
[Samba.git] / lib / smbconf / smbconf_txt.c
blob70a35ec43049a4f4147bd9dc7248cea0ed01ade9
1 /*
2 * Unix SMB/CIFS implementation.
3 * libsmbconf - Samba configuration library, text backend
4 * Copyright (C) Michael Adam 2008
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 * This is a sample implementation of a libsmbconf text backend
22 * using the params.c parser.
24 * It is read only.
25 * Don't expect brilliant performance, since it is not hashing the lists.
28 #include "includes.h"
29 #include "smbconf_private.h"
30 #include "lib/smbconf/smbconf_txt.h"
32 struct txt_cache {
33 uint32_t current_share;
34 uint32_t num_shares;
35 char **share_names;
36 uint32_t *num_params;
37 char ***param_names;
38 char ***param_values;
41 struct txt_private_data {
42 struct txt_cache *cache;
43 uint64_t csn;
44 bool verbatim;
47 /**********************************************************************
49 * helper functions
51 **********************************************************************/
53 /**
54 * a convenience helper to cast the private data structure
56 static struct txt_private_data *pd(struct smbconf_ctx *ctx)
58 return (struct txt_private_data *)(ctx->data);
61 static bool smbconf_txt_do_section(const char *section, void *private_data)
63 sbcErr err;
64 uint32_t idx;
65 struct txt_private_data *tpd = (struct txt_private_data *)private_data;
66 struct txt_cache *cache = tpd->cache;
68 if (smbconf_find_in_array(section, cache->share_names,
69 cache->num_shares, &idx))
71 cache->current_share = idx;
72 return true;
75 err = smbconf_add_string_to_array(cache, &(cache->share_names),
76 cache->num_shares, section);
77 if (!SBC_ERROR_IS_OK(err)) {
78 return false;
80 cache->current_share = cache->num_shares;
81 cache->num_shares++;
83 cache->param_names = talloc_realloc(cache,
84 cache->param_names,
85 char **,
86 cache->num_shares);
87 if (cache->param_names == NULL) {
88 return false;
90 cache->param_names[cache->current_share] = NULL;
92 cache->param_values = talloc_realloc(cache,
93 cache->param_values,
94 char **,
95 cache->num_shares);
96 if (cache->param_values == NULL) {
97 return false;
99 cache->param_values[cache->current_share] = NULL;
101 cache->num_params = talloc_realloc(cache,
102 cache->num_params,
103 uint32_t,
104 cache->num_shares);
105 if (cache->num_params == NULL) {
106 return false;
108 cache->num_params[cache->current_share] = 0;
110 return true;
113 static bool smbconf_txt_do_parameter(const char *param_name,
114 const char *param_value,
115 void *private_data)
117 sbcErr err;
118 char **param_names, **param_values;
119 uint32_t num_params;
120 uint32_t idx;
121 struct txt_private_data *tpd = (struct txt_private_data *)private_data;
122 struct txt_cache *cache = tpd->cache;
124 if (cache->num_shares == 0) {
126 * not in any share yet,
127 * initialize the "empty" section (NULL):
128 * parameters without a previous [section] are stored here.
130 if (!smbconf_txt_do_section(NULL, private_data)) {
131 return false;
135 param_names = cache->param_names[cache->current_share];
136 param_values = cache->param_values[cache->current_share];
137 num_params = cache->num_params[cache->current_share];
139 if (!(tpd->verbatim) &&
140 smbconf_find_in_array(param_name, param_names, num_params, &idx))
142 talloc_free(param_values[idx]);
143 param_values[idx] = talloc_strdup(cache, param_value);
144 if (param_values[idx] == NULL) {
145 return false;
147 return true;
149 err = smbconf_add_string_to_array(cache,
150 &(cache->param_names[cache->current_share]),
151 num_params, param_name);
152 if (!SBC_ERROR_IS_OK(err)) {
153 return false;
155 err = smbconf_add_string_to_array(cache,
156 &(cache->param_values[cache->current_share]),
157 num_params, param_value);
158 cache->num_params[cache->current_share]++;
159 return SBC_ERROR_IS_OK(err);
162 static void smbconf_txt_flush_cache(struct smbconf_ctx *ctx)
164 talloc_free(pd(ctx)->cache);
165 pd(ctx)->cache = NULL;
168 static sbcErr smbconf_txt_init_cache(struct smbconf_ctx *ctx)
170 if (pd(ctx)->cache != NULL) {
171 smbconf_txt_flush_cache(ctx);
174 pd(ctx)->cache = talloc_zero(pd(ctx), struct txt_cache);
176 if (pd(ctx)->cache == NULL) {
177 return SBC_ERR_NOMEM;
180 return SBC_ERR_OK;
183 static sbcErr smbconf_txt_load_file(struct smbconf_ctx *ctx)
185 sbcErr err;
186 uint64_t new_csn;
187 int rc;
188 struct timespec mt = {0};
190 if (!file_exist(ctx->path)) {
191 return SBC_ERR_BADFILE;
194 rc = file_modtime(ctx->path, &mt);
195 if (rc != 0) {
197 * Not worth mapping errno returned
198 * in rc to SBC_ERR_XXX. Just assume
199 * access denied.
201 return SBC_ERR_ACCESS_DENIED;
203 new_csn = (uint64_t)mt.tv_sec;
204 if (new_csn == pd(ctx)->csn) {
205 return SBC_ERR_OK;
208 err = smbconf_txt_init_cache(ctx);
209 if (!SBC_ERROR_IS_OK(err)) {
210 return err;
213 if (!pm_process(ctx->path, smbconf_txt_do_section,
214 smbconf_txt_do_parameter, pd(ctx)))
216 return SBC_ERR_CAN_NOT_COMPLETE;
219 pd(ctx)->csn = new_csn;
221 return SBC_ERR_OK;
225 /**********************************************************************
227 * smbconf operations: text backend implementations
229 **********************************************************************/
232 * initialize the text based smbconf backend
234 static sbcErr smbconf_txt_init(struct smbconf_ctx *ctx, const char *path)
236 if (path == NULL) {
237 return SBC_ERR_BADFILE;
239 ctx->path = talloc_strdup(ctx, path);
240 if (ctx->path == NULL) {
241 return SBC_ERR_NOMEM;
244 ctx->data = talloc_zero(ctx, struct txt_private_data);
245 if (ctx->data == NULL) {
246 return SBC_ERR_NOMEM;
249 pd(ctx)->verbatim = true;
251 return SBC_ERR_OK;
254 static int smbconf_txt_shutdown(struct smbconf_ctx *ctx)
256 return ctx->ops->close_conf(ctx);
259 static bool smbconf_txt_requires_messaging(struct smbconf_ctx *ctx)
261 return false;
264 static bool smbconf_txt_is_writeable(struct smbconf_ctx *ctx)
266 /* no write support in this backend yet... */
267 return false;
270 static sbcErr smbconf_txt_open(struct smbconf_ctx *ctx)
272 return smbconf_txt_load_file(ctx);
275 static int smbconf_txt_close(struct smbconf_ctx *ctx)
277 smbconf_txt_flush_cache(ctx);
278 return 0;
282 * Get the change sequence number of the given service/parameter.
283 * service and parameter strings may be NULL.
285 static void smbconf_txt_get_csn(struct smbconf_ctx *ctx,
286 struct smbconf_csn *csn,
287 const char *service, const char *param)
289 struct timespec mt = {0};
291 if (csn == NULL) {
292 return;
295 (void)file_modtime(ctx->path, &mt);
296 csn->csn = (uint64_t)mt.tv_sec;
300 * Drop the whole configuration (restarting empty)
302 static sbcErr smbconf_txt_drop(struct smbconf_ctx *ctx)
304 return SBC_ERR_NOT_SUPPORTED;
308 * get the list of share names defined in the configuration.
310 static sbcErr smbconf_txt_get_share_names(struct smbconf_ctx *ctx,
311 TALLOC_CTX *mem_ctx,
312 uint32_t *num_shares,
313 char ***share_names)
315 uint32_t count;
316 uint32_t added_count = 0;
317 TALLOC_CTX *tmp_ctx = NULL;
318 sbcErr err = SBC_ERR_OK;
319 char **tmp_share_names = NULL;
321 if ((num_shares == NULL) || (share_names == NULL)) {
322 return SBC_ERR_INVALID_PARAM;
325 err = smbconf_txt_load_file(ctx);
326 if (!SBC_ERROR_IS_OK(err)) {
327 return err;
330 tmp_ctx = talloc_stackframe();
332 /* make sure "global" is always listed first,
333 * possibly after NULL section */
335 if (smbconf_share_exists(ctx, NULL)) {
336 err = smbconf_add_string_to_array(tmp_ctx, &tmp_share_names,
337 0, NULL);
338 if (!SBC_ERROR_IS_OK(err)) {
339 goto done;
341 added_count++;
344 if (smbconf_share_exists(ctx, GLOBAL_NAME)) {
345 err = smbconf_add_string_to_array(tmp_ctx, &tmp_share_names,
346 added_count, GLOBAL_NAME);
347 if (!SBC_ERROR_IS_OK(err)) {
348 goto done;
350 added_count++;
353 for (count = 0; count < pd(ctx)->cache->num_shares; count++) {
354 if (strequal(pd(ctx)->cache->share_names[count], GLOBAL_NAME) ||
355 (pd(ctx)->cache->share_names[count] == NULL))
357 continue;
360 err = smbconf_add_string_to_array(tmp_ctx, &tmp_share_names,
361 added_count,
362 pd(ctx)->cache->share_names[count]);
363 if (!SBC_ERROR_IS_OK(err)) {
364 goto done;
366 added_count++;
369 *num_shares = added_count;
370 if (added_count > 0) {
371 *share_names = talloc_move(mem_ctx, &tmp_share_names);
372 } else {
373 *share_names = NULL;
376 done:
377 talloc_free(tmp_ctx);
378 return err;
382 * check if a share/service of a given name exists
384 static bool smbconf_txt_share_exists(struct smbconf_ctx *ctx,
385 const char *servicename)
387 sbcErr err;
389 err = smbconf_txt_load_file(ctx);
390 if (!SBC_ERROR_IS_OK(err)) {
391 return false;
394 return smbconf_find_in_array(servicename,
395 pd(ctx)->cache->share_names,
396 pd(ctx)->cache->num_shares, NULL);
400 * Add a service if it does not already exist
402 static sbcErr smbconf_txt_create_share(struct smbconf_ctx *ctx,
403 const char *servicename)
405 return SBC_ERR_NOT_SUPPORTED;
409 * get a definition of a share (service) from configuration.
411 static sbcErr smbconf_txt_get_share(struct smbconf_ctx *ctx,
412 TALLOC_CTX *mem_ctx,
413 const char *servicename,
414 struct smbconf_service **service)
416 sbcErr err;
417 uint32_t sidx, count;
418 bool found;
419 TALLOC_CTX *tmp_ctx = NULL;
420 struct smbconf_service *tmp_service = NULL;
422 err = smbconf_txt_load_file(ctx);
423 if (!SBC_ERROR_IS_OK(err)) {
424 return err;
427 found = smbconf_find_in_array(servicename,
428 pd(ctx)->cache->share_names,
429 pd(ctx)->cache->num_shares,
430 &sidx);
431 if (!found) {
432 return SBC_ERR_NO_SUCH_SERVICE;
435 tmp_ctx = talloc_stackframe();
437 tmp_service = talloc_zero(tmp_ctx, struct smbconf_service);
438 if (tmp_service == NULL) {
439 err = SBC_ERR_NOMEM;
440 goto done;
443 if (servicename != NULL) {
444 tmp_service->name = talloc_strdup(tmp_service, servicename);
445 if (tmp_service->name == NULL) {
446 err = SBC_ERR_NOMEM;
447 goto done;
451 for (count = 0; count < pd(ctx)->cache->num_params[sidx]; count++) {
452 err = smbconf_add_string_to_array(tmp_service,
453 &(tmp_service->param_names),
454 count,
455 pd(ctx)->cache->param_names[sidx][count]);
456 if (!SBC_ERROR_IS_OK(err)) {
457 goto done;
459 err = smbconf_add_string_to_array(tmp_service,
460 &(tmp_service->param_values),
461 count,
462 pd(ctx)->cache->param_values[sidx][count]);
463 if (!SBC_ERROR_IS_OK(err)) {
464 goto done;
468 tmp_service->num_params = count;
469 *service = talloc_move(mem_ctx, &tmp_service);
471 done:
472 talloc_free(tmp_ctx);
473 return err;
477 * delete a service from configuration
479 static sbcErr smbconf_txt_delete_share(struct smbconf_ctx *ctx,
480 const char *servicename)
482 return SBC_ERR_NOT_SUPPORTED;
486 * set a configuration parameter to the value provided.
488 static sbcErr smbconf_txt_set_parameter(struct smbconf_ctx *ctx,
489 const char *service,
490 const char *param,
491 const char *valstr)
493 return SBC_ERR_NOT_SUPPORTED;
497 * get the value of a configuration parameter as a string
499 static sbcErr smbconf_txt_get_parameter(struct smbconf_ctx *ctx,
500 TALLOC_CTX *mem_ctx,
501 const char *service,
502 const char *param,
503 char **valstr)
505 sbcErr err;
506 bool found;
507 uint32_t share_index, param_index;
509 err = smbconf_txt_load_file(ctx);
510 if (!SBC_ERROR_IS_OK(err)) {
511 return err;
514 found = smbconf_find_in_array(service,
515 pd(ctx)->cache->share_names,
516 pd(ctx)->cache->num_shares,
517 &share_index);
518 if (!found) {
519 return SBC_ERR_NO_SUCH_SERVICE;
522 found = smbconf_reverse_find_in_array(param,
523 pd(ctx)->cache->param_names[share_index],
524 pd(ctx)->cache->num_params[share_index],
525 &param_index);
526 if (!found) {
527 return SBC_ERR_INVALID_PARAM;
530 *valstr = talloc_strdup(mem_ctx,
531 pd(ctx)->cache->param_values[share_index][param_index]);
533 if (*valstr == NULL) {
534 return SBC_ERR_NOMEM;
537 return SBC_ERR_OK;
541 * delete a parameter from configuration
543 static sbcErr smbconf_txt_delete_parameter(struct smbconf_ctx *ctx,
544 const char *service,
545 const char *param)
547 return SBC_ERR_NOT_SUPPORTED;
550 static sbcErr smbconf_txt_get_includes(struct smbconf_ctx *ctx,
551 TALLOC_CTX *mem_ctx,
552 const char *service,
553 uint32_t *num_includes,
554 char ***includes)
556 sbcErr err;
557 bool found;
558 uint32_t sidx, count;
559 TALLOC_CTX *tmp_ctx = NULL;
560 uint32_t tmp_num_includes = 0;
561 char **tmp_includes = NULL;
563 err = smbconf_txt_load_file(ctx);
564 if (!SBC_ERROR_IS_OK(err)) {
565 return err;
568 found = smbconf_find_in_array(service,
569 pd(ctx)->cache->share_names,
570 pd(ctx)->cache->num_shares,
571 &sidx);
572 if (!found) {
573 return SBC_ERR_NO_SUCH_SERVICE;
576 tmp_ctx = talloc_stackframe();
578 for (count = 0; count < pd(ctx)->cache->num_params[sidx]; count++) {
579 if (strequal(pd(ctx)->cache->param_names[sidx][count],
580 "include"))
582 err = smbconf_add_string_to_array(tmp_ctx,
583 &tmp_includes,
584 tmp_num_includes,
585 pd(ctx)->cache->param_values[sidx][count]);
586 if (!SBC_ERROR_IS_OK(err)) {
587 goto done;
589 tmp_num_includes++;
593 *num_includes = tmp_num_includes;
594 if (*num_includes > 0) {
595 *includes = talloc_move(mem_ctx, &tmp_includes);
596 if (*includes == NULL) {
597 err = SBC_ERR_NOMEM;
598 goto done;
600 } else {
601 *includes = NULL;
604 err = SBC_ERR_OK;
606 done:
607 talloc_free(tmp_ctx);
608 return err;
611 static sbcErr smbconf_txt_set_includes(struct smbconf_ctx *ctx,
612 const char *service,
613 uint32_t num_includes,
614 const char **includes)
616 return SBC_ERR_NOT_SUPPORTED;
619 static sbcErr smbconf_txt_delete_includes(struct smbconf_ctx *ctx,
620 const char *service)
622 return SBC_ERR_NOT_SUPPORTED;
625 static sbcErr smbconf_txt_transaction_start(struct smbconf_ctx *ctx)
627 return SBC_ERR_OK;
630 static sbcErr smbconf_txt_transaction_commit(struct smbconf_ctx *ctx)
632 return SBC_ERR_OK;
635 static sbcErr smbconf_txt_transaction_cancel(struct smbconf_ctx *ctx)
637 return SBC_ERR_OK;
640 static struct smbconf_ops smbconf_ops_txt = {
641 .init = smbconf_txt_init,
642 .shutdown = smbconf_txt_shutdown,
643 .requires_messaging = smbconf_txt_requires_messaging,
644 .is_writeable = smbconf_txt_is_writeable,
645 .open_conf = smbconf_txt_open,
646 .close_conf = smbconf_txt_close,
647 .get_csn = smbconf_txt_get_csn,
648 .drop = smbconf_txt_drop,
649 .get_share_names = smbconf_txt_get_share_names,
650 .share_exists = smbconf_txt_share_exists,
651 .create_share = smbconf_txt_create_share,
652 .get_share = smbconf_txt_get_share,
653 .delete_share = smbconf_txt_delete_share,
654 .set_parameter = smbconf_txt_set_parameter,
655 .get_parameter = smbconf_txt_get_parameter,
656 .delete_parameter = smbconf_txt_delete_parameter,
657 .get_includes = smbconf_txt_get_includes,
658 .set_includes = smbconf_txt_set_includes,
659 .delete_includes = smbconf_txt_delete_includes,
660 .transaction_start = smbconf_txt_transaction_start,
661 .transaction_commit = smbconf_txt_transaction_commit,
662 .transaction_cancel = smbconf_txt_transaction_cancel,
667 * initialize the smbconf text backend
668 * the only function that is exported from this module
670 sbcErr smbconf_init_txt(TALLOC_CTX *mem_ctx,
671 struct smbconf_ctx **conf_ctx,
672 const char *path)
674 sbcErr err;
676 err = smbconf_init_internal(mem_ctx, conf_ctx, path, &smbconf_ops_txt);
677 if (!SBC_ERROR_IS_OK(err)) {
678 return err;
681 return smbconf_txt_load_file(*conf_ctx);