summaryrefslogtreecommitdiff
path: root/lib/sqfshelper/data_writer.c
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2019-09-26 17:21:44 +0200
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2019-09-26 17:22:53 +0200
commitff4a9235feb4237854ff102c4dc7cf4645d6a116 (patch)
tree18017f7f1b6776ef76360fef0ff127d42f3007cd /lib/sqfshelper/data_writer.c
parent248765bd9f6ead4fbe4e5822bcaf46b85fe1687f (diff)
Add file API stub to sqfs data writer
Basically move the state tracking from the old data writer over to the new one. Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib/sqfshelper/data_writer.c')
-rw-r--r--lib/sqfshelper/data_writer.c87
1 files changed, 9 insertions, 78 deletions
diff --git a/lib/sqfshelper/data_writer.c b/lib/sqfshelper/data_writer.c
index 5c01cb8..ca47447 100644
--- a/lib/sqfshelper/data_writer.c
+++ b/lib/sqfshelper/data_writer.c
@@ -9,47 +9,17 @@
#include "highlevel.h"
#include "util.h"
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-#include <stdio.h>
-
-static bool is_zero_block(unsigned char *ptr, size_t size)
-{
- return ptr[0] == 0 && memcmp(ptr, ptr + 1, size - 1) == 0;
-}
-
-static int add_sentinel_block(sqfs_data_writer_t *data,
- sqfs_inode_generic_t *inode,
- uint32_t flags)
-{
- sqfs_block_t *blk = calloc(1, sizeof(*blk));
-
- if (blk == NULL) {
- perror("creating sentinel block");
- return -1;
- }
-
- blk->inode = inode;
- blk->flags = flags;
-
- return sqfs_data_writer_enqueue(data, blk);
-}
-
int write_data_from_file(sqfs_data_writer_t *data, sqfs_inode_generic_t *inode,
sqfs_file_t *file, size_t block_size, int flags)
{
- uint32_t blk_flags = SQFS_BLK_FIRST_BLOCK;
uint64_t filesz, offset;
- size_t diff, i = 0;
sqfs_block_t *blk;
+ size_t diff;
int ret;
+ (void)flags;
- if (flags & DW_DONT_COMPRESS)
- blk_flags |= SQFS_BLK_DONT_COMPRESS;
-
- if (flags & DW_ALLIGN_DEVBLK)
- blk_flags |= SQFS_BLK_ALLIGN;
+ if (sqfs_data_writer_begin_file(data, inode, 0))
+ return -1;
sqfs_inode_get_file_size(inode, &filesz);
@@ -60,54 +30,15 @@ int write_data_from_file(sqfs_data_writer_t *data, sqfs_inode_generic_t *inode,
diff = filesz - offset;
}
- ret = sqfs_file_create_block(file, offset, diff, inode,
- blk_flags, &blk);
+ ret = sqfs_file_create_block(file, offset, diff,
+ NULL, 0, &blk);
if (ret)
return -1;
- blk->index = i++;
-
- if (is_zero_block(blk->data, blk->size)) {
- sqfs_inode_make_extended(inode);
- inode->data.file_ext.sparse += blk->size;
- inode->num_file_blocks += 1;
-
- inode->block_sizes[blk->index] = 0;
- free(blk);
- continue;
- }
-
- if (diff < block_size && !(flags & DW_DONT_FRAGMENT)) {
- if (!(blk_flags & (SQFS_BLK_FIRST_BLOCK |
- SQFS_BLK_LAST_BLOCK))) {
- blk_flags |= SQFS_BLK_LAST_BLOCK;
-
- if (add_sentinel_block(data, inode,
- blk_flags)) {
- free(blk);
- return -1;
- }
- }
-
- blk->flags |= SQFS_BLK_IS_FRAGMENT;
-
- if (sqfs_data_writer_enqueue(data, blk))
- return -1;
- } else {
- if (sqfs_data_writer_enqueue(data, blk))
- return -1;
-
- blk_flags &= ~SQFS_BLK_FIRST_BLOCK;
- inode->num_file_blocks += 1;
- }
- }
-
- if (!(blk_flags & (SQFS_BLK_FIRST_BLOCK | SQFS_BLK_LAST_BLOCK))) {
- blk_flags |= SQFS_BLK_LAST_BLOCK;
-
- if (add_sentinel_block(data, inode, blk_flags))
+ if (sqfs_data_writer_enqueue(data, blk))
return -1;
}
- return 0;
+
+ return sqfs_data_writer_end_file(data);
}