1 /*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 #include <ctype.h>
18 #include <errno.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22
23 #define MAX_PATH 4096
24 #define MAX_FILE_VERSION 100
25
usage(char * filename)26 static void usage(char *filename)
27 {
28 fprintf(stderr, "Usage: %s input_blk_alloc_file output_base_fs_file \n", filename);
29 }
30
main(int argc,char ** argv)31 int main(int argc, char **argv)
32 {
33 FILE *blk_alloc_file = NULL, *base_fs_file = NULL;
34 char filename[MAX_PATH], file_version[MAX_FILE_VERSION], *spaced_allocs = NULL;
35 size_t spaced_allocs_len = 0;
36
37 if (argc != 3) {
38 usage(argv[0]);
39 exit(EXIT_FAILURE);
40 }
41 blk_alloc_file = fopen(argv[1], "r");
42 if (blk_alloc_file == NULL) {
43 fprintf(stderr, "failed to open %s: %s\n", argv[1], strerror(errno));
44 exit(EXIT_FAILURE);
45 }
46 base_fs_file = fopen(argv[2], "w");
47 if (base_fs_file == NULL) {
48 fprintf(stderr, "failed to open %s: %s\n", argv[2], strerror(errno));
49 exit(EXIT_FAILURE);
50 }
51 if (fscanf(blk_alloc_file, "Base EXT4 version %s", file_version) > 0) {
52 char c;
53 printf("%s is already in *.base_fs format, just copying into %s...\n", argv[1], argv[2]);
54 rewind(blk_alloc_file);
55 while ((c = fgetc(blk_alloc_file)) != EOF) {
56 fputc(c, base_fs_file);
57 }
58 return 0;
59 } else {
60 printf("Converting %s into *.base_fs format as %s...\n", argv[1], argv[2]);
61 rewind(blk_alloc_file);
62 }
63 fprintf(base_fs_file, "Base EXT4 version 1.0\n");
64 while(fscanf(blk_alloc_file, "%s ", filename) != EOF) {
65 int i;
66 fprintf(base_fs_file, "%s ", filename);
67 if (getline(&spaced_allocs, &spaced_allocs_len, blk_alloc_file) == -1) {
68 fprintf(stderr, "Bad blk_alloc format\n");
69 exit(EXIT_FAILURE);
70 }
71 for (i = 0; spaced_allocs[i]; i++) {
72 if (spaced_allocs[i] == ' ') {
73 if (!isspace(spaced_allocs[i + 1])) fputc(',', base_fs_file);
74 } else fputc(spaced_allocs[i], base_fs_file);
75 }
76 }
77 free(spaced_allocs);
78 fclose(blk_alloc_file);
79 fclose(base_fs_file);
80 return 0;
81 }
82