]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/filed/xattr.c
fix #1938 about PATH_MAX on hurd
[bacula/bacula] / bacula / src / filed / xattr.c
index 38ea0a19491ed213206990a01af7e8f42f2d9c2c..faf86d578c6a7275c9840123ac407d357f796db6 100644 (file)
@@ -48,7 +48,7 @@
  *   - Tru64 (Extended Attributes)
  *
  *   Written by Marco van Wieringen, November 2008
- *   Major overhaul January 2012
+ *   Major overhaul January 2012 + June 2012
  */
 
 #include "bacula.h"
@@ -340,28 +340,31 @@ static int os_default_xattr_streams[1] = {
 
 static bxattr_exit_code aix_xattr_build_streams(JCR *jcr, FF_PKT *ff_pkt)
 {
+   char *bp;
    bool skip_xattr;
-   char *xattr_list, *bp;
+   char *xattr_list = NULL;
    int cnt, xattr_count = 0;
    uint32_t name_length;
    int32_t xattr_list_len,
            xattr_value_len;
    uint32_t expected_serialize_len = 0;
-   xattr_t *current_xattr = NULL;
+   xattr_t *current_xattr;
    alist *xattr_value_list = NULL;
    bxattr_exit_code retval = bxattr_exit_error;
-   berrno be;
 
    /*
     * First get the length of the available list with extended attributes.
     */
    xattr_list_len = llistea(jcr->last_fname, NULL, 0);
    switch (xattr_list_len) {
-   case -1:
+   case -1: {
+      berrno be;
+
       switch (errno) {
       case ENOENT:
       case EFORMAT:
-         return bxattr_exit_ok;
+         retval = bxattr_exit_ok;
+         goto bail_out;
       case ENOTSUP:
          /*
           * If the filesystem reports it doesn't support XATTRs we clear the
@@ -370,18 +373,21 @@ static bxattr_exit_code aix_xattr_build_streams(JCR *jcr, FF_PKT *ff_pkt)
           * when we change from one filesystem to an other.
           */
          jcr->xattr_data->flags &= ~BXATTR_FLAG_SAVE_NATIVE;
-         return bxattr_exit_ok;
+         retval = bxattr_exit_ok;
+         goto bail_out;
       default:
          Mmsg2(jcr->errmsg,
                _("llistea error on file \"%s\": ERR=%s\n"),
                jcr->last_fname, be.bstrerror());
          Dmsg2(100, "llistea error file=%s ERR=%s\n",
                jcr->last_fname, be.bstrerror());
-         return bxattr_exit_error;
+         goto bail_out;
       }
       break;
+   }
    case 0:
-      return bxattr_exit_ok;
+      retval = bxattr_exit_ok;
+      goto bail_out;
    default:
       break;
    }
@@ -397,7 +403,9 @@ static bxattr_exit_code aix_xattr_build_streams(JCR *jcr, FF_PKT *ff_pkt)
     */
    xattr_list_len = llistea(jcr->last_fname, xattr_list, xattr_list_len);
    switch (xattr_list_len) {
-   case -1:
+   case -1: {
+      berrno be;
+
       switch (errno) {
       case ENOENT:
       case EFORMAT:
@@ -412,6 +420,7 @@ static bxattr_exit_code aix_xattr_build_streams(JCR *jcr, FF_PKT *ff_pkt)
          goto bail_out;
       }
       break;
+   }
    default:
       break;
    }
@@ -421,8 +430,9 @@ static bxattr_exit_code aix_xattr_build_streams(JCR *jcr, FF_PKT *ff_pkt)
     * Walk the list of extended attributes names and retrieve the data.
     * We already count the bytes needed for serializing the stream later on.
     */
-   bp = xattr_list;
-   while ((bp - xattr_list) + 1 < xattr_list_len) {
+   for (bp = xattr_list;
+       (bp - xattr_list) + 1 < xattr_list_len;
+        bp = strchr(bp, '\0') + 1) {
       skip_xattr = false;
 
       /*
@@ -435,33 +445,17 @@ static bxattr_exit_code aix_xattr_build_streams(JCR *jcr, FF_PKT *ff_pkt)
       name_length = strlen(bp);
       if (skip_xattr || name_length == 0) {
          Dmsg1(100, "Skipping xattr named %s\n", bp);
-         bp = strchr(bp, '\0') + 1;
          continue;
       }
 
-      /*
-       * Each xattr valuepair starts with a magic so we can parse it easier.
-       */
-      current_xattr = (xattr_t *)malloc(sizeof(xattr_t));
-      current_xattr->magic = XATTR_MAGIC;
-      expected_serialize_len += sizeof(current_xattr->magic);
-
-      /*
-       * Allocate space for storing the name.
-       */
-      current_xattr->name_length = name_length;
-      current_xattr->name = (char *)malloc(current_xattr->name_length);
-      memcpy(current_xattr->name, bp, current_xattr->name_length);
-
-      expected_serialize_len += sizeof(current_xattr->name_length) +
-                                current_xattr->name_length;
-
       /*
        * First see how long the value is for the extended attribute.
        */
       xattr_value_len = lgetea(jcr->last_fname, bp, NULL, 0);
       switch (xattr_value_len) {
-      case -1:
+      case -1: {
+         berrno be;
+
          switch (errno) {
          case ENOENT:
          case EFORMAT:
@@ -476,6 +470,29 @@ static bxattr_exit_code aix_xattr_build_streams(JCR *jcr, FF_PKT *ff_pkt)
             goto bail_out;
          }
          break;
+      }
+      default:
+         break;
+      }
+
+      /*
+       * Each xattr valuepair starts with a magic so we can parse it easier.
+       */
+      current_xattr = (xattr_t *)malloc(sizeof(xattr_t));
+      current_xattr->magic = XATTR_MAGIC;
+      expected_serialize_len += sizeof(current_xattr->magic);
+
+      /*
+       * Allocate space for storing the name.
+       */
+      current_xattr->name_length = name_length;
+      current_xattr->name = (char *)malloc(current_xattr->name_length);
+      memcpy(current_xattr->name, bp, current_xattr->name_length);
+
+      expected_serialize_len += sizeof(current_xattr->name_length) +
+                                current_xattr->name_length;
+
+      switch (xattr_value_len) {
       case 0:
          current_xattr->value = NULL;
          current_xattr->value_length = 0;
@@ -490,19 +507,29 @@ static bxattr_exit_code aix_xattr_build_streams(JCR *jcr, FF_PKT *ff_pkt)
 
          xattr_value_len = lgetea(jcr->last_fname, bp, current_xattr->value, xattr_value_len);
          if (xattr_value_len < 0) {
+            berrno be;
+
             switch (errno) {
             case ENOENT:
             case EFORMAT:
                retval = bxattr_exit_ok;
-               goto bail_out;
+               break;
             default:
                Mmsg2(jcr->errmsg,
                      _("lgetea error on file \"%s\": ERR=%s\n"),
                      jcr->last_fname, be.bstrerror());
                Dmsg2(100, "lgetea error file=%s ERR=%s\n",
                      jcr->last_fname, be.bstrerror());
-               goto bail_out;
+               break;
             }
+
+            /*
+             * Default failure path out when retrieval of attr fails.
+             */
+            free(current_xattr->value);
+            free(current_xattr->name);
+            free(current_xattr);
+            goto bail_out;
          }
          /*
           * Store the actual length of the value.
@@ -510,16 +537,6 @@ static bxattr_exit_code aix_xattr_build_streams(JCR *jcr, FF_PKT *ff_pkt)
          current_xattr->value_length = xattr_value_len;
          expected_serialize_len += sizeof(current_xattr->value_length) +
                                    current_xattr->value_length;
-
-         /*
-          * Protect ourself against things getting out of hand.
-          */
-         if (expected_serialize_len >= MAX_XATTR_STREAM) {
-            Mmsg2(jcr->errmsg,
-            _("Xattr stream on file \"%s\" exceeds maximum size of %d bytes\n"),
-                  jcr->last_fname, MAX_XATTR_STREAM);
-            goto bail_out;
-         }
          break;
       }
 
@@ -528,9 +545,17 @@ static bxattr_exit_code aix_xattr_build_streams(JCR *jcr, FF_PKT *ff_pkt)
       }
 
       xattr_value_list->append(current_xattr);
-      current_xattr = NULL;
       xattr_count++;
-      bp = strchr(bp, '\0') + 1;
+
+      /*
+       * Protect ourself against things getting out of hand.
+       */
+      if (expected_serialize_len >= MAX_XATTR_STREAM) {
+         Mmsg2(jcr->errmsg,
+               _("Xattr stream on file \"%s\" exceeds maximum size of %d bytes\n"),
+               jcr->last_fname, MAX_XATTR_STREAM);
+         goto bail_out;
+      }
    }
 
    free(xattr_list);
@@ -563,21 +588,13 @@ static bxattr_exit_code aix_xattr_build_streams(JCR *jcr, FF_PKT *ff_pkt)
    }
 
 bail_out:
-   if (current_xattr != NULL) {
-      if (current_xattr->value != NULL) {
-         free(current_xattr->value);
-      }
-      if (current_xattr->name != NULL) {
-         free(current_xattr->name);
-      }
-      free(current_xattr);
-   }
    if (xattr_list != NULL) {
       free(xattr_list);
    }
    if (xattr_value_list != NULL) {
       xattr_drop_internal_table(xattr_value_list);
    }
+
    return retval;
 }
 
@@ -588,7 +605,7 @@ static bxattr_exit_code aix_parse_xattr_streams(JCR *jcr,
 {
    xattr_t *current_xattr;
    alist *xattr_value_list;
-   berrno be;
+   bxattr_exit_code retval = bxattr_exit_error;
 
    xattr_value_list = New(alist(10, not_owned_by_alist));
 
@@ -596,8 +613,7 @@ static bxattr_exit_code aix_parse_xattr_streams(JCR *jcr,
                                 content,
                                 content_length,
                                 xattr_value_list) != bxattr_exit_ok) {
-      xattr_drop_internal_table(xattr_value_list);
-      return bxattr_exit_error;
+      goto bail_out;
    }
 
    foreach_alist(current_xattr, xattr_value_list) {
@@ -605,6 +621,8 @@ static bxattr_exit_code aix_parse_xattr_streams(JCR *jcr,
                  current_xattr->name,
                  current_xattr->value,
                  current_xattr->value_length, 0) != 0) {
+         berrno be;
+
          switch (errno) {
          case ENOENT:
          case EFORMAT:
@@ -630,12 +648,12 @@ static bxattr_exit_code aix_parse_xattr_streams(JCR *jcr,
       }
    }
 
-   xattr_drop_internal_table(xattr_value_list);
-   return bxattr_exit_ok;
+   retval = bxattr_exit_ok;
 
 bail_out:
    xattr_drop_internal_table(xattr_value_list);
-   return bxattr_exit_error;
+
+   return retval;
 }
 
 /*
@@ -685,22 +703,24 @@ static xattr_naming_space xattr_naming_spaces[] = {
 
 static bxattr_exit_code irix_xattr_build_streams(JCR *jcr, FF_PKT *ff_pkt)
 {
+   char dummy[32];
    int cnt, length, xattr_count = 0;
    attrlist_cursor_t cursor;
    attrlist_t *attrlist;
    attrlist_ent_t *attrlist_ent;
-   xattr_t *current_xattr = NULL;
+   xattr_t *current_xattr;
    alist *xattr_value_list = NULL;
    uint32_t expected_serialize_len = 0;
    bxattr_exit_code retval = bxattr_exit_error;
    POOLMEM *xattrbuf = get_memory(ATTR_MAX_VALUELEN);
-   berrno be;
 
    for (cnt = 0; xattr_naming_spaces[cnt].name != NULL; cnt++) {
       memset(&cursor, 0, sizeof(attrlist_cursor_t));
       while (1) {
          if (attr_list(jcr->last_fname, xattrbuf, ATTR_MAX_VALUELEN,
                        xattr_naming_spaces[cnt].flags, &cursor) != 0) {
+            berrno be;
+
             switch (errno) {
             case ENOENT:
                retval = bxattr_exit_ok;
@@ -723,6 +743,36 @@ static bxattr_exit_code irix_xattr_build_streams(JCR *jcr, FF_PKT *ff_pkt)
          for (cnt = 0; cnt < attrlist->al_count; cnt++) {
             attrlist_ent = ATTR_ENTRY(xattrbuf, cnt);
 
+            /*
+             * First determine if we can retrieve the xattr and how big it really is.
+             */
+            length = sizeof(dummy);
+            if (attr_get(jcr->last_fname, attrlist_ent->a_name, dummy,
+                         &length, xattr_naming_spaces[cnt].flags) != 0) {
+               berrno be;
+
+               switch (errno) {
+               case ENOENT:
+               case ENOATTR:
+                  retval = bxattr_exit_ok;
+                  goto bail_out;
+               case E2BIG:
+                  /*
+                   * Size of the xattr is bigger then the 32 bytes dummy which is
+                   * likely. As length now contains its actual length we can allocate
+                   * a properly size buffer for the real retrieval.
+                   */
+                  break;
+               default:
+                  Mmsg2(jcr->errmsg,
+                        _("attr_list error on file \"%s\": ERR=%s\n"),
+                        jcr->last_fname, be.bstrerror());
+                  Dmsg2(100, "attr_list error file=%s ERR=%s\n",
+                        jcr->last_fname, be.bstrerror());
+                  goto bail_out;
+               }
+            }
+
             /*
              * Each xattr valuepair starts with a magic so we can parse it easier.
              */
@@ -743,7 +793,7 @@ static bxattr_exit_code irix_xattr_build_streams(JCR *jcr, FF_PKT *ff_pkt)
             expected_serialize_len += sizeof(current_xattr->name_length) +
                                       current_xattr->name_length;
 
-            current_xattr->value_length = attrlist_ent->a_valuelen;
+            current_xattr->value_length = length;
             current_xattr->value = (char *)malloc(current_xattr->value_length);
 
             /*
@@ -751,37 +801,41 @@ static bxattr_exit_code irix_xattr_build_streams(JCR *jcr, FF_PKT *ff_pkt)
              */
             if (attr_get(jcr->last_fname, attrlist_ent->a_name, current_xattr->value,
                          &length, xattr_naming_spaces[cnt].flags) != 0) {
+               berrno be;
+
                switch (errno) {
                case ENOENT:
                case ENOATTR:
                   retval = bxattr_exit_ok;
-                  goto bail_out;
+                  break;
                case E2BIG:
                   /*
                    * The buffer for the xattr isn't big enough. the value of
                    * current_xattr->value_length is updated with the actual size
                    * of the xattr. So we free the old buffer and create a new one
-                   * and try again.
+                   * and try again. Normally this cannot happen as we size the
+                   * buffer using a call to attr_get before but in case of an
+                   * race condition it might happen.
                    */
                   free(current_xattr->value);
-                  current_xattr->value = (char *)malloc(current_xattr->value_length);
+                  current_xattr->value = (char *)malloc(length);
                   if (attr_get(jcr->last_fname, attrlist_ent->a_name, current_xattr->value,
                                &length, xattr_naming_spaces[cnt].flags) != 0) {
                      switch (errno) {
                      case ENOENT:
                      case ENOATTR:
                         retval = bxattr_exit_ok;
-                        goto bail_out;
+                        break;
                      default:
                         Mmsg2(jcr->errmsg,
                               _("attr_list error on file \"%s\": ERR=%s\n"),
-                              jcr->last_fname, be.bstrerror());
+                              jcr->last_fname, be.bstrerror(errno));
                         Dmsg2(100, "attr_list error file=%s ERR=%s\n",
                               jcr->last_fname, be.bstrerror());
-                        goto bail_out;
+                        break;
                      }
                   } else {
-                     current_xattr->value_length = length;
+                     goto ok_continue;
                   }
                   break;
                default:
@@ -790,15 +844,30 @@ static bxattr_exit_code irix_xattr_build_streams(JCR *jcr, FF_PKT *ff_pkt)
                         jcr->last_fname, be.bstrerror());
                   Dmsg2(100, "attr_list error file=%s ERR=%s\n",
                         jcr->last_fname, be.bstrerror());
-                  goto bail_out;
+                  break;
                }
-            } else {
-               current_xattr->value_length = length;
+
+               /*
+                * Default failure path out when retrieval of attr fails.
+                */
+               free(current_xattr->value);
+               free(current_xattr->name);
+               free(current_xattr);
+               goto bail_out;
             }
 
+ok_continue:
+            current_xattr->value_length = length;
             expected_serialize_len += sizeof(current_xattr->value_length) +
                                       current_xattr->value_length;
 
+            if (xattr_value_list == NULL) {
+               xattr_value_list = New(alist(10, not_owned_by_alist));
+            }
+
+            xattr_value_list->append(current_xattr);
+            xattr_count++;
+
             /*
              * Protect ourself against things getting out of hand.
              */
@@ -808,14 +877,6 @@ static bxattr_exit_code irix_xattr_build_streams(JCR *jcr, FF_PKT *ff_pkt)
                      jcr->last_fname, MAX_XATTR_STREAM);
                goto bail_out;
             }
-
-            if (xattr_value_list == NULL) {
-               xattr_value_list = New(alist(10, not_owned_by_alist));
-            }
-
-            xattr_value_list->append(current_xattr);
-            current_xattr = NULL;
-            xattr_count++;
          }
 
          /*
@@ -854,20 +915,12 @@ static bxattr_exit_code irix_xattr_build_streams(JCR *jcr, FF_PKT *ff_pkt)
    }
 
 bail_out:
-   if (current_xattr != NULL) {
-      if (current_xattr->value != NULL) {
-         free(current_xattr->value);
-      }
-      if (current_xattr->name != NULL) {
-         free(current_xattr->name);
-      }
-      free(current_xattr);
-   }
    free_pool_memory(xattrbuf);
 
    if (xattr_value_list != NULL) {
       xattr_drop_internal_table(xattr_value_list);
    }
+
    return retval;
 }
 
@@ -881,7 +934,6 @@ static bxattr_exit_code irix_parse_xattr_streams(JCR *jcr,
    xattr_t *current_xattr;
    alist *xattr_value_list;
    bxattr_exit_code retval = bxattr_exit_error;
-   berrno be;
 
    xattr_value_list = New(alist(10, not_owned_by_alist));
 
@@ -889,8 +941,7 @@ static bxattr_exit_code irix_parse_xattr_streams(JCR *jcr,
                                 content,
                                 content_length,
                                 xattr_value_list) != bxattr_exit_ok) {
-      xattr_drop_internal_table(xattr_value_list);
-      return bxattr_exit_error;
+      goto bail_out;
    }
 
    foreach_alist(current_xattr, xattr_value_list) {
@@ -927,6 +978,8 @@ static bxattr_exit_code irix_parse_xattr_streams(JCR *jcr,
       bp = strchr(current_xattr->name, '.');
       if (attr_set(jcr->last_fname, ++bp, current_xattr->value,
                    current_xattr->value_length, flags) != 0) {
+         berrno be;
+
          switch (errno) {
          case ENOENT:
             retval = bxattr_exit_ok;
@@ -945,7 +998,7 @@ static bxattr_exit_code irix_parse_xattr_streams(JCR *jcr,
                default:
                   Mmsg2(jcr->errmsg,
                         _("attr_set error on file \"%s\": ERR=%s\n"),
-                        jcr->last_fname, be.bstrerror());
+                        jcr->last_fname, be.bstrerror(errno));
                   Dmsg2(100, "attr_set error file=%s ERR=%s\n",
                         jcr->last_fname, be.bstrerror());
                   goto bail_out;
@@ -963,12 +1016,12 @@ static bxattr_exit_code irix_parse_xattr_streams(JCR *jcr,
       }
    }
 
-   xattr_drop_internal_table(xattr_value_list);
-   return bxattr_exit_ok;
+   retval = bxattr_exit_ok;
 
 bail_out:
    xattr_drop_internal_table(xattr_value_list);
-   return bxattr_exit_error;
+
+   return retval;
 }
 
 /*
@@ -1056,27 +1109,30 @@ static const char *xattr_skiplist[1] = {
 
 static bxattr_exit_code generic_xattr_build_streams(JCR *jcr, FF_PKT *ff_pkt)
 {
+   char *bp;
    bool skip_xattr;
-   char *xattr_list, *bp;
+   char *xattr_list = NULL;
    int cnt, xattr_count = 0;
    uint32_t name_length;
    int32_t xattr_list_len,
            xattr_value_len;
    uint32_t expected_serialize_len = 0;
-   xattr_t *current_xattr = NULL;
+   xattr_t *current_xattr;
    alist *xattr_value_list = NULL;
    bxattr_exit_code retval = bxattr_exit_error;
-   berrno be;
 
    /*
     * First get the length of the available list with extended attributes.
     */
    xattr_list_len = llistxattr(jcr->last_fname, NULL, 0);
    switch (xattr_list_len) {
-   case -1:
+   case -1: {
+      berrno be;
+
       switch (errno) {
       case ENOENT:
-         return bxattr_exit_ok;
+         retval = bxattr_exit_ok;
+         goto bail_out;
       case BXATTR_ENOTSUP:
          /*
           * If the filesystem reports it doesn't support XATTRs we clear
@@ -1086,18 +1142,21 @@ static bxattr_exit_code generic_xattr_build_streams(JCR *jcr, FF_PKT *ff_pkt)
           * change from one filesystem to an other.
           */
          jcr->xattr_data->flags &= ~BXATTR_FLAG_SAVE_NATIVE;
-         return bxattr_exit_ok;
+         retval = bxattr_exit_ok;
+         goto bail_out;
       default:
          Mmsg2(jcr->errmsg,
                _("llistxattr error on file \"%s\": ERR=%s\n"),
                jcr->last_fname, be.bstrerror());
          Dmsg2(100, "llistxattr error file=%s ERR=%s\n",
                jcr->last_fname, be.bstrerror());
-         return bxattr_exit_error;
+         goto bail_out;
       }
       break;
+   }
    case 0:
-      return bxattr_exit_ok;
+      retval = bxattr_exit_ok;
+      goto bail_out;
    default:
       break;
    }
@@ -1113,7 +1172,9 @@ static bxattr_exit_code generic_xattr_build_streams(JCR *jcr, FF_PKT *ff_pkt)
     */
    xattr_list_len = llistxattr(jcr->last_fname, xattr_list, xattr_list_len);
    switch (xattr_list_len) {
-   case -1:
+   case -1: {
+      berrno be;
+
       switch (errno) {
       case ENOENT:
          retval = bxattr_exit_ok;
@@ -1127,6 +1188,7 @@ static bxattr_exit_code generic_xattr_build_streams(JCR *jcr, FF_PKT *ff_pkt)
          goto bail_out;
       }
       break;
+   }
    default:
       break;
    }
@@ -1136,8 +1198,9 @@ static bxattr_exit_code generic_xattr_build_streams(JCR *jcr, FF_PKT *ff_pkt)
     * Walk the list of extended attributes names and retrieve the data.
     * We already count the bytes needed for serializing the stream later on.
     */
-   bp = xattr_list;
-   while ((bp - xattr_list) + 1 < xattr_list_len) {
+   for (bp = xattr_list;
+       (bp - xattr_list) + 1 < xattr_list_len;
+        bp = strchr(bp, '\0') + 1) {
       skip_xattr = false;
 
       /*
@@ -1169,32 +1232,17 @@ static bxattr_exit_code generic_xattr_build_streams(JCR *jcr, FF_PKT *ff_pkt)
       name_length = strlen(bp);
       if (skip_xattr || name_length == 0) {
          Dmsg1(100, "Skipping xattr named %s\n", bp);
-         bp = strchr(bp, '\0') + 1;
          continue;
       }
 
-      /*
-       * Each xattr valuepair starts with a magic so we can parse it easier.
-       */
-      current_xattr = (xattr_t *)malloc(sizeof(xattr_t));
-      current_xattr->magic = XATTR_MAGIC;
-      expected_serialize_len += sizeof(current_xattr->magic);
-
-      /*
-       * Allocate space for storing the name.
-       */
-      current_xattr->name_length = name_length;
-      current_xattr->name = (char *)malloc(current_xattr->name_length);
-      memcpy(current_xattr->name, bp, current_xattr->name_length);
-
-      expected_serialize_len += sizeof(current_xattr->name_length) + current_xattr->name_length;
-
       /*
        * First see how long the value is for the extended attribute.
        */
       xattr_value_len = lgetxattr(jcr->last_fname, bp, NULL, 0);
       switch (xattr_value_len) {
-      case -1:
+      case -1: {
+         berrno be;
+
          switch (errno) {
          case ENOENT:
             retval = bxattr_exit_ok;
@@ -1208,6 +1256,29 @@ static bxattr_exit_code generic_xattr_build_streams(JCR *jcr, FF_PKT *ff_pkt)
             goto bail_out;
          }
          break;
+      }
+      default:
+         break;
+      }
+
+      /*
+       * Each xattr valuepair starts with a magic so we can parse it easier.
+       */
+      current_xattr = (xattr_t *)malloc(sizeof(xattr_t));
+      current_xattr->magic = XATTR_MAGIC;
+      current_xattr->value = NULL;
+      expected_serialize_len += sizeof(current_xattr->magic);
+
+      /*
+       * Allocate space for storing the name.
+       */
+      current_xattr->name_length = name_length;
+      current_xattr->name = (char *)malloc(current_xattr->name_length);
+      memcpy(current_xattr->name, bp, current_xattr->name_length);
+
+      expected_serialize_len += sizeof(current_xattr->name_length) + current_xattr->name_length;
+
+      switch (xattr_value_len) {
       case 0:
          current_xattr->value = NULL;
          current_xattr->value_length = 0;
@@ -1222,34 +1293,36 @@ static bxattr_exit_code generic_xattr_build_streams(JCR *jcr, FF_PKT *ff_pkt)
 
          xattr_value_len = lgetxattr(jcr->last_fname, bp, current_xattr->value, xattr_value_len);
          if (xattr_value_len < 0) {
+            berrno be;
+
             switch (errno) {
             case ENOENT:
                retval = bxattr_exit_ok;
-               goto bail_out;
+               break;
             default:
                Mmsg2(jcr->errmsg,
                      _("lgetxattr error on file \"%s\": ERR=%s\n"),
                      jcr->last_fname, be.bstrerror());
                Dmsg2(100, "lgetxattr error file=%s ERR=%s\n",
                      jcr->last_fname, be.bstrerror());
-               goto bail_out;
+               break;
             }
+
+            /*
+             * Default failure path out when retrieval of attr fails.
+             */
+            free(current_xattr->value);
+            free(current_xattr->name);
+            free(current_xattr);
+            goto bail_out;
          }
+
          /*
           * Store the actual length of the value.
           */
          current_xattr->value_length = xattr_value_len;
          expected_serialize_len += sizeof(current_xattr->value_length) + current_xattr->value_length;
-
-         /*
-          * Protect ourself against things getting out of hand.
-          */
-         if (expected_serialize_len >= MAX_XATTR_STREAM) {
-            Mmsg2(jcr->errmsg,
-                  _("Xattr stream on file \"%s\" exceeds maximum size of %d bytes\n"),
-                  jcr->last_fname, MAX_XATTR_STREAM);
-            goto bail_out;
-         }
+         break;
       }
 
       if (xattr_value_list == NULL) {
@@ -1257,10 +1330,17 @@ static bxattr_exit_code generic_xattr_build_streams(JCR *jcr, FF_PKT *ff_pkt)
       }
 
       xattr_value_list->append(current_xattr);
-      current_xattr = NULL;
       xattr_count++;
-      bp = strchr(bp, '\0') + 1;
-      break;
+
+      /*
+       * Protect ourself against things getting out of hand.
+       */
+      if (expected_serialize_len >= MAX_XATTR_STREAM) {
+         Mmsg2(jcr->errmsg,
+               _("Xattr stream on file \"%s\" exceeds maximum size of %d bytes\n"),
+               jcr->last_fname, MAX_XATTR_STREAM);
+         goto bail_out;
+      }
    }
 
    free(xattr_list);
@@ -1293,21 +1373,13 @@ static bxattr_exit_code generic_xattr_build_streams(JCR *jcr, FF_PKT *ff_pkt)
    }
 
 bail_out:
-   if (current_xattr != NULL) {
-      if (current_xattr->value != NULL) {
-         free(current_xattr->value);
-      }
-      if (current_xattr->name != NULL) {
-         free(current_xattr->name);
-      }
-      free(current_xattr);
-   }
    if (xattr_list != NULL) {
       free(xattr_list);
    }
    if (xattr_value_list != NULL) {
       xattr_drop_internal_table(xattr_value_list);
    }
+
    return retval;
 }
 
@@ -1318,7 +1390,7 @@ static bxattr_exit_code generic_parse_xattr_streams(JCR *jcr,
 {
    xattr_t *current_xattr;
    alist *xattr_value_list;
-   berrno be;
+   bxattr_exit_code retval = bxattr_exit_error;
 
    xattr_value_list = New(alist(10, not_owned_by_alist));
 
@@ -1326,12 +1398,13 @@ static bxattr_exit_code generic_parse_xattr_streams(JCR *jcr,
                                 content,
                                 content_length,
                                 xattr_value_list) != bxattr_exit_ok) {
-      xattr_drop_internal_table(xattr_value_list);
-      return bxattr_exit_error;
+      goto bail_out;
    }
 
    foreach_alist(current_xattr, xattr_value_list) {
       if (lsetxattr(jcr->last_fname, current_xattr->name, current_xattr->value, current_xattr->value_length, 0) != 0) {
+         berrno be;
+
          switch (errno) {
          case ENOENT:
             goto bail_out;
@@ -1356,12 +1429,12 @@ static bxattr_exit_code generic_parse_xattr_streams(JCR *jcr,
       }
    }
 
-   xattr_drop_internal_table(xattr_value_list);
-   return bxattr_exit_ok;
+   retval = bxattr_exit_ok;
 
 bail_out:
    xattr_drop_internal_table(xattr_value_list);
-   return bxattr_exit_error;
+
+   return retval;
 }
 
 /*
@@ -1456,7 +1529,7 @@ static const char *xattr_skiplist[1] = {
 static bxattr_exit_code bsd_build_xattr_streams(JCR *jcr, FF_PKT *ff_pkt)
 {
    bool skip_xattr;
-   char *xattr_list;
+   char *xattr_list = NULL;
    int cnt, index, xattr_count = 0;
    int32_t xattr_list_len,
            xattr_value_len;
@@ -1465,10 +1538,9 @@ static bxattr_exit_code bsd_build_xattr_streams(JCR *jcr, FF_PKT *ff_pkt)
    int attrnamespace;
    char *current_attrnamespace = NULL;
    char current_attrname[XATTR_BUFSIZ], current_attrtuple[XATTR_BUFSIZ];
-   xattr_t *current_xattr = NULL;
+   xattr_t *current_xattr;
    alist *xattr_value_list = NULL;
    bxattr_exit_code retval = bxattr_exit_error;
-   berrno be;
 
    /*
     * Loop over all available xattr namespaces.
@@ -1487,7 +1559,9 @@ static bxattr_exit_code bsd_build_xattr_streams(JCR *jcr, FF_PKT *ff_pkt)
        */
       xattr_list_len = extattr_list_link(jcr->last_fname, attrnamespace, NULL, 0);
       switch (xattr_list_len) {
-      case -1:
+      case -1: {
+         berrno be;
+
          switch (errno) {
          case ENOENT:
             retval = bxattr_exit_ok;
@@ -1511,6 +1585,7 @@ static bxattr_exit_code bsd_build_xattr_streams(JCR *jcr, FF_PKT *ff_pkt)
             goto bail_out;
          }
          break;
+      }
       case 0:
          continue;
       default:
@@ -1529,7 +1604,9 @@ static bxattr_exit_code bsd_build_xattr_streams(JCR *jcr, FF_PKT *ff_pkt)
       xattr_list_len = extattr_list_link(jcr->last_fname, attrnamespace,
                                          xattr_list, xattr_list_len);
       switch (xattr_list_len) {
-      case -1:
+      case -1: {
+         berrno be;
+
          switch (errno) {
          case ENOENT:
             retval = bxattr_exit_ok;
@@ -1543,6 +1620,7 @@ static bxattr_exit_code bsd_build_xattr_streams(JCR *jcr, FF_PKT *ff_pkt)
             goto bail_out;
          }
          break;
+      }
       default:
          break;
       }
@@ -1620,30 +1698,15 @@ static bxattr_exit_code bsd_build_xattr_streams(JCR *jcr, FF_PKT *ff_pkt)
             continue;
          }
 
-         /*
-          * Each xattr valuepair starts with a magic so we can parse it easier.
-          */
-         current_xattr = (xattr_t *)malloc(sizeof(xattr_t));
-         current_xattr->magic = XATTR_MAGIC;
-         expected_serialize_len += sizeof(current_xattr->magic);
-
-         /*
-          * Allocate space for storing the name.
-          */
-         current_xattr->name_length = strlen(current_attrtuple);
-         current_xattr->name = (char *)malloc(current_xattr->name_length);
-         memcpy(current_xattr->name, current_attrtuple, current_xattr->name_length);
-
-         expected_serialize_len += sizeof(current_xattr->name_length) +
-                                   current_xattr->name_length;
-
          /*
           * First see how long the value is for the extended attribute.
           */
          xattr_value_len = extattr_get_link(jcr->last_fname, attrnamespace,
                                             current_attrname, NULL, 0);
          switch (xattr_value_len) {
-         case -1:
+         case -1: {
+            berrno be;
+
             switch (errno) {
             case ENOENT:
                retval = bxattr_exit_ok;
@@ -1657,6 +1720,30 @@ static bxattr_exit_code bsd_build_xattr_streams(JCR *jcr, FF_PKT *ff_pkt)
                goto bail_out;
             }
             break;
+         }
+         default:
+            break;
+         }
+
+         /*
+          * Each xattr valuepair starts with a magic so we can parse it easier.
+          */
+         current_xattr = (xattr_t *)malloc(sizeof(xattr_t));
+         current_xattr->magic = XATTR_MAGIC;
+         current_xattr->value = NULL;
+         expected_serialize_len += sizeof(current_xattr->magic);
+
+         /*
+          * Allocate space for storing the name.
+          */
+         current_xattr->name_length = strlen(current_attrtuple);
+         current_xattr->name = (char *)malloc(current_xattr->name_length);
+         memcpy(current_xattr->name, current_attrtuple, current_xattr->name_length);
+
+         expected_serialize_len += sizeof(current_xattr->name_length) +
+                                   current_xattr->name_length;
+
+         switch (xattr_value_len) {
          case 0:
             current_xattr->value = NULL;
             current_xattr->value_length = 0;
@@ -1673,18 +1760,28 @@ static bxattr_exit_code bsd_build_xattr_streams(JCR *jcr, FF_PKT *ff_pkt)
                                                current_attrname, current_xattr->value,
                                                xattr_value_len);
             if (xattr_value_len < 0) {
+               berrno be;
+
                switch (errno) {
                case ENOENT:
                   retval = bxattr_exit_ok;
-                  goto bail_out;
+                  break;
                default:
                   Mmsg2(jcr->errmsg,
                         _("extattr_get_link error on file \"%s\": ERR=%s\n"),
                         jcr->last_fname, be.bstrerror());
                   Dmsg2(100, "extattr_get_link error file=%s ERR=%s\n",
                         jcr->last_fname, be.bstrerror());
-                  goto bail_out;
+                  break;
                }
+
+               /*
+                * Default failure path out when retrieval of attr fails.
+                */
+               free(current_xattr->value);
+               free(current_xattr->name);
+               free(current_xattr);
+               goto bail_out;
             }
 
             /*
@@ -1693,16 +1790,6 @@ static bxattr_exit_code bsd_build_xattr_streams(JCR *jcr, FF_PKT *ff_pkt)
             current_xattr->value_length = xattr_value_len;
             expected_serialize_len += sizeof(current_xattr->value_length) +
                                       current_xattr->value_length;
-
-            /*
-             * Protect ourself against things getting out of hand.
-             */
-            if (expected_serialize_len >= MAX_XATTR_STREAM) {
-               Mmsg2(jcr->errmsg,
-                     _("Xattr stream on file \"%s\" exceeds maximum size of %d bytes\n"),
-                     jcr->last_fname, MAX_XATTR_STREAM);
-               goto bail_out;
-            }
             break;
          }
 
@@ -1711,9 +1798,17 @@ static bxattr_exit_code bsd_build_xattr_streams(JCR *jcr, FF_PKT *ff_pkt)
          }
 
          xattr_value_list->append(current_xattr);
-         current_xattr = NULL;
          xattr_count++;
 
+         /*
+          * Protect ourself against things getting out of hand.
+          */
+         if (expected_serialize_len >= MAX_XATTR_STREAM) {
+            Mmsg2(jcr->errmsg,
+                  _("Xattr stream on file \"%s\" exceeds maximum size of %d bytes\n"),
+                  jcr->last_fname, MAX_XATTR_STREAM);
+            goto bail_out;
+         }
       }
 
       /*
@@ -1759,21 +1854,13 @@ bail_out:
    if (current_attrnamespace != NULL) {
       actuallyfree(current_attrnamespace);
    }
-   if (current_xattr != NULL) {
-      if (current_xattr->value != NULL) {
-         free(current_xattr->value);
-      }
-      if (current_xattr->name != NULL) {
-         free(current_xattr->name);
-      }
-      free(current_xattr);
-   }
    if (xattr_list != NULL) {
       free(xattr_list);
    }
    if (xattr_value_list != NULL) {
       xattr_drop_internal_table(xattr_value_list);
    }
+
    return retval;
 }
 
@@ -1786,7 +1873,7 @@ static bxattr_exit_code bsd_parse_xattr_streams(JCR *jcr,
    alist *xattr_value_list;
    int current_attrnamespace, cnt;
    char *attrnamespace, *attrname;
-   berrno be;
+   bxattr_exit_code retval = bxattr_exit_error;
 
    xattr_value_list = New(alist(10, not_owned_by_alist));
 
@@ -1794,8 +1881,7 @@ static bxattr_exit_code bsd_parse_xattr_streams(JCR *jcr,
                                 content,
                                 content_length,
                                 xattr_value_list) != bxattr_exit_ok) {
-      xattr_drop_internal_table(xattr_value_list);
-      return bxattr_exit_error;
+      goto bail_out;
    }
 
    foreach_alist(current_xattr, xattr_value_list) {
@@ -1832,6 +1918,8 @@ static bxattr_exit_code bsd_parse_xattr_streams(JCR *jcr,
       cnt = extattr_set_link(jcr->last_fname, current_attrnamespace,
                              attrname, current_xattr->value, current_xattr->value_length);
       if (cnt < 0 || cnt != (int)current_xattr->value_length) {
+         berrno be;
+
          switch (errno) {
          case ENOENT:
             goto bail_out;
@@ -1848,12 +1936,12 @@ static bxattr_exit_code bsd_parse_xattr_streams(JCR *jcr,
       }
    }
 
-   xattr_drop_internal_table(xattr_value_list);
-   return bxattr_exit_ok;
+   retval = bxattr_exit_ok;
 
 bail_out:
    xattr_drop_internal_table(xattr_value_list);
-   return bxattr_exit_error;
+
+   return retval;
 }
 
 /*
@@ -1909,12 +1997,11 @@ static bxattr_exit_code tru64_build_xattr_streams(JCR *jcr, FF_PKT *ff_pkt)
            xattrbuf_size,
            xattrbuf_min_size;
    uint32_t expected_serialize_len = 0;
-   xattr_t *current_xattr = NULL;
+   xattr_t *current_xattr;
    alist *xattr_value_list = NULL;
    struct proplistname_args prop_args;
    bxattr_exit_code retval = bxattr_exit_error;
    POOLMEM *xattrbuf = get_pool_memory(PM_MESSAGE);
-   berrno be;
 
    xattrbuf_size = sizeof_pool_memory(xattrbuf);
    xattrbuf_min_size = 0;
@@ -1925,7 +2012,9 @@ static bxattr_exit_code tru64_build_xattr_streams(JCR *jcr, FF_PKT *ff_pkt)
     * See what xattr are available.
     */
    switch (xattr_list_len) {
-   case -1:
+   case -1: {
+      berrno be;
+
       switch (errno) {
       case EOPNOTSUPP:
          /*
@@ -1947,6 +2036,7 @@ static bxattr_exit_code tru64_build_xattr_streams(JCR *jcr, FF_PKT *ff_pkt)
          goto bail_out;
       }
       break;
+   }
    case 0:
       if (xattrbuf_min_size) {
          /*
@@ -1958,7 +2048,9 @@ static bxattr_exit_code tru64_build_xattr_streams(JCR *jcr, FF_PKT *ff_pkt)
          xattr_list_len = getproplist(jcr->last_fname, 1, &prop_args, xattrbuf_size,
                                    xattrbuf, &xattrbuf_min_size);
          switch (xattr_list_len) {
-         case -1:
+         case -1: {
+            berrno be;
+
             switch (errno) {
             default:
                Mmsg2(jcr->errmsg,
@@ -1969,6 +2061,7 @@ static bxattr_exit_code tru64_build_xattr_streams(JCR *jcr, FF_PKT *ff_pkt)
                goto bail_out;
             }
             break;
+         }
          case 0:
             /*
              * This should never happen as we sized the buffer according to the minimumsize
@@ -2056,6 +2149,13 @@ static bxattr_exit_code tru64_build_xattr_streams(JCR *jcr, FF_PKT *ff_pkt)
       expected_serialize_len += sizeof(current_xattr->value_length) +
                                 current_xattr->value_length;
 
+      if (xattr_value_list == NULL) {
+         xattr_value_list = New(alist(10, not_owned_by_alist));
+      }
+
+      xattr_value_list->append(current_xattr);
+      xattr_count++;
+
       /*
        * Protect ourself against things getting out of hand.
        */
@@ -2065,14 +2165,6 @@ static bxattr_exit_code tru64_build_xattr_streams(JCR *jcr, FF_PKT *ff_pkt)
                jcr->last_fname, MAX_XATTR_STREAM);
          goto bail_out;
       }
-
-      if (xattr_value_list == NULL) {
-         xattr_value_list = New(alist(10, not_owned_by_alist));
-      }
-
-      xattr_value_list->append(current_xattr);
-      current_xattr = NULL;
-      xattr_count++;
    }
 
    /*
@@ -2102,15 +2194,6 @@ static bxattr_exit_code tru64_build_xattr_streams(JCR *jcr, FF_PKT *ff_pkt)
    }
 
 bail_out:
-   if (current_xattr != NULL) {
-      if (current_xattr->value != NULL) {
-         free(current_xattr->value);
-      }
-      if (current_xattr->name != NULL) {
-         free(current_xattr->name);
-      }
-      free(current_xattr);
-   }
    if (xattr_value_list != NULL) {
       xattr_drop_internal_table(xattr_value_list);
    }
@@ -2129,7 +2212,6 @@ static bxattr_exit_code tru64_parse_xattr_streams(JCR *jcr,
    xattr_t *current_xattr;
    alist *xattr_value_list;
    bxattr_exit_code retval = bxattr_exit_error;
-   berrno be;
 
    xattr_value_list = New(alist(10, not_owned_by_alist));
 
@@ -2137,8 +2219,7 @@ static bxattr_exit_code tru64_parse_xattr_streams(JCR *jcr,
                                 content,
                                 content_length,
                                 xattr_value_list) != bxattr_exit_ok) {
-      xattr_drop_internal_table(xattr_value_list);
-      return bxattr_exit_error;
+      goto bail_out;
    }
 
    /*
@@ -2178,7 +2259,9 @@ static bxattr_exit_code tru64_parse_xattr_streams(JCR *jcr,
     */
    cnt = setproplist(jcr->last_fname, 1, xattrbuf_size, xattrbuf);
    switch (cnt) {
-   case -1:
+   case -1: {
+      berrno be;
+
       switch (errno) {
       case EOPNOTSUPP:
          /*
@@ -2200,21 +2283,20 @@ static bxattr_exit_code tru64_parse_xattr_streams(JCR *jcr,
          goto bail_out;
       }
       break;
+   }
    default:
       break;
    }
 
-   free(xattrbuf);
-
-   xattr_drop_internal_table(xattr_value_list);
-   return bxattr_exit_ok;
+   retval = bxattr_exit_ok;
 
 bail_out:
    if (xattrbuf) {
       free(xattrbuf);
    }
    xattr_drop_internal_table(xattr_value_list);
-   return bxattr_exit_error;
+
+   return retval;
 }
 
 /*
@@ -2348,7 +2430,7 @@ static int os_default_xattr_streams[1] = {
  * This code creates a temporary cache with entries for each xattr which has
  * a link count > 1 (which indicates it has one or more hard linked counterpart(s))
  */
-static xattr_link_cache_entry_t *find_xattr_link_cache_entry(JCR *jcr, ino_t inum)
+static inline xattr_link_cache_entry_t *find_xattr_link_cache_entry(JCR *jcr, ino_t inum)
 {
    xattr_link_cache_entry_t *ptr;
 
@@ -2360,14 +2442,14 @@ static xattr_link_cache_entry_t *find_xattr_link_cache_entry(JCR *jcr, ino_t inu
    return NULL;
 }
 
-static void add_xattr_link_cache_entry(JCR *jcr, ino_t inum, char *target)
+static inline void add_xattr_link_cache_entry(JCR *jcr, ino_t inum, char *target)
 {
    xattr_link_cache_entry_t *ptr;
 
    ptr = (xattr_link_cache_entry_t *)malloc(sizeof(xattr_link_cache_entry_t));
    memset(ptr, 0, sizeof(xattr_link_cache_entry_t));
    ptr->inum = inum;
-   bstrncpy(ptr->target, target, sizeof(ptr->target));
+   ptr->target = bstrdup(target);
 
    if (!jcr->xattr_data->u.build->link_cache) {
       jcr->xattr_data->u.build->link_cache = New(alist(10, not_owned_by_alist));
@@ -2375,6 +2457,22 @@ static void add_xattr_link_cache_entry(JCR *jcr, ino_t inum, char *target)
    jcr->xattr_data->u.build->link_cache->append(ptr);
 }
 
+static inline void drop_xattr_link_cache(JCR *jcr)
+{
+   xattr_link_cache_entry_t *ptr;
+
+   /*
+    * Walk the list of xattr link cache entries and free allocated memory on traversing.
+    */
+   foreach_alist(ptr, jcr->xattr_data->u.build->link_cache) {
+      free(ptr->target);
+      free(ptr);
+   }
+
+   delete jcr->xattr_data->u.build->link_cache;
+   jcr->xattr_data->u.build->link_cache = NULL;
+}
+
 #if defined(HAVE_SYS_NVPAIR_H) && defined(_PC_SATTR_ENABLED)
 /*
  * This function returns true if a non default extended system attribute
@@ -2472,11 +2570,11 @@ static bool acl_is_trivial(int count, aclent_t *entries)
 
 static bxattr_exit_code solaris_save_xattr_acl(JCR *jcr, int fd, const char *attrname, char **acl_text)
 {
+   bxattr_exit_code retval = bxattr_exit_error;
 #ifdef HAVE_ACL
 #ifdef HAVE_EXTENDED_ACL
    int flags;
    acl_t *aclp = NULL;
-   berrno be;
 
    /*
     * See if this attribute has an ACL
@@ -2488,16 +2586,19 @@ static bxattr_exit_code solaris_save_xattr_acl(JCR *jcr, int fd, const char *att
        */
       if ((fd != -1 && facl_get(fd, ACL_NO_TRIVIAL, &aclp) != 0) ||
            acl_get(attrname, ACL_NO_TRIVIAL, &aclp) != 0) {
+         berrno be;
+
          switch (errno) {
          case ENOENT:
-            return bxattr_exit_ok;
+            retval = bxattr_exit_ok;
+            goto bail_out;
          default:
             Mmsg3(jcr->errmsg,
                   _("Unable to get acl on xattr %s on file \"%s\": ERR=%s\n"),
                   attrname, jcr->last_fname, be.bstrerror());
             Dmsg3(100, "facl_get/acl_get of xattr %s on \"%s\" failed: ERR=%s\n",
                   attrname, jcr->last_fname, be.bstrerror());
-            return bxattr_exit_error;
+            goto bail_out;
          }
       }
 
@@ -2519,11 +2620,10 @@ static bxattr_exit_code solaris_save_xattr_acl(JCR *jcr, int fd, const char *att
    } else {
       *acl_text = NULL;
    }
-   return bxattr_exit_ok;
+   retval = bxattr_exit_ok;
 #else /* HAVE_EXTENDED_ACL */
    int n;
    aclent_t *acls = NULL;
-   berrno be;
 
    /*
     * See if this attribute has an ACL
@@ -2538,10 +2638,13 @@ static bxattr_exit_code solaris_save_xattr_acl(JCR *jcr, int fd, const char *att
       acls = (aclent_t *)malloc(n * sizeof(aclent_t));
       if ((fd != -1 && facl(fd, GETACL, n, acls) != n) ||
           acl(attrname, GETACL, n, acls) != n) {
+         berrno be;
+
          switch (errno) {
          case ENOENT:
             free(acls);
-            return bxattr_exit_ok;
+            retval = bxattr_exit_ok;
+            goto bail_out;
          default:
             Mmsg3(jcr->errmsg,
                   _("Unable to get acl on xattr %s on file \"%s\": ERR=%s\n"),
@@ -2549,7 +2652,7 @@ static bxattr_exit_code solaris_save_xattr_acl(JCR *jcr, int fd, const char *att
             Dmsg3(100, "facl/acl of xattr %s on \"%s\" failed: ERR=%s\n",
                   attrname, jcr->last_fname, be.bstrerror());
             free(acls);
-            return bxattr_exit_error;
+            goto bail_out;
          }
       }
 
@@ -2558,13 +2661,15 @@ static bxattr_exit_code solaris_save_xattr_acl(JCR *jcr, int fd, const char *att
        */
       if (!acl_is_trivial(n, acls)) {
          if ((*acl_text = acltotext(acls, n)) == NULL) {
+            berrno be;
+
             Mmsg3(jcr->errmsg,
                   _("Unable to get acl text on xattr %s on file \"%s\": ERR=%s\n"),
                   attrname, jcr->last_fname, be.bstrerror());
             Dmsg3(100, "acltotext of xattr %s on \"%s\" failed: ERR=%s\n",
                   attrname, jcr->last_fname, be.bstrerror());
             free(acls);
-            return bxattr_exit_error;
+            goto bail_out;
          }
       } else {
          *acl_text = NULL;
@@ -2574,12 +2679,15 @@ static bxattr_exit_code solaris_save_xattr_acl(JCR *jcr, int fd, const char *att
    } else {
       *acl_text = NULL;
    }
-   return bxattr_exit_ok;
+   retval = bxattr_exit_ok;
 #endif /* HAVE_EXTENDED_ACL */
 
 #else /* HAVE_ACL */
-   return bxattr_exit_ok;
+   retval = bxattr_exit_ok;
 #endif /* HAVE_ACL */
+
+bail_out:
+   return retval;
 }
 
 /*
@@ -2615,7 +2723,6 @@ static bxattr_exit_code solaris_save_xattr(JCR *jcr, int fd, const char *xattr_n
    char attribs[MAXSTRING];
    char buffer[XATTR_BUFSIZ];
    bxattr_exit_code retval = bxattr_exit_error;
-   berrno be;
 
    bsnprintf(target_attrname, sizeof(target_attrname), "%s%s", xattr_namespace, attrname);
 
@@ -2623,6 +2730,8 @@ static bxattr_exit_code solaris_save_xattr(JCR *jcr, int fd, const char *xattr_n
     * Get the stats of the extended or extensible attribute.
     */
    if (fstatat(fd, attrname, &st, AT_SYMLINK_NOFOLLOW) < 0) {
+      berrno be;
+
       switch (errno) {
       case ENOENT:
          retval = bxattr_exit_ok;
@@ -2754,6 +2863,8 @@ static bxattr_exit_code solaris_save_xattr(JCR *jcr, int fd, const char *xattr_n
        * Open the extended or extensible attribute file.
        */
       if ((attrfd = openat(fd, attrname, O_RDONLY)) < 0) {
+         berrno be;
+
          switch (errno) {
          case ENOENT:
             retval = bxattr_exit_ok;
@@ -2775,6 +2886,8 @@ static bxattr_exit_code solaris_save_xattr(JCR *jcr, int fd, const char *xattr_n
        * Encode the stat struct into an ASCII representation.
        */
       if (readlink(attrname, link_source, sizeof(link_source)) < 0) {
+         berrno be;
+
          switch (errno) {
          case ENOENT:
             retval = bxattr_exit_ok;
@@ -2888,6 +3001,8 @@ static bxattr_exit_code solaris_save_xattr(JCR *jcr, int fd, const char *xattr_n
     * The recursive call could change our working dir so change back to the wanted workdir.
     */
    if (fchdir(fd) < 0) {
+      berrno be;
+
       switch (errno) {
       case ENOENT:
          retval = bxattr_exit_ok;
@@ -2920,7 +3035,6 @@ static bxattr_exit_code solaris_save_xattrs(JCR *jcr, const char *xattr_namespac
    struct dirent *dp;
    char current_xattr_namespace[PATH_MAX];
    bxattr_exit_code retval = bxattr_exit_error;
-   berrno be;
  
    /*
     * Determine what argument to use. Use attr_parent when set
@@ -2944,6 +3058,8 @@ static bxattr_exit_code solaris_save_xattrs(JCR *jcr, const char *xattr_namespac
     * Open the file on which to save the xattrs read-only.
     */
    if ((filefd = open(name, O_RDONLY | O_NONBLOCK)) < 0) {
+      berrno be;
+
       switch (errno) {
       case ENOENT:
          retval = bxattr_exit_ok;
@@ -2962,6 +3078,8 @@ static bxattr_exit_code solaris_save_xattrs(JCR *jcr, const char *xattr_namespac
     * Open the xattr naming space.
     */
    if ((attrdirfd = openat(filefd, ".", O_RDONLY | O_XATTR)) < 0) {
+      berrno be;
+
       switch (errno) {
       case EINVAL:
          /*
@@ -2989,6 +3107,8 @@ static bxattr_exit_code solaris_save_xattrs(JCR *jcr, const char *xattr_namespac
    * attributes should be saved.
    */
    if (fchdir(attrdirfd) < 0) {
+      berrno be;
+
       Mmsg2(jcr->errmsg,
             _("Unable to chdir to xattr space on file \"%s\": ERR=%s\n"),
             jcr->last_fname, be.bstrerror());
@@ -3008,6 +3128,8 @@ static bxattr_exit_code solaris_save_xattrs(JCR *jcr, const char *xattr_namespac
 
    if ((fd = dup(attrdirfd)) == -1 ||
        (dirp = fdopendir(fd)) == (DIR *)NULL) {
+      berrno be;
+
       Mmsg2(jcr->errmsg,
             _("Unable to list the xattr space on file \"%s\": ERR=%s\n"),
             jcr->last_fname, be.bstrerror());
@@ -3098,7 +3220,6 @@ static bxattr_exit_code solaris_restore_xattr_acl(JCR *jcr,
 #ifdef HAVE_EXTENDED_ACL
    int error;
    acl_t *aclp = NULL;
-   berrno be;
 
    if ((error = acl_fromtext(acl_text, &aclp)) != 0) {
       Mmsg1(jcr->errmsg,
@@ -3109,6 +3230,8 @@ static bxattr_exit_code solaris_restore_xattr_acl(JCR *jcr,
 
    if ((fd != -1 && facl_set(fd, aclp) != 0) ||
         acl_set(attrname, aclp) != 0) {
+      berrno be;
+
       Mmsg3(jcr->errmsg,
             _("Unable to restore acl of xattr %s on file \"%s\": ERR=%s\n"),
             attrname, jcr->last_fname, be.bstrerror());
@@ -3125,12 +3248,13 @@ static bxattr_exit_code solaris_restore_xattr_acl(JCR *jcr,
 #else /* HAVE_EXTENDED_ACL */
    int n;
    aclent_t *acls = NULL;
-   berrno be;
 
    acls = aclfromtext(acl_text, &n);
    if (!acls) {
       if ((fd != -1 && facl(fd, SETACL, n, acls) != 0) ||
            acl(attrname, SETACL, n, acls) != 0) {
+         berrno be;
+
          Mmsg3(jcr->errmsg,
                _("Unable to restore acl of xattr %s on file \"%s\": ERR=%s\n"),
                attrname, jcr->last_fname, be.bstrerror());
@@ -3166,7 +3290,6 @@ static bxattr_exit_code solaris_restore_xattrs(JCR *jcr,
    struct stat st;
    struct timeval times[2];
    bxattr_exit_code retval = bxattr_exit_error;
-   berrno be;
 
    /*
     * Parse the xattr stream. First the part that is the same for all xattrs.
@@ -3189,6 +3312,8 @@ static bxattr_exit_code solaris_restore_xattrs(JCR *jcr,
     * Open the file on which to restore the xattrs read-only.
     */
    if ((filefd = open(jcr->last_fname, O_RDONLY | O_NONBLOCK)) < 0) {
+      berrno be;
+
       Mmsg2(jcr->errmsg,
             _("Unable to open file \"%s\": ERR=%s\n"),
             jcr->last_fname, be.bstrerror());
@@ -3201,6 +3326,8 @@ static bxattr_exit_code solaris_restore_xattrs(JCR *jcr,
     * Open the xattr naming space and make it the current working dir.
     */
    if ((attrdirfd = openat(filefd, ".", O_RDONLY | O_XATTR)) < 0) {
+      berrno be;
+
       Mmsg2(jcr->errmsg,
             _("Unable to open xattr space on file \"%s\": ERR=%s\n"),
             jcr->last_fname, be.bstrerror());
@@ -3210,6 +3337,8 @@ static bxattr_exit_code solaris_restore_xattrs(JCR *jcr,
    }
 
    if (fchdir(attrdirfd) < 0) {
+      berrno be;
+
       Mmsg2(jcr->errmsg,
             _("Unable to chdir to xattr space on file \"%s\": ERR=%s\n"),
             jcr->last_fname, be.bstrerror());
@@ -3227,6 +3356,8 @@ static bxattr_exit_code solaris_restore_xattrs(JCR *jcr,
       *bp = '\0';
 
       if ((fd = open(target_attrname, O_RDONLY | O_NONBLOCK)) < 0) {
+         berrno be;
+
          Mmsg3(jcr->errmsg,
                _("Unable to open xattr %s on file \"%s\": ERR=%s\n"),
                target_attrname, jcr->last_fname, be.bstrerror());
@@ -3242,6 +3373,8 @@ static bxattr_exit_code solaris_restore_xattrs(JCR *jcr,
        * Open the xattr naming space.
        */
       if ((fd = openat(filefd, ".", O_RDONLY | O_XATTR)) < 0) {
+         berrno be;
+
          Mmsg3(jcr->errmsg,
                _("Unable to open xattr space %s on file \"%s\": ERR=%s\n"),
                target_attrname, jcr->last_fname, be.bstrerror());
@@ -3257,6 +3390,8 @@ static bxattr_exit_code solaris_restore_xattrs(JCR *jcr,
        * Make the xattr space our current workingdir.
        */
       if (fchdir(attrdirfd) < 0) {
+         berrno be;
+
          Mmsg3(jcr->errmsg,
                _("Unable to chdir to xattr space %s on file \"%s\": ERR=%s\n"),
                target_attrname, jcr->last_fname, be.bstrerror());
@@ -3295,6 +3430,8 @@ static bxattr_exit_code solaris_restore_xattrs(JCR *jcr,
        */
       unlinkat(attrdirfd, target_attrname, 0);
       if (mkfifo(target_attrname, st.st_mode) < 0) {
+         berrno be;
+
          Mmsg3(jcr->errmsg,
                _("Unable to mkfifo xattr %s on file \"%s\": ERR=%s\n"),
                target_attrname, jcr->last_fname, be.bstrerror());
@@ -3311,6 +3448,8 @@ static bxattr_exit_code solaris_restore_xattrs(JCR *jcr,
        */
       unlinkat(attrdirfd, target_attrname, 0);
       if (mknod(target_attrname, st.st_mode, st.st_rdev) < 0) {
+         berrno be;
+
          Mmsg3(jcr->errmsg,
                _("Unable to mknod xattr %s on file \"%s\": ERR=%s\n"),
                target_attrname, jcr->last_fname, be.bstrerror());
@@ -3328,6 +3467,8 @@ static bxattr_exit_code solaris_restore_xattrs(JCR *jcr,
       if (!bstrcmp(target_attrname, ".")) {
          unlinkat(attrdirfd, target_attrname, AT_REMOVEDIR);
          if (mkdir(target_attrname, st.st_mode) < 0) {
+            berrno be;
+
             Jmsg3(jcr, M_WARNING, 0, _("Unable to mkdir xattr %s on file \"%s\": ERR=%s\n"),
                target_attrname, jcr->last_fname, be.bstrerror());
             Dmsg3(100, "Unable to mkdir xattr %s on file \"%s\": ERR=%s\n",
@@ -3345,6 +3486,8 @@ static bxattr_exit_code solaris_restore_xattrs(JCR *jcr,
 
          unlinkat(attrdirfd, target_attrname, 0);
          if (link(linked_target, target_attrname) < 0) {
+            berrno be;
+
             Mmsg4(jcr->errmsg,
                   _("Unable to link xattr %s to %s on file \"%s\": ERR=%s\n"),
                   target_attrname, linked_target, jcr->last_fname, be.bstrerror());
@@ -3375,6 +3518,8 @@ static bxattr_exit_code solaris_restore_xattrs(JCR *jcr,
          }
 
          if ((attrfd = openat(attrdirfd, target_attrname, O_RDWR | O_CREAT | O_TRUNC, st.st_mode)) < 0) {
+            berrno be;
+
             Mmsg3(jcr->errmsg,
                   _("Unable to open xattr %s on file \"%s\": ERR=%s\n"),
                   target_attrname, jcr->last_fname, be.bstrerror());
@@ -3407,6 +3552,8 @@ static bxattr_exit_code solaris_restore_xattrs(JCR *jcr,
          while (cnt > 0) {
             cnt = write(attrfd, data, cnt);
             if (cnt < 0) {
+               berrno be;
+
                Mmsg3(jcr->errmsg,
                      _("Unable to restore data of xattr %s on file \"%s\": ERR=%s\n"),
                      target_attrname, jcr->last_fname, be.bstrerror());
@@ -3428,6 +3575,8 @@ static bxattr_exit_code solaris_restore_xattrs(JCR *jcr,
       linked_target = bp;
 
       if (symlink(linked_target, target_attrname) < 0) {
+         berrno be;
+
          Mmsg4(jcr->errmsg,
                _("Unable to symlink xattr %s to %s on file \"%s\": ERR=%s\n"),
                target_attrname, linked_target, jcr->last_fname, be.bstrerror());
@@ -3450,6 +3599,8 @@ static bxattr_exit_code solaris_restore_xattrs(JCR *jcr,
     */
    if (!is_extensible) {
       if (fchownat(attrdirfd, target_attrname, st.st_uid, st.st_gid, AT_SYMLINK_NOFOLLOW) < 0) {
+         berrno be;
+
          switch (errno) {
          case EINVAL:
             /*
@@ -3488,6 +3639,8 @@ static bxattr_exit_code solaris_restore_xattrs(JCR *jcr,
       times[1].tv_usec = 0;
 
       if (futimesat(attrdirfd, target_attrname, times) < 0) {
+         berrno be;
+
          Mmsg3(jcr->errmsg,
                _("Unable to restore filetimes of xattr %s on file \"%s\": ERR=%s\n"),
                target_attrname, jcr->last_fname, be.bstrerror());
@@ -3543,8 +3696,7 @@ static bxattr_exit_code solaris_build_xattr_streams(JCR *jcr, FF_PKT *ff_pkt)
       retval = solaris_save_xattrs(jcr, NULL, NULL);
       chdir(cwd);
       if (jcr->xattr_data->u.build->link_cache) {
-         delete jcr->xattr_data->u.build->link_cache;
-         jcr->xattr_data->u.build->link_cache = NULL;
+         drop_xattr_link_cache(jcr);
       }
    }
    return retval;
@@ -3557,7 +3709,7 @@ static bxattr_exit_code solaris_parse_xattr_streams(JCR *jcr,
 {
    char cwd[PATH_MAX];
    bool is_extensible = false;
-   bxattr_exit_code retval;
+   bxattr_exit_code retval = bxattr_exit_error;
 
    /*
     * First make sure we can restore xattr on the filesystem.
@@ -3571,7 +3723,7 @@ static bxattr_exit_code solaris_parse_xattr_streams(JCR *jcr,
          jcr->last_fname);
          Dmsg1(100, "Unable to restore extensible attributes on file \"%s\", filesystem doesn't support this\n",
             jcr->last_fname);
-         return bxattr_exit_error;
+         goto bail_out;
       }
 
       is_extensible = true;
@@ -3584,11 +3736,11 @@ static bxattr_exit_code solaris_parse_xattr_streams(JCR *jcr,
                jcr->last_fname);
          Dmsg1(100, "Unable to restore extended attributes on file \"%s\", filesystem doesn't support this\n",
             jcr->last_fname);
-         return bxattr_exit_error;
+         goto bail_out;
       }
       break;
    default:
-      return bxattr_exit_error;
+      goto bail_out;
    }
 
    /*
@@ -3598,6 +3750,8 @@ static bxattr_exit_code solaris_parse_xattr_streams(JCR *jcr,
    getcwd(cwd, sizeof(cwd));
    retval = solaris_restore_xattrs(jcr, is_extensible, content, content_length);
    chdir(cwd);
+
+bail_out:
    return retval;
 }
 
@@ -3651,9 +3805,9 @@ bxattr_exit_code parse_xattr_streams(JCR *jcr,
                                      uint32_t content_length)
 {
    int ret;
-   berrno be;
    struct stat st;
    unsigned int cnt;
+   bxattr_exit_code retval = bxattr_exit_error;
 
    /*
     * See if we are changing from one device to an other.
@@ -3663,19 +3817,23 @@ bxattr_exit_code parse_xattr_streams(JCR *jcr,
     */
    ret = lstat(jcr->last_fname, &st);
    switch (ret) {
-   case -1:
+   case -1: {
+      berrno be;
+
       switch (errno) {
       case ENOENT:
-         return bxattr_exit_ok;
+         retval = bxattr_exit_ok;
+         goto bail_out;
       default:
          Mmsg2(jcr->errmsg,
                _("Unable to stat file \"%s\": ERR=%s\n"),
                jcr->last_fname, be.bstrerror());
          Dmsg2(100, "Unable to stat file \"%s\": ERR=%s\n",
                jcr->last_fname, be.bstrerror());
-         return bxattr_exit_error;
+         goto bail_out;
       }
       break;
+   }
    case 0:
       break;
    }
@@ -3701,7 +3859,8 @@ bxattr_exit_code parse_xattr_streams(JCR *jcr,
        */
       for (cnt = 0; cnt < sizeof(os_default_xattr_streams) / sizeof(int); cnt++) {
          if (os_default_xattr_streams[cnt] == stream) {
-            return os_parse_xattr_streams(jcr, stream, content, content_length);
+            retval = os_parse_xattr_streams(jcr, stream, content, content_length);
+            goto bail_out;
          }
       }
    } else {
@@ -3709,15 +3868,18 @@ bxattr_exit_code parse_xattr_streams(JCR *jcr,
        * Increment error count but don't log an error again for the same filesystem.
        */
       jcr->xattr_data->u.parse->nr_errors++;
-      return bxattr_exit_ok;
+      retval = bxattr_exit_ok;
+      goto bail_out;
    }
 
    /*
     * Issue a warning and discard the message. But pretend the restore was ok.
     */
    Jmsg2(jcr, M_WARNING, 0,
-      _("Can't restore Extended Attributes of %s - incompatible xattr stream encountered - %d\n"),
-      jcr->last_fname, stream);
-   return bxattr_exit_error;
+         _("Can't restore Extended Attributes of %s - incompatible xattr stream encountered - %d\n"),
+         jcr->last_fname, stream);
+
+bail_out:
+   return retval;
 }
 #endif