summaryrefslogtreecommitdiff
path: root/ext4_utils/output_file.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/output_file.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/output_file.c')
-rw-r--r--ext4_utils/output_file.c51
1 files changed, 37 insertions, 14 deletions
diff --git a/ext4_utils/output_file.c b/ext4_utils/output_file.c
index d7cf91bc..eae7e322 100644
--- a/ext4_utils/output_file.c
+++ b/ext4_utils/output_file.c
@@ -20,12 +20,13 @@
#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>
@@ -48,6 +49,7 @@ struct output_file_ops {
struct output_file {
int fd;
gzFile gz_fd;
+ bool close_fd;
int sparse;
u64 cur_out_ptr;
u32 chunk_cnt;
@@ -85,7 +87,9 @@ static int file_write(struct output_file *out, u8 *data, int len)
static void file_close(struct output_file *out)
{
- close(out->fd);
+ if (out->close_fd) {
+ close(out->fd);
+ }
}
@@ -343,7 +347,7 @@ void close_output_file(struct output_file *out)
out->ops->close(out);
}
-struct output_file *open_output_file(const char *filename, int gz, int sparse,
+struct output_file *open_output_fd(int fd, int gz, int sparse,
int chunks, int crc, int wipe)
{
int ret;
@@ -362,25 +366,17 @@ struct output_file *open_output_file(const char *filename, int gz, int sparse,
if (gz) {
out->ops = &gz_file_ops;
- out->gz_fd = gzopen(filename, "wb9");
+ out->gz_fd = gzdopen(fd, "wb9");
if (!out->gz_fd) {
error_errno("gzopen");
free(out);
return NULL;
}
} else {
- 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->fd = fd;
out->ops = &file_ops;
}
+ out->close_fd = false;
out->sparse = sparse;
out->cur_out_ptr = 0ll;
out->chunk_cnt = 0;
@@ -407,6 +403,33 @@ struct output_file *open_output_file(const char *filename, 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;