Home | History | Annotate | Download | only in ext4_utils
      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 
     32 static void usage(char *filename)
     33 {
     34     fprintf(stderr, "Usage: %s input_blk_alloc_file output_base_fs_file \n", filename);
     35 }
     36 
     37 int main(int argc, char **argv)
     38 {
     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;
     42 
     43     if (argc != 3) {
     44         usage(argv[0]);
     45         exit(EXIT_FAILURE);
     46     }
     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));
     50         exit(EXIT_FAILURE);
     51     }
     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));
     55         exit(EXIT_FAILURE);
     56     }
     57     if (fscanf(blk_alloc_file, "Base EXT4 version %" ___STRING(MAX_FILE_VERSION) "s", file_version) > 0) {
     58         char c;
     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);
     63         }
     64         return 0;
     65     } else {
     66         printf("Converting %s into *.base_fs format as %s...\n", argv[1], argv[2]);
     67         rewind(blk_alloc_file);
     68     }
     69     fprintf(base_fs_file, "Base EXT4 version 1.0\n");
     70     while(fscanf(blk_alloc_file, "%" ___STRING(MAX_PATH) "s ", filename) != EOF) {
     71         int i;
     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");
     75             exit(EXIT_FAILURE);
     76         }
     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);
     81         }
     82     }
     83     free(spaced_allocs);
     84     fclose(blk_alloc_file);
     85     fclose(base_fs_file);
     86     return 0;
     87 }
     88