src/t_immutable: factor out some helpers
[xfstests-dev.git] / src / t_immutable.c
index 7bb3154f25ee817bfe20ae12dee1e99a952d2a8b..7431e75d731dccf4234b3c76b73ae8594aa0f74a 100644 (file)
@@ -1,27 +1,16 @@
+// SPDX-License-Identifier: GPL-2.0+
 /* compile with: gcc -g -O0 -Wall -I/usr/include/xfs -o t_immutable t_immutable.c -lhandle -lacl -lattr */
 
 /*
  *  t_immutable.c - hideous test suite for immutable/append-only flags.
- *
  *  Copyright (C) 2003 Ethan Benson
- *
- *  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
- *  the Free Software Foundation; either version 2 of the License, or
- *  (at your option) any later version.
- *
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  */
 
 #define TEST_UTIME
 
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE
+#endif
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <grp.h>
 #include <libgen.h>
 #include <sys/acl.h>
-#include <attr/xattr.h>
+#include <sys/xattr.h>
+#include <linux/fs.h>
+#include <linux/magic.h>
 #include <xfs/xfs.h>
 #include <xfs/handle.h>
 #include <xfs/jdm.h>
 
-#define EXT2_SUPER_MAGIC       0xEF53
-#define EXT2_IMMUTABLE_FL       0x00000010
-#define EXT2_APPEND_FL          0x00000020
-#define EXT2_IOC_SETFLAGS      _IOW('f', 2, long)
-
 #ifndef XFS_SUPER_MAGIC
 #define XFS_SUPER_MAGIC 0x58465342
 #endif
@@ -55,52 +41,33 @@ extern const char *__progname;
 
 static int fsetflag(const char *path, int fd, int on, int immutable)
 {
-     int e2flags = 0;
-     struct fsxattr attr;
-     struct statfs stfs;
-     int xfsfl;
-     int e2fl;
-
-     if (immutable) {
-         xfsfl = XFS_XFLAG_IMMUTABLE;
-         e2fl = EXT2_IMMUTABLE_FL;
-     } else {
-         xfsfl = XFS_XFLAG_APPEND;
-         e2fl = EXT2_APPEND_FL;
-     }
+#ifdef FS_IOC_SETFLAGS
+     int fsflags = 0;
+     int fsfl;
 
-     if (fstatfs(fd, &stfs) != 0)
+     if (ioctl(fd, FS_IOC_GETFLAGS, &fsflags) < 0) {
+         close(fd);
          return 1;
-
-     if (stfs.f_type == XFS_SUPER_MAGIC) {
-         if (xfsctl(path, fd, XFS_IOC_FSGETXATTR, &attr) < 0) {
-              close(fd);
-              return 1;
-         }
-         if (on)
-              attr.fsx_xflags |= xfsfl;
-         else
-              attr.fsx_xflags &= ~xfsfl;
-         if (xfsctl(path, fd, XFS_IOC_FSSETXATTR, &attr) < 0) {
-              close(fd);
-              return 1;
-         }
-     } else if (stfs.f_type == EXT2_SUPER_MAGIC) {
-         if (on)
-              e2flags |= e2fl;
-         else
-              e2flags &= ~e2fl;
-         if (ioctl(fd, EXT2_IOC_SETFLAGS, &e2flags) < 0) {
-              close(fd);
-              return 1;
-         }
-     } else {
-         errno = EOPNOTSUPP;
+     }
+     if (immutable)
+         fsfl = FS_IMMUTABLE_FL;
+     else
+         fsfl = FS_APPEND_FL;
+     if (on)
+         fsflags |= fsfl;
+     else
+         fsflags &= ~fsfl;
+     if (ioctl(fd, FS_IOC_SETFLAGS, &fsflags) < 0) {
          close(fd);
          return 1;
      }
      close(fd);
      return 0;
+#else
+     errno = EOPNOTSUPP;
+     close(fd);
+     return 1;
+#endif
 }
 
 static int add_acl(const char *path, const char *acl_text)
@@ -187,8 +154,8 @@ static int test_immutable(const char *dir)
          fprintf(stderr, "open(%s, O_RDWR) did not fail\n", path);
          fail++;
          close(fd);
-     } else if (errno != EACCES) {
-         fprintf(stderr, "open(%s, O_RDWR) did not set errno == EACCES\n", path);
+     } else if (errno != EACCES && errno != EPERM) {
+         fprintf(stderr, "open(%s, O_RDWR) did not set errno == EACCES or EPERM\n", path);
          fail++;
      }
 
@@ -197,8 +164,8 @@ static int test_immutable(const char *dir)
          fprintf(stderr, "open(%s, O_WRONLY) did not fail\n", path);
          fail++;
          close(fd);
-     } else if (errno != EACCES) {
-          fprintf(stderr, "open(%s, O_WRONLY) did not set errno == EACCES\n", path);
+     } else if (errno != EACCES && errno != EPERM) {
+          fprintf(stderr, "open(%s, O_WRONLY) did not set errno == EACCES or EPERM\n", path);
           fail++;
      }
 
@@ -207,8 +174,8 @@ static int test_immutable(const char *dir)
           fprintf(stderr, "open(%s, O_RDWR|O_TRUNC) did not fail\n", path);
           fail++;
           close(fd);
-     } else if (errno != EACCES) {
-          fprintf(stderr, "open(%s, O_RDWR|O_TRUNC) did not set errno == EACCES\n", path);
+     } else if (errno != EACCES && errno != EPERM) {
+          fprintf(stderr, "open(%s, O_RDWR|O_TRUNC) did not set errno == EACCES or EPERM\n", path);
           fail++;
      }
 
@@ -217,8 +184,8 @@ static int test_immutable(const char *dir)
           fprintf(stderr, "open(%s, O_WRONLY|O_TRUNC) did not fail\n", path);
           fail++;
           close(fd);
-     } else if (errno != EACCES) {
-          fprintf(stderr, "open(%s, O_WRONLY|O_TRUNC did not set errno == EACCES\n", path);
+     } else if (errno != EACCES && errno != EPERM) {
+          fprintf(stderr, "open(%s, O_WRONLY|O_TRUNC did not set errno == EACCES or EPERM\n", path);
           fail++;
      }
 
@@ -227,8 +194,8 @@ static int test_immutable(const char *dir)
           fprintf(stderr, "open(%s, O_RDWR|O_APPEND) did not fail\n", path);
           fail++;
           close(fd);
-     } else if (errno != EACCES) {
-          fprintf(stderr, "open(%s, O_RDWR|O_APPEND) did not set errno == EACCES\n", path);
+     } else if (errno != EACCES && errno != EPERM) {
+          fprintf(stderr, "open(%s, O_RDWR|O_APPEND) did not set errno == EACCES or EPERM\n", path);
           fail++;
      }
 
@@ -237,8 +204,8 @@ static int test_immutable(const char *dir)
           fprintf(stderr, "open(%s, O_WRONLY|O_APPEND) did not fail\n", path);
           fail++;
           close(fd);
-     } else if (errno != EACCES) {
-          fprintf(stderr, "open(%s, O_WRONLY|O_APPEND) did not set errno == EACCES\n", path);
+     } else if (errno != EACCES && errno != EPERM) {
+          fprintf(stderr, "open(%s, O_WRONLY|O_APPEND) did not set errno == EACCES or EPERM\n", path);
           fail++;
      }
 
@@ -247,8 +214,8 @@ static int test_immutable(const char *dir)
           fprintf(stderr, "open(%s, O_RDWR|O_APPEND|O_TRUNC) did not fail\n", path);
           fail++;
           close(fd);
-     } else if (errno != EACCES) {
-          fprintf(stderr, "open(%s, O_RDWR|O_APPEND|O_TRUNC) did not set errno == EACCES\n", path);
+     } else if (errno != EACCES && errno != EPERM) {
+          fprintf(stderr, "open(%s, O_RDWR|O_APPEND|O_TRUNC) did not set errno == EACCES or EPERM\n", path);
           fail++;
      }
 
@@ -257,15 +224,15 @@ static int test_immutable(const char *dir)
           fprintf(stderr, "open(%s, O_WRONLY|O_APPEND|O_TRUNC) did not fail\n", path);
           fail++;
           close(fd);
-     } else if (errno != EACCES) {
-          fprintf(stderr, "open(%s, O_WRONLY|O_APPEND|O_TRUNC) did not set errno == EACCES\n", path);
+     } else if (errno != EACCES && errno != EPERM) {
+          fprintf(stderr, "open(%s, O_WRONLY|O_APPEND|O_TRUNC) did not set errno == EACCES or EPERM\n", path);
           fail++;
      }
 
      if (stfs.f_type == XFS_SUPER_MAGIC && !getuid()) {
          jdm_fshandle_t *fshandle;
-         xfs_bstat_t bstat;
-         xfs_fsop_bulkreq_t  bulkreq;
+         struct xfs_bstat bstat;
+         struct xfs_fsop_bulkreq  bulkreq;
          xfs_ino_t ino;
          char *dirpath;
 
@@ -305,9 +272,9 @@ static int test_immutable(const char *dir)
               fprintf(stderr, "jdm_open(%s, O_RDWR) did not fail\n", path);
               fail++;
               close(fd);
-         } else if (errno != EACCES) {
+         } else if (errno != EACCES && errno != EPERM) {
               perror("jdm_open");
-              fprintf(stderr, "jdm_open(%s, O_RDWR) did not set errno == EACCES\n", path);
+              fprintf(stderr, "jdm_open(%s, O_RDWR) did not set errno == EACCES or EPERM\n", path);
               fail++;
          }
 
@@ -316,8 +283,8 @@ static int test_immutable(const char *dir)
               fprintf(stderr, "jdm_open(%s, O_WRONLY) did not fail\n", path);
               fail++;
               close(fd);
-         } else if (errno != EACCES) {
-              fprintf(stderr, "jdm_open(%s, O_WRONLY) did not set errno == EACCES\n", path);
+         } else if (errno != EACCES && errno != EPERM) {
+              fprintf(stderr, "jdm_open(%s, O_WRONLY) did not set errno == EACCES or EPERM\n", path);
               fail++;
          }
 
@@ -326,8 +293,8 @@ static int test_immutable(const char *dir)
               fprintf(stderr, "jdm_open(%s, O_RDWR|O_TRUNC) did not fail\n", path);
               fail++;
               close(fd);
-         } else if (errno != EACCES) {
-              fprintf(stderr, "jdm_open(%s, O_RDWR|O_TRUNC) did not set errno == EACCES\n", path);
+         } else if (errno != EACCES && errno != EPERM) {
+              fprintf(stderr, "jdm_open(%s, O_RDWR|O_TRUNC) did not set errno == EACCES or EPERM\n", path);
               fail++;
          }
 
@@ -336,8 +303,8 @@ static int test_immutable(const char *dir)
               fprintf(stderr, "jdm_open(%s, O_WRONLY|O_TRUNC) did not fail\n", path);
               fail++;
               close(fd);
-         } else if (errno != EACCES) {
-              fprintf(stderr, "jdm_open(%s, O_WRONLY|O_TRUNC did not set errno == EACCES\n", path);
+         } else if (errno != EACCES && errno != EPERM) {
+              fprintf(stderr, "jdm_open(%s, O_WRONLY|O_TRUNC did not set errno == EACCES or EPERM\n", path);
               fail++;
          }
 
@@ -346,8 +313,8 @@ static int test_immutable(const char *dir)
               fprintf(stderr, "jdm_open(%s, O_RDWR|O_APPEND) did not fail\n", path);
               fail++;
               close(fd);
-         } else if (errno != EACCES) {
-              fprintf(stderr, "jdm_open(%s, O_RDWR|O_APPEND) did not set errno == EACCES\n", path);
+         } else if (errno != EACCES && errno != EPERM) {
+              fprintf(stderr, "jdm_open(%s, O_RDWR|O_APPEND) did not set errno == EACCES or EPERM\n", path);
               fail++;
          }
 
@@ -356,8 +323,8 @@ static int test_immutable(const char *dir)
               fprintf(stderr, "jdm_open(%s, O_WRONLY|O_APPEND) did not fail\n", path);
               fail++;
               close(fd);
-         } else if (errno != EACCES) {
-              fprintf(stderr, "jdm_open(%s, O_WRONLY|O_APPEND) did not set errno == EACCES\n", path);
+         } else if (errno != EACCES && errno != EPERM) {
+              fprintf(stderr, "jdm_open(%s, O_WRONLY|O_APPEND) did not set errno == EACCES or EPERM\n", path);
               fail++;
          }
 
@@ -366,8 +333,8 @@ static int test_immutable(const char *dir)
               fprintf(stderr, "jdm_open(%s, O_RDWR|O_APPEND|O_TRUNC) did not fail\n", path);
               fail++;
               close(fd);
-         } else if (errno != EACCES) {
-              fprintf(stderr, "jdm_open(%s, O_RDWR|O_APPEND|O_TRUNC) did not set errno == EACCES\n", path);
+         } else if (errno != EACCES && errno != EPERM) {
+              fprintf(stderr, "jdm_open(%s, O_RDWR|O_APPEND|O_TRUNC) did not set errno == EACCES or EPERM\n", path);
               fail++;
          }
 
@@ -376,8 +343,8 @@ static int test_immutable(const char *dir)
               fprintf(stderr, "jdm_open(%s, O_WRONLY|O_APPEND|O_TRUNC) did not fail\n", path);
               fail++;
               close(fd);
-         } else if (errno != EACCES) {
-              fprintf(stderr, "jdm_open(%s, O_WRONLY|O_APPEND|O_TRUNC) did not set errno == EACCES\n", path);
+         } else if (errno != EACCES && errno != EPERM) {
+              fprintf(stderr, "jdm_open(%s, O_WRONLY|O_APPEND|O_TRUNC) did not set errno == EACCES or EPERM\n", path);
               fail++;
          }
      }
@@ -386,8 +353,8 @@ static int test_immutable(const char *dir)
      if (truncate(path, 0) != -1) {
          fprintf(stderr, "truncate(%s, 0) did not fail\n", path);
          fail++;
-     } else if (errno != EACCES) {
-         fprintf(stderr, "truncate(%s, 0) did not set errno == EACCES\n", path);
+     } else if (errno != EACCES && errno != EPERM) {
+         fprintf(stderr, "truncate(%s, 0) did not set errno == EACCES or EPERM\n", path);
          fail++;
      }
 
@@ -405,8 +372,8 @@ static int test_immutable(const char *dir)
      if (utime(path, NULL) != -1) {
           fprintf(stderr, "utime(%s, NULL) did not fail\n", path);
           fail++;
-     } else if (errno != EACCES) {
-          fprintf(stderr, "utime(%s, NULL) did not set errno == EACCES\n", path);
+     } else if (errno != EACCES && errno != EPERM) {
+          fprintf(stderr, "utime(%s, NULL) did not set errno == EACCES or EPERM\n", path);
           fail++;
      }
 #endif /* TEST_UTIME */
@@ -592,16 +559,16 @@ static int test_immutable(const char *dir)
          fprintf(stderr, "link(%s, %s) did not fail\n", path, linkpath);
          fail++;
          unlink(linkpath);
-     } else if (errno != EACCES) {
-         fprintf(stderr, "link(%s, %s) did not set errno == EACCES\n", path, linkpath);
+     } else if (errno != EACCES && errno != EPERM) {
+         fprintf(stderr, "link(%s, %s) did not set errno == EACCES or EPERM\n", path, linkpath);
          fail++;
      }
      if (symlink(path, linkpath) != -1) {
          fprintf(stderr, "symlink(%s, %s) did not fail\n", path, linkpath);
          fail++;
          unlink(linkpath);
-     } else if (errno != EACCES) {
-         fprintf(stderr, "symlink(%s, %s) did not set errno == EACCES\n", path, linkpath);
+     } else if (errno != EACCES && errno != EPERM) {
+         fprintf(stderr, "symlink(%s, %s) did not set errno == EACCES or EPERM\n", path, linkpath);
           fail++;
      }
      free(linkpath);
@@ -610,8 +577,8 @@ static int test_immutable(const char *dir)
          fprintf(stderr, "rename(%s, %s) did not fail\n", path, linkpath);
          fail++;
          rename(linkpath, path);
-     } else if (errno != EACCES) {
-         fprintf(stderr, "rename(%s, %s) did not set errno == EACCES\n", path, linkpath);
+     } else if (errno != EACCES && errno != EPERM) {
+         fprintf(stderr, "rename(%s, %s) did not set errno == EACCES or EPERM\n", path, linkpath);
           fail++;
      }
      free(linkpath);
@@ -619,8 +586,8 @@ static int test_immutable(const char *dir)
      if (unlink(path) != -1) {
          fprintf(stderr, "unlink(%s) did not fail\n", path);
          fail++;
-     } else if (errno != EACCES) {
-         fprintf(stderr, "unlink(%s) did not set errno == EACCES\n", path);
+     } else if (errno != EACCES && errno != EPERM) {
+         fprintf(stderr, "unlink(%s) did not set errno == EACCES or EPERM\n", path);
           fail++;
      }
 
@@ -631,8 +598,8 @@ static int test_immutable(const char *dir)
          fprintf(stderr, "open(%s, O_RDWR|O_CREAT, 0666) did not fail\n", path);
          fail++;
          unlink(path);
-     } else if (errno != EACCES) {
-         fprintf(stderr, "open(%s, O_RDWR|O_CREAT, 0666) did not set errno == EACCES\n", path);
+     } else if (errno != EACCES && errno != EPERM) {
+         fprintf(stderr, "open(%s, O_RDWR|O_CREAT, 0666) did not set errno == EACCES or EPERM\n", path);
          fail++;
      }
      if (!getuid()) {
@@ -641,8 +608,8 @@ static int test_immutable(const char *dir)
                    fprintf(stderr, "mknod(%s, S_IFCHR|0666, %lld) did not fail\n", path, (long long int)st.st_rdev);
                    fail++;
                    unlink(path);
-              } else if (errno != EACCES) {
-                   fprintf(stderr, "mknod(%s, S_IFCHR|0666, %lld) did not set errno == EACCESS\n", path, (long long int)st.st_rdev);
+              } else if (errno != EACCES && errno != EPERM) {
+                   fprintf(stderr, "mknod(%s, S_IFCHR|0666, %lld) did not set errno == EACCES or EPERM\n", path, (long long int)st.st_rdev);
                    fail++;
               }
          }
@@ -655,8 +622,8 @@ static int test_immutable(const char *dir)
          fprintf(stderr, "mkdir(%s, 0777) did not fail\n", path);
          fail++;
          rmdir(path);
-     } else if (errno != EACCES) {
-         fprintf(stderr, "mkdir(%s, 0777) did not set errno == EACCES\n", path);
+     } else if (errno != EACCES && errno != EPERM) {
+         fprintf(stderr, "mkdir(%s, 0777) did not set errno == EACCES or EPERM\n", path);
           fail++;
      }
 
@@ -692,8 +659,8 @@ static int test_immutable(const char *dir)
      if (rmdir(path) != -1) {
          fprintf(stderr, "rmdir(%s) did not fail\n", path);
          fail++;
-     } else if (errno != EACCES) {
-         fprintf(stderr, "rmdir(%s) did not set errno == EACCES\n", path);
+     } else if (errno != EACCES && errno != EPERM) {
+         fprintf(stderr, "rmdir(%s) did not set errno == EACCES or EPERM\n", path);
          fail++;
      }
 
@@ -714,8 +681,8 @@ static int test_immutable(const char *dir)
      if (utime(path, NULL) != -1) {
           fprintf(stderr, "utime(%s, NULL) did not fail\n", path);
           fail++;
-     } else if (errno != EACCES) {
-          fprintf(stderr, "utime(%s, NULL) did not set errno == EACCES\n", path);
+     } else if (errno != EACCES && errno != EPERM) {
+          fprintf(stderr, "utime(%s, NULL) did not set errno == EACCES or EPERM\n", path);
           fail++;
      }
 #endif /* TEST_UTIME */
@@ -913,6 +880,21 @@ static int test_append(const char *dir)
           fail++;
      }
 
+     errno = 0;
+     if ((fd = open(path, O_RDWR|O_APPEND)) == -1) {
+         fprintf(stderr, "open(%s, O_RDWR|O_APPEND) failed: %s\n", path, strerror(errno));
+         fail++;
+     } else {
+            if (ftruncate(fd, 0) != -1) {
+                    fprintf(stderr, "ftruncate(%s, 0) did not fail\n", path);
+                    fail++;
+            } else if (errno != EPERM) {
+                    fprintf(stderr, "ftruncate(%s, 0) did not set errno == EPERM\n", path);
+                    fail++;
+            }
+            close(fd);
+     }
+
      errno = 0;
      if (truncate(path, 0) != -1) {
          fprintf(stderr, "truncate(%s, 0) did not fail\n", path);
@@ -924,8 +906,8 @@ static int test_append(const char *dir)
 
      if (stfs.f_type == XFS_SUPER_MAGIC && !getuid()) {
          jdm_fshandle_t *fshandle;
-         xfs_bstat_t bstat;
-         xfs_fsop_bulkreq_t  bulkreq;
+         struct xfs_bstat bstat;
+         struct xfs_fsop_bulkreq  bulkreq;
          xfs_ino_t ino;
          char *dirpath;
 
@@ -1309,8 +1291,8 @@ static int test_append(const char *dir)
 
      if (stfs.f_type == XFS_SUPER_MAGIC && !getuid()) {
           jdm_fshandle_t *fshandle;
-          xfs_bstat_t bstat;
-          xfs_fsop_bulkreq_t  bulkreq;
+          struct xfs_bstat bstat;
+          struct xfs_fsop_bulkreq  bulkreq;
           xfs_ino_t ino;
           char *dirpath;
 
@@ -1916,13 +1898,66 @@ static int check_test_area(const char *dir)
      return 0;
 }
 
+static int create_dir(char **ppath, const char *fmt, const char *dir)
+{
+     const char *path;
+     struct stat st;
+
+     if (asprintf(ppath, fmt, dir) == -1) {
+         return 1;
+     }
+     path = *ppath;
+     if (stat(path, &st) == 0) {
+         fprintf(stderr, "%s: Test area directory %s must not exist for test area creation.\n",
+                 __progname, path);
+         return 1;
+     }
+     if (mkdir(path, 0777) != 0) {
+         fprintf(stderr, "%s: error creating directory %s: %s\n", __progname, path, strerror(errno));
+         return 1;
+     }
+     return 0;
+}
+
+static int create_file(char **ppath, const char *fmt, const char *dir)
+{
+     const char *path;
+     int fd;
+
+     if (asprintf(ppath, fmt, dir) == -1) {
+         return 1;
+     }
+     path = *ppath;
+     if ((fd = open(path, O_WRONLY|O_CREAT|O_EXCL, 0666)) == -1) {
+         fprintf(stderr, "%s: error creating file %s: %s\n", __progname, path, strerror(errno));
+          return 1;
+     }
+     return fd;
+}
+
+static int create_xattrs(int fd)
+{
+     if (fsetxattr(fd, "trusted.test", "readonly", strlen("readonly"), XATTR_CREATE) != 0) {
+         if (errno != EOPNOTSUPP) {
+              perror("setxattr");
+              return 1;
+         }
+     }
+     if (fsetxattr(fd, "user.test", "readonly", strlen("readonly"), XATTR_CREATE) != 0) {
+         if (errno != EOPNOTSUPP) {
+              perror("setxattr");
+              return 1;
+         }
+     }
+     return 0;
+}
+
 static int create_test_area(const char *dir)
 {
      int fd;
      char *path;
      static const char *acl_u_text = "u::rw-,g::rw-,o::rw-,u:nobody:rw-,m::rw-";
      static const char *acl_u_text_d = "u::rwx,g::rwx,o::rwx,u:nobody:rwx,m::rwx";
-     struct stat st;
      static const char *immutable = "This is an immutable file.\nIts contents cannot be altered.\n";
      static const char *append_only = "This is an append-only file.\nIts contents cannot be altered.\n"
          "Data can only be appended.\n---\n";
@@ -1932,79 +1967,45 @@ static int create_test_area(const char *dir)
          return 1;
      }
 
-     if (stat(dir, &st) == 0) {
-         fprintf(stderr, "%s: Test area directory %s must not exist for test area creation.\n",
-                 __progname, dir);
-         return 1;
-     }
-
      umask(0000);
-     if (mkdir(dir, 0777) != 0) {
-         fprintf(stderr, "%s: error creating directory %s: %s\n", __progname, dir, strerror(errno));
+     if (create_dir(&path, "%s", dir)) {
          return 1;
      }
-
-     asprintf(&path, "%s/immutable.d", dir);
-     if (mkdir(path, 0777) != 0) {
-          fprintf(stderr, "%s: error creating directory %s: %s\n", __progname, path, strerror(errno));
-          return 1;
-     }
      free(path);
 
-     asprintf(&path, "%s/empty-immutable.d", dir);
-     if (mkdir(path, 0777) != 0) {
-          fprintf(stderr, "%s: error creating directory %s: %s\n", __progname, path, strerror(errno));
-          return 1;
-     }
-     free(path);
-
-     asprintf(&path, "%s/append-only.d", dir);
-     if (mkdir(path, 0777) != 0) {
-          fprintf(stderr, "%s: error creating directory %s: %s\n", __progname, path, strerror(errno));
-          return 1;
+     if (create_dir(&path, "%s/append-only.d", dir)) {
+         return 1;
      }
      free(path);
 
-     asprintf(&path, "%s/empty-append-only.d", dir);
-     if (mkdir(path, 0777) != 0) {
-          fprintf(stderr, "%s: error creating directory %s: %s\n", __progname, path, strerror(errno));
-          return 1;
+     if (create_dir(&path, "%s/append-only.d/dir", dir)) {
+         return 1;
      }
      free(path);
 
-     asprintf(&path, "%s/immutable.d/dir", dir);
-     if (mkdir(path, 0777) != 0) {
-          fprintf(stderr, "%s: error creating directory %s: %s\n", __progname, path, strerror(errno));
+     if ((fd = create_file(&path, "%s/append-only.d/file", dir)) == -1) {
           return 1;
      }
+     close(fd);
      free(path);
 
-     asprintf(&path, "%s/append-only.d/dir", dir);
-     if (mkdir(path, 0777) != 0) {
-          fprintf(stderr, "%s: error creating directory %s: %s\n", __progname, path, strerror(errno));
-          return 1;
+     if (create_dir(&path, "%s/immutable.d", dir)) {
+         return 1;
      }
      free(path);
 
-     asprintf(&path, "%s/append-only.d/file", dir);
-     if ((fd = open(path, O_WRONLY|O_CREAT|O_EXCL, 0666)) == -1) {
-         fprintf(stderr, "%s: error creating file %s: %s\n", __progname, path, strerror(errno));
-          return 1;
+     if (create_dir(&path, "%s/immutable.d/dir", dir)) {
+         return 1;
      }
-     close(fd);
      free(path);
 
-     asprintf(&path, "%s/immutable.d/file", dir);
-     if ((fd = open(path, O_WRONLY|O_CREAT|O_EXCL, 0666)) == -1) {
-          fprintf(stderr, "%s: error creating file %s: %s\n", __progname, path, strerror(errno));
+     if ((fd = create_file(&path, "%s/immutable.d/file", dir)) == -1) {
           return 1;
      }
      close(fd);
      free(path);
 
-     asprintf(&path, "%s/immutable.f", dir);
-     if ((fd = open(path, O_WRONLY|O_CREAT|O_EXCL, 0666)) == -1) {
-          fprintf(stderr, "%s: error creating file %s: %s\n", __progname, path, strerror(errno));
+     if ((fd = create_file(&path, "%s/immutable.f", dir)) == -1) {
           return 1;
      }
      if (write(fd, immutable, strlen(immutable)) != strlen(immutable)) {
@@ -2015,17 +2016,8 @@ static int create_test_area(const char *dir)
          perror("acl");
          return 1;
      }
-     if (fsetxattr(fd, "trusted.test", "readonly", strlen("readonly"), XATTR_CREATE) != 0) {
-         if (errno != EOPNOTSUPP) {
-              perror("setxattr");
-              return 1;
-         }
-     }
-     if (fsetxattr(fd, "user.test", "readonly", strlen("readonly"), XATTR_CREATE) != 0) {
-         if (errno != EOPNOTSUPP) {
-              perror("setxattr");
-              return 1;
-         }
+     if (create_xattrs(fd)) {
+         return 1;
      }
      if (fsetflag(path, fd, 1, 1)) {
           perror("fsetflag");
@@ -2035,8 +2027,7 @@ static int create_test_area(const char *dir)
      close(fd);
      free(path);
 
-     asprintf(&path, "%s/append-only.f", dir);
-     if ((fd = open(path, O_WRONLY|O_CREAT|O_EXCL, 0666)) == -1) {
+     if ((fd = create_file(&path, "%s/append-only.f", dir)) == -1) {
           fprintf(stderr, "%s: error creating file %s: %s\n", __progname, path, strerror(errno));
           return 1;
      }
@@ -2048,17 +2039,8 @@ static int create_test_area(const char *dir)
           perror("acl");
           return 1;
      }
-     if (fsetxattr(fd, "trusted.test", "readonly", strlen("readonly"), XATTR_CREATE) != 0) {
-         if (errno != EOPNOTSUPP) {
-              perror("setxattr");
-              return 1;
-         }
-     }
-     if (fsetxattr(fd, "user.test", "readonly", strlen("readonly"), XATTR_CREATE) != 0) {
-         if (errno != EOPNOTSUPP) {
-              perror("setxattr");
-              return 1;
-         }
+     if (create_xattrs(fd)) {
+         return 1;
      }
      if (fsetflag(path, fd, 1, 0)) {
           perror("fsetflag");
@@ -2077,17 +2059,8 @@ static int create_test_area(const char *dir)
           perror("acl");
           return 1;
      }
-     if (fsetxattr(fd, "trusted.test", "readonly", strlen("readonly"), XATTR_CREATE) != 0) {
-         if (errno != EOPNOTSUPP) {
-              perror("setxattr");
-              return 1;
-         }
-     }
-     if (fsetxattr(fd, "user.test", "readonly", strlen("readonly"), XATTR_CREATE) != 0) {
-         if (errno != EOPNOTSUPP) {
-              perror("setxattr");
-              return 1;
-         }
+     if (create_xattrs(fd)) {
+         return 1;
      }
      if (fsetflag(path, fd, 1, 1)) {
           perror("fsetflag");
@@ -2097,7 +2070,9 @@ static int create_test_area(const char *dir)
      close(fd);
      free(path);
 
-     asprintf(&path, "%s/empty-immutable.d", dir);
+     if (create_dir(&path, "%s/empty-immutable.d", dir)) {
+         return 1;
+     }
      if ((fd = open(path, O_RDONLY)) == -1) {
           fprintf(stderr, "%s: error opening %s: %s\n", __progname, path, strerror(errno));
           return 1;
@@ -2119,17 +2094,8 @@ static int create_test_area(const char *dir)
           perror("acl");
           return 1;
      }
-     if (fsetxattr(fd, "trusted.test", "readonly", strlen("readonly"), XATTR_CREATE) != 0) {
-         if (errno != EOPNOTSUPP) {
-              perror("setxattr");
-              return 1;
-         }
-     }
-     if (fsetxattr(fd, "user.test", "readonly", strlen("readonly"), XATTR_CREATE) != 0) {
-         if (errno != EOPNOTSUPP) {
-              perror("setxattr");
-              return 1;
-         }
+     if (create_xattrs(fd)) {
+         return 1;
      }
      if (fsetflag(path, fd, 1, 0)) {
           perror("fsetflag");
@@ -2139,7 +2105,9 @@ static int create_test_area(const char *dir)
      close(fd);
      free(path);
 
-     asprintf(&path, "%s/empty-append-only.d", dir);
+     if (create_dir(&path, "%s/empty-append-only.d", dir)) {
+         return 1;
+     }
      if ((fd = open(path, O_RDONLY)) == -1) {
           fprintf(stderr, "%s: error opening %s: %s\n", __progname, path, strerror(errno));
           return 1;
@@ -2263,6 +2231,7 @@ int main(int argc, char **argv)
 {
      int ret;
      int failed = 0;
+     int runtest = 1, create = 0, remove = 0;
 
 /* this arg parsing is gross, but who cares, its a test program */
 
@@ -2272,32 +2241,29 @@ int main(int argc, char **argv)
      }
 
      if (!strcmp(argv[1], "-c")) {
-         if (argc == 3) {
-              if ((ret = create_test_area(argv[argc-1])))
-                   return ret;
-         } else {
-              fprintf(stderr, "usage: t_immutable -c test_area_dir\n");
-              return 1;
-         }
+         create = 1;
      } else if (!strcmp(argv[1], "-C")) {
-          if (argc == 3) {
-               return create_test_area(argv[argc-1]);
-          } else {
-               fprintf(stderr, "usage: t_immutable -C test_area_dir\n");
-               return 1;
-          }
+         /* Prepare test area without running tests */
+         create = 1;
+         runtest = 0;
      } else if (!strcmp(argv[1], "-r")) {
-         if (argc == 3)
-              return remove_test_area(argv[argc-1]);
-         else {
-              fprintf(stderr, "usage: t_immutable -r test_area_dir\n");
-              return 1;
-         }
-     } else if (argc != 2) {
-         fprintf(stderr, "usage: t_immutable [-c|-r] test_area_dir\n");
+         remove = 1;
+     }
+
+     if (argc != 2 + (create | remove)) {
+         fprintf(stderr, "usage: t_immutable [-C|-c|-r] test_area_dir\n");
          return 1;
      }
 
+     if (create) {
+         ret = create_test_area(argv[argc-1]);
+         if (ret || !runtest) {
+               return ret;
+         }
+     } else if (remove) {
+         return remove_test_area(argv[argc-1]);
+     }
+
      umask(0000);
 
      if (check_test_area(argv[argc-1]))