Re: [PATCH] vfs: Add PERM_* symbolic helpers for common file mode/permissions

From: Ingo Molnar
Date: Tue Nov 28 2017 - 06:12:24 EST



* Linus Torvalds <torvalds@xxxxxxxxxxxxxxxxxxxx> wrote:

> On Mon, Nov 27, 2017 at 2:06 AM, Ingo Molnar <mingo@xxxxxxxxxx> wrote:
> >
> >
> > +/*
> > + * Human readable symbolic definitions for common
> > + * file permissions:
> > + */
> > +#define PERM_r________ 0400
> > +#define PERM_r__r_____ 0440
> > +#define PERM_r__r__r__ 0444
>
> I'm not a fan. Particularly as you have a very random set of
> permissions (rx and wx? Not very common),

So I originally created those defines based on a grep of patterns used in the
kernel, and added the 'wx' variants for completeness.

We would only need a small subset. Here's a git grep based histogram of octal file
permission masks used in the kernel source:

# mode
21 0200
8 0220
14 0222
33 0400
11 0440
219 0444
91 0555
39 0600
906 0644
12 0660
12 0664
18 0666
14 0755
31 0777

So there's literally only 14 variants used, and 0644 and 0444 make up 95% of the
cases. We get the patch below if we extend these existing patterns using their
natural (looking) generators to a complete group - 19 patterns that should cover
all the sensible combinations.

> but also because it's just not that legible.

Fair enough.

Thanks,

Ingo

---
include/linux/stat.h | 28 ++++++++++++++++++++++++++++
1 file changed, 28 insertions(+)

Index: tip/include/linux/stat.h
===================================================================
--- tip.orig/include/linux/stat.h
+++ tip/include/linux/stat.h
@@ -6,6 +6,34 @@
#include <asm/stat.h>
#include <uapi/linux/stat.h>

+/*
+ * Human readable symbolic definitions for common
+ * file permissions:
+ */
+#define PERM_r________ 0400
+#define PERM_r__r_____ 0440
+#define PERM_r__r__r__ 0444
+
+#define PERM_rw_______ 0600
+#define PERM_rw_r_____ 0640
+#define PERM_rw_r__r__ 0644
+#define PERM_rw_rw_r__ 0664
+#define PERM_rw_rw_rw_ 0666
+
+#define PERM__w_______ 0200
+#define PERM__w__w____ 0220
+#define PERM__w__w__w_ 0222
+
+#define PERM_r_x______ 0500
+#define PERM_r_xr_x___ 0550
+#define PERM_r_xr_xr_x 0555
+
+#define PERM_rwx______ 0700
+#define PERM_rwxr_x___ 0750
+#define PERM_rwxr_xr_x 0755
+#define PERM_rwxrwxr_x 0775
+#define PERM_rwxrwxrwx 0777
+
#define S_IRWXUGO (S_IRWXU|S_IRWXG|S_IRWXO)
#define S_IALLUGO (S_ISUID|S_ISGID|S_ISVTX|S_IRWXUGO)
#define S_IRUGO (S_IRUSR|S_IRGRP|S_IROTH)