mirror of
https://github.com/samba-team/samba.git
synced 2024-12-22 13:34:15 +03:00
ctdb-common: Add utility code to get various paths
This will construct correct paths when running with CTDB_TEST_MODE. Signed-off-by: Amitay Isaacs <amitay@gmail.com> Reviewed-by: Martin Schwenke <martin@meltin.net>
This commit is contained in:
parent
356dacc6d7
commit
fe25aa7538
179
ctdb/common/path.c
Normal file
179
ctdb/common/path.c
Normal file
@ -0,0 +1,179 @@
|
||||
/*
|
||||
Construct runtime paths
|
||||
|
||||
Copyright (C) Amitay Isaacs 2018
|
||||
|
||||
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 "replace.h"
|
||||
#include "system/filesys.h"
|
||||
|
||||
#include "lib/util/debug.h"
|
||||
|
||||
#include "common/path.h"
|
||||
|
||||
#define CTDB_CONFIG_FILE "ctdb.conf"
|
||||
|
||||
struct {
|
||||
char *basedir;
|
||||
char etcdir[PATH_MAX];
|
||||
char rundir[PATH_MAX];
|
||||
char vardir[PATH_MAX];
|
||||
bool test_mode;
|
||||
bool basedir_set;
|
||||
bool etcdir_set;
|
||||
bool rundir_set;
|
||||
bool vardir_set;
|
||||
} ctdb_paths = {
|
||||
.etcdir = CTDB_ETCDIR,
|
||||
.rundir = CTDB_RUNDIR,
|
||||
.vardir = CTDB_VARDIR,
|
||||
};
|
||||
|
||||
static void path_set_basedir(void)
|
||||
{
|
||||
const char *t;
|
||||
|
||||
t = getenv("CTDB_TEST_MODE");
|
||||
if (t == NULL) {
|
||||
goto done;
|
||||
}
|
||||
|
||||
ctdb_paths.test_mode = true;
|
||||
|
||||
ctdb_paths.basedir = getenv("CTDB_BASE");
|
||||
if (ctdb_paths.basedir == NULL) {
|
||||
D_ERR("Broken CTDB setup, CTDB_BASE not set in test mode\n");
|
||||
abort();
|
||||
}
|
||||
|
||||
done:
|
||||
ctdb_paths.basedir_set = true;
|
||||
}
|
||||
|
||||
static bool path_construct(char *path, const char *subdir)
|
||||
{
|
||||
char p[PATH_MAX];
|
||||
int len;
|
||||
|
||||
if (! ctdb_paths.basedir_set) {
|
||||
path_set_basedir();
|
||||
}
|
||||
|
||||
if (! ctdb_paths.test_mode) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (subdir == NULL) {
|
||||
len = snprintf(p, sizeof(p), "%s", ctdb_paths.basedir);
|
||||
} else {
|
||||
len = snprintf(p,
|
||||
sizeof(p),
|
||||
"%s/%s",
|
||||
ctdb_paths.basedir,
|
||||
subdir);
|
||||
}
|
||||
|
||||
if (len >= sizeof(p)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
strncpy(path, p, PATH_MAX);
|
||||
return true;
|
||||
}
|
||||
|
||||
const char *path_etcdir(void)
|
||||
{
|
||||
bool ok;
|
||||
|
||||
if (! ctdb_paths.etcdir_set) {
|
||||
ok = path_construct(ctdb_paths.etcdir, NULL);
|
||||
if (!ok) {
|
||||
D_ERR("Failed to construct ETCDIR\n");
|
||||
} else {
|
||||
ctdb_paths.etcdir_set = true;
|
||||
}
|
||||
}
|
||||
|
||||
return ctdb_paths.etcdir;
|
||||
}
|
||||
|
||||
const char *path_rundir(void)
|
||||
{
|
||||
bool ok;
|
||||
|
||||
if (! ctdb_paths.rundir_set) {
|
||||
ok = path_construct(ctdb_paths.rundir, "run");
|
||||
if (!ok) {
|
||||
D_ERR("Failed to construct RUNDIR\n");
|
||||
} else {
|
||||
ctdb_paths.rundir_set = true;
|
||||
}
|
||||
}
|
||||
|
||||
return ctdb_paths.rundir;
|
||||
}
|
||||
|
||||
const char *path_vardir(void)
|
||||
{
|
||||
bool ok;
|
||||
|
||||
if (! ctdb_paths.rundir_set) {
|
||||
ok = path_construct(ctdb_paths.vardir, "var");
|
||||
if (!ok) {
|
||||
D_ERR("Failed to construct VARDIR\n");
|
||||
} else {
|
||||
ctdb_paths.vardir_set = true;
|
||||
}
|
||||
}
|
||||
|
||||
return ctdb_paths.vardir;
|
||||
}
|
||||
|
||||
char *path_etcdir_append(TALLOC_CTX *mem_ctx, const char *path)
|
||||
{
|
||||
return talloc_asprintf(mem_ctx, "%s/%s", path_etcdir(), path);
|
||||
}
|
||||
|
||||
char *path_rundir_append(TALLOC_CTX *mem_ctx, const char *path)
|
||||
{
|
||||
return talloc_asprintf(mem_ctx, "%s/%s", path_rundir(), path);
|
||||
}
|
||||
|
||||
char *path_vardir_append(TALLOC_CTX *mem_ctx, const char *path)
|
||||
{
|
||||
return talloc_asprintf(mem_ctx, "%s/%s", path_vardir(), path);
|
||||
}
|
||||
|
||||
char *path_config(TALLOC_CTX *mem_ctx)
|
||||
{
|
||||
return path_etcdir_append(mem_ctx, CTDB_CONFIG_FILE);
|
||||
}
|
||||
|
||||
char *path_socket(TALLOC_CTX *mem_ctx, const char *daemon)
|
||||
{
|
||||
return talloc_asprintf(mem_ctx,
|
||||
"%s/%s.socket",
|
||||
path_rundir(),
|
||||
daemon);
|
||||
}
|
||||
|
||||
char *path_pidfile(TALLOC_CTX *mem_ctx, const char *daemon)
|
||||
{
|
||||
return talloc_asprintf(mem_ctx,
|
||||
"%s/%s.pid",
|
||||
path_rundir(),
|
||||
daemon);
|
||||
}
|
37
ctdb/common/path.h
Normal file
37
ctdb/common/path.h
Normal file
@ -0,0 +1,37 @@
|
||||
/*
|
||||
Construct runtime paths
|
||||
|
||||
Copyright (C) Amitay Isaacs 2018
|
||||
|
||||
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/>.
|
||||
*/
|
||||
|
||||
#ifndef __CTDB_PATH_H__
|
||||
#define __CTDB_PATH_H__
|
||||
|
||||
#include <talloc.h>
|
||||
|
||||
const char *path_etcdir(void);
|
||||
const char *path_rundir(void);
|
||||
const char *path_vardir(void);
|
||||
|
||||
char *path_etcdir_append(TALLOC_CTX *mem_ctx, const char *path);
|
||||
char *path_rundir_append(TALLOC_CTX *mem_ctx, const char *path);
|
||||
char *path_vardir_append(TALLOC_CTX *mem_ctx, const char *path);
|
||||
|
||||
char *path_config(TALLOC_CTX *mem_ctx);
|
||||
char *path_socket(TALLOC_CTX *mem_ctx, const char *daemon);
|
||||
char *path_pidfile(TALLOC_CTX *mem_ctx, const char *daemon);
|
||||
|
||||
#endif /* __CTDB_PATH_H__ */
|
@ -400,7 +400,7 @@ def build(bld):
|
||||
pidfile.c run_proc.c
|
||||
hash_count.c run_event.c
|
||||
sock_client.c version.c
|
||||
cmdline.c
|
||||
cmdline.c path.c
|
||||
'''),
|
||||
deps='''samba-util sys_rw tevent-util
|
||||
replace talloc tevent tdb popt''')
|
||||
|
Loading…
Reference in New Issue
Block a user