OSDN Git Service

Introduce blk_alloc_to_base_fs utility
authorMohamad Ayyash <mkayyash@google.com>
Fri, 26 Feb 2016 21:58:44 +0000 (13:58 -0800)
committerMohamad Ayyash <mkayyash@google.com>
Thu, 3 Mar 2016 01:29:18 +0000 (17:29 -0800)
Converts regular block mapping files into the new base_fs file format
for generating incremental ext4 images

Internal Design Doc: go/incremental-ext4
BUG: 26839493
Change-Id: If8dbb49718226771495c044eb1a8403e99a971fb
Signed-off-by: Mohamad Ayyash <mkayyash@google.com>
ext4_utils/Android.mk
ext4_utils/blk_alloc_to_base_fs.c [new file with mode: 0644]

index babf5ca..3dd5409 100644 (file)
@@ -48,6 +48,13 @@ LOCAL_CFLAGS_darwin := -DHOST
 LOCAL_CFLAGS_linux := -DHOST
 include $(BUILD_HOST_EXECUTABLE)
 
+include $(CLEAR_VARS)
+LOCAL_SRC_FILES := blk_alloc_to_base_fs.c
+LOCAL_MODULE := blk_alloc_to_base_fs
+LOCAL_SHARED_LIBRARIES += libcutils
+LOCAL_CFLAGS_darwin := -DHOST
+LOCAL_CFLAGS_linux := -DHOST
+include $(BUILD_HOST_EXECUTABLE)
 
 #
 # -- All host/targets excluding windows
diff --git a/ext4_utils/blk_alloc_to_base_fs.c b/ext4_utils/blk_alloc_to_base_fs.c
new file mode 100644 (file)
index 0000000..1761fda
--- /dev/null
@@ -0,0 +1,81 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <ctype.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#define MAX_PATH 4096
+#define MAX_FILE_VERSION 100
+
+static void usage(char *filename)
+{
+    fprintf(stderr, "Usage: %s input_blk_alloc_file output_base_fs_file \n", filename);
+}
+
+int main(int argc, char **argv)
+{
+    FILE *blk_alloc_file = NULL, *base_fs_file = NULL;
+    char filename[MAX_PATH], file_version[MAX_FILE_VERSION], *spaced_allocs = NULL;
+    size_t spaced_allocs_len = 0;
+
+    if (argc != 3) {
+        usage(argv[0]);
+        exit(EXIT_FAILURE);
+    }
+    blk_alloc_file = fopen(argv[1], "r");
+    if (blk_alloc_file == NULL) {
+        fprintf(stderr, "failed to open %s: %s\n", argv[1], strerror(errno));
+        exit(EXIT_FAILURE);
+    }
+    base_fs_file = fopen(argv[2], "w");
+    if (base_fs_file == NULL) {
+        fprintf(stderr, "failed to open %s: %s\n", argv[2], strerror(errno));
+        exit(EXIT_FAILURE);
+    }
+    if (fscanf(blk_alloc_file, "Base EXT4 version %s", file_version) > 0) {
+        char c;
+        printf("%s is already in *.base_fs format, just copying into %s...\n", argv[1], argv[2]);
+        rewind(blk_alloc_file);
+        while ((c = fgetc(blk_alloc_file)) != EOF) {
+            fputc(c, base_fs_file);
+        }
+        return 0;
+    } else {
+        printf("Converting %s into *.base_fs format as %s...\n", argv[1], argv[2]);
+        rewind(blk_alloc_file);
+    }
+    fprintf(base_fs_file, "Base EXT4 version 1.0\n");
+    while(fscanf(blk_alloc_file, "%s ", filename) != EOF) {
+        int i;
+        fprintf(base_fs_file, "%s ", filename);
+        if (getline(&spaced_allocs, &spaced_allocs_len, blk_alloc_file) == -1) {
+            fprintf(stderr, "Bad blk_alloc format\n");
+            exit(EXIT_FAILURE);
+        }
+        for (i = 0; spaced_allocs[i]; i++) {
+            if (spaced_allocs[i] == ' ') {
+                if (!isspace(spaced_allocs[i + 1])) fputc(',', base_fs_file);
+            } else fputc(spaced_allocs[i], base_fs_file);
+        }
+    }
+    free(spaced_allocs);
+    fclose(blk_alloc_file);
+    fclose(base_fs_file);
+    return 0;
+}