diff options
author | Scott Mayhew <smayhew@redhat.com> | 2017-02-15 10:16:35 -0500 |
---|---|---|
committer | Steve Dickson <steved@redhat.com> | 2017-02-15 10:41:59 -0500 |
commit | 1789737ec6dd43c9d1436aeb6c07fab52206f412 (patch) | |
tree | 8de3910340ba6eaa293933cea8549cdd9f24e71e /support/misc | |
parent | 0712b5507866d6b3c900623eb1f81fffaec80ae2 (diff) | |
download | nfs-utils-1789737ec6dd43c9d1436aeb6c07fab52206f412.tar.gz |
libnsm.a: refactor nsm_setup_pathnames() and nsm_make_pathname()
Move the logic in nsm_setup_pathnames() and nsm_make_pathname() to
similar generic functions in libmisc.a so that the exportfs and
rpc.mountd programs can make use of them later.
Reviewed-by: NeilBrown <neilb@suse.com>
Signed-off-by: Scott Mayhew <smayhew@redhat.com>
Signed-off-by: Steve Dickson <steved@redhat.com>
Diffstat (limited to 'support/misc')
-rw-r--r-- | support/misc/Makefile.am | 2 | ||||
-rw-r--r-- | support/misc/file.c | 110 |
2 files changed, 111 insertions, 1 deletions
diff --git a/support/misc/Makefile.am b/support/misc/Makefile.am index 1048580..8936b0d 100644 --- a/support/misc/Makefile.am +++ b/support/misc/Makefile.am @@ -1,6 +1,6 @@ ## Process this file with automake to produce Makefile.in noinst_LIBRARIES = libmisc.a -libmisc_a_SOURCES = tcpwrapper.c from_local.c mountpoint.c +libmisc_a_SOURCES = tcpwrapper.c from_local.c mountpoint.c file.c MAINTAINERCLEANFILES = Makefile.in diff --git a/support/misc/file.c b/support/misc/file.c new file mode 100644 index 0000000..63597df --- /dev/null +++ b/support/misc/file.c @@ -0,0 +1,110 @@ +/* + * Copyright 2009 Oracle. All rights reserved. + * Copyright 2017 Red Hat, Inc. All rights reserved. + * + * This file is part of nfs-utils. + * + * nfs-utils 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 2 of the License, or + * (at your option) any later version. + * + * nfs-utils 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 nfs-utils. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <sys/stat.h> + +#include <string.h> +#include <libgen.h> +#include <stdio.h> +#include <errno.h> +#include <dirent.h> +#include <stdlib.h> +#include <stdbool.h> + +#include "xlog.h" +#include "misc.h" + +/* + * Returns a dynamically allocated, '\0'-terminated buffer + * containing an appropriate pathname, or NULL if an error + * occurs. Caller must free the returned result with free(3). + */ +__attribute__((__malloc__)) +char * +generic_make_pathname(const char *base, const char *leaf) +{ + size_t size; + char *path; + int len; + + size = strlen(base) + strlen(leaf) + 2; + if (size > PATH_MAX) + return NULL; + + path = malloc(size); + if (path == NULL) + return NULL; + + len = snprintf(path, size, "%s/%s", base, leaf); + if ((len < 0) || ((size_t)len >= size)) { + free(path); + return NULL; + } + + return path; +} + + +/** + * generic_setup_basedir - set up basedir + * @progname: C string containing name of program, for error messages + * @parentdir: C string containing pathname to on-disk state, or NULL + * @base: character buffer to contain the basedir that is set up + * @baselen: size of @base in bytes + * + * This runs before logging is set up, so error messages are directed + * to stderr. + * + * Returns true and sets up our basedir, if @parentdir was valid + * and usable; otherwise false is returned. + */ +_Bool +generic_setup_basedir(const char *progname, const char *parentdir, char *base, + const size_t baselen) +{ + static char buf[PATH_MAX]; + struct stat st; + char *path; + + /* First: test length of name and whether it exists */ + if ((strlen(parentdir) >= baselen) || (strlen(parentdir) >= PATH_MAX)) { + (void)fprintf(stderr, "%s: Directory name too long: %s", + progname, parentdir); + return false; + } + if (lstat(parentdir, &st) == -1) { + (void)fprintf(stderr, "%s: Failed to stat %s: %s", + progname, parentdir, strerror(errno)); + return false; + } + + /* Ensure we have a clean directory pathname */ + strncpy(buf, parentdir, sizeof(buf)); + path = dirname(buf); + if (*path == '.') { + (void)fprintf(stderr, "%s: Unusable directory %s", + progname, parentdir); + return false; + } + + xlog(D_CALL, "Using %s as the state directory", parentdir); + strcpy(base, parentdir); + return true; +} |