summaryrefslogtreecommitdiff
path: root/ext4_utils/make_ext4fs.c
diff options
context:
space:
mode:
authorMohamad Ayyash <mkayyash@google.com>2016-02-19 21:16:34 +0000
committerMohamad Ayyash <mkayyash@google.com>2016-02-19 21:16:34 +0000
commit18785a86a30135ac65b88db9886bfc22d6608849 (patch)
tree504052c4896bd2ea8170ec082c0eb622e1619f81 /ext4_utils/make_ext4fs.c
parent3d960843a2260a98c8df5cc39ca0dbf3d675f1a2 (diff)
downloadextras-18785a86a30135ac65b88db9886bfc22d6608849.tar.gz
Revert "Redesign make_ext4fs to incrementally generate ext4 images"
This reverts commit 3d960843a2260a98c8df5cc39ca0dbf3d675f1a2. Change-Id: I0fb7d65f094feb4013924685270fc847693b6889
Diffstat (limited to 'ext4_utils/make_ext4fs.c')
-rw-r--r--ext4_utils/make_ext4fs.c181
1 files changed, 22 insertions, 159 deletions
diff --git a/ext4_utils/make_ext4fs.c b/ext4_utils/make_ext4fs.c
index 9cadc30a..913a40df 100644
--- a/ext4_utils/make_ext4fs.c
+++ b/ext4_utils/make_ext4fs.c
@@ -5,7 +5,7 @@
* 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
+ * 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,
@@ -79,13 +79,6 @@
#endif
-#define MAX_PATH 4096
-#define MAX_BLK_MAPPING_STR 1000
-
-const int blk_file_major_ver = 1;
-const int blk_file_minor_ver = 0;
-const char *blk_file_header_fmt = "Base EXT4 version %d.%d";
-
/* TODO: Not implemented:
Allocating blocks in the same block group as the file inode
Hash or binary tree directories
@@ -98,7 +91,7 @@ static int filter_dot(const struct dirent *d)
}
static u32 build_default_directory_structure(const char *dir_path,
- struct selabel_handle *sehnd)
+ struct selabel_handle *sehnd)
{
u32 inode;
u32 root_inode;
@@ -432,8 +425,8 @@ int make_ext4fs_sparse_fd_directory(int fd, long long len,
info.len = len;
return make_ext4fs_internal(fd, directory, NULL, mountpoint, NULL,
- 0, 1, 0, 0, 0,
- sehnd, 0, -1, NULL, NULL);
+ 0, 1, 0, 0, 0,
+ sehnd, 0, -1, NULL);
}
int make_ext4fs(const char *filename, long long len,
@@ -443,8 +436,8 @@ int make_ext4fs(const char *filename, long long len,
}
int make_ext4fs_directory(const char *filename, long long len,
- const char *mountpoint, struct selabel_handle *sehnd,
- const char *directory)
+ const char *mountpoint, struct selabel_handle *sehnd,
+ const char *directory)
{
int fd;
int status;
@@ -459,8 +452,8 @@ int make_ext4fs_directory(const char *filename, long long len,
}
status = make_ext4fs_internal(fd, directory, NULL, mountpoint, NULL,
- 0, 0, 0, 1, 0,
- sehnd, 0, -1, NULL, NULL);
+ 0, 0, 0, 1, 0,
+ sehnd, 0, -1, NULL);
close(fd);
return status;
@@ -526,150 +519,11 @@ static char *canonicalize_rel_slashes(const char *str)
return canonicalize_slashes(str, false);
}
-static int compare_chunks(const void* chunk1, const void* chunk2) {
- struct region* c1 = (struct region*) chunk1;
- struct region* c2 = (struct region*) chunk2;
- return c1->block - c2->block;
-}
-
-static int get_block_group(u32 block) {
- int i, group = 0;
- for(i = 0; i < aux_info.groups; i++) {
- if (block >= aux_info.bgs[i].first_block)
- group = i;
- else
- break;
- }
- return group;
-}
-
-static void extract_base_fs_allocations(const char *directory, const char *mountpoint,
- FILE* base_fs_file) {
-#define err_msg "base file badly formatted"
- // FORMAT Version 1.0: filename blk_mapping
- const char *base_fs_file_format = "%s %s";
- const int base_file_format_param_count = 2;
-
- char stored_file_name[MAX_PATH], real_file_name[MAX_PATH], file_map[MAX_BLK_MAPPING_STR];
- struct block_allocation *fs_alloc;
- struct block_group_info *bgs = aux_info.bgs;
- int i, major_version = 0, minor_version = 0;
- char *base_file_line = NULL;
- size_t base_file_line_len = 0;
-
- printf("[v%d.%d] Generating an Incremental EXT4 image\n",
- blk_file_major_ver, blk_file_minor_ver);
- if (base_fs_allocations == NULL)
- base_fs_allocations = create_allocation();
- fs_alloc = base_fs_allocations;
-
- fscanf(base_fs_file, blk_file_header_fmt, &major_version, &minor_version);
- if (major_version == 0) {
- critical_error("Invalid base file");
- }
-
- if (major_version != blk_file_major_ver) {
- critical_error("Incompatible base file: version required is %d.X",
- blk_file_major_ver);
- }
-
- if (minor_version < blk_file_minor_ver) {
- critical_error("Incompatible base file: version required is %d.%d or above",
- blk_file_major_ver, blk_file_minor_ver);
- }
-
- while (getline(&base_file_line, &base_file_line_len, base_fs_file) != -1) {
- if (sscanf(base_file_line, base_fs_file_format, &stored_file_name, &file_map)
- != base_file_format_param_count) {
- continue;
- }
- if (strlen(stored_file_name) < strlen(mountpoint)) {
- continue;
- }
- snprintf(real_file_name, MAX_PATH, "%s%s", directory, stored_file_name + strlen(mountpoint));
- if (!access(real_file_name, R_OK)) {
- char *block_range, *end_string;
- int real_file_fd;
- u32 start_block, end_block, block_file_size;
- u32 real_file_block_size;
-
- real_file_fd = open(real_file_name, O_RDONLY);
- if (real_file_fd == -1) {
- critical_error(err_msg);
- }
- real_file_block_size = get_file_size(real_file_fd);
- close(real_file_fd);
- real_file_block_size = DIV_ROUND_UP(real_file_block_size, info.block_size);
- fs_alloc->filename = strdup(real_file_name);
- block_range = strtok_r(file_map, ",", &end_string);
- while (block_range && real_file_block_size) {
- int block_group;
- char *range, *end_token = NULL;
- range = strtok_r(block_range, "-", &end_token);
- if (!range) {
- critical_error(err_msg);
- }
- start_block = parse_num(range);
- range = strtok_r(NULL, "-", &end_token);
- if (!range) {
- end_block = start_block;
- } else {
- end_block = parse_num(range);
- }
- block_file_size = end_block - start_block + 1;
- if (block_file_size > real_file_block_size) {
- block_file_size = real_file_block_size;
- }
- // Assummption is that allocations are within the same block group
- block_group = get_block_group(start_block);
- if (block_group != get_block_group(end_block)) {
- critical_error("base file allocation's end block is in a different "
- "block group than start block. did you change fs params?");
- }
- block_range = strtok_r(NULL, ",", &end_string);
- append_region(fs_alloc, start_block, block_file_size, block_group);
- reserve_bg_chunk(block_group, start_block - bgs[block_group].first_block, block_file_size);
- real_file_block_size -= block_file_size;
- }
- fs_alloc->next = create_allocation();
- fs_alloc = fs_alloc->next;
- }
- }
-
- for (i = 0; i < aux_info.groups; i++) {
- qsort(bgs[i].chunks, bgs[i].chunk_count, sizeof(struct region), compare_chunks);
- }
-
- free(base_file_line);
-
-#undef err_msg
-}
-
-void generate_block_list_file(FILE* block_list_file, char* dir, char* mountpoint,
- struct block_allocation* p)
-{
- size_t dirlen = dir ? strlen(dir) : 0;
- fprintf(block_list_file, blk_file_header_fmt, blk_file_major_ver, blk_file_minor_ver);
- fputc('\n', block_list_file);
- while (p) {
- if (dir && strncmp(p->filename, dir, dirlen) == 0) {
- // substitute mountpoint for the leading directory in the filename, in the output file
- fprintf(block_list_file, "%s%s", mountpoint, p->filename + dirlen);
- } else {
- fprintf(block_list_file, "%s", p->filename);
- }
- print_blocks(block_list_file, p);
- struct block_allocation* pn = p->next;
- free_alloc(p);
- p = pn;
- }
-}
-
int make_ext4fs_internal(int fd, const char *_directory, const char *_target_out_directory,
const char *_mountpoint, fs_config_func_t fs_config_func, int gzip,
int sparse, int crc, int wipe, int real_uuid,
struct selabel_handle *sehnd, int verbose, time_t fixed_time,
- FILE* block_list_file, FILE* base_fs_file)
+ FILE* block_list_file)
{
u32 root_inode_num;
u16 root_mode;
@@ -773,9 +627,6 @@ int make_ext4fs_internal(int fd, const char *_directory, const char *_target_out
ext4_fill_in_sb(real_uuid);
- if (base_fs_file) {
- extract_base_fs_allocations(directory, mountpoint, base_fs_file);
- }
if (reserve_inodes(0, 10) == EXT4_ALLOCATE_FAILED)
error("failed to reserve first 10 inodes");
@@ -820,8 +671,20 @@ int make_ext4fs_internal(int fd, const char *_directory, const char *_target_out
ext4_update_free();
if (block_list_file) {
+ size_t dirlen = directory ? strlen(directory) : 0;
struct block_allocation* p = get_saved_allocation_chain();
- generate_block_list_file(block_list_file, directory, mountpoint, p);
+ while (p) {
+ if (directory && strncmp(p->filename, directory, dirlen) == 0) {
+ // substitute mountpoint for the leading directory in the filename, in the output file
+ fprintf(block_list_file, "%s%s", mountpoint, p->filename + dirlen);
+ } else {
+ fprintf(block_list_file, "%s", p->filename);
+ }
+ print_blocks(block_list_file, p);
+ struct block_allocation* pn = p->next;
+ free_alloc(p);
+ p = pn;
+ }
}
printf("Created filesystem with %d/%d inodes and %d/%d blocks\n",