]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
format-table: add cell type for "mode_t" values
authorLennart Poettering <lennart@poettering.net>
Wed, 6 Jan 2021 14:51:05 +0000 (15:51 +0100)
committerLennart Poettering <lennart@poettering.net>
Thu, 8 Jul 2021 08:29:43 +0000 (10:29 +0200)
src/shared/format-table.c
src/shared/format-table.h

index c4c34395417b9d8dd9410790b50f8273f3b6a86e..71885e6ac7d701c9ecab6de790db9c182f291de8 100644 (file)
@@ -11,6 +11,7 @@
 #include "fileio.h"
 #include "format-table.h"
 #include "format-util.h"
+#include "fs-util.h"
 #include "gunicode.h"
 #include "id128-util.h"
 #include "in-addr-util.h"
@@ -106,6 +107,7 @@ typedef struct TableData {
                 uid_t uid;
                 gid_t gid;
                 pid_t pid;
+                mode_t mode;
                 /* … add more here as we start supporting more cell data types … */
         };
 } TableData;
@@ -309,6 +311,9 @@ static size_t table_data_size(TableDataType type, const void *data) {
         case TABLE_PID:
                 return sizeof(pid_t);
 
+        case TABLE_MODE:
+                return sizeof(mode_t);
+
         default:
                 assert_not_reached("Uh? Unexpected cell type");
         }
@@ -815,6 +820,7 @@ int table_add_many_internal(Table *t, TableDataType first_type, ...) {
                         uid_t uid;
                         gid_t gid;
                         pid_t pid;
+                        mode_t mode;
                 } buffer;
 
                 switch (type) {
@@ -961,6 +967,11 @@ int table_add_many_internal(Table *t, TableDataType first_type, ...) {
                         data = &buffer.pid;
                         break;
 
+                case TABLE_MODE:
+                        buffer.mode = va_arg(ap, mode_t);
+                        data = &buffer.mode;
+                        break;
+
                 case TABLE_SET_MINIMUM_WIDTH: {
                         size_t w = va_arg(ap, size_t);
 
@@ -1273,6 +1284,9 @@ static int cell_data_compare(TableData *a, size_t index_a, TableData *b, size_t
                 case TABLE_PID:
                         return CMP(a->pid, b->pid);
 
+                case TABLE_MODE:
+                        return CMP(a->mode, b->mode);
+
                 default:
                         ;
                 }
@@ -1718,6 +1732,21 @@ static const char *table_data_format(Table *t, TableData *d, bool avoid_uppercas
                 break;
         }
 
+        case TABLE_MODE: {
+                _cleanup_free_ char *p;
+
+                if (d->mode == MODE_INVALID)
+                        return "n/a";
+
+                p = new(char, 4 + 1);
+                if (!p)
+                        return NULL;
+
+                sprintf(p, "%04o", d->mode & 07777);
+                d->formatted = TAKE_PTR(p);
+                break;
+        }
+
         default:
                 assert_not_reached("Unexpected type?");
         }
@@ -2525,6 +2554,12 @@ static int table_data_to_json(TableData *d, JsonVariant **ret) {
 
                 return json_variant_new_integer(ret, d->int_val);
 
+        case TABLE_MODE:
+                if (d->mode == MODE_INVALID)
+                        return json_variant_new_null(ret);
+
+                return json_variant_new_unsigned(ret, d->mode);
+
         default:
                 return -EINVAL;
         }
index 57f167f7f11eb749db15863d61b61dea79015e2a..411922acf77076bd1bf4f686832603115e22b1ae 100644 (file)
@@ -43,6 +43,7 @@ typedef enum TableDataType {
         TABLE_GID,
         TABLE_PID,
         TABLE_SIGNAL,
+        TABLE_MODE,     /* as in UNIX file mode (mode_t), in typical octal output */
         _TABLE_DATA_TYPE_MAX,
 
         /* The following are not really data types, but commands for table_add_cell_many() to make changes to