summaryrefslogtreecommitdiff
path: root/ext4_utils/ext4_utils.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-17 09:09:11 -0800
commit0349bd9f14d252673a7a25767da4a80121aaaaf2 (patch)
treebf5e4d65aa227bac5d9bb42063e6b5a76fbf5d28 /ext4_utils/ext4_utils.c
parent879d01e18825e09c1196cc5bdeb6cbfddd92571a (diff)
downloadextras-0349bd9f14d252673a7a25767da4a80121aaaaf2.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: Iba3758a0e13a898920d30d7fa5da696c22daa2b7
Diffstat (limited to 'ext4_utils/ext4_utils.c')
-rw-r--r--ext4_utils/ext4_utils.c20
1 files changed, 7 insertions, 13 deletions
diff --git a/ext4_utils/ext4_utils.c b/ext4_utils/ext4_utils.c
index 9bb523f5..7f226c8b 100644
--- a/ext4_utils/ext4_utils.c
+++ b/ext4_utils/ext4_utils.c
@@ -142,11 +142,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(const char *filename, int gz, int sparse, int crc,
- int wipe)
+void write_ext4_image(int fd, int gz, int sparse, int crc, int wipe)
{
int ret = 0;
- struct output_file *out = open_output_file(filename, gz, sparse,
+
+ struct output_file *out = open_output_fd(fd, gz, sparse,
count_sparse_chunks(), crc, wipe);
if (!out)
@@ -451,15 +451,11 @@ void ext4_update_free()
}
}
-static u64 get_block_device_size(const char *filename)
+static u64 get_block_device_size(int fd)
{
- 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__)
@@ -469,22 +465,20 @@ static u64 get_block_device_size(const char *filename)
return 0;
#endif
- close(fd);
-
if (ret)
return 0;
return size;
}
-u64 get_file_size(const char *filename)
+u64 get_file_size(int fd)
{
struct stat buf;
int ret;
u64 reserve_len = 0;
s64 computed_size;
- ret = stat(filename, &buf);
+ ret = fstat(fd, &buf);
if (ret)
return 0;
@@ -494,7 +488,7 @@ u64 get_file_size(const char *filename)
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(filename) - reserve_len;
+ computed_size = get_block_device_size(fd) - reserve_len;
else
computed_size = 0;