2 * Copyright (C) 2016 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
22 #include <sys/cdefs.h>
25 #define MAX_FILE_VERSION 100
28 #define __STRING(x) #x
30 #define ___STRING(x) __STRING(x)
32 static void usage(char *filename)
34 fprintf(stderr, "Usage: %s input_blk_alloc_file output_base_fs_file \n", filename);
37 int main(int argc, char **argv)
39 FILE *blk_alloc_file = NULL, *base_fs_file = NULL;
40 char filename[MAX_PATH+1], file_version[MAX_FILE_VERSION+1], *spaced_allocs = NULL;
41 size_t spaced_allocs_len = 0;
47 blk_alloc_file = fopen(argv[1], "r");
48 if (blk_alloc_file == NULL) {
49 fprintf(stderr, "failed to open %s: %s\n", argv[1], strerror(errno));
52 base_fs_file = fopen(argv[2], "w");
53 if (base_fs_file == NULL) {
54 fprintf(stderr, "failed to open %s: %s\n", argv[2], strerror(errno));
57 if (fscanf(blk_alloc_file, "Base EXT4 version %" ___STRING(MAX_FILE_VERSION) "s", file_version) > 0) {
59 printf("%s is already in *.base_fs format, just copying into %s...\n", argv[1], argv[2]);
60 rewind(blk_alloc_file);
61 while ((c = fgetc(blk_alloc_file)) != EOF) {
62 fputc(c, base_fs_file);
66 printf("Converting %s into *.base_fs format as %s...\n", argv[1], argv[2]);
67 rewind(blk_alloc_file);
69 fprintf(base_fs_file, "Base EXT4 version 1.0\n");
70 while(fscanf(blk_alloc_file, "%" ___STRING(MAX_PATH) "s ", filename) != EOF) {
72 fprintf(base_fs_file, "%s ", filename);
73 if (getline(&spaced_allocs, &spaced_allocs_len, blk_alloc_file) == -1) {
74 fprintf(stderr, "Bad blk_alloc format\n");
77 for (i = 0; spaced_allocs[i]; i++) {
78 if (spaced_allocs[i] == ' ') {
79 if (!isspace(spaced_allocs[i + 1])) fputc(',', base_fs_file);
80 } else fputc(spaced_allocs[i], base_fs_file);
84 fclose(blk_alloc_file);