diff mbox series

[2/4] src/t_immutable: factor out some helpers

Message ID 20210116165619.494265-3-amir73il@gmail.com (mailing list archive)
State New, archived
Headers show
Series Tests for overlayfs immutable/append-only files | expand

Commit Message

Amir Goldstein Jan. 16, 2021, 4:56 p.m. UTC
Reduce boilerplate code.
define _GNU_SOURCE needed for asprintf.

Signed-off-by: Amir Goldstein <amir73il@gmail.com>
---
 src/t_immutable.c | 221 ++++++++++++++++++++++------------------------
 1 file changed, 104 insertions(+), 117 deletions(-)

Comments

Eryu Guan Jan. 24, 2021, 3:09 p.m. UTC | #1
On Sat, Jan 16, 2021 at 06:56:17PM +0200, Amir Goldstein wrote:
> Reduce boilerplate code.
> define _GNU_SOURCE needed for asprintf.
> 
> Signed-off-by: Amir Goldstein <amir73il@gmail.com>
> ---
>  src/t_immutable.c | 221 ++++++++++++++++++++++------------------------
>  1 file changed, 104 insertions(+), 117 deletions(-)
> 
> diff --git a/src/t_immutable.c b/src/t_immutable.c
> index 86c567ed..b6a76af0 100644
> --- a/src/t_immutable.c
> +++ b/src/t_immutable.c
> @@ -8,6 +8,9 @@
>  
>  #define TEST_UTIME
>  
> +#ifndef _GNU_SOURCE
> +#define _GNU_SOURCE
> +#endif
>  #include <stdio.h>
>  #include <stdlib.h>
>  #include <string.h>
> @@ -1895,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;

Other places return -1 but 1 is returned here, should be -1 as well?

Thanks,
Eryu

> +     }
> +     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";
> @@ -1911,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;
> +     if (create_dir(&path, "%s/append-only.d", dir)) {
> +	  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", 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));
> +     if ((fd = create_file(&path, "%s/append-only.d/file", dir)) == -1) {
>            return 1;
>       }
> +     close(fd);
>       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));
> -          return 1;
> +     if (create_dir(&path, "%s/immutable.d", dir)) {
> +	  return 1;
>       }
>       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", 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));
> +     if ((fd = create_file(&path, "%s/immutable.d/file", dir)) == -1) {
>            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));
> -          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)) {
> @@ -1994,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");
> @@ -2014,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;
>       }
> @@ -2027,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");
> @@ -2056,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");
> @@ -2076,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;
> @@ -2098,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");
> @@ -2118,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;
> @@ -2242,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 */
>  
> @@ -2251,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]))
> -- 
> 2.25.1
Amir Goldstein Jan. 24, 2021, 3:29 p.m. UTC | #2
On Sun, Jan 24, 2021 at 5:09 PM Eryu Guan <guan@eryu.me> wrote:
>
> On Sat, Jan 16, 2021 at 06:56:17PM +0200, Amir Goldstein wrote:
> > Reduce boilerplate code.
> > define _GNU_SOURCE needed for asprintf.
> >
> > Signed-off-by: Amir Goldstein <amir73il@gmail.com>
> > ---
> >  src/t_immutable.c | 221 ++++++++++++++++++++++------------------------
> >  1 file changed, 104 insertions(+), 117 deletions(-)
> >
> > diff --git a/src/t_immutable.c b/src/t_immutable.c
> > index 86c567ed..b6a76af0 100644
> > --- a/src/t_immutable.c
> > +++ b/src/t_immutable.c
> > @@ -8,6 +8,9 @@
> >
> >  #define TEST_UTIME
> >
> > +#ifndef _GNU_SOURCE
> > +#define _GNU_SOURCE
> > +#endif
> >  #include <stdio.h>
> >  #include <stdlib.h>
> >  #include <string.h>
> > @@ -1895,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;
>
> Other places return -1 but 1 is returned here, should be -1 as well?
>

It is a semantically different return value.

-1 are error cases, 1 means already existing, so the caller that requested to
create the dir could treat this as success.
I did not end up implementing the 'allow_existing' feature in this way, but I
see no reason to change the return value, because future implementation
could make use of this distinction. Unless you insist.

Thanks,
Amir.
Eryu Guan Jan. 25, 2021, 12:35 p.m. UTC | #3
On Sun, Jan 24, 2021 at 05:29:33PM +0200, Amir Goldstein wrote:
> On Sun, Jan 24, 2021 at 5:09 PM Eryu Guan <guan@eryu.me> wrote:
> >
> > On Sat, Jan 16, 2021 at 06:56:17PM +0200, Amir Goldstein wrote:
> > > Reduce boilerplate code.
> > > define _GNU_SOURCE needed for asprintf.
> > >
> > > Signed-off-by: Amir Goldstein <amir73il@gmail.com>
> > > ---
> > >  src/t_immutable.c | 221 ++++++++++++++++++++++------------------------
> > >  1 file changed, 104 insertions(+), 117 deletions(-)
> > >
> > > diff --git a/src/t_immutable.c b/src/t_immutable.c
> > > index 86c567ed..b6a76af0 100644
> > > --- a/src/t_immutable.c
> > > +++ b/src/t_immutable.c
> > > @@ -8,6 +8,9 @@
> > >
> > >  #define TEST_UTIME
> > >
> > > +#ifndef _GNU_SOURCE
> > > +#define _GNU_SOURCE
> > > +#endif
> > >  #include <stdio.h>
> > >  #include <stdlib.h>
> > >  #include <string.h>
> > > @@ -1895,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;
> >
> > Other places return -1 but 1 is returned here, should be -1 as well?
> >
> 
> It is a semantically different return value.
> 
> -1 are error cases, 1 means already existing, so the caller that requested to
> create the dir could treat this as success.
> I did not end up implementing the 'allow_existing' feature in this way, but I
> see no reason to change the return value, because future implementation
> could make use of this distinction. Unless you insist.

I can live with it :) I'm just curious why the return values are
different, as I didn't see different values in original code.

Thanks,
Eryu
diff mbox series

Patch

diff --git a/src/t_immutable.c b/src/t_immutable.c
index 86c567ed..b6a76af0 100644
--- a/src/t_immutable.c
+++ b/src/t_immutable.c
@@ -8,6 +8,9 @@ 
 
 #define TEST_UTIME
 
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE
+#endif
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -1895,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";
@@ -1911,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;
+     if (create_dir(&path, "%s/append-only.d", dir)) {
+	  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", 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));
+     if ((fd = create_file(&path, "%s/append-only.d/file", dir)) == -1) {
           return 1;
      }
+     close(fd);
      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));
-          return 1;
+     if (create_dir(&path, "%s/immutable.d", dir)) {
+	  return 1;
      }
      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", 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));
+     if ((fd = create_file(&path, "%s/immutable.d/file", dir)) == -1) {
           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));
-          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)) {
@@ -1994,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");
@@ -2014,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;
      }
@@ -2027,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");
@@ -2056,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");
@@ -2076,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;
@@ -2098,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");
@@ -2118,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;
@@ -2242,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 */
 
@@ -2251,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]))