aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2019-07-10 11:26:22 +0200
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2019-07-10 11:26:22 +0200
commit20fd0c2f2ba1d16df07cfd9fad641cdd90aba193 (patch)
tree06830fd35878679069ecb74f1766b9d79f4acdc0 /lib
parentb57f3fe97074b2a374e52e9fb4920449bcb6b319 (diff)
libsqfs: eleminate use of temporary file
Tell the directory writer to keep the directory blocks in memory, then write them out once everything is done and the inodes are all written out. Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib')
-rw-r--r--lib/sqfs/serialize_fstree.c72
1 files changed, 15 insertions, 57 deletions
diff --git a/lib/sqfs/serialize_fstree.c b/lib/sqfs/serialize_fstree.c
index f3e7bba..d786244 100644
--- a/lib/sqfs/serialize_fstree.c
+++ b/lib/sqfs/serialize_fstree.c
@@ -10,42 +10,31 @@ int sqfs_serialize_fstree(int outfd, sqfs_super_t *super, fstree_t *fs,
compressor_t *cmp, id_table_t *idtbl)
{
meta_writer_t *im, *dm;
- uint8_t buffer[1024];
uint32_t offset;
uint64_t block;
- size_t i, diff;
- ssize_t ret;
- FILE *tmp;
- int tmpfd;
-
- tmp = tmpfile();
- if (tmp == NULL) {
- perror("tmpfile");
- return -1;
- }
-
- tmpfd = fileno(tmp);
+ int ret = -1;
+ size_t i;
im = meta_writer_create(outfd, cmp, false);
if (im == NULL)
- goto fail_tmp;
+ return -1;
- dm = meta_writer_create(tmpfd, cmp, false);
+ dm = meta_writer_create(outfd, cmp, true);
if (dm == NULL)
- goto fail_im;
+ goto out_im;
for (i = 2; i < fs->inode_tbl_size; ++i) {
if (meta_writer_write_inode(fs, idtbl, im, dm,
fs->inode_table[i])) {
- goto fail;
+ goto out;
}
}
if (meta_writer_flush(im))
- goto fail;
+ goto out;
if (meta_writer_flush(dm))
- goto fail;
+ goto out;
super->root_inode_ref = fs->root->inode_ref;
@@ -53,48 +42,17 @@ int sqfs_serialize_fstree(int outfd, sqfs_super_t *super, fstree_t *fs,
super->inode_table_start = super->bytes_used;
super->bytes_used += block;
- super->directory_table_start = super->bytes_used;
meta_writer_get_position(dm, &block, &offset);
+ super->directory_table_start = super->bytes_used;
super->bytes_used += block;
- if (lseek(tmpfd, 0, SEEK_SET) == (off_t)-1) {
- perror("rewind on directory temp file");
- goto fail;
- }
-
- for (;;) {
- ret = read_retry(tmpfd, buffer, sizeof(buffer));
+ if (meta_write_write_to_file(dm))
+ goto out;
- if (ret < 0) {
- perror("read from temp file");
- goto fail;
- }
- if (ret == 0)
- break;
-
- diff = ret;
- ret = write_retry(outfd, buffer, diff);
-
- if (ret < 0) {
- perror("write to image file");
- goto fail;
- }
- if ((size_t)ret < diff) {
- fputs("copying meta data to image file: "
- "truncated write\n", stderr);
- goto fail;
- }
- }
-
- meta_writer_destroy(dm);
- meta_writer_destroy(im);
- fclose(tmp);
- return 0;
-fail:
+ ret = 0;
+out:
meta_writer_destroy(dm);
-fail_im:
+out_im:
meta_writer_destroy(im);
-fail_tmp:
- fclose(tmp);
- return -1;
+ return ret;
}