bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

acl: refactor error messages in copy-acl.c


From: Bruno Haible
Subject: acl: refactor error messages in copy-acl.c
Date: Sun, 1 Jun 2008 23:51:28 +0200
User-agent: KMail/1.5.4

Hi Jim,

After adding support for all platforms, copy-acl.c contains 7 times the error
message regarding the source file, and 11 times the error message regarding
the destination file. This begs for refactoring. Doing this refactoring
ahead of the other changes will make these changes simpler.

OK?


2008-06-01  Bruno Haible  <address@hidden>

        * lib/copy-acl.c (qcopy_acl): New function, extracted from copy_acl.
        (copy_acl): Just a wrapper around qcopy_acl that emits the error
        messages.

*** lib/copy-acl.c.orig 2008-06-01 23:49:05.000000000 +0200
--- lib/copy-acl.c      2008-06-01 23:45:34.000000000 +0200
***************
*** 23,28 ****
--- 23,29 ----
  
  #include "acl-internal.h"
  
+ 
  /* Copy access control lists from one file to another. If SOURCE_DESC is
     a valid file descriptor, use file descriptor operations, else use
     filename based operations on SRC_NAME. Likewise for DEST_DESC and
***************
*** 30,40 ****
     If access control lists are not available, fchmod the target file to
     MODE.  Also sets the non-permission bits of the destination file
     (S_ISUID, S_ISGID, S_ISVTX) to those from MODE if any are set.
!    Return 0 if successful, otherwise output a diagnostic and return -1.  */
! 
! int
! copy_acl (const char *src_name, int source_desc, const char *dst_name,
!         int dest_desc, mode_t mode)
  {
    int ret;
  
--- 31,43 ----
     If access control lists are not available, fchmod the target file to
     MODE.  Also sets the non-permission bits of the destination file
     (S_ISUID, S_ISGID, S_ISVTX) to those from MODE if any are set.
!    Return 0 if successful.
!    Return -2 and set errno for an error relating to the source file.
!    Return -1 and set errno for an error relating to the destination file.  */
! 
! static int
! qcopy_acl (const char *src_name, int source_desc, const char *dst_name,
!          int dest_desc, mode_t mode)
  {
    int ret;
  
***************
*** 52,61 ****
        if (ACL_NOT_WELL_SUPPORTED (errno))
        return set_acl (dst_name, dest_desc, mode);
        else
!         {
!         error (0, errno, "%s", quote (src_name));
!         return -1;
!       }
      }
  
    if (HAVE_ACL_SET_FD && dest_desc != -1)
--- 55,61 ----
        if (ACL_NOT_WELL_SUPPORTED (errno))
        return set_acl (dst_name, dest_desc, mode);
        else
!         return -2;
      }
  
    if (HAVE_ACL_SET_FD && dest_desc != -1)
***************
*** 92,99 ****
          acl_free (acl);
          chmod_or_fchmod (dst_name, dest_desc, mode);
        }
!       error (0, saved_errno, _("preserving permissions for %s"),
!            quote (dst_name));
        return -1;
      }
    else
--- 92,98 ----
          acl_free (acl);
          chmod_or_fchmod (dst_name, dest_desc, mode);
        }
!       errno = saved_errno;
        return -1;
      }
    else
***************
*** 105,131 ****
         separate or special bits are to be set which don't fit into ACLs.  */
  
        if (chmod_or_fchmod (dst_name, dest_desc, mode) != 0)
!       {
!         error (0, errno, _("preserving permissions for %s"),
!                quote (dst_name));
!         return -1;
!       }
      }
  
    if (S_ISDIR (mode))
      {
        acl = acl_get_file (src_name, ACL_TYPE_DEFAULT);
        if (acl == NULL)
!       {
!         error (0, errno, "%s", quote (src_name));
!         return -1;
!       }
  
        if (acl_set_file (dst_name, ACL_TYPE_DEFAULT, acl))
        {
!         error (0, errno, _("preserving permissions for %s"),
!                quote (dst_name));
          acl_free (acl);
          return -1;
        }
        else
--- 104,124 ----
         separate or special bits are to be set which don't fit into ACLs.  */
  
        if (chmod_or_fchmod (dst_name, dest_desc, mode) != 0)
!       return -1;
      }
  
    if (S_ISDIR (mode))
      {
        acl = acl_get_file (src_name, ACL_TYPE_DEFAULT);
        if (acl == NULL)
!       return -2;
  
        if (acl_set_file (dst_name, ACL_TYPE_DEFAULT, acl))
        {
!         int saved_errno = errno;
! 
          acl_free (acl);
+         errno = saved_errno;
          return -1;
        }
        else
***************
*** 142,169 ****
         ? acl_get (src_name, ACL_NO_TRIVIAL, &aclp)
         : facl_get (source_desc, ACL_NO_TRIVIAL, &aclp));
    if (ret != 0 && errno != ENOSYS)
!     {
!       error (0, errno, "%s", quote (src_name));
!       return ret;
!     }
  # endif
  
    ret = qset_acl (dst_name, dest_desc, mode);
    if (ret != 0)
!     error (0, errno, _("preserving permissions for %s"), quote (dst_name));
  
  # if USE_ACL && defined ACL_NO_TRIVIAL
!   if (ret == 0 && aclp)
      {
        ret = (dest_desc < 0
             ? acl_set (dst_name, aclp)
             : facl_set (dest_desc, aclp));
        if (ret != 0)
!       error (0, errno, _("preserving permissions for %s"), quote (dst_name));
        acl_free (aclp);
      }
  # endif
  
!   return ret;
  #endif
  }
--- 135,195 ----
         ? acl_get (src_name, ACL_NO_TRIVIAL, &aclp)
         : facl_get (source_desc, ACL_NO_TRIVIAL, &aclp));
    if (ret != 0 && errno != ENOSYS)
!     return -2;
  # endif
  
    ret = qset_acl (dst_name, dest_desc, mode);
    if (ret != 0)
!     return -1;
  
  # if USE_ACL && defined ACL_NO_TRIVIAL
!   if (aclp)
      {
        ret = (dest_desc < 0
             ? acl_set (dst_name, aclp)
             : facl_set (dest_desc, aclp));
        if (ret != 0)
!       {
!         int saved_errno = errno;
! 
!         acl_free (aclp);
!         errno = saved_errno;
!         return -1;
!       }
        acl_free (aclp);
      }
  # endif
  
!   return 0;
  #endif
  }
+ 
+ 
+ /* Copy access control lists from one file to another. If SOURCE_DESC is
+    a valid file descriptor, use file descriptor operations, else use
+    filename based operations on SRC_NAME. Likewise for DEST_DESC and
+    DST_NAME.
+    If access control lists are not available, fchmod the target file to
+    MODE.  Also sets the non-permission bits of the destination file
+    (S_ISUID, S_ISGID, S_ISVTX) to those from MODE if any are set.
+    Return 0 if successful, otherwise output a diagnostic and return -1.  */
+ 
+ int
+ copy_acl (const char *src_name, int source_desc, const char *dst_name,
+         int dest_desc, mode_t mode)
+ {
+   int ret = qcopy_acl (src_name, source_desc, dst_name, dest_desc, mode);
+   switch (ret)
+     {
+     case -2:
+       error (0, errno, "%s", quote (src_name));
+       return -1;
+ 
+     case -1:
+       error (0, errno, _("preserving permissions for %s"), quote (dst_name));
+       return -1;
+ 
+     default:
+       return 0;
+     }
+ }





reply via email to

[Prev in Thread] Current Thread [Next in Thread]