]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
validatefs: split out validating gpt label and type 37447/head
authorYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 14 May 2025 17:34:18 +0000 (02:34 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 14 May 2025 17:58:05 +0000 (02:58 +0900)
No functional change, just refactoring.
This addresses https://github.com/systemd/systemd/pull/37434#discussion_r2088950725.

src/validatefs/validatefs.c

index ea0b159f500c8387387db490b68e9b6780c2d6cf..8dfe5073da1f0f936029f8fceb31310647a9caf5 100644 (file)
@@ -246,6 +246,56 @@ static int validate_mount_point(const char *path, const ValidateFields *f) {
                         strna(joined), path);
 }
 
+static int validate_gpt_label(blkid_probe b, const ValidateFields *f) {
+        assert(b);
+        assert(f);
+
+        if (strv_isempty(f->gpt_label))
+                return 0;
+
+        const char *v = NULL;
+        (void) blkid_probe_lookup_value(b, "PART_ENTRY_NAME", &v, /* ret_len= */ NULL);
+
+        if (strv_contains(f->gpt_label, strempty(v)))
+                return 0;
+
+        _cleanup_free_ char *joined = strv_join(f->gpt_label, "', '");
+        return log_error_errno(
+                        SYNTHETIC_ERRNO(EPERM),
+                        "File system is supposed to be placed in a partition with labels '%s' only, but is placed in one labelled '%s', refusing.",
+                        strna(joined), strempty(v));
+}
+
+static int validate_gpt_type(blkid_probe b, const ValidateFields *f) {
+        assert(b);
+        assert(f);
+
+        if (f->n_gpt_type_uuid == 0)
+                return 0;
+
+        const char *v = NULL;
+        (void) blkid_probe_lookup_value(b, "PART_ENTRY_TYPE", &v, /* ret_len= */ NULL);
+
+        sd_id128_t id;
+        if (!v || sd_id128_from_string(v, &id) < 0) {
+                _cleanup_free_ char *joined = validate_fields_gpt_type_uuid_as_string(f);
+                return log_error_errno(
+                                SYNTHETIC_ERRNO(EPERM),
+                                "File system is supposed to be placed in a partition of type UUIDs %s only, but has no type, refusing.",
+                                strna(joined));
+        }
+
+        FOREACH_ARRAY(u, f->gpt_type_uuid, f->n_gpt_type_uuid)
+                if (sd_id128_equal(*u, id))
+                        return 0;
+
+        _cleanup_free_ char *joined = validate_fields_gpt_type_uuid_as_string(f);
+        return log_error_errno(
+                        SYNTHETIC_ERRNO(EPERM),
+                        "File system is supposed to be placed in a partition of type UUIDs %s only, but has type '%s', refusing.",
+                        strna(joined), SD_ID128_TO_UUID_STRING(id));
+}
+
 static int validate_gpt_metadata_one(sd_device *d, const char *path, const ValidateFields *f) {
         int r;
 
@@ -286,50 +336,13 @@ static int validate_gpt_metadata_one(sd_device *d, const char *path, const Valid
         if (!streq_ptr(v, "gpt"))
                 return log_error_errno(SYNTHETIC_ERRNO(EPERM), "File system is supposed to be on a GPT partition table, but is not, refusing.");
 
-        if (!strv_isempty(f->gpt_label)) {
-                v = NULL;
-                (void) blkid_probe_lookup_value(b, "PART_ENTRY_NAME", &v, /* ret_len= */ NULL);
-
-                if (!strv_contains(f->gpt_label, strempty(v))) {
-                        _cleanup_free_ char *joined = strv_join(f->gpt_label, "', '");
-
-                        return log_error_errno(
-                                        SYNTHETIC_ERRNO(EPERM),
-                                        "File system is supposed to be placed in a partition with labels '%s' only, but is placed in one labelled '%s', refusing.",
-                                        strna(joined), strempty(v));
-                }
-        }
-
-        if (f->n_gpt_type_uuid > 0) {
-                v = NULL;
-                (void) blkid_probe_lookup_value(b, "PART_ENTRY_TYPE", &v, /* ret_len= */ NULL);
-
-                sd_id128_t id;
-                if (!v || sd_id128_from_string(v, &id) < 0) {
-                        _cleanup_free_ char *joined = validate_fields_gpt_type_uuid_as_string(f);
-
-                        return log_error_errno(
-                                        SYNTHETIC_ERRNO(EPERM),
-                                        "File system is supposed to be placed in a partition of type UUIDs %s only, but has no type, refusing.",
-                                        strna(joined));
-                }
-
-                bool found = false;
-                FOREACH_ARRAY(u, f->gpt_type_uuid, f->n_gpt_type_uuid)
-                        if (sd_id128_equal(*u, id)) {
-                                found = true;
-                                break;
-                        }
-
-                if (!found) {
-                        _cleanup_free_ char *joined = validate_fields_gpt_type_uuid_as_string(f);
+        r = validate_gpt_label(b, f);
+        if (r < 0)
+                return r;
 
-                        return log_error_errno(
-                                        SYNTHETIC_ERRNO(EPERM),
-                                        "File system is supposed to be placed in a partition of type UUIDs %s only, but has type '%s', refusing.",
-                                        strna(joined), SD_ID128_TO_UUID_STRING(id));
-                }
-        }
+        r = validate_gpt_type(b, f);
+        if (r < 0)
+                return r;
 
         return 0;
 }