Merge commit 'stable/20110609'
[gnulib.git] / lib / copy-acl.c
index 1e82240..f0dc116 100644 (file)
@@ -1,6 +1,6 @@
 /* copy-acl.c - copy access control list from one file to another file
 
-   Copyright (C) 2002-2003, 2005-2010 Free Software Foundation, Inc.
+   Copyright (C) 2002-2003, 2005-2011 Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -294,7 +294,7 @@ qcopy_acl (const char *src_name, int source_desc, const char *dst_name,
 
       if (count < 0)
         {
-          if (errno == ENOSYS || errno == ENOTSUP)
+          if (errno == ENOSYS || errno == ENOTSUP || errno == EOPNOTSUPP)
             {
               count = 0;
               entries = NULL;
@@ -358,7 +358,8 @@ qcopy_acl (const char *src_name, int source_desc, const char *dst_name,
       if (ret < 0 && saved_errno == 0)
         {
           saved_errno = errno;
-          if (errno == ENOSYS && !acl_nontrivial (count, entries))
+          if ((errno == ENOSYS || errno == EOPNOTSUPP || errno == EINVAL)
+              && !acl_nontrivial (count, entries))
             saved_errno = 0;
         }
       else
@@ -419,7 +420,7 @@ qcopy_acl (const char *src_name, int source_desc, const char *dst_name,
 
       if (count < 0)
         {
-          if (errno == ENOSYS || errno == EOPNOTSUPP)
+          if (ACL_NOT_WELL_SUPPORTED (errno))
             {
               count = 0;
               break;
@@ -454,7 +455,7 @@ qcopy_acl (const char *src_name, int source_desc, const char *dst_name,
     {
       int saved_errno = errno;
 
-      if (errno == ENOSYS || errno == EOPNOTSUPP)
+      if (ACL_NOT_WELL_SUPPORTED (errno))
         {
           struct stat source_statbuf;
 
@@ -515,6 +516,68 @@ qcopy_acl (const char *src_name, int source_desc, const char *dst_name,
 
   return 0;
 
+#elif USE_ACL && HAVE_ACLSORT /* NonStop Kernel */
+
+  int count;
+  struct acl entries[NACLENTRIES];
+  int ret;
+
+  for (;;)
+    {
+      count = acl ((char *) src_name, ACL_CNT, NACLENTRIES, NULL);
+
+      if (count < 0)
+        {
+          if (0)
+            {
+              count = 0;
+              break;
+            }
+          else
+            return -2;
+        }
+
+      if (count == 0)
+        break;
+
+      if (count > NACLENTRIES)
+        /* If NACLENTRIES cannot be trusted, use dynamic memory allocation.  */
+        abort ();
+
+      if (acl ((char *) src_name, ACL_GET, count, entries) == count)
+        break;
+      /* Huh? The number of ACL entries changed since the last call.
+         Repeat.  */
+    }
+
+  if (count == 0)
+    return qset_acl (dst_name, dest_desc, mode);
+
+  ret = acl ((char *) dst_name, ACL_SET, count, entries);
+  if (ret < 0)
+    {
+      int saved_errno = errno;
+
+      if (0)
+        {
+          if (!acl_nontrivial (count, entries))
+            return chmod_or_fchmod (dst_name, dest_desc, mode);
+        }
+
+      chmod_or_fchmod (dst_name, dest_desc, mode);
+      errno = saved_errno;
+      return -1;
+    }
+
+  if (mode & (S_ISUID | S_ISGID | S_ISVTX))
+    {
+      /* We did not call chmod so far, and either the mode and the ACL are
+         separate or special bits are to be set which don't fit into ACLs.  */
+
+      return chmod_or_fchmod (dst_name, dest_desc, mode);
+    }
+  return 0;
+
 #else
 
   return qset_acl (dst_name, dest_desc, mode);