diff options
author | Anatol Pomazau <anatol@google.com> | 2012-02-03 10:57:40 -0800 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2012-02-03 10:57:40 -0800 |
commit | ae4f7dccadfafc36470a3e6f3084c6cf3cc63415 (patch) | |
tree | e9df9977417c5df1e15c07df7ef6cb6dad75cac8 /ext4_utils | |
parent | 93eb1dc9e68d2e9dea94f56d8bce478c1a52b354 (diff) | |
download | extras-ae4f7dccadfafc36470a3e6f3084c6cf3cc63415.tar.gz |
Revert "Pass file descriptor instead of file name"
This reverts commit 93eb1dc9e68d2e9dea94f56d8bce478c1a52b354
Diffstat (limited to 'ext4_utils')
-rw-r--r-- | ext4_utils/ext2simg.c | 25 | ||||
-rw-r--r-- | ext4_utils/ext4_utils.c | 20 | ||||
-rw-r--r-- | ext4_utils/ext4_utils.h | 4 | ||||
-rw-r--r-- | ext4_utils/make_ext4fs.c | 32 | ||||
-rw-r--r-- | ext4_utils/make_ext4fs.h | 2 | ||||
-rw-r--r-- | ext4_utils/make_ext4fs_main.c | 20 | ||||
-rw-r--r-- | ext4_utils/output_file.c | 51 | ||||
-rw-r--r-- | ext4_utils/output_file.h | 2 |
8 files changed, 48 insertions, 108 deletions
diff --git a/ext4_utils/ext2simg.c b/ext4_utils/ext2simg.c index d5fc24ff..9332bada 100644 --- a/ext4_utils/ext2simg.c +++ b/ext4_utils/ext2simg.c @@ -169,7 +169,7 @@ int main(int argc, char **argv) const char *out = NULL; int gzip = 0; int sparse = 1; - int infd, outfd; + int fd; int crc = 0; while ((opt = getopt(argc, argv, "cvzS")) != -1) { @@ -211,29 +211,18 @@ int main(int argc, char **argv) exit(EXIT_FAILURE); } - infd = open(in, O_RDONLY); + fd = open(in, O_RDONLY); - if (infd < 0) + if (fd < 0) critical_error_errno("failed to open input image"); - read_ext(infd); + read_ext(fd); - build_sparse_ext(infd, in); + build_sparse_ext(fd, in); - close(infd); + close(fd); - if (strcmp(out, "-")) { - outfd = open(out, O_WRONLY | O_CREAT | O_TRUNC, 0644); - if (outfd < 0) { - error_errno("open"); - return EXIT_FAILURE; - } - } else { - outfd = STDOUT_FILENO; - } - - write_ext4_image(outfd, gzip, sparse, crc, 0); - close(outfd); + write_ext4_image(out, gzip, sparse, crc, 0); return 0; } diff --git a/ext4_utils/ext4_utils.c b/ext4_utils/ext4_utils.c index 0016b652..3d5895e3 100644 --- a/ext4_utils/ext4_utils.c +++ b/ext4_utils/ext4_utils.c @@ -137,11 +137,11 @@ static void ext4_write_data_file(void *priv, u64 off, const char *file, } /* Write the filesystem image to a file */ -void write_ext4_image(int fd, int gz, int sparse, int crc, int wipe) +void write_ext4_image(const char *filename, int gz, int sparse, int crc, + int wipe) { int ret = 0; - - struct output_file *out = open_output_fd(fd, gz, sparse, + struct output_file *out = open_output_file(filename, gz, sparse, count_sparse_chunks(), crc, wipe); if (!out) @@ -446,11 +446,15 @@ void ext4_update_free() } } -static u64 get_block_device_size(int fd) +static u64 get_block_device_size(const char *filename) { + int fd = open(filename, O_RDONLY); u64 size = 0; int ret; + if (fd < 0) + return 0; + #if defined(__linux__) ret = ioctl(fd, BLKGETSIZE64, &size); #elif defined(__APPLE__) && defined(__MACH__) @@ -460,20 +464,22 @@ static u64 get_block_device_size(int fd) return 0; #endif + close(fd); + if (ret) return 0; return size; } -u64 get_file_size(int fd) +u64 get_file_size(const char *filename) { struct stat buf; int ret; u64 reserve_len = 0; s64 computed_size; - ret = fstat(fd, &buf); + ret = stat(filename, &buf); if (ret) return 0; @@ -483,7 +489,7 @@ u64 get_file_size(int fd) if (S_ISREG(buf.st_mode)) computed_size = buf.st_size - reserve_len; else if (S_ISBLK(buf.st_mode)) - computed_size = get_block_device_size(fd) - reserve_len; + computed_size = get_block_device_size(filename) - reserve_len; else computed_size = 0; diff --git a/ext4_utils/ext4_utils.h b/ext4_utils/ext4_utils.h index 033e6b7e..bb9c09e8 100644 --- a/ext4_utils/ext4_utils.h +++ b/ext4_utils/ext4_utils.h @@ -151,7 +151,7 @@ static inline int log_2(int j) } int ext4_bg_has_super_block(int bg); -void write_ext4_image(int fd, int gz, int sparse, int crc, +void write_ext4_image(const char *filename, int gz, int sparse, int crc, int wipe); void ext4_create_fs_aux_info(void); void ext4_free_fs_aux_info(void); @@ -160,7 +160,7 @@ void ext4_create_resize_inode(void); void ext4_create_journal_inode(void); void ext4_update_free(void); void ext4_queue_sb(void); -u64 get_file_size(int fd); +u64 get_file_size(const char *filename); u64 parse_num(const char *arg); void ext4_parse_sb(struct ext4_super_block *sb); diff --git a/ext4_utils/make_ext4fs.c b/ext4_utils/make_ext4fs.c index 1c7cb9d1..dbffc8d1 100644 --- a/ext4_utils/make_ext4fs.c +++ b/ext4_utils/make_ext4fs.c @@ -23,7 +23,6 @@ #include "backed_block.h" #include <dirent.h> -#include <fcntl.h> #include <libgen.h> #include <stdio.h> #include <stdlib.h> @@ -253,40 +252,27 @@ void reset_ext4fs_info() { int make_ext4fs(const char *filename, s64 len) { - int fd; - int status; - - reset_ext4fs_info(); - info.len = len; - - fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644); - if (fd < 0) { - error_errno("open"); - return EXIT_FAILURE; - } - - status = make_ext4fs_internal(fd, NULL, NULL, 0, 0, 0, 0, 1, 0); - close(fd); - - return status; + reset_ext4fs_info(); + info.len = len; + return make_ext4fs_internal(filename, NULL, NULL, 0, 0, 0, 0, 1, 0); } -int make_ext4fs_internal(int fd, const char *directory, +int make_ext4fs_internal(const char *filename, const char *directory, char *mountpoint, int android, int gzip, int sparse, int crc, int wipe, int init_itabs) { - u32 root_inode_num; - u16 root_mode; + u32 root_inode_num; + u16 root_mode; if (setjmp(setjmp_env)) return EXIT_FAILURE; /* Handle a call to longjmp() */ if (info.len <= 0) - info.len = get_file_size(fd); + info.len = get_file_size(filename); if (info.len <= 0) { fprintf(stderr, "Need size of filesystem\n"); - return EXIT_FAILURE; + return EXIT_FAILURE; } if (info.block_size <= 0) @@ -380,7 +366,7 @@ int make_ext4fs_internal(int fd, const char *directory, aux_info.sb->s_blocks_count_lo - aux_info.sb->s_free_blocks_count_lo, aux_info.sb->s_blocks_count_lo); - write_ext4_image(fd, gzip, sparse, crc, wipe); + write_ext4_image(filename, gzip, sparse, crc, wipe); return 0; } diff --git a/ext4_utils/make_ext4fs.h b/ext4_utils/make_ext4fs.h index bda9c688..3053d719 100644 --- a/ext4_utils/make_ext4fs.h +++ b/ext4_utils/make_ext4fs.h @@ -26,7 +26,7 @@ extern "C" { void reset_ext4fs_info(); int make_ext4fs(const char *filename, s64 len); -int make_ext4fs_internal(int fd, const char *directory, +int make_ext4fs_internal(const char *filename, const char *directory, char *mountpoint, int android, int gzip, int sparse, int crc, int wipe, int init_itabs); diff --git a/ext4_utils/make_ext4fs_main.c b/ext4_utils/make_ext4fs_main.c index b83f57ee..d616c6d4 100644 --- a/ext4_utils/make_ext4fs_main.c +++ b/ext4_utils/make_ext4fs_main.c @@ -14,9 +14,8 @@ * limitations under the License. */ -#include <fcntl.h> -#include <libgen.h> #include <unistd.h> +#include <libgen.h> #if defined(__linux__) #include <linux/fs.h> @@ -50,8 +49,6 @@ int main(int argc, char **argv) int crc = 0; int wipe = 0; int init_itabs = 0; - int fd; - int exitcode; while ((opt = getopt(argc, argv, "l:j:b:g:i:I:L:a:fwzJsct")) != -1) { switch (opt) { @@ -142,19 +139,6 @@ int main(int argc, char **argv) exit(EXIT_FAILURE); } - if (strcmp(filename, "-")) { - fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644); - if (fd < 0) { - error_errno("open"); - return EXIT_FAILURE; - } - } else { - fd = STDOUT_FILENO; - } - - exitcode = make_ext4fs_internal(fd, directory, mountpoint, android, gzip, + return make_ext4fs_internal(filename, directory, mountpoint, android, gzip, sparse, crc, wipe, init_itabs); - close(fd); - - return exitcode; } diff --git a/ext4_utils/output_file.c b/ext4_utils/output_file.c index eae7e322..d7cf91bc 100644 --- a/ext4_utils/output_file.c +++ b/ext4_utils/output_file.c @@ -20,13 +20,12 @@ #include "sparse_crc32.h" #include "wipe.h" -#include <fcntl.h> -#include <stdbool.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/types.h> #include <sys/mman.h> #include <unistd.h> +#include <fcntl.h> #include <zlib.h> @@ -49,7 +48,6 @@ struct output_file_ops { struct output_file { int fd; gzFile gz_fd; - bool close_fd; int sparse; u64 cur_out_ptr; u32 chunk_cnt; @@ -87,9 +85,7 @@ static int file_write(struct output_file *out, u8 *data, int len) static void file_close(struct output_file *out) { - if (out->close_fd) { - close(out->fd); - } + close(out->fd); } @@ -347,7 +343,7 @@ void close_output_file(struct output_file *out) out->ops->close(out); } -struct output_file *open_output_fd(int fd, int gz, int sparse, +struct output_file *open_output_file(const char *filename, int gz, int sparse, int chunks, int crc, int wipe) { int ret; @@ -366,17 +362,25 @@ struct output_file *open_output_fd(int fd, int gz, int sparse, if (gz) { out->ops = &gz_file_ops; - out->gz_fd = gzdopen(fd, "wb9"); + out->gz_fd = gzopen(filename, "wb9"); if (!out->gz_fd) { error_errno("gzopen"); free(out); return NULL; } } else { - out->fd = fd; + if (strcmp(filename, "-")) { + out->fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644); + if (out->fd < 0) { + error_errno("open"); + free(out); + return NULL; + } + } else { + out->fd = STDOUT_FILENO; + } out->ops = &file_ops; } - out->close_fd = false; out->sparse = sparse; out->cur_out_ptr = 0ll; out->chunk_cnt = 0; @@ -403,33 +407,6 @@ struct output_file *open_output_fd(int fd, int gz, int sparse, return out; } -struct output_file *open_output_file(const char *filename, int gz, int sparse, - int chunks, int crc, int wipe) { - - int fd; - struct output_file *file; - - if (strcmp(filename, "-")) { - fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644); - if (fd < 0) { - error_errno("open"); - return NULL; - } - } else { - fd = STDOUT_FILENO; - } - - file = open_output_fd(fd, gz, sparse, chunks, crc, wipe); - if (!file) { - close(fd); - return NULL; - } - - file->close_fd = true; // we opened descriptor thus we responsible for closing it - - return file; -} - void pad_output_file(struct output_file *out, u64 len) { int ret; diff --git a/ext4_utils/output_file.h b/ext4_utils/output_file.h index a578c80f..b4487066 100644 --- a/ext4_utils/output_file.h +++ b/ext4_utils/output_file.h @@ -21,8 +21,6 @@ struct output_file; struct output_file *open_output_file(const char *filename, int gz, int sparse, int chunks, int crc, int wipe); -struct output_file *open_output_fd(int fd, int gz, int sparse, - int chunks, int crc, int wipe); void write_data_block(struct output_file *out, u64 off, u8 *data, int len); void write_fill_block(struct output_file *out, u64 off, u32 fill_val, int len); void write_data_file(struct output_file *out, u64 off, const char *file, |