diff options
author | Nathan Scott <nathans@sgi.com> | 2003-02-25 09:06:10 +0000 |
---|---|---|
committer | Nathan Scott <nathans@sgi.com> | 2003-02-25 09:06:10 +0000 |
commit | 7b33ebd60e68795347c2c844d81d90e38fd9091e (patch) | |
tree | b8ad0b3165562c3d939d64720c109fd8d2a41116 /examples | |
parent | 6562399105b5ff0ec4f5ebea154dc7a96b597653 (diff) | |
download | attr-7b33ebd60e68795347c2c844d81d90e38fd9091e.tar.gz |
New attr userspace package - transition to the trusted namespace for XFS,
Add in the new attribute copying routines from Andreas, and also update
the license info in the syscall man pages so that other folks can use em
too.
Diffstat (limited to 'examples')
-rw-r--r-- | examples/Makefile | 41 | ||||
-rw-r--r-- | examples/Makefile.examples | 9 | ||||
-rw-r--r-- | examples/copyattr.c | 122 |
3 files changed, 172 insertions, 0 deletions
diff --git a/examples/Makefile b/examples/Makefile new file mode 100644 index 0000000..c2e7156 --- /dev/null +++ b/examples/Makefile @@ -0,0 +1,41 @@ +# +# Copyright (c) 2001-2002 Silicon Graphics, Inc. All Rights Reserved. +# +# This program is free software; you can redistribute it and/or modify it +# under the terms of version 2 of the GNU General Public License as +# published by the Free Software Foundation. +# +# This program is distributed in the hope that it would be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +# +# Further, this software is distributed without any warranty that it is +# free of the rightful claim of any third person regarding infringement +# or the like. Any license provided herein, whether implied or +# otherwise, applies only to this software file. Patent licenses, if +# any, provided herein do not apply to combinations of this program with +# other software, or any other product whatsoever. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write the Free Software Foundation, Inc., 59 +# Temple Place - Suite 330, Boston MA 02111-1307, USA. +# +# Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, +# Mountain View, CA 94043, or: +# +# http://www.sgi.com +# +# For further information regarding this notice, see: +# +# http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/ +# + +TOPDIR = .. +include $(TOPDIR)/include/builddefs + +LSRCFILES = copyattr.c +LDIRT = copyattr + +include $(BUILDRULES) + +default install install-dev install-lib: diff --git a/examples/Makefile.examples b/examples/Makefile.examples new file mode 100644 index 0000000..05c7831 --- /dev/null +++ b/examples/Makefile.examples @@ -0,0 +1,9 @@ +CFLAGS = -g -Wall +LDFLAGS = -lattr + +PROGS = copyattr + +all : $(PROGS) + +clean: + rm -f $(PROGS) diff --git a/examples/copyattr.c b/examples/copyattr.c new file mode 100644 index 0000000..1013206 --- /dev/null +++ b/examples/copyattr.c @@ -0,0 +1,122 @@ +/* + * Example how to preserve Extended Attributes in file manager style + * applications. This does NOT also copy Access Control Lists! + * + * Andreas Gruenbacher, SuSE Labs, SuSE Linux AG + * 23 January 2003 + */ + +#include "config.h" + +#include <sys/stat.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <errno.h> +#include <stdarg.h> +#include <locale.h> +/* #include <libintl.h> */ +#ifdef HAVE_ATTR_ERROR_CONTEXT_H +# include <attr/error_context.h> +#endif +#ifdef HAVE_ATTR_LIBATTR_H +# include <attr/libattr.h> +#endif + +/* + * We don't fully internationalize this example! + */ +#define _(msg) (msg) + +/* + * Optional error handler for attr_copy_file(). CTX is the error + * context passed to attr_copy_file(), ERR is the errno value + * that occurred. FMT and the rest are printf style arguments. + */ +static void +error(struct error_context *ctx, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + if (vfprintf(stderr, fmt, ap)) + fprintf(stderr, ": "); + fprintf(stderr, "%s\n", strerror(errno)); + va_end(ap); +} + +/* + * Optional handler for quoting path names in error messages. + * (This is a very stupid example!) + */ +static const char * +quote(struct error_context *ctx, const char *pathname) +{ + char *pn = strdup(pathname), *p; + pathname = strdup(pathname); + for (p = pn; *p != '\0'; p++) + if (*p & 0x80) + *p='?'; + return pn; +} + +static void +quote_free(struct error_context *ctx, const char *name) +{ + free((void *)name); +} + +/* + * The error context we pass to attr_copy_file(). + */ +struct error_context ctx = { error, quote, quote_free }; + +/* + * Optional attribute filter for attr_copy_file(). This example + * excludes all attributes other than extended user attributes. + */ +static int is_user_attr(const char *name, struct error_context *ctx) +{ + return strcmp(name, "user.") == 0; +} + +int +main(int argc, char *argv[]) +{ + int ret; + + /* + * Set the locale to enable message translation + */ + setlocale(LC_MESSAGES, ""); + setlocale(LC_CTYPE, ""); + + if (argc != 3) { + fprintf(stderr, _("Usage: %s from to\n"), argv[0]); + exit(1); + } + +#if defined(HAVE_ATTR_COPY_FILE) + /* + * If the third parameter is NULL, all extended attributes + * except those that define Access Control Lists are copied. + * ACLs are excluded by default because copying them between + * file systems with and without ACL support needs some + * additional logic so that no unexpected permissions result. + * + * For copying ACLs, please use perm_copy_file() from libacl. + * + * The CTX parameter could also be NULL, in which case attr_copy_file + * would print no error messages. + */ + ret = attr_copy_file(argv[1], argv[2], is_user_attr, &ctx); +#else + fprintf(stderr, _("No support for copying extended attributes\n")); + ret = -1; +#endif + + if (ret != 0) + return EXIT_FAILURE; + return EXIT_SUCCESS; +} + |