summaryrefslogtreecommitdiff
path: root/ext4_utils/make_ext4fs.c
diff options
context:
space:
mode:
authorAnatol Pomazau <anatol@google.com>2012-01-11 15:12:27 -0800
committerAnatol Pomazau <anatol@google.com>2012-02-03 14:40:49 -0800
commit89ddaab97e9214cf331baffee2de4595ad14dc79 (patch)
tree08f4de0ccce24a3b3672db9ec3f60e461c52f74c /ext4_utils/make_ext4fs.c
parentae4f7dccadfafc36470a3e6f3084c6cf3cc63415 (diff)
downloadextras-89ddaab97e9214cf331baffee2de4595ad14dc79.tar.gz
Pass file descriptor instead of file name
Passing a file descriptor to make_ext4fs_internal() is more flexible. We can use tmpfile() to create a temporary file. tmpfile() is better than other solutions because it unlinks the file right after creating it, so closing fd effectively removes temp file. Thus we don't have to worry about large temp files accidently left on the filesystem in case of the program crash. Change-Id: I44146704572c314e1d6cfca7ce918efa7fb92a7a
Diffstat (limited to 'ext4_utils/make_ext4fs.c')
-rw-r--r--ext4_utils/make_ext4fs.c32
1 files changed, 23 insertions, 9 deletions
diff --git a/ext4_utils/make_ext4fs.c b/ext4_utils/make_ext4fs.c
index dbffc8d1..1c7cb9d1 100644
--- a/ext4_utils/make_ext4fs.c
+++ b/ext4_utils/make_ext4fs.c
@@ -23,6 +23,7 @@
#include "backed_block.h"
#include <dirent.h>
+#include <fcntl.h>
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
@@ -252,27 +253,40 @@ void reset_ext4fs_info() {
int make_ext4fs(const char *filename, s64 len)
{
- reset_ext4fs_info();
- info.len = len;
- return make_ext4fs_internal(filename, NULL, NULL, 0, 0, 0, 0, 1, 0);
+ 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;
}
-int make_ext4fs_internal(const char *filename, const char *directory,
+int make_ext4fs_internal(int fd, 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(filename);
+ info.len = get_file_size(fd);
if (info.len <= 0) {
fprintf(stderr, "Need size of filesystem\n");
- return EXIT_FAILURE;
+ return EXIT_FAILURE;
}
if (info.block_size <= 0)
@@ -366,7 +380,7 @@ int make_ext4fs_internal(const char *filename, 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(filename, gzip, sparse, crc, wipe);
+ write_ext4_image(fd, gzip, sparse, crc, wipe);
return 0;
}