libutil: make TOOLS_LIBDIR and TOOLS_DATADIR configurable

Add generic functions

util_sysdir();
util_sysdir_path()

and their special cases supplied for users:

util_libdir();
util_libdir_path();
util_datadir();
util_datadir_path()

The function util_sysdir() determines the absolute name of a
s390-tools system directory. It could be data, or library directory.

The function util_sysdir_path() determines the absolute name of a
file installed in the s390-tools system directory.

The function util_libdir() determines the absolute name of a
s390-tools library directory. By default that name is defined
by the compile-time macro TOOLS_LIBDIR (/lib/s390-tools).
Users can specify an override by setting environment variable
S390TOOLS_LIBDIR.

The function util_libdir_path() determines the absolute name of a
file installed in the s390-tools library directory.

The function util_datadir() determines the absolute name of a
s390-tools system data directory. By default the name is defined
by the compile-time macro TOOLS_DATADIR (/usr/share/s390-tools/).
Users can specify an override by setting environment variable
S390TOOLS_DATADIR.

The function util_datadir_path() determines the absolute name of a
file installed in the s390-tools data directory.

The ability to override the setting for TOOLS_LIBDIR and
TOOLS_DATADIR is required for implementing tests on tools that are
not installed in their default system path locations.

Signed-off-by: Eduard Shishkin <edward6@linux.ibm.com>
Reviewed-by: Jan Höppner <hoeppner@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
This commit is contained in:
Eduard Shishkin
2021-06-18 15:33:24 +02:00
committed by Jan Höppner
parent 761a185938
commit a645ec0fc0
3 changed files with 95 additions and 0 deletions

View File

@@ -20,6 +20,10 @@ void util_hexdump(FILE *fh, const char *tag, const void *data, int cnt);
void util_hexdump_grp(FILE *fh, const char *tag, const void *data, int group,
int cnt, int indent);
void util_print_indented(const char *str, int indent);
const char *util_libdir(void);
const char *util_libdir_path(const char *filename);
const char *util_datadir(void);
const char *util_datadir_path(const char *filename);
static inline void util_ptr_vec_free(void **ptr_vec, int count)
{

View File

@@ -52,6 +52,7 @@
#define TOOLS_LIBDIR STRINGIFY (S390_TOOLS_LIBDIR)
#define TOOLS_SYSCONFDIR STRINGIFY (S390_TOOLS_SYSCONFDIR)
#define TOOLS_BINDIR STRINGIFY (S390_TOOLS_BINDIR)
#define TOOLS_DATADIR STRINGIFY (S390_TOOLS_DATADIR)
#define __noreturn __attribute__((noreturn))
#define __packed __attribute__((packed))

View File

@@ -9,6 +9,8 @@
* it under the terms of the MIT license. See LICENSE for details.
*/
#include <err.h>
#include <limits.h>
#include <string.h>
#include "lib/util_base.h"
@@ -91,3 +93,91 @@ void util_print_indented(const char *str, int indent)
printf("\n");
free(desc_ptr);
}
/**
* Determines the absolute name of an s390-tools system directory
* (it could be data, or library directory)
*/
static const char *util_sysdir(const char *env_var, const char *default_dir)
{
return secure_getenv(env_var) ?: default_dir;
}
/**
* Determines the absolute name of a file installed in the s390-tools
* system directory
*/
static const char *util_sysdir_path(const char *filename, const char *dirname)
{
static char libdir_pathname[PATH_MAX];
int ret;
ret = snprintf(libdir_pathname, sizeof(libdir_pathname),
"%s/%s", dirname, filename);
if (ret < 0 || ret >= (int)sizeof(libdir_pathname))
errx(EXIT_FAILURE,
"Could not compose absolute pathname of %s and %s",
dirname, filename);
return libdir_pathname;
}
/**
* Determines the absolute name of a s390-tools library directory
*
* Resources are handled by the library
*
* @returns Pointer to a buffer, which contains the null-terminated name
*/
const char *util_libdir(void)
{
return util_sysdir("S390TOOLS_LIBDIR", TOOLS_LIBDIR);
}
/**
* Determines the absolute name of a file installed in the s390-tools
* library directory by its relative name.
*
* Resources are handled by the library. A second call of this function
* overwrites previously returned data. Care must be taken when attempting
* to do a second call.
*
* @param[in] filename Null-terminated file name relative to the s390-tools
* library directory
* @returns Pointer to a buffer of PATH_MAX size, which contains the
* null-terminated absolute name
*/
const char *util_libdir_path(const char *filename)
{
return util_sysdir_path(filename, util_libdir());
}
/**
* Determines the absolute name of a s390-tools data directory
*
* Resources are handled by the library
*
* @returns Pointer to a buffer which contains the null-terminated name
*/
const char *util_datadir(void)
{
return util_sysdir("S390TOOLS_DATADIR", TOOLS_DATADIR);
}
/**
* Determines the absolute name of a file installed in the s390-tools
* data directory by its relative name.
*
* Resources are handled by the library. A second call of this function
* overwrites previously returned data. Care must be taken when attempting
* to do a second call.
*
* @param[in] filename Null-terminated file name relative to the s390-tools
* data directory
* @returns Pointer to a buffer of PATH_MAX size, which contains the
* null-terminated absolute name
*/
const char *util_datadir_path(const char *filename)
{
return util_sysdir_path(filename, util_datadir());
}