2 Copyright (C) 2009 Andreas Gruenbacher <agruen@suse.de>
4 This program is free software: you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * Example how to preserve Extended Attributes in file manager style
20 * applications. This does NOT also copy Access Control Lists!
22 * Andreas Gruenbacher, SuSE Labs, SuSE Linux AG
35 /* #include <libintl.h> */
36 #ifdef HAVE_ATTR_ERROR_CONTEXT_H
37 # include <attr/error_context.h>
39 #ifdef HAVE_ATTR_LIBATTR_H
40 # include <attr/libattr.h>
44 * We don't fully internationalize this example!
49 * Optional error handler for attr_copy_file(). CTX is the error
50 * context passed to attr_copy_file(), ERR is the errno value
51 * that occurred. FMT and the rest are printf style arguments.
54 error(struct error_context *ctx, const char *fmt, ...)
59 if (vfprintf(stderr, fmt, ap))
60 fprintf(stderr, ": ");
61 fprintf(stderr, "%s\n", strerror(errno));
66 * Optional handler for quoting path names in error messages.
67 * (This is a very stupid example!)
70 quote(struct error_context *ctx, const char *pathname)
72 char *pn = strdup(pathname), *p;
73 pathname = strdup(pathname);
74 for (p = pn; *p != '\0'; p++)
81 quote_free(struct error_context *ctx, const char *name)
87 * The error context we pass to attr_copy_file().
89 struct error_context ctx = { error, quote, quote_free };
92 * Optional attribute filter for attr_copy_file(). This example
93 * excludes all attributes other than extended user attributes.
95 static int is_user_attr(const char *name, struct error_context *ctx)
97 return strcmp(name, "user.") == 0;
101 main(int argc, char *argv[])
106 * Set the locale to enable message translation
108 setlocale(LC_MESSAGES, "");
109 setlocale(LC_CTYPE, "");
112 fprintf(stderr, _("Usage: %s from to\n"), argv[0]);
116 #if defined(HAVE_ATTR_COPY_FILE)
118 * If the third parameter is NULL, all extended attributes
119 * except those that define Access Control Lists are copied.
120 * ACLs are excluded by default because copying them between
121 * file systems with and without ACL support needs some
122 * additional logic so that no unexpected permissions result.
124 * For copying ACLs, please use perm_copy_file() from libacl.
126 * The CTX parameter could also be NULL, in which case attr_copy_file
127 * would print no error messages.
129 ret = attr_copy_file(argv[1], argv[2], is_user_attr, &ctx);
131 fprintf(stderr, _("No support for copying extended attributes\n"));