2 Bacula® - The Network Backup Solution
4 Copyright (C) 2008-2009 Free Software Foundation Europe e.V.
6 The main author of Bacula is Kern Sibbald, with contributions from
7 many others, a complete list can be found in the file AUTHORS.
8 This program is Free Software; you can redistribute it and/or
9 modify it under the terms of version two of the GNU General Public
10 License as published by the Free Software Foundation and included
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 Bacula® is a registered trademark of Kern Sibbald.
24 The licensor of Bacula is the Free Software Foundation Europe
25 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26 Switzerland, email:ftf@fsfeurope.org.
29 * Functions to handle Extended Attributes for bacula.
31 * Extended Attributes are so OS specific we only restore Extended Attributes if
32 * they were saved using a filed on the same platform.
34 * Currently we support the following OSes:
35 * - FreeBSD (Extended Attributes)
36 * - Darwin (Extended Attributes)
37 * - Linux (Extended Attributes)
38 * - NetBSD (Extended Attributes)
39 * - Solaris (Extended Attributes and Extensible Attributes)
41 * Written by Marco van Wieringen, November MMVIII
49 #if !defined(HAVE_XATTR)
51 * Entry points when compiled without support for XATTRs or on an unsupported platform.
53 bxattr_exit_code build_xattr_streams(JCR *jcr, FF_PKT *ff_pkt)
55 return bxattr_exit_fatal;
58 bxattr_exit_code parse_xattr_streams(JCR *jcr, int stream)
60 return bxattr_exit_fatal;
64 * Send a XATTR stream to the SD.
66 static bxattr_exit_code send_xattr_stream(JCR *jcr, int stream)
68 BSOCK *sd = jcr->store_bsock;
70 #ifdef FD_NO_SEND_TEST
71 return bxattr_exit_ok;
77 if (jcr->xattr_data->content_length <= 0) {
78 return bxattr_exit_ok;
84 if (!sd->fsend("%ld %d 0", jcr->JobFiles, stream)) {
85 Jmsg1(jcr, M_FATAL, 0, _("Network send error to SD. ERR=%s\n"),
87 return bxattr_exit_fatal;
91 * Send the buffer to the storage deamon
93 Dmsg1(400, "Backing up XATTR <%s>\n", jcr->xattr_data->content);
95 sd->msg = jcr->xattr_data->content;
96 sd->msglen = jcr->xattr_data->content_length;
100 Jmsg1(jcr, M_FATAL, 0, _("Network send error to SD. ERR=%s\n"),
102 return bxattr_exit_fatal;
105 jcr->JobBytes += sd->msglen;
107 if (!sd->signal(BNET_EOD)) {
108 Jmsg1(jcr, M_FATAL, 0, _("Network send error to SD. ERR=%s\n"),
110 return bxattr_exit_fatal;
112 Dmsg1(200, "XATTR of file: %s successfully backed up!\n", jcr->last_fname);
113 return bxattr_exit_ok;
117 * This is a supported OS, See what kind of interface we should use.
118 * Start with the generic interface used by most OS-es.
120 #if defined(HAVE_DARWIN_OS) || \
121 defined(HAVE_FREEBSD_OS) || \
122 defined(HAVE_LINUX_OS) || \
123 defined(HAVE_NETBSD_OS)
125 #ifdef HAVE_SYS_XATTR_H
126 #include <sys/xattr.h>
130 * Define the supported XATTR streams for this OS
132 #if defined(HAVE_DARWIN_OS)
133 static int os_default_xattr_streams[1] = { STREAM_XATTR_DARWIN };
134 #elif defined(HAVE_FREEBSD_OS)
135 static int os_default_xattr_streams[1] = { STREAM_XATTR_FREEBSD };
136 #elif defined(HAVE_LINUX_OS)
137 static int os_default_xattr_streams[1] = { STREAM_XATTR_LINUX };
138 #elif defined(HAVE_NETBSD_OS)
139 static int os_default_xattr_streams[1] = { STREAM_XATTR_NETBSD };
143 * OSX doesn't have llistxattr, lgetxattr and lsetxattr but has
144 * listxattr, getxattr and setxattr with an extra options argument
145 * which mimics the l variants of the functions when we specify
146 * XATTR_NOFOLLOW as the options value.
148 #if defined(HAVE_DARWIN_OS)
149 #define llistxattr(path, list, size) listxattr((path), (list), (size), XATTR_NOFOLLOW)
150 #define lgetxattr(path, name, value, size) getxattr((path), (name), (value), (size), 0, XATTR_NOFOLLOW)
151 #define lsetxattr(path, name, value, size, flags) setxattr((path), (name), (value), (size), (flags), XATTR_NOFOLLOW)
154 * Fallback to the non l-functions when those are not available.
156 #if defined(HAVE_GETXATTR) && !defined(HAVE_LGETXATTR)
157 #define lgetxattr getxattr
159 #if defined(HAVE_SETXATTR) && !defined(HAVE_LSETXATTR)
160 #define lsetxattr setxattr
162 #if defined(HAVE_LISTXATTR) && !defined(HAVE_LLISTXATTR)
163 #define llistxattr listxattr
167 static void xattr_drop_internal_table(xattr_t *xattr_value_list)
169 xattr_t *current_xattr;
172 * Walk the list of xattrs and free allocated memory on traversing.
174 for (current_xattr = xattr_value_list;
175 current_xattr != (xattr_t *)NULL;
178 * See if we can shortcut.
180 if (current_xattr->magic != XATTR_MAGIC)
183 free(current_xattr->name);
185 if (current_xattr->value_length > 0)
186 free(current_xattr->value);
190 * Free the array of control structs.
192 free(xattr_value_list);
196 * The xattr stream for OSX, FreeBSD, Linux and NetBSD is a serialized stream of bytes
197 * which encodes one or more xattr_t structures.
199 * The Serialized stream consists of the following elements:
200 * magic - A magic string which makes it easy to detect any binary incompatabilites
201 * name_length - The length of the following xattr name
202 * name - The name of the extended attribute
203 * value_length - The length of the following xattr data
204 * value - The actual content of the extended attribute
206 * This is repeated 1 or more times.
209 static uint32_t serialize_xattr_stream(JCR *jcr, uint32_t expected_serialize_len, xattr_t *xattr_value_list)
211 xattr_t *current_xattr;
215 * Make sure the serialized stream fits in the poolmem buffer.
216 * We allocate some more to be sure the stream is gonna fit.
218 jcr->xattr_data->content = check_pool_memory_size(jcr->xattr_data->content, expected_serialize_len + 10);
219 ser_begin(jcr->xattr_data->content, expected_serialize_len + 10);
222 * Walk the list of xattrs and serialize the data.
224 for (current_xattr = xattr_value_list; current_xattr != (xattr_t *)NULL; current_xattr++) {
226 * See if we can shortcut.
228 if (current_xattr->magic != XATTR_MAGIC)
231 ser_uint32(current_xattr->magic);
232 ser_uint32(current_xattr->name_length);
233 ser_bytes(current_xattr->name, current_xattr->name_length);
235 ser_uint32(current_xattr->value_length);
236 ser_bytes(current_xattr->value, current_xattr->value_length);
239 ser_end(jcr->xattr_data->content, expected_serialize_len + 10);
240 jcr->xattr_data->content_length = ser_length(jcr->xattr_data->content);
241 return jcr->xattr_data->content_length;
244 static bxattr_exit_code generic_xattr_build_streams(JCR *jcr, FF_PKT *ff_pkt)
247 int32_t xattr_list_len,
249 uint32_t expected_serialize_len = 0;
250 char *xattr_list, *bp;
251 xattr_t *xattr_value_list = NULL, *current_xattr;
252 bxattr_exit_code retval = bxattr_exit_error;
256 * First get the length of the available list with extended attributes.
258 xattr_list_len = llistxattr(jcr->last_fname, NULL, 0);
259 if (xattr_list_len < 0) {
262 return bxattr_exit_ok;
264 Mmsg2(jcr->errmsg, _("llistxattr error on file \"%s\": ERR=%s\n"),
265 jcr->last_fname, be.bstrerror());
266 Dmsg2(100, "llistxattr error file=%s ERR=%s\n",
267 jcr->last_fname, be.bstrerror());
268 return bxattr_exit_error;
270 } else if (xattr_list_len == 0) {
271 return bxattr_exit_ok;
275 * Allocate room for the extented attribute list.
277 xattr_list = (char *)malloc(xattr_list_len + 1);
278 memset((caddr_t)xattr_list, 0, xattr_list_len + 1);
281 * Get the actual list of extended attributes names for a file.
283 xattr_list_len = llistxattr(jcr->last_fname, xattr_list, xattr_list_len);
284 if (xattr_list_len < 0) {
287 retval = bxattr_exit_ok;
290 Mmsg2(jcr->errmsg, _("llistxattr error on file \"%s\": ERR=%s\n"),
291 jcr->last_fname, be.bstrerror());
292 Dmsg2(100, "llistxattr error file=%s ERR=%s\n",
293 jcr->last_fname, be.bstrerror());
297 xattr_list[xattr_list_len] = '\0';
300 * Count the number of extended attributes on a file.
303 while ((bp - xattr_list) + 1 < xattr_list_len) {
304 #if defined(HAVE_LINUX_OS)
306 * On Linux you also get the acls in the extented attribute list.
307 * So we check if we are already backing up acls and if we do we
308 * don't store the extended attribute with the same info.
310 if ((ff_pkt->flags & FO_ACL) == 0 || strcmp(bp, "system.posix_acl_access"))
316 bp = strchr(bp, '\0') + 1;
320 retval = bxattr_exit_ok;
325 * Allocate enough room to hold all extended attributes.
326 * After allocating the storage make sure its empty by zeroing it.
328 xattr_value_list = (xattr_t *)malloc(count * sizeof(xattr_t));
329 memset((caddr_t)xattr_value_list, 0, count * sizeof(xattr_t));
332 * Walk the list of extended attributes names and retrieve the data.
333 * We already count the bytes needed for serializing the stream later on.
335 current_xattr = xattr_value_list;
337 while ((bp - xattr_list) + 1 < xattr_list_len) {
338 #if defined(HAVE_LINUX_OS)
340 * On Linux you also get the acls in the extented attribute list.
341 * So we check if we are already backing up acls and if we do we
342 * don't store the extended attribute with the same info.
344 if (ff_pkt->flags & FO_ACL && !strcmp(bp, "system.posix_acl_access")) {
345 bp = strchr(bp, '\0') + 1;
351 * Each xattr valuepair starts with a magic so we can parse it easier.
353 current_xattr->magic = XATTR_MAGIC;
354 expected_serialize_len += sizeof(current_xattr->magic);
357 * Allocate space for storing the name.
359 current_xattr->name_length = strlen(bp);
360 current_xattr->name = (char *)malloc(current_xattr->name_length);
361 memcpy((caddr_t)current_xattr->name, (caddr_t)bp, current_xattr->name_length);
363 expected_serialize_len += sizeof(current_xattr->name_length) + current_xattr->name_length;
366 * First see how long the value is for the extended attribute.
368 xattr_value_len = lgetxattr(jcr->last_fname, bp, NULL, 0);
369 if (xattr_value_len < 0) {
372 retval = bxattr_exit_ok;
375 Mmsg2(jcr->errmsg, _("lgetxattr error on file \"%s\": ERR=%s\n"),
376 jcr->last_fname, be.bstrerror());
377 Dmsg2(100, "lgetxattr error file=%s ERR=%s\n",
378 jcr->last_fname, be.bstrerror());
384 * Allocate space for storing the value.
386 current_xattr->value = (char *)malloc(xattr_value_len);
387 memset((caddr_t)current_xattr->value, 0, xattr_value_len);
389 xattr_value_len = lgetxattr(jcr->last_fname, bp, current_xattr->value, xattr_value_len);
390 if (xattr_value_len < 0) {
393 retval = bxattr_exit_ok;
396 Mmsg2(jcr->errmsg, _("lgetxattr error on file \"%s\": ERR=%s\n"),
397 jcr->last_fname, be.bstrerror());
398 Dmsg2(100, "lgetxattr error file=%s ERR=%s\n",
399 jcr->last_fname, be.bstrerror());
405 * Store the actual length of the value.
407 current_xattr->value_length = xattr_value_len;
408 expected_serialize_len += sizeof(current_xattr->value_length) + current_xattr->value_length;
411 * Protect ourself against things getting out of hand.
413 if (expected_serialize_len >= MAX_XATTR_STREAM) {
414 Mmsg2(jcr->errmsg, _("Xattr stream on file \"%s\" exceeds maximum size of %d bytes\n"),
415 jcr->last_fname, MAX_XATTR_STREAM);
423 bp = strchr(bp, '\0') + 1;
427 * Serialize the datastream.
429 if (serialize_xattr_stream(jcr, expected_serialize_len, xattr_value_list) < expected_serialize_len) {
430 Mmsg1(jcr->errmsg, _("Failed to serialize extended attributes on file \"%s\"\n"),
432 Dmsg1(100, "Failed to serialize extended attributes on file \"%s\"\n",
437 xattr_drop_internal_table(xattr_value_list);
441 * Send the datastream to the SD.
443 return send_xattr_stream(jcr, os_default_xattr_streams[0]);
446 if (xattr_value_list) {
447 xattr_drop_internal_table(xattr_value_list);
453 static bxattr_exit_code generic_xattr_parse_streams(JCR *jcr, int stream)
456 xattr_t current_xattr;
457 bxattr_exit_code retval = bxattr_exit_ok;
461 * Parse the stream and perform the setxattr calls on the file.
463 * Start unserializing the data. We keep on looping while we have not
464 * unserialized all bytes in the stream.
466 unser_begin(jcr->xattr_data->content, jcr->xattr_data->content_length);
467 while (unser_length(jcr->xattr_data->content) < jcr->xattr_data->content_length) {
469 * First make sure the magic is present. This way we can easily catch corruption.
470 * Any missing MAGIC is fatal we do NOT try to continue.
472 unser_uint32(current_xattr.magic);
473 if (current_xattr.magic != XATTR_MAGIC) {
474 Mmsg1(jcr->errmsg, _("Illegal xattr stream, no XATTR_MAGIC on file \"%s\"\n"),
476 Dmsg1(100, "Illegal xattr stream, no XATTR_MAGIC on file \"%s\"\n",
478 return bxattr_exit_error;
482 * Decode the valuepair. First decode the length of the name.
484 unser_uint32(current_xattr.name_length);
487 * Allocate room for the name and decode its content.
489 current_xattr.name = (char *)malloc(current_xattr.name_length + 1);
490 unser_bytes(current_xattr.name, current_xattr.name_length);
493 * The xattr_name needs to be null terminated for lsetxattr.
495 current_xattr.name[current_xattr.name_length] = '\0';
498 * Decode the value length.
500 unser_uint32(current_xattr.value_length);
503 * Allocate room for the value and decode its content.
505 current_xattr.value = (char *)malloc(current_xattr.value_length);
506 unser_bytes(current_xattr.value, current_xattr.value_length);
509 * Try to set the extended attribute on the file.
510 * If we fail to set this attribute we flag the error but its not fatal,
511 * we try to restore the other extended attributes too.
513 if (lsetxattr(jcr->last_fname, current_xattr.name, current_xattr.value,
514 current_xattr.value_length, 0) != 0) {
519 Mmsg2(jcr->errmsg, _("lsetxattr error on file \"%s\": ERR=%s\n"),
520 jcr->last_fname, be.bstrerror());
521 Dmsg2(100, "lsetxattr error file=%s ERR=%s\n",
522 jcr->last_fname, be.bstrerror());
523 retval = bxattr_exit_error;
529 * Free the temporary buffers.
531 free(current_xattr.name);
532 free(current_xattr.value);
535 unser_end(jcr->xattr_data->content, jcr->xattr_data->content_length);
540 * For all these os-es setup the build and parse function pointer to the generic functions.
542 static bxattr_exit_code (*os_build_xattr_streams)(JCR *jcr, FF_PKT *ff_pkt) = generic_xattr_build_streams;
543 static bxattr_exit_code (*os_parse_xattr_streams)(JCR *jcr, int stream) = generic_xattr_parse_streams;
545 #elif defined(HAVE_SUN_OS)
547 * Solaris extended attributes were introduced in Solaris 9
550 * Solaris extensible attributes were introduced in OpenSolaris
551 * by PSARC 2007/315 Solaris extensible attributes are also
552 * sometimes called extended system attributes.
554 * man fsattr(5) on Solaris gives a wealth of info. The most
555 * important bits are:
557 * Attributes are logically supported as files within the file
558 * system. The file system is therefore augmented with an
559 * orthogonal name space of file attributes. Any file (includ-
560 * ing attribute files) can have an arbitrarily deep attribute
561 * tree associated with it. Attribute values are accessed by
562 * file descriptors obtained through a special attribute inter-
563 * face. This logical view of "attributes as files" allows the
564 * leveraging of existing file system interface functionality
565 * to support the construction, deletion, and manipulation of
568 * The special files "." and ".." retain their accustomed
569 * semantics within the attribute hierarchy. The "." attribute
570 * file refers to the current directory and the ".." attribute
571 * file refers to the parent directory. The unnamed directory
572 * at the head of each attribute tree is considered the "child"
573 * of the file it is associated with and the ".." file refers
574 * to the associated file. For any non-directory file with
575 * attributes, the ".." entry in the unnamed directory refers
576 * to a file that is not a directory.
578 * Conceptually, the attribute model is fully general. Extended
579 * attributes can be any type of file (doors, links, direc-
580 * tories, and so forth) and can even have their own attributes
581 * (fully recursive). As a result, the attributes associated
582 * with a file could be an arbitrarily deep directory hierarchy
583 * where each attribute could have an equally complex attribute
584 * tree associated with it. Not all implementations are able
585 * to, or want to, support the full model. Implementation are
586 * therefore permitted to reject operations that are not sup-
587 * ported. For example, the implementation for the UFS file
588 * system allows only regular files as attributes (for example,
589 * no sub-directories) and rejects attempts to place attributes
592 * The following list details the operations that are rejected
593 * in the current implementation:
595 * link Any attempt to create links between
596 * attribute and non-attribute space
597 * is rejected to prevent security-
598 * related or otherwise sensitive
599 * attributes from being exposed, and
600 * therefore manipulable, as regular
603 * rename Any attempt to rename between
604 * attribute and non-attribute space
605 * is rejected to prevent an already
606 * linked file from being renamed and
607 * thereby circumventing the link res-
610 * mkdir, symlink, mknod Any attempt to create a "non-
611 * regular" file in attribute space is
612 * rejected to reduce the functional-
613 * ity, and therefore exposure and
614 * risk, of the initial implementa-
617 * The entire available name space has been allocated to "gen-
618 * eral use" to bring the implementation in line with the NFSv4
619 * draft standard [NFSv4]. That standard defines "named attri-
620 * butes" (equivalent to Solaris Extended Attributes) with no
621 * naming restrictions. All Sun applications making use of
622 * opaque extended attributes will use the prefix "SUNW".
625 #ifdef HAVE_SYS_ATTR_H
626 #include <sys/attr.h>
633 #ifdef HAVE_SYS_NVPAIR_H
634 #include <sys/nvpair.h>
637 #ifdef HAVE_SYS_ACL_H
642 * Define the supported XATTR streams for this OS
644 #if defined(HAVE_SYS_NVPAIR_H) && defined(_PC_SATTR_ENABLED)
645 static int os_default_xattr_streams[2] = { STREAM_XATTR_SOLARIS, STREAM_XATTR_SOLARIS_SYS};
647 static int os_default_xattr_streams[1] = { STREAM_XATTR_SOLARIS };
648 #endif /* defined(HAVE_SYS_NVPAIR_H) && defined(_PC_SATTR_ENABLED) */
651 * This code creates a temporary cache with entries for each xattr which has
652 * a link count > 1 (which indicates it has one or more hard linked counterpart(s))
654 static xattr_link_cache_entry_t *find_xattr_link_cache_entry(JCR *jcr, ino_t inum)
656 xattr_link_cache_entry_t *ptr;
658 foreach_alist(ptr, jcr->xattr_data->link_cache) {
659 if (ptr && ptr->inum == inum) {
666 static void add_xattr_link_cache_entry(JCR *jcr, ino_t inum, char *target)
668 xattr_link_cache_entry_t *ptr;
670 ptr = (xattr_link_cache_entry_t *)malloc(sizeof(xattr_link_cache_entry_t));
671 memset((caddr_t)ptr, 0, sizeof(xattr_link_cache_entry_t));
673 bstrncpy(ptr->target, target, sizeof(ptr->target));
674 jcr->xattr_data->link_cache->append(ptr);
677 static void flush_xattr_link_cache(JCR *jcr)
679 xattr_link_cache_entry_t *ptr;
681 foreach_alist(ptr, jcr->xattr_data->link_cache) {
682 if (ptr && ptr->target) {
686 >>>>>>> Add all acl and xattr related variables which are either global or already part of the JCR:bacula/src/filed/xattr.c
689 #if defined(HAVE_SYS_NVPAIR_H) && defined(_PC_SATTR_ENABLED)
691 * This function returns true if a non default extended system attribute
692 * list is associated with fd and returns false when an error has occured
693 * or when only extended system attributes other than archive,
694 * av_modified or crtime are set.
696 * The function returns true for the following cases:
698 * - any extended system attribute other than the default attributes
699 * ('archive', 'av_modified' and 'crtime') is set
700 * - nvlist has NULL name string
701 * - nvpair has data type of 'nvlist'
702 * - default data type.
704 static bool solaris_has_non_transient_extensible_attributes(int fd)
714 if (fgetattr(fd, XATTR_VIEW_READWRITE, &response) != 0) {
719 while ((pair = nvlist_next_nvpair(response, pair)) != NULL) {
720 name = nvpair_name(pair);
723 fattr = name_to_attr(name);
729 type = nvpair_type(pair);
731 case DATA_TYPE_BOOLEAN_VALUE:
732 if (nvpair_value_boolean_value(pair, &value) != 0) {
735 if (value && fattr != F_ARCHIVE &&
736 fattr != F_AV_MODIFIED) {
741 case DATA_TYPE_UINT64_ARRAY:
742 if (fattr != F_CRTIME) {
747 case DATA_TYPE_NVLIST:
755 if (response != NULL) {
756 nvlist_free(response);
760 #endif /* HAVE_SYS_NVPAIR_H && _PC_SATTR_ENABLED */
762 #if defined(HAVE_ACL) && !defined(HAVE_EXTENDED_ACL)
764 * See if an acl is a trivial one (e.g. just the stat bits encoded as acl.)
765 * There is no need to store those acls as we already store the stat bits too.
767 static bool acl_is_trivial(int count, aclent_t *entries)
772 for (n = 0; n < count; n++) {
774 if (!(ace->a_type == USER_OBJ ||
775 ace->a_type == GROUP_OBJ ||
776 ace->a_type == OTHER_OBJ ||
777 ace->a_type == CLASS_OBJ))
782 #endif /* HAVE_ACL && !HAVE_EXTENDED_ACL */
784 static bxattr_exit_code solaris_save_xattr_acl(JCR *jcr, int fd, const char *attrname, char **acl_text)
787 #ifdef HAVE_EXTENDED_ACL
793 * See if this attribute has an ACL
795 if ((fd != -1 && fpathconf(fd, _PC_ACL_ENABLED) > 0) ||
796 pathconf(attrname, _PC_ACL_ENABLED) > 0) {
798 * See if there is a non trivial acl on the file.
800 if ((fd != -1 && facl_get(fd, ACL_NO_TRIVIAL, &aclp) != 0) ||
801 acl_get(attrname, ACL_NO_TRIVIAL, &aclp) != 0) {
804 return bxattr_exit_ok;
806 Mmsg3(jcr->errmsg, _("Unable to get acl on xattr %s on file \"%s\": ERR=%s\n"),
807 attrname, jcr->last_fname, be.bstrerror());
808 Dmsg3(100, "facl_get/acl_get of xattr %s on \"%s\" failed: ERR=%s\n",
809 attrname, jcr->last_fname, be.bstrerror());
810 return bxattr_exit_error;
815 #if defined(ACL_SID_FMT)
817 * New format flag added in newer Solaris versions.
819 flags = ACL_APPEND_ID | ACL_COMPACT_FMT | ACL_SID_FMT;
821 flags = ACL_APPEND_ID | ACL_COMPACT_FMT;
822 #endif /* ACL_SID_FMT */
824 *acl_text = acl_totext(aclp, flags);
832 return bxattr_exit_ok;
833 #else /* HAVE_EXTENDED_ACL */
835 aclent_t *acls = NULL;
839 * See if this attribute has an ACL
842 n = facl(fd, GETACLCNT, 0, NULL);
844 n = acl(attrname, GETACLCNT, 0, NULL);
847 if (n >= MIN_ACL_ENTRIES) {
848 acls = (aclent_t *)malloc(n * sizeof(aclent_t));
849 if ((fd != -1 && facl(fd, GETACL, n, acls) != n) ||
850 acl(attrname, GETACL, n, acls) != n) {
854 return bxattr_exit_ok;
856 Mmsg3(jcr->errmsg, _("Unable to get acl on xattr %s on file \"%s\": ERR=%s\n"),
857 attrname, jcr->last_fname, be.bstrerror());
858 Dmsg3(100, "facl/acl of xattr %s on \"%s\" failed: ERR=%s\n",
859 attrname, jcr->last_fname, be.bstrerror());
861 return bxattr_exit_error;
866 * See if there is a non trivial acl on the file.
868 if (!acl_is_trivial(n, acls)) {
869 if ((*acl_text = acltotext(acls, n)) == NULL) {
870 Mmsg3(jcr->errmsg, _("Unable to get acl text on xattr %s on file \"%s\": ERR=%s\n"),
871 attrname, jcr->last_fname, be.bstrerror());
872 Dmsg3(100, "acltotext of xattr %s on \"%s\" failed: ERR=%s\n",
873 attrname, jcr->last_fname, be.bstrerror());
875 return bxattr_exit_error;
885 return bxattr_exit_ok;
886 #endif /* HAVE_EXTENDED_ACL */
889 return bxattr_exit_ok;
890 #endif /* HAVE_ACL */
894 * Forward declaration for recursive function call.
896 static bxattr_exit_code solaris_save_xattrs(JCR *jcr, const char *xattr_namespace, const char *attr_parent);
899 * Save an extended or extensible attribute.
900 * This is stored as an opaque stream of bytes with the following encoding:
902 * <xattr_name>\0<stat_buffer>\0<acl_string>\0<actual_xattr_data>
904 * or for a hardlinked or symlinked attribute
906 * <xattr_name>\0<stat_buffer>\0<xattr_link_source>\0
908 * xattr_name can be a subpath relative to the file the xattr is on.
909 * stat_buffer is the string representation of the stat struct.
910 * acl_string is an acl text when a non trivial acl is set on the xattr.
911 * actual_xattr_data is the content of the xattr file.
913 static bxattr_exit_code solaris_save_xattr(JCR *jcr, int fd, const char *xattr_namespace,
914 const char *attrname, bool toplevel_hidden_dir, int stream)
919 xattr_link_cache_entry_t *xlce;
920 char target_attrname[PATH_MAX];
921 char link_source[PATH_MAX];
922 char *acl_text = NULL;
923 char attribs[MAXSTRING];
925 bxattr_exit_code retval = bxattr_exit_error;
928 bsnprintf(target_attrname, sizeof(target_attrname), "%s%s", xattr_namespace, attrname);
931 * Get the stats of the extended or extensible attribute.
933 if (fstatat(fd, attrname, &st, AT_SYMLINK_NOFOLLOW) < 0) {
936 retval = bxattr_exit_ok;
939 Mmsg3(jcr->errmsg, _("Unable to get status on xattr %s on file \"%s\": ERR=%s\n"),
940 target_attrname, jcr->last_fname, be.bstrerror());
941 Dmsg3(100, "fstatat of xattr %s on \"%s\" failed: ERR=%s\n",
942 target_attrname, jcr->last_fname, be.bstrerror());
948 * Based on the filetype perform the correct action. We support most filetypes here, more
949 * then the actual implementation on Solaris supports so some code may never get executed
950 * due to limitations in the implementation.
952 switch (st.st_mode & S_IFMT) {
957 * Get any acl on the xattr.
959 if (solaris_save_xattr_acl(jcr, attrfd, attrname, &acl_text) != bxattr_exit_ok)
963 * The current implementation of xattr on Solaris doesn't support this, but if it ever does we are prepared.
964 * Encode the stat struct into an ASCII representation.
966 encode_stat(attribs, &st, 0, stream);
967 cnt = bsnprintf(buffer, sizeof(buffer), "%s%c%s%c%s%c",
968 target_attrname, 0, attribs, 0, (acl_text) ? acl_text : "", 0);
973 * Get any acl on the xattr.
975 if (solaris_save_xattr_acl(jcr, attrfd, attrname, &acl_text) != bxattr_exit_ok)
979 * See if this is the toplevel_hidden_dir being saved.
981 if (toplevel_hidden_dir) {
983 * Save the data for later storage when we encounter a real xattr. We store the data
984 * in the jcr->xattr_data->content buffer and flush that just before sending out the
985 * first real xattr. Encode the stat struct into an ASCII representation and jump
986 * out of the function.
988 encode_stat(attribs, &st, 0, stream);
989 cnt = bsnprintf(buffer, sizeof(buffer),
991 target_attrname, 0, attribs, 0, (acl_text) ? acl_text : "", 0);
992 pm_memcpy(jcr->xattr_data->content, buffer, cnt);
993 jcr->xattr_data->content_length = cnt;
997 * The current implementation of xattr on Solaris doesn't support this, but if it ever does we are prepared.
998 * Encode the stat struct into an ASCII representation.
1000 encode_stat(attribs, &st, 0, stream);
1001 cnt = bsnprintf(buffer, sizeof(buffer),
1003 target_attrname, 0, attribs, 0, (acl_text) ? acl_text : "", 0);
1008 * If this is a hardlinked file check the inode cache for a hit.
1010 if (st.st_nlink > 1) {
1012 * See if the cache already knows this inode number.
1014 if ((xlce = find_xattr_link_cache_entry(jcr, st.st_ino)) != NULL) {
1016 * Generate a xattr encoding with the reference to the target in there.
1018 encode_stat(attribs, &st, st.st_ino, stream);
1019 cnt = bsnprintf(buffer, sizeof(buffer),
1021 target_attrname, 0, attribs, 0, xlce->target, 0);
1022 pm_memcpy(jcr->xattr_data->content, buffer, cnt);
1023 jcr->xattr_data->content_length = cnt;
1024 retval = send_xattr_stream(jcr, stream);
1027 * For a hard linked file we are ready now, no need to recursively save the attributes.
1033 * Store this hard linked file in the cache.
1034 * Store the name relative to the top level xattr space.
1036 add_xattr_link_cache_entry(jcr, st.st_ino, target_attrname + 1);
1040 * Get any acl on the xattr.
1042 if (solaris_save_xattr_acl(jcr, attrfd, attrname, &acl_text) != bxattr_exit_ok) {
1047 * Encode the stat struct into an ASCII representation.
1049 encode_stat(attribs, &st, 0, stream);
1050 cnt = bsnprintf(buffer, sizeof(buffer),
1052 target_attrname, 0, attribs, 0, (acl_text) ? acl_text : "", 0);
1055 * Open the extended or extensible attribute file.
1057 if ((attrfd = openat(fd, attrname, O_RDONLY)) < 0) {
1060 retval = bxattr_exit_ok;
1063 Mmsg3(jcr->errmsg, _("Unable to open xattr %s on \"%s\": ERR=%s\n"),
1064 target_attrname, jcr->last_fname, be.bstrerror());
1065 Dmsg3(100, "openat of xattr %s on \"%s\" failed: ERR=%s\n",
1066 target_attrname, jcr->last_fname, be.bstrerror());
1074 * The current implementation of xattr on Solaris doesn't support this, but if it ever does we are prepared.
1075 * Encode the stat struct into an ASCII representation.
1077 if (readlink(attrname, link_source, sizeof(link_source)) < 0) {
1080 retval = bxattr_exit_ok;
1083 Mmsg3(jcr->errmsg, _("Unable to read symlin %s on \"%s\": ERR=%s\n"),
1084 target_attrname, jcr->last_fname, be.bstrerror());
1085 Dmsg3(100, "readlink of xattr %s on \"%s\" failed: ERR=%s\n",
1086 target_attrname, jcr->last_fname, be.bstrerror());
1092 * Generate a xattr encoding with the reference to the target in there.
1094 encode_stat(attribs, &st, st.st_ino, stream);
1095 cnt = bsnprintf(buffer, sizeof(buffer),
1097 target_attrname, 0, attribs, 0, link_source, 0);
1098 pm_memcpy(jcr->xattr_data->content, buffer, cnt);
1099 jcr->xattr_data->content_length = cnt;
1100 retval = send_xattr_stream(jcr, stream);
1103 * For a soft linked file we are ready now, no need to recursively save the attributes.
1112 * See if this is the first real xattr being saved.
1113 * If it is save the toplevel_hidden_dir attributes first.
1114 * This is easy as its stored already in the jcr->xattr_data->content buffer.
1116 if (jcr->xattr_data->nr_saved == 0) {
1117 retval = send_xattr_stream(jcr, STREAM_XATTR_SOLARIS);
1118 if (retval != bxattr_exit_ok) {
1123 pm_memcpy(jcr->xattr_data->content, buffer, cnt);
1124 jcr->xattr_data->content_length = cnt;
1127 * Only dump the content of regular files.
1129 switch (st.st_mode & S_IFMT) {
1131 if (st.st_size > 0) {
1133 * Protect ourself against things getting out of hand.
1135 if (st.st_size >= MAX_XATTR_STREAM) {
1136 Mmsg2(jcr->errmsg, _("Xattr stream on file \"%s\" exceeds maximum size of %d bytes\n"),
1137 jcr->last_fname, MAX_XATTR_STREAM);
1141 while ((cnt = read(attrfd, buffer, sizeof(buffer))) > 0) {
1142 jcr->xattr_data->content = check_pool_memory_size(jcr->xattr_data->content, jcr->xattr_data->content_length + cnt);
1143 memcpy(jcr->xattr_data->content + jcr->xattr_data->content_length, buffer, cnt);
1144 jcr->xattr_data->content_length += cnt;
1148 Mmsg2(jcr->errmsg, _("Unable to read content of xattr %s on file \"%s\"\n"),
1149 target_attrname, jcr->last_fname);
1150 Dmsg2(100, "read of data from xattr %s on \"%s\" failed\n",
1151 target_attrname, jcr->last_fname);
1162 retval = send_xattr_stream(jcr, stream);
1163 jcr->xattr_data->nr_saved++;
1167 * Recursivly call solaris_save_extended_attributes for archiving the attributes
1168 * available on this extended attribute.
1171 retval = solaris_save_xattrs(jcr, xattr_namespace, attrname);
1174 * The recursive call could change our working dir so change back to the wanted workdir.
1176 if (fchdir(fd) < 0) {
1179 retval = bxattr_exit_ok;
1182 Mmsg2(jcr->errmsg, _("Unable to chdir to xattr space of file \"%s\": ERR=%s\n"),
1183 jcr->last_fname, be.bstrerror());
1184 Dmsg3(100, "Unable to fchdir to xattr space of file \"%s\" using fd %d: ERR=%s\n",
1185 jcr->last_fname, fd, be.bstrerror());
1201 static bxattr_exit_code solaris_save_xattrs(JCR *jcr, const char *xattr_namespace, const char *attr_parent)
1204 int fd, filefd = -1, attrdirfd = -1;
1207 char current_xattr_namespace[PATH_MAX];
1208 bxattr_exit_code retval = bxattr_exit_error;
1212 * Determine what argument to use. Use attr_parent when set
1213 * (recursive call) or jcr->last_fname for first call. Also save
1214 * the current depth of the xattr_space we are in.
1218 if (xattr_namespace) {
1219 bsnprintf(current_xattr_namespace, sizeof(current_xattr_namespace), "%s%s/",
1220 xattr_namespace, attr_parent);
1222 bstrncpy(current_xattr_namespace, "/", sizeof(current_xattr_namespace));
1225 name = jcr->last_fname;
1226 bstrncpy(current_xattr_namespace, "/", sizeof(current_xattr_namespace));
1230 * Open the file on which to save the xattrs read-only.
1232 if ((filefd = open(name, O_RDONLY | O_NONBLOCK)) < 0) {
1235 retval = bxattr_exit_ok;
1238 Mmsg2(jcr->errmsg, _("Unable to open file \"%s\": ERR=%s\n"),
1239 jcr->last_fname, be.bstrerror());
1240 Dmsg2(100, "Unable to open file \"%s\": ERR=%s\n",
1241 jcr->last_fname, be.bstrerror());
1247 * Open the xattr naming space.
1249 if ((attrdirfd = openat(filefd, ".", O_RDONLY | O_XATTR)) < 0) {
1253 * Gentile way of the system saying this type of xattr layering is not supported.
1254 * Which is not problem we just forget about this this xattr.
1255 * But as this is not an error we return a positive return value.
1257 retval = bxattr_exit_ok;
1260 retval = bxattr_exit_ok;
1263 Mmsg3(jcr->errmsg, _("Unable to open xattr space %s on file \"%s\": ERR=%s\n"),
1264 name, jcr->last_fname, be.bstrerror());
1265 Dmsg3(100, "Unable to open xattr space %s on file \"%s\": ERR=%s\n",
1266 name, jcr->last_fname, be.bstrerror());
1272 * We need to change into the attribute directory to determine if each of the
1273 * attributes should be saved.
1275 if (fchdir(attrdirfd) < 0) {
1276 Mmsg2(jcr->errmsg, _("Unable to chdir to xattr space on file \"%s\": ERR=%s\n"),
1277 jcr->last_fname, be.bstrerror());
1278 Dmsg3(100, "Unable to fchdir to xattr space on file \"%s\" using fd %d: ERR=%s\n",
1279 jcr->last_fname, attrdirfd, be.bstrerror());
1284 * Save the data of the toplevel xattr hidden_dir. We save this one before anything
1285 * else because the readdir returns "." entry after the extensible attr entry.
1286 * And as we want this entry before anything else we better just save its data.
1289 solaris_save_xattr(jcr, attrdirfd, current_xattr_namespace, ".",
1290 true, STREAM_XATTR_SOLARIS);
1292 if ((fd = dup(attrdirfd)) == -1 ||
1293 (dirp = fdopendir(fd)) == (DIR *)NULL) {
1294 Mmsg2(jcr->errmsg, _("Unable to list the xattr space on file \"%s\": ERR=%s\n"),
1295 jcr->last_fname, be.bstrerror());
1296 Dmsg3(100, "Unable to fdopendir xattr space on file \"%s\" using fd %d: ERR=%s\n",
1297 jcr->last_fname, fd, be.bstrerror());
1303 * Walk the namespace.
1305 while (dp = readdir(dirp)) {
1307 * Skip only the toplevel . dir.
1309 if (!attr_parent && !strcmp(dp->d_name, "."))
1313 * Skip all .. directories
1315 if (!strcmp(dp->d_name, ".."))
1318 Dmsg3(400, "processing extended attribute %s%s on file \"%s\"\n",
1319 current_xattr_namespace, dp->d_name, jcr->last_fname);
1321 #if defined(HAVE_SYS_NVPAIR_H) && defined(_PC_SATTR_ENABLED)
1323 * We are not interested in read-only extensible attributes.
1325 if (!strcmp(dp->d_name, VIEW_READONLY)) {
1326 Dmsg3(400, "Skipping readonly extensible attributes %s%s on file \"%s\"\n",
1327 current_xattr_namespace, dp->d_name, jcr->last_fname);
1333 * We are only interested in read-write extensible attributes
1334 * when they contain non-transient values.
1336 if (!strcmp(dp->d_name, VIEW_READWRITE)) {
1338 * Determine if there are non-transient system attributes at the toplevel.
1339 * We need to provide a fd to the open file.
1341 if (!solaris_has_non_transient_extensible_attributes(filefd)) {
1342 Dmsg3(400, "Skipping transient extensible attributes %s%s on file \"%s\"\n",
1343 current_xattr_namespace, dp->d_name, jcr->last_fname);
1350 solaris_save_xattr(jcr, attrdirfd, current_xattr_namespace, dp->d_name,
1351 false, STREAM_XATTR_SOLARIS_SYS);
1354 #endif /* HAVE_SYS_NVPAIR_H && _PC_SATTR_ENABLED */
1359 solaris_save_xattr(jcr, attrdirfd, current_xattr_namespace, dp->d_name,
1360 false, STREAM_XATTR_SOLARIS);
1364 retval = bxattr_exit_ok;
1367 if (attrdirfd != -1)
1375 static bxattr_exit_code solaris_restore_xattr_acl(JCR *jcr, int fd, const char *attrname, char *acl_text)
1377 #ifdef HAVE_EXTENDED_ACL
1382 if ((error = acl_fromtext(acl_text, &aclp)) != 0) {
1383 Mmsg1(jcr->errmsg, _("Unable to convert acl from text on file \"%s\"\n"),
1385 return bxattr_exit_error;
1388 if ((fd != -1 && facl_set(fd, aclp) != 0) ||
1389 acl_set(attrname, aclp) != 0) {
1390 Mmsg3(jcr->errmsg, _("Unable to restore acl of xattr %s on file \"%s\": ERR=%s\n"),
1391 attrname, jcr->last_fname, be.bstrerror());
1392 Dmsg3(100, "Unable to restore acl of xattr %s on file \"%s\": ERR=%s\n",
1393 attrname, jcr->last_fname, be.bstrerror());
1394 return bxattr_exit_error;
1400 return bxattr_exit_ok;
1402 #else /* HAVE_EXTENDED_ACL */
1404 aclent_t *acls = NULL;
1407 acls = aclfromtext(acl_text, &n);
1409 if ((fd != -1 && facl(fd, SETACL, n, acls) != 0) ||
1410 acl(attrname, SETACL, n, acls) != 0) {
1411 Mmsg3(jcr->errmsg, _("Unable to restore acl of xattr %s on file \"%s\": ERR=%s\n"),
1412 attrname, jcr->last_fname, be.bstrerror());
1413 Dmsg3(100, "Unable to restore acl of xattr %s on file \"%s\": ERR=%s\n",
1414 attrname, jcr->last_fname, be.bstrerror());
1415 return bxattr_exit_error;
1422 return bxattr_exit_ok;
1424 #endif /* HAVE_EXTENDED_ACL */
1427 #endif /* HAVE_ACL */
1429 static bxattr_exit_code solaris_restore_xattrs(JCR *jcr, bool is_extensible)
1431 int fd, filefd = -1, attrdirfd = -1, attrfd = -1;
1432 int used_bytes, total_bytes, cnt;
1433 char *bp, *target_attrname, *attribs;
1434 char *linked_target = NULL;
1435 char *acl_text = NULL;
1439 struct timeval times[2];
1440 bxattr_exit_code retval = bxattr_exit_error;
1444 * Parse the xattr stream. First the part that is the same for all xattrs.
1447 total_bytes = jcr->xattr_data->content_length;
1450 * The name of the target xattr has a leading / we are not interested
1451 * in that so skip it when decoding the string. We always start a the /
1452 * of the xattr space anyway.
1454 target_attrname = jcr->xattr_data->content + 1;
1455 if ((bp = strchr(target_attrname, '\0')) == (char *)NULL ||
1456 (used_bytes = (bp - jcr->xattr_data->content)) >= (total_bytes - 1)) {
1462 * Open the file on which to restore the xattrs read-only.
1464 if ((filefd = open(jcr->last_fname, O_RDONLY | O_NONBLOCK)) < 0) {
1465 Mmsg2(jcr->errmsg, _("Unable to open file \"%s\": ERR=%s\n"),
1466 jcr->last_fname, be.bstrerror());
1467 Dmsg2(100, "Unable to open file \"%s\": ERR=%s\n",
1468 jcr->last_fname, be.bstrerror());
1473 * Open the xattr naming space and make it the current working dir.
1475 if ((attrdirfd = openat(filefd, ".", O_RDONLY | O_XATTR)) < 0) {
1476 Mmsg2(jcr->errmsg, _("Unable to open xattr space on file \"%s\": ERR=%s\n"),
1477 jcr->last_fname, be.bstrerror());
1478 Dmsg2(100, "Unable to open xattr space on file \"%s\": ERR=%s\n",
1479 jcr->last_fname, be.bstrerror());
1483 if (fchdir(attrdirfd) < 0) {
1484 Mmsg2(jcr->errmsg, _("Unable to chdir to xattr space on file \"%s\": ERR=%s\n"),
1485 jcr->last_fname, be.bstrerror());
1486 Dmsg3(100, "Unable to fchdir to xattr space on file \"%s\" using fd %d: ERR=%s\n",
1487 jcr->last_fname, attrdirfd, be.bstrerror());
1492 * Try to open the correct xattr subdir based on the target_attrname given.
1493 * e.g. check if its a subdir attrname. Each / in the string makes us go
1496 while ((bp = strchr(target_attrname, '/')) != (char *)NULL) {
1499 if ((fd = open(target_attrname, O_RDONLY | O_NONBLOCK)) < 0) {
1500 Mmsg3(jcr->errmsg, _("Unable to open xattr %s on file \"%s\": ERR=%s\n"),
1501 target_attrname, jcr->last_fname, be.bstrerror());
1502 Dmsg3(100, "Unable to open xattr %s on file \"%s\": ERR=%s\n",
1503 target_attrname, jcr->last_fname, be.bstrerror());
1511 * Open the xattr naming space.
1513 if ((fd = openat(filefd, ".", O_RDONLY | O_XATTR)) < 0) {
1514 Mmsg3(jcr->errmsg, _("Unable to open xattr space %s on file \"%s\": ERR=%s\n"),
1515 target_attrname, jcr->last_fname, be.bstrerror());
1516 Dmsg3(100, "Unable to open xattr space %s on file \"%s\": ERR=%s\n",
1517 target_attrname, jcr->last_fname, be.bstrerror());
1525 * Make the xattr space our current workingdir.
1527 if (fchdir(attrdirfd) < 0) {
1528 Mmsg3(jcr->errmsg, _("Unable to chdir to xattr space %s on file \"%s\": ERR=%s\n"),
1529 target_attrname, jcr->last_fname, be.bstrerror());
1530 Dmsg4(100, "Unable to fchdir to xattr space %s on file \"%s\" using fd %d: ERR=%s\n",
1531 target_attrname, jcr->last_fname, attrdirfd, be.bstrerror());
1535 target_attrname = ++bp;
1539 * Decode the attributes from the stream.
1541 decode_stat(attribs, &st, &inum);
1544 * Decode the next field (acl_text).
1546 if ((bp = strchr(attribs, '\0')) == (char *)NULL ||
1547 (used_bytes = (bp - jcr->xattr_data->content)) >= (total_bytes - 1)) {
1553 * Based on the filetype perform the correct action. We support most filetypes here, more
1554 * then the actual implementation on Solaris supports so some code may never get executed
1555 * due to limitations in the implementation.
1557 switch (st.st_mode & S_IFMT) {
1560 * The current implementation of xattr on Solaris doesn't support this, but if it ever does we are prepared.
1562 unlinkat(attrdirfd, target_attrname, 0);
1563 if (mkfifo(target_attrname, st.st_mode) < 0) {
1564 Mmsg3(jcr->errmsg, _("Unable to mkfifo xattr %s on file \"%s\": ERR=%s\n"),
1565 target_attrname, jcr->last_fname, be.bstrerror());
1566 Dmsg3(100, "Unable to mkfifo xattr %s on file \"%s\": ERR=%s\n",
1567 target_attrname, jcr->last_fname, be.bstrerror());
1574 * The current implementation of xattr on Solaris doesn't support this, but if it ever does we are prepared.
1576 unlinkat(attrdirfd, target_attrname, 0);
1577 if (mknod(target_attrname, st.st_mode, st.st_rdev) < 0) {
1578 Mmsg3(jcr->errmsg, _("Unable to mknod xattr %s on file \"%s\": ERR=%s\n"),
1579 target_attrname, jcr->last_fname, be.bstrerror());
1580 Dmsg3(100, "Unable to mknod xattr %s on file \"%s\": ERR=%s\n",
1581 target_attrname, jcr->last_fname, be.bstrerror());
1587 * If its not the hidden_dir create the entry.
1588 * The current implementation of xattr on Solaris doesn't support this, but if it ever does we are prepared.
1590 if (strcmp(target_attrname, ".")) {
1591 unlinkat(attrdirfd, target_attrname, AT_REMOVEDIR);
1592 if (mkdir(target_attrname, st.st_mode) < 0) {
1593 Jmsg3(jcr, M_WARNING, 0, _("Unable to mkdir xattr %s on file \"%s\": ERR=%s\n"),
1594 target_attrname, jcr->last_fname, be.bstrerror());
1595 Dmsg3(100, "Unable to mkdir xattr %s on file \"%s\": ERR=%s\n",
1596 target_attrname, jcr->last_fname, be.bstrerror());
1603 * See if this is a hard linked file. e.g. inum != 0
1608 unlinkat(attrdirfd, target_attrname, 0);
1609 if (link(linked_target, target_attrname) < 0) {
1610 Mmsg4(jcr->errmsg, _("Unable to link xattr %s to %s on file \"%s\": ERR=%s\n"),
1611 target_attrname, linked_target, jcr->last_fname, be.bstrerror());
1612 Dmsg4(100, "Unable to link xattr %s to %s on file \"%s\": ERR=%s\n",
1613 target_attrname, linked_target, jcr->last_fname, be.bstrerror());
1618 * Successfully restored xattr.
1620 retval = bxattr_exit_ok;
1623 if ((bp = strchr(acl_text, '\0')) == (char *)NULL ||
1624 (used_bytes = (bp - jcr->xattr_data->content)) >= total_bytes) {
1628 if (used_bytes < (total_bytes - 1))
1632 * Restore the actual xattr.
1634 if (!is_extensible) {
1635 unlinkat(attrdirfd, target_attrname, 0);
1638 if ((attrfd = openat(attrdirfd, target_attrname, O_RDWR | O_CREAT | O_TRUNC, st.st_mode)) < 0) {
1639 Mmsg3(jcr->errmsg, _("Unable to open xattr %s on file \"%s\": ERR=%s\n"),
1640 target_attrname, jcr->last_fname, be.bstrerror());
1641 Dmsg3(100, "Unable to open xattr %s on file \"%s\": ERR=%s\n",
1642 target_attrname, jcr->last_fname, be.bstrerror());
1648 * Restore the actual data.
1650 if (st.st_size > 0) {
1651 used_bytes = (data - jcr->xattr_data->content);
1652 cnt = total_bytes - used_bytes;
1655 * Do a sanity check, the st.st_size should be the same as the number of bytes
1656 * we have available as data of the stream.
1658 if (cnt != st.st_size) {
1659 Mmsg2(jcr->errmsg, _("Unable to restore data of xattr %s on file \"%s\": Not all data available in xattr stream\n"),
1660 target_attrname, jcr->last_fname);
1661 Dmsg2(100, "Unable to restore data of xattr %s on file \"%s\": Not all data available in xattr stream\n",
1662 target_attrname, jcr->last_fname);
1667 cnt = write(attrfd, data, cnt);
1669 Mmsg3(jcr->errmsg, _("Unable to restore data of xattr %s on file \"%s\": ERR=%s\n"),
1670 target_attrname, jcr->last_fname, be.bstrerror());
1671 Dmsg3(100, "Unable to restore data of xattr %s on file \"%s\": ERR=%s\n",
1672 target_attrname, jcr->last_fname, be.bstrerror());
1678 cnt = total_bytes - used_bytes;
1684 * The current implementation of xattr on Solaris doesn't support this, but if it ever does we are prepared.
1688 if (symlink(linked_target, target_attrname) < 0) {
1689 Mmsg4(jcr->errmsg, _("Unable to symlink xattr %s to %s on file \"%s\": ERR=%s\n"),
1690 target_attrname, linked_target, jcr->last_fname, be.bstrerror());
1691 Dmsg4(100, "Unable to symlink xattr %s to %s on file \"%s\": ERR=%s\n",
1692 target_attrname, linked_target, jcr->last_fname, be.bstrerror());
1697 * Successfully restored xattr.
1699 retval = bxattr_exit_ok;
1706 * Restore owner and acl for non extensible attributes.
1708 if (!is_extensible) {
1709 if (fchownat(attrdirfd, target_attrname, st.st_uid, st.st_gid, AT_SYMLINK_NOFOLLOW) < 0) {
1713 * Gentile way of the system saying this type of xattr layering is not supported.
1714 * But as this is not an error we return a positive return value.
1716 retval = bxattr_exit_ok;
1719 retval = bxattr_exit_ok;
1722 Mmsg3(jcr->errmsg, _("Unable to restore owner of xattr %s on file \"%s\": ERR=%s\n"),
1723 target_attrname, jcr->last_fname, be.bstrerror());
1724 Dmsg3(100, "Unable to restore owner of xattr %s on file \"%s\": ERR=%s\n",
1725 target_attrname, jcr->last_fname, be.bstrerror());
1732 if (acl_text && *acl_text)
1733 if (solaris_restore_xattr_acl(jcr, attrfd, target_attrname, acl_text) != bxattr_exit_ok)
1735 #endif /* HAVE_ACL */
1738 * For a non extensible attribute restore access and modification time on the xattr.
1740 if (!is_extensible) {
1741 times[0].tv_sec = st.st_atime;
1742 times[0].tv_usec = 0;
1743 times[1].tv_sec = st.st_mtime;
1744 times[1].tv_usec = 0;
1746 if (futimesat(attrdirfd, target_attrname, times) < 0) {
1747 Mmsg3(jcr->errmsg, _("Unable to restore filetimes of xattr %s on file \"%s\": ERR=%s\n"),
1748 target_attrname, jcr->last_fname, be.bstrerror());
1749 Dmsg3(100, "Unable to restore filetimes of xattr %s on file \"%s\": ERR=%s\n",
1750 target_attrname, jcr->last_fname, be.bstrerror());
1756 * Successfully restored xattr.
1758 retval = bxattr_exit_ok;
1762 Mmsg1(jcr->errmsg, _("Illegal xattr stream, failed to parse xattr stream on file \"%s\"\n"),
1764 Dmsg1(100, "Illegal xattr stream, failed to parse xattr stream on file \"%s\"\n",
1771 if (attrdirfd != -1) {
1780 static bxattr_exit_code solaris_build_xattr_streams(JCR *jcr, FF_PKT *ff_pkt)
1783 bxattr_exit_code retval = bxattr_exit_ok;
1786 * First see if extended attributes or extensible attributes are present.
1787 * If not just pretend things went ok.
1789 if (pathconf(jcr->last_fname, _PC_XATTR_EXISTS) > 0) {
1790 jcr->xattr_data->nr_saved = 0;
1793 * As we change the cwd in the save function save the current cwd
1794 * for restore after return from the solaris_save_xattrs function.
1796 jcr->xattr_data->link_cache = New(alist(10, not_owned_by_alist));
1797 getcwd(cwd, sizeof(cwd));
1798 retval = solaris_save_xattrs(jcr, NULL, NULL);
1800 flush_xattr_link_cache(jcr);
1801 delete jcr->xattr_data->link_cache;
1802 jcr->xattr_data->link_cache = NULL;
1807 static bxattr_exit_code solaris_parse_xattr_streams(JCR *jcr, int stream)
1810 bool is_extensible = false;
1811 bxattr_exit_code retval;
1814 * First make sure we can restore xattr on the filesystem.
1817 #if defined(HAVE_SYS_NVPAIR_H) && defined(_PC_SATTR_ENABLED)
1818 case STREAM_XATTR_SOLARIS_SYS:
1819 if (pathconf(jcr->last_fname, _PC_SATTR_ENABLED) <= 0) {
1820 Qmsg1(jcr, M_WARNING, 0,
1821 _("Failed to restore extensible attributes on file \"%s\"\n"),
1823 Dmsg1(100, "Unable to restore extensible attributes on file \"%s\", filesystem doesn't support this\n",
1825 return bxattr_exit_error;
1828 is_extensible = true;
1831 case STREAM_XATTR_SOLARIS:
1832 if (pathconf(jcr->last_fname, _PC_XATTR_ENABLED) <= 0) {
1833 Qmsg1(jcr, M_WARNING, 0,
1834 _("Failed to restore extended attributes on file \"%s\"\n"),
1836 Dmsg1(100, "Unable to restore extended attributes on file \"%s\", filesystem doesn't support this\n",
1838 return bxattr_exit_error;
1842 return bxattr_exit_error;
1846 * As we change the cwd in the restore function save the current cwd
1847 * for restore after return from the solaris_restore_xattrs function.
1849 getcwd(cwd, sizeof(cwd));
1850 retval = solaris_restore_xattrs(jcr, is_extensible);
1857 * Function pointers to the build and parse function to use for these xattrs.
1859 static bxattr_exit_code (*os_build_xattr_streams)(JCR *jcr, FF_PKT *ff_pkt) = solaris_build_xattr_streams;
1860 static bxattr_exit_code (*os_parse_xattr_streams)(JCR *jcr, int stream) = solaris_parse_xattr_streams;
1862 #endif /* defined(HAVE_SUN_OS) */
1865 * Entry points when compiled with support for XATTRs on a supported platform.
1867 bxattr_exit_code build_xattr_streams(JCR *jcr, FF_PKT *ff_pkt)
1869 if (os_build_xattr_streams) {
1870 return (*os_build_xattr_streams)(jcr, ff_pkt);
1872 return bxattr_exit_error;
1875 bxattr_exit_code parse_xattr_streams(JCR *jcr, int stream)
1879 if (os_parse_xattr_streams) {
1881 * See if we can parse this stream, and ifso give it a try.
1883 for (cnt = 0; cnt < sizeof(os_default_xattr_streams) / sizeof(int); cnt++) {
1884 if (os_default_xattr_streams[cnt] == stream) {
1885 return (*os_parse_xattr_streams)(jcr, stream);
1890 * Issue a warning and discard the message. But pretend the restore was ok.
1892 Jmsg2(jcr, M_WARNING, 0,
1893 _("Can't restore Extended Attributes of %s - incompatible xattr stream encountered - %d\n"),
1894 jcr->last_fname, stream);
1895 return bxattr_exit_error;