2025-01-30 18:10:38 +00:00
|
|
|
#include "bin.h"
|
2024-11-02 11:17:36 +00:00
|
|
|
#include "commands.h"
|
2025-01-30 18:10:38 +00:00
|
|
|
#include "write.h"
|
2024-11-02 11:17:36 +00:00
|
|
|
|
|
|
|
|
#include <blue/cmd.h>
|
2025-01-30 18:10:38 +00:00
|
|
|
#include <blue/term.h>
|
|
|
|
|
#include <errno.h>
|
2024-11-02 11:17:36 +00:00
|
|
|
|
|
|
|
|
enum {
|
|
|
|
|
OPT_OUTPATH,
|
|
|
|
|
OPT_OUTPATH_PATH,
|
|
|
|
|
|
|
|
|
|
ARG_FILE,
|
|
|
|
|
|
|
|
|
|
OPT_TAGGED_FILE,
|
|
|
|
|
OPT_TAGGED_FILE_TAG,
|
|
|
|
|
OPT_TAGGED_FILE_PATH,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static int wrap(
|
|
|
|
|
const b_command *self,
|
|
|
|
|
const b_arglist *opt,
|
|
|
|
|
const b_array *args)
|
|
|
|
|
{
|
2025-01-30 18:10:38 +00:00
|
|
|
const char *in_path = NULL, *out_path = NULL;
|
|
|
|
|
b_arglist_get_string(opt, B_COMMAND_INVALID_ID, ARG_FILE, 0, &in_path);
|
|
|
|
|
b_arglist_get_string(opt, OPT_OUTPATH, OPT_OUTPATH_PATH, 0, &out_path);
|
|
|
|
|
|
|
|
|
|
printf("in path: %s\n", in_path);
|
|
|
|
|
printf("out path: %s\n", out_path);
|
|
|
|
|
|
|
|
|
|
FILE *inp = fopen(in_path, "rb");
|
|
|
|
|
if (!inp) {
|
|
|
|
|
b_err("cannot open '%s'", in_path);
|
|
|
|
|
b_i("reason: %s", strerror(errno));
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FILE *outp = fopen(out_path, "wb");
|
|
|
|
|
if (!outp) {
|
|
|
|
|
b_err("cannot open '%s'", out_path);
|
|
|
|
|
b_i("reason: %s", strerror(errno));
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct ec3_writer *writer = NULL;
|
|
|
|
|
struct ec3_parameters param = {
|
|
|
|
|
.p_outp = outp,
|
|
|
|
|
.p_cluster_size = EC3_CLUSTER_16K,
|
|
|
|
|
.p_compression_func = EC3_COMPRESSION_ZSTD,
|
|
|
|
|
};
|
|
|
|
|
enum ec3_status status = ec3_writer_create(¶m, &writer);
|
|
|
|
|
|
|
|
|
|
if (status != EC3_SUCCESS) {
|
|
|
|
|
b_err("cannot initialise EC3 writer");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct ec3_tag_writer *tag = NULL;
|
|
|
|
|
status = ec3_writer_create_tag(writer, EC3_TAG_BLOB, 0, 0, &tag);
|
|
|
|
|
|
|
|
|
|
if (status != EC3_SUCCESS) {
|
|
|
|
|
b_err("cannot initialise EC3 tag writer");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char buf[4096];
|
|
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
|
size_t r = fread(buf, 1, sizeof buf, inp);
|
|
|
|
|
status = ec3_tag_writer_write(tag, buf, r);
|
|
|
|
|
|
|
|
|
|
if (r < sizeof buf) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ec3_tag_writer_finish(tag);
|
|
|
|
|
ec3_writer_finish(writer);
|
|
|
|
|
|
|
|
|
|
fclose(inp);
|
|
|
|
|
fclose(outp);
|
|
|
|
|
|
2024-11-02 11:17:36 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
B_COMMAND(CMD_WRAP, CMD_ROOT)
|
|
|
|
|
{
|
|
|
|
|
B_COMMAND_NAME("wrap");
|
|
|
|
|
B_COMMAND_SHORT_NAME('W');
|
|
|
|
|
B_COMMAND_DESC(
|
|
|
|
|
"wrap one or more files into an ec3 container. each file will "
|
2025-01-30 18:10:38 +00:00
|
|
|
"be stored in a separate blob tag within the created "
|
|
|
|
|
"container.");
|
2024-11-02 11:17:36 +00:00
|
|
|
B_COMMAND_FLAGS(B_COMMAND_SHOW_HELP_BY_DEFAULT);
|
|
|
|
|
B_COMMAND_FUNCTION(wrap);
|
|
|
|
|
|
|
|
|
|
B_COMMAND_HELP_OPTION();
|
|
|
|
|
|
|
|
|
|
B_COMMAND_OPTION(OPT_OUTPATH)
|
|
|
|
|
{
|
|
|
|
|
B_OPTION_SHORT_NAME('o');
|
|
|
|
|
B_OPTION_LONG_NAME("out");
|
|
|
|
|
B_OPTION_DESC("the path to save the new file to");
|
|
|
|
|
|
|
|
|
|
B_OPTION_ARG(OPT_OUTPATH_PATH)
|
|
|
|
|
{
|
|
|
|
|
B_ARG_NAME("path");
|
|
|
|
|
B_ARG_NR_VALUES(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
B_COMMAND_ARG(ARG_FILE)
|
|
|
|
|
{
|
|
|
|
|
B_ARG_NAME("file");
|
|
|
|
|
B_ARG_DESC("a file to add to the container");
|
|
|
|
|
|
|
|
|
|
B_ARG_NR_VALUES(B_ARG_1_OR_MORE_VALUES);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
B_COMMAND_OPTION(OPT_TAGGED_FILE)
|
|
|
|
|
{
|
|
|
|
|
B_OPTION_SHORT_NAME('I');
|
|
|
|
|
B_OPTION_LONG_NAME("tagged-file");
|
|
|
|
|
B_OPTION_DESC(
|
|
|
|
|
"a file to add to the container, with an associated "
|
|
|
|
|
"tag. "
|
|
|
|
|
"the tag must be either: (a) a 64-bit hexadecimal "
|
|
|
|
|
"number; "
|
|
|
|
|
"or (b) a string of no more than 8 characters.");
|
|
|
|
|
|
|
|
|
|
B_OPTION_ARG(OPT_TAGGED_FILE_TAG)
|
|
|
|
|
{
|
|
|
|
|
B_ARG_NAME("tag");
|
|
|
|
|
B_ARG_DESC("the tag!");
|
|
|
|
|
B_ARG_NR_VALUES(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
B_OPTION_ARG(OPT_TAGGED_FILE_PATH)
|
|
|
|
|
{
|
|
|
|
|
B_ARG_NAME("path");
|
|
|
|
|
B_ARG_NR_VALUES(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
B_COMMAND_USAGE()
|
|
|
|
|
{
|
|
|
|
|
B_COMMAND_USAGE_OPT(OPT_OUTPATH);
|
|
|
|
|
B_COMMAND_USAGE_ARG(ARG_FILE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
B_COMMAND_USAGE()
|
|
|
|
|
{
|
|
|
|
|
B_COMMAND_USAGE_OPT(OPT_OUTPATH);
|
|
|
|
|
B_COMMAND_USAGE_OPT(OPT_TAGGED_FILE);
|
|
|
|
|
}
|
|
|
|
|
}
|