2 Bacula(R) - The Network Backup Solution
4 Copyright (C) 2000-2016 Kern Sibbald
6 The original author of Bacula is Kern Sibbald, with contributions
7 from many others, a complete list can be found in the file AUTHORS.
9 You may use this file and others of this release according to the
10 license defined in the LICENSE file, which includes the Affero General
11 Public License, v3.0 ("AGPLv3") and some additional permissions and
12 terms pursuant to its AGPLv3 Section 7.
14 This notice must be preserved when any source code is
15 conveyed and/or propagated.
17 Bacula(R) is a registered trademark of Kern Sibbald.
20 * Major refactoring of XATTR code written by:
22 * Radosław Korzeniewski, MMXVI
23 * radoslaw@korzeniewski.net, radekk@inteos.pl
24 * Inteos Sp. z o.o. http://www.inteos.pl/
30 #include "bxattr_freebsd.h"
32 #if defined(HAVE_FREEBSD_OS)
34 /* check if XATTR support is enabled */
35 #if defined(HAVE_XATTR)
38 * Define the supported XATTR streams for this OS
40 static const int os_xattr_streams[] = {
41 STREAM_XACL_FREEBSD_XATTR,
45 static const int os_xattr_namespaces[] = {
46 EXTATTR_NAMESPACE_USER,
47 EXTATTR_NAMESPACE_SYSTEM,
51 static const char *os_xattr_acl_skiplist[] = {
52 "system.posix1e.acl_access",
53 "system.posix1e.acl_default",
58 static const char *os_xattr_skiplist[] = {
63 * OS specific constructor
65 BXATTR_FreeBSD::BXATTR_FreeBSD()
67 set_xattr_streams(os_xattr_streams);
68 set_xattr_skiplists(os_xattr_skiplist, os_xattr_acl_skiplist);
72 * Perform OS specific extended attribute backup
74 * in/out - check API at bxattr.h
76 bRC_BXATTR BXATTR_FreeBSD::os_backup_xattr (JCR *jcr, FF_PKT *ff_pkt){
86 uint32_t name_gen_len;
90 alist *xattr_list = NULL;
96 for (a = 0; os_xattr_namespaces[a] != -1; a++){ // loop through all available namespaces
97 /* xlist is allocated as POOLMEM by os_get_xattr_names */
98 rc = os_get_xattr_names(jcr, os_xattr_namespaces[a], &xlist, &xlen);
101 /* it's ok, so go further */
103 case bRC_BXATTR_skip:
104 case bRC_BXATTR_cont:
105 /* no xattr available, so skip rest of it */
106 return bRC_BXATTR_ok;
111 /* get a string representation of the namespace */
112 if (extattr_namespace_to_string(os_xattr_namespaces[a], &namespace_str) != 0){
113 Mmsg2(jcr->errmsg, _("Failed to convert %d into namespace on file \"%s\"\n"), os_xattr_namespaces[a], jcr->last_fname);
114 Dmsg2(100, "Failed to convert %d into namespace on file \"%s\"\n", os_xattr_namespaces[a], jcr->last_fname);
117 namespace_len = strlen(namespace_str);
119 /* follow the list of xattr names and get the values */
120 for (name = xlist; (name - xlist) + 1 < xlen; name = strchr(name, '\0') + 1){
121 name_len = strlen(name);
122 name_gen = get_pool_memory(PM_FNAME);
123 name_gen = check_pool_memory_size(name_gen, name_len + namespace_len + 2);
124 bsnprintf(name_gen, name_len + namespace_len + 2, "%s.%s", namespace_str, name);
125 name_gen_len = strlen(name_gen);
127 skip = check_xattr_skiplists(jcr, ff_pkt, name_gen);
128 if (skip || name_len == 0){
129 Dmsg1(100, "Skipping xattr named %s\n", name_gen);
133 /* value is allocated as POOLMEM by os_get_xattr_value */
134 rc = os_get_xattr_value(jcr, os_xattr_namespaces[a], name, &value, &value_len);
137 /* it's ok, so go further */
139 case bRC_BXATTR_skip:
140 /* no xattr available, so skip rest of it */
149 * we have a name of the extended attribute in the name variable
150 * and value of the extended attribute in the value variable
151 * so we need to build a list
153 xattr = (BXATTR_xattr*)malloc(sizeof(BXATTR_xattr));
154 xattr->name_len = name_gen_len;
155 xattr->name = name_gen;
156 xattr->value_len = value_len;
157 xattr->value = value;
158 /* magic name_len name value_len value */
159 len += sizeof(uint32_t) + sizeof(uint32_t) + name_gen_len + sizeof(uint32_t) + value_len;
161 if (xattr_list == NULL){
162 xattr_list = New(alist(10, not_owned_by_alist));
164 xattr_list->append(xattr);
167 if (xattr_count > 0){
168 /* serialize the stream */
169 rc = serialize_xattr_stream(jcr, len, xattr_list);
170 if (rc != bRC_BXATTR_ok){
171 Mmsg(jcr->errmsg, _("Failed to serialize extended attributes on file \"%s\"\n"), jcr->last_fname);
172 Dmsg1(100, "Failed to serialize extended attributes on file \"%s\"\n", jcr->last_fname);
175 /* send data to SD */
176 rc = send_xattr_stream(jcr, STREAM_XACL_FREEBSD_XATTR);
183 /* free allocated data */
184 if (xattr_list != NULL){
185 foreach_alist(xattr, xattr_list){
190 free_pool_memory(name_gen);
207 * Perform OS specific XATTR restore. Runtime is called only when stream is supported by OS.
209 * in/out - check API at bxattr.h
211 bRC_BXATTR BXATTR_FreeBSD::os_restore_xattr (JCR *jcr, int stream, char *content, uint32_t length){
212 return generic_restore_xattr(jcr, stream);
216 * Return a list of xattr names in newly allocated pool memory and a length of the allocated buffer.
217 * It allocates a memory with poolmem subroutines every time a function is called, so it must be freed
220 * in/out - check API at bxattr.h
222 * As a FreeBSD uses a different attributes name schema/format then this method is a very different
223 * from a standard generic method because it uses a namespace (ns) value for os dependent optimization.
225 bRC_BXATTR BXATTR_FreeBSD::os_get_xattr_names (JCR *jcr, int ns, POOLMEM ** pxlist, uint32_t * xlen){
233 /* check input data */
234 if (jcr == NULL || xlen == NULL || pxlist == NULL){
235 return bRC_BXATTR_inval;
237 /* get the length of the extended attributes */
238 len = extattr_list_link(jcr->last_fname, ns, NULL, 0);
245 /* no file available, skip it */
246 return bRC_BXATTR_skip;
248 /* no xattr supported on filesystem, clear a flag and skip it */
249 clear_flag(BXATTR_FLAG_NATIVE);
251 return bRC_BXATTR_skip;
253 if (ns == EXTATTR_NAMESPACE_SYSTEM){
254 return bRC_BXATTR_cont;
255 } /* else show error */
257 Mmsg2(jcr->errmsg, _("extattr_list_link error on file \"%s\": ERR=%s\n"), jcr->last_fname, be.bstrerror());
258 Dmsg2(100, "extattr_list_link error file=%s ERR=%s\n", jcr->last_fname, be.bstrerror());
259 return bRC_BXATTR_error;
264 /* xattr available but empty, skip it */
265 return bRC_BXATTR_skip;
271 * allocate memory for the extented attribute list
272 * default size is a 4k for PM_BSOCK, which should be sufficient on almost all
273 * Linux system where xattrs a limited in size to single filesystem block ~4kB
274 * so we need to check required size
276 list = get_pool_memory(PM_BSOCK);
277 list = check_pool_memory_size(list, len + 1);
278 memset(list, 0, len + 1);
280 /* get the list of extended attributes names for a file */
281 len = extattr_list_link(jcr->last_fname, ns, list, len);
288 /* no file available, skip it, first release allocated memory */
289 free_pool_memory(list);
290 return bRC_BXATTR_skip;
292 if (ns == EXTATTR_NAMESPACE_SYSTEM){
293 return bRC_BXATTR_cont;
294 } /* else show error */
296 Mmsg2(jcr->errmsg, _("extattr_list_link error on file \"%s\": ERR=%s\n"), jcr->last_fname, be.bstrerror());
297 Dmsg2(100, "extattr_list_link error file=%s ERR=%s\n", jcr->last_fname, be.bstrerror());
298 free_pool_memory(list);
299 return bRC_BXATTR_error;
306 /* convert FreeBSD list type to the generic one */
307 genlist = get_pool_memory(PM_BSOCK);
308 genlist = check_pool_memory_size(genlist, len + 1);
309 memset(genlist, 0, len + 1);
310 for (a = 0; a < len; a += list[a] + 1){
312 memcpy(genlist + a, list + a + 1, stra);
313 genlist[a + stra] = '\0';
315 free_pool_memory(list);
316 /* setup return data */
319 return bRC_BXATTR_ok;
323 * Return a value of the requested attribute name and a length of the allocated buffer.
324 * It allocates a memory with poolmem subroutines every time a function is called, so it must be freed
327 * in/out - check API at bxattr.h
329 * As a FreeBSD uses a different attributes name schema/format then this method is a very different
330 * from a standard generic method because it uses a namespace (ns) value for os dependent optimization.
332 bRC_BXATTR BXATTR_FreeBSD::os_get_xattr_value (JCR *jcr, int ns, char * name, char ** pvalue, uint32_t * plen){
337 /* check input data */
338 if (jcr == NULL || name == NULL || plen == NULL || pvalue == NULL){
339 return bRC_BXATTR_inval;
341 /* get the length of the value for extended attribute */
342 len = extattr_get_link(jcr->last_fname, ns, name, NULL, 0);
349 /* no file available, skip it */
350 return bRC_BXATTR_skip;
352 /* XXX: what about ENOATTR error value? */
353 Mmsg2(jcr->errmsg, _("extattr_get_link error on file \"%s\": ERR=%s\n"), jcr->last_fname, be.bstrerror());
354 Dmsg2(100, "extattr_get_link error file=%s ERR=%s\n", jcr->last_fname, be.bstrerror());
355 return bRC_BXATTR_error;
365 * allocate memory for the extented attribute value
366 * default size is a 256B for PM_MESSAGE, so we need to check required size
368 value = get_pool_memory(PM_MESSAGE);
369 value = check_pool_memory_size(value, len + 1);
370 memset(value, 0, len + 1);
371 /* value is not empty, get a data */
372 len = extattr_get_link(jcr->last_fname, ns, name, value, len);
379 /* no file available, skip it, first release allocated memory */
380 free_pool_memory(value);
381 return bRC_BXATTR_skip;
383 Mmsg2(jcr->errmsg, _("extattr_get_link error on file \"%s\": ERR=%s\n"), jcr->last_fname, be.bstrerror());
384 Dmsg2(100, "extattr_get_link error file=%s ERR=%s\n", jcr->last_fname, be.bstrerror());
385 free_pool_memory(value);
386 return bRC_BXATTR_error;
393 /* ensure a value is nul terminated */
400 /* setup return data */
403 return bRC_BXATTR_ok;
407 * Low level OS specific runtime to set extended attribute on file
409 * in/out - check API at bxattr.h
411 * xattr->name should be in '<namespace>.<name>' format which
412 * function handle without problem, otherwise it returns an error
413 * TODO: it is possible to handle a different attributes name format
414 * for OS portability where default namespace 'user' can be used
416 bRC_BXATTR BXATTR_FreeBSD::os_set_xattr (JCR *jcr, BXATTR_xattr *xattr){
423 /* check input data */
424 if (jcr == NULL || xattr == NULL){
425 return bRC_BXATTR_inval;
428 /* search for attribute namespace which is distinguished from attribute name by a dot '.' character */
429 if ((name = strchr(xattr->name, '.')) == (char *)NULL){
430 Mmsg2(jcr->errmsg, _("Failed to split %s into namespace and name part on file \"%s\"\n"), xattr->name, jcr->last_fname);
431 Dmsg2(100, "Failed to split %s into namespace and name part on file \"%s\"\n", xattr->name, jcr->last_fname);
432 return bRC_BXATTR_error;
435 /* split namespace and name of the attribute */
436 nspace = xattr->name;
439 /* check if namespace is valid on this system */
440 if (extattr_string_to_namespace(nspace, &ns) != 0){
441 Mmsg2(jcr->errmsg, _("Failed to convert %s into namespace on file \"%s\"\n"), nspace, jcr->last_fname);
442 Dmsg2(100, "Failed to convert %s into namespace on file \"%s\"\n", nspace, jcr->last_fname);
443 return bRC_BXATTR_error;
446 /* set extattr on file */
447 rc = extattr_set_link(jcr->last_fname, ns, name, xattr->value, xattr->value_len);
448 if (rc < 0 || rc != (int)xattr->value_len){
455 Mmsg2(jcr->errmsg, _("extattr_set_link error on file \"%s\": ERR=%s\n"), jcr->last_fname, be.bstrerror());
456 Dmsg2(100, "extattr_set_link error file=%s ERR=%s\n", jcr->last_fname, be.bstrerror());
457 return bRC_BXATTR_error;
460 return bRC_BXATTR_ok;
463 #endif /* HAVE_XATTR */
465 #endif /* HAVE_FREEBSD_OS */