Patchwork [8/8] libjunction: Add a small tool for content of a live junction

login
register
mail settings
Submitter Chuck Lever
Date Oct. 11, 2012, 7:09 p.m.
Message ID <20121011190948.31447.51820.stgit@seurat.1015granger.net>
Download mbox | patch
Permalink /patch/190979/
State Accepted
Headers show

Comments

Chuck Lever - Oct. 11, 2012, 7:09 p.m.
Introduce the "display-junction" command, which takes a pathname
and displays the contents of the junction XML.

This is a debugging tool, not an installed program, so no man page
is provided.

Signed-off-by: Chuck Lever <chuck.lever@oracle.com>
---

 .gitignore                         |    1 
 src/libjunction/Makefile.am        |    4 +
 src/libjunction/display-junction.c |  111 ++++++++++++++++++++++++++++++++++++
 3 files changed, 116 insertions(+), 0 deletions(-)
 create mode 100644 src/libjunction/display-junction.c

Patch

diff --git a/.gitignore b/.gitignore
index 10c43a2..57e67c0 100644
--- a/.gitignore
+++ b/.gitignore
@@ -21,6 +21,7 @@  libparser.a
 libpath.a
 libsi.a
 libxlog.a
+display-junction
 fedfs-create-junction
 fedfs-create-replication
 fedfs-delete-junction
diff --git a/src/libjunction/Makefile.am b/src/libjunction/Makefile.am
index 4898d01..fd35365 100644
--- a/src/libjunction/Makefile.am
+++ b/src/libjunction/Makefile.am
@@ -26,8 +26,12 @@ 
 noinst_HEADERS		= junction-internal.h
 
 noinst_LTLIBRARIES	= libjunction.la
+noinst_PROGRAMS		= display-junction
 libjunction_la_SOURCES	= export-cache.c fedfs.c junction.c \
 			  locations.c nfs.c xml.c
+LDADD			= $(top_builddir)/src/libxlog/libxlog.la \
+			  $(top_builddir)/src/libjunction/libjunction.la \
+			  $(top_builddir)/src/libnsdb/libnsdb.la
 
 CLEANFILES		= cscope.in.out cscope.out cscope.po.out *~
 DISTCLEANFILES		= Makefile.in
diff --git a/src/libjunction/display-junction.c b/src/libjunction/display-junction.c
new file mode 100644
index 0000000..8635bfc
--- /dev/null
+++ b/src/libjunction/display-junction.c
@@ -0,0 +1,111 @@ 
+/**
+ * @file src/libjunction/display-junction.c
+ * @brief Tool to retrieve and display junction XML document
+ */
+
+/*
+ * Copyright 2012 Oracle.  All rights reserved.
+ *
+ * This file is part of fedfs-utils.
+ *
+ * fedfs-utils is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2.0 as
+ * published by the Free Software Foundation.
+ *
+ * fedfs-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 version 2.0 for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * version 2.0 along with fedfs-utils.  If not, see:
+ *
+ *	http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
+ */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <locale.h>
+#include <langinfo.h>
+
+#include <attr/xattr.h>
+
+#include "junction.h"
+#include "junction-internal.h"
+#include "xlog.h"
+
+/**
+ * Restore an object's mode bits
+ *
+ * @param pathname NUL-terminated C string containing pathname of a directory
+ * @return a FedFsStatus code
+ */
+int
+main(int argc, char **argv)
+{
+	char *progname, *pathname, *buf = NULL;
+	FedFsStatus retval;
+	int fd;
+
+	/* Ensure UTF-8 strings can be handled transparently */
+	if (setlocale(LC_CTYPE, "") == NULL ||
+	    strcmp(nl_langinfo(CODESET), "UTF-8") != 0) {
+		fprintf(stderr, "Failed to set locale and langinfo\n");
+		exit(EXIT_FAILURE);
+	}
+
+	/* For the libraries */
+	if ((progname = strrchr(argv[0], '/')) != NULL)
+		progname++;
+	else
+		progname = argv[0];
+	xlog_stderr(1);
+	xlog_syslog(0);
+	xlog_open(progname);
+
+	if (argc != 2) {
+		fprintf(stderr, "Usage: %s <pathname>\n", progname);
+		return EXIT_FAILURE;
+	}
+	pathname = argv[1];
+
+	retval = junction_open_path(pathname, &fd);
+	switch (retval) {
+	case FEDFS_OK:
+		break;
+	case FEDFS_ERR_PERM:
+		fprintf(stderr, "Failed to open junction %s: not running as root\n",
+			pathname);
+		return EXIT_FAILURE;
+	default:
+		fprintf(stderr, "Failed to open junction %s: %s\n",
+			pathname, nsdb_display_fedfsstatus(retval));
+		return EXIT_FAILURE;
+	}
+
+	retval = junction_read_xattr(fd, pathname, JUNCTION_XATTR_NAME_NFS, &buf);
+	switch (retval) {
+	case FEDFS_OK:
+		break;
+	case FEDFS_ERR_ACCESS:
+		fprintf(stderr, "Object %s is not a junction\n", pathname);
+		return EXIT_FAILURE;
+	default:
+		fprintf(stderr, "Failed to read junction %s: %s\n",
+			pathname, nsdb_display_fedfsstatus(retval));
+		return EXIT_FAILURE;
+	}
+
+	printf("%s\n", buf);
+
+	free(buf);
+	(void)close(fd);
+	return retval;
+}