• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <sys/cdefs.h>
23 
24 #define MAX_PATH 4096
25 #define MAX_FILE_VERSION 100
26 
27 #ifndef __STRING
28 #define __STRING(x) #x
29 #endif
30 #define ___STRING(x) __STRING(x)
31 
usage(char * filename)32 static void usage(char* filename) {
33     fprintf(stderr, "Usage: %s input_blk_alloc_file output_base_fs_file \n", filename);
34 }
35 
main(int argc,char ** argv)36 int main(int argc, char** argv) {
37     FILE *blk_alloc_file = NULL, *base_fs_file = NULL;
38     char filename[MAX_PATH + 1], file_version[MAX_FILE_VERSION + 1], *spaced_allocs = NULL;
39     size_t spaced_allocs_len = 0;
40 
41     if (argc != 3) {
42         usage(argv[0]);
43         exit(EXIT_FAILURE);
44     }
45     blk_alloc_file = fopen(argv[1], "r");
46     if (blk_alloc_file == NULL) {
47         fprintf(stderr, "failed to open %s: %s\n", argv[1], strerror(errno));
48         exit(EXIT_FAILURE);
49     }
50     base_fs_file = fopen(argv[2], "w");
51     if (base_fs_file == NULL) {
52         fprintf(stderr, "failed to open %s: %s\n", argv[2], strerror(errno));
53         exit(EXIT_FAILURE);
54     }
55     if (fscanf(blk_alloc_file, "Base EXT4 version %" ___STRING(MAX_FILE_VERSION) "s",
56                file_version) > 0) {
57         int c;
58         printf("%s is already in *.base_fs format, just copying into %s...\n", argv[1], argv[2]);
59         rewind(blk_alloc_file);
60         while ((c = fgetc(blk_alloc_file)) != EOF) {
61             fputc(c, base_fs_file);
62         }
63         return 0;
64     } else {
65         printf("Converting %s into *.base_fs format as %s...\n", argv[1], argv[2]);
66         rewind(blk_alloc_file);
67     }
68     fprintf(base_fs_file, "Base EXT4 version 1.0\n");
69     while (fscanf(blk_alloc_file, "%" ___STRING(MAX_PATH) "s ", filename) != EOF) {
70         int i;
71         fprintf(base_fs_file, "%s ", filename);
72         if (getline(&spaced_allocs, &spaced_allocs_len, blk_alloc_file) == -1) {
73             fprintf(stderr, "Bad blk_alloc format\n");
74             exit(EXIT_FAILURE);
75         }
76         for (i = 0; spaced_allocs[i]; i++) {
77             if (spaced_allocs[i] == ' ') {
78                 if (!isspace(spaced_allocs[i + 1])) fputc(',', base_fs_file);
79             } else
80                 fputc(spaced_allocs[i], base_fs_file);
81         }
82     }
83     free(spaced_allocs);
84     fclose(blk_alloc_file);
85     fclose(base_fs_file);
86     return 0;
87 }
88