replace ec3_reader and ec3_writer with a unified image and tag io interface
This commit is contained in:
68
src/wrap.c
68
src/wrap.c
@@ -1,10 +1,12 @@
|
||||
#include "bin.h"
|
||||
#include "commands.h"
|
||||
#include "write.h"
|
||||
#include "image.h"
|
||||
|
||||
#include <blue/cmd.h>
|
||||
#include <blue/io/path.h>
|
||||
#include <blue/term.h>
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
enum {
|
||||
OPT_OUTPATH,
|
||||
@@ -28,7 +30,7 @@ enum {
|
||||
};
|
||||
|
||||
static enum ec3_status add_file(
|
||||
struct ec3_writer *writer,
|
||||
struct ec3_image_ioctx *image,
|
||||
unsigned long type,
|
||||
uint64_t id,
|
||||
const char *path)
|
||||
@@ -40,32 +42,40 @@ static enum ec3_status add_file(
|
||||
return EC3_ERR_NO_ENTRY;
|
||||
}
|
||||
|
||||
struct ec3_tag_writer *tag = NULL;
|
||||
enum ec3_status status = ec3_writer_create_tag(
|
||||
writer,
|
||||
EC3_TAG_WRITER_BUFFERED,
|
||||
struct ec3_tag_ioctx *tag = NULL;
|
||||
enum ec3_status status = ec3_image_ioctx_create_tag(
|
||||
image,
|
||||
type,
|
||||
id,
|
||||
0,
|
||||
EC3_TAG_IO_WRITE | EC3_TAG_IO_SEQUENTIAL,
|
||||
&tag);
|
||||
|
||||
if (status != EC3_SUCCESS) {
|
||||
b_err("cannot initialise EC3 tag writer");
|
||||
b_i("reason: %s", ec3_status_to_string(status));
|
||||
return status;
|
||||
}
|
||||
|
||||
char buf[4096];
|
||||
const struct ec3_image_info *image_info
|
||||
= ec3_image_ioctx_get_info(image);
|
||||
size_t cluster_size = image_info->img_cluster_size;
|
||||
|
||||
char *buf = malloc(cluster_size);
|
||||
|
||||
size_t i = 0;
|
||||
|
||||
while (1) {
|
||||
size_t r = fread(buf, 1, sizeof buf, inp);
|
||||
status = ec3_tag_writer_write(tag, buf, r);
|
||||
size_t r = fread(buf, 1, cluster_size, inp);
|
||||
size_t w;
|
||||
status = ec3_tag_ioctx_write_cluster(tag, i++, buf, r, &w);
|
||||
|
||||
if (r < sizeof buf) {
|
||||
if (r < cluster_size) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
ec3_tag_writer_finish(tag);
|
||||
free(buf);
|
||||
ec3_tag_ioctx_close(tag);
|
||||
fclose(inp);
|
||||
|
||||
return EC3_SUCCESS;
|
||||
@@ -79,13 +89,6 @@ static int wrap(
|
||||
const char *out_path = NULL;
|
||||
b_arglist_get_string(opt, OPT_OUTPATH, OPT_OUTPATH_PATH, 0, &out_path);
|
||||
|
||||
FILE *outp = fopen(out_path, "wb");
|
||||
if (!outp) {
|
||||
b_err("cannot open '%s'", out_path);
|
||||
b_i("reason: %s", strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
enum ec3_status status = EC3_SUCCESS;
|
||||
|
||||
uint64_t ident = 0;
|
||||
@@ -100,14 +103,23 @@ static int wrap(
|
||||
return -1;
|
||||
}
|
||||
|
||||
struct ec3_writer *writer = NULL;
|
||||
b_path *image_path = b_path_create_from_cstr(out_path);
|
||||
if (b_path_exists(image_path)) {
|
||||
b_path_unlink(image_path);
|
||||
}
|
||||
b_path_release(image_path);
|
||||
|
||||
struct ec3_image_ioctx *image = NULL;
|
||||
struct ec3_parameters param = {
|
||||
.p_outp = outp,
|
||||
.p_cluster_size = EC3_CLUSTER_16K,
|
||||
.p_compression_func = EC3_COMPRESSION_ZSTD,
|
||||
.p_ident = ident,
|
||||
};
|
||||
status = ec3_writer_create(¶m, &writer);
|
||||
status = ec3_image_ioctx_open(
|
||||
out_path,
|
||||
¶m,
|
||||
EC3_IMAGE_IO_WRITE,
|
||||
&image);
|
||||
|
||||
if (status != EC3_SUCCESS) {
|
||||
b_err("cannot initialise EC3 writer");
|
||||
@@ -122,7 +134,7 @@ static int wrap(
|
||||
printf("%s\n", it.value->val_str);
|
||||
#if 1
|
||||
status = add_file(
|
||||
writer,
|
||||
image,
|
||||
EC3_TAG_BLOB,
|
||||
next_auto_id,
|
||||
it.value->val_str);
|
||||
@@ -141,7 +153,7 @@ static int wrap(
|
||||
printf("%s\n", it.value->val_str);
|
||||
#if 1
|
||||
status = add_file(
|
||||
writer,
|
||||
image,
|
||||
EC3_TAG_EXEC,
|
||||
next_auto_id,
|
||||
it.value->val_str);
|
||||
@@ -189,7 +201,7 @@ static int wrap(
|
||||
return -1;
|
||||
}
|
||||
|
||||
status = add_file(writer, EC3_TAG_BLOB, id, path->val_str);
|
||||
status = add_file(image, EC3_TAG_BLOB, id, path->val_str);
|
||||
|
||||
if (status != EC3_SUCCESS) {
|
||||
b_err("an error occurred while writing to the "
|
||||
@@ -233,7 +245,7 @@ static int wrap(
|
||||
return -1;
|
||||
}
|
||||
|
||||
status = add_file(writer, EC3_TAG_EXEC, id, path->val_str);
|
||||
status = add_file(image, EC3_TAG_EXEC, id, path->val_str);
|
||||
|
||||
if (status != EC3_SUCCESS) {
|
||||
b_err("an error occurred while writing to the "
|
||||
@@ -243,9 +255,7 @@ static int wrap(
|
||||
#endif
|
||||
}
|
||||
|
||||
ec3_writer_finish(writer);
|
||||
|
||||
fclose(outp);
|
||||
ec3_image_ioctx_close(image);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user