Skip to content

Commit

Permalink
Now also reading default-args from .bcrepo/CONFIG
Browse files Browse the repository at this point in the history
  • Loading branch information
rafael-santiago committed May 14, 2019
1 parent 339db39 commit e893ca2
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 13 deletions.
110 changes: 97 additions & 13 deletions src/cmd/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*
*/
#include <cmd/options.h>
#include <fs/bcrepo/config.h>
#include <string.h>
#include <stdio.h>

Expand All @@ -15,6 +16,8 @@ static char **g_blackcat_argv = NULL;

static int g_blackcat_argc = 0;

static char **g_blackcat_argv_head = NULL;

char *blackcat_get_option(const char *option, char *default_option) {
char temp[4096];
int a;
Expand Down Expand Up @@ -58,14 +61,87 @@ char *blackcat_get_command(void) {
}

void blackcat_set_argc_argv(int argc, char **argv) {
struct bcrepo_config_ctx *cfg = NULL;
char *cmdline = NULL, *cp;
size_t cmdline_size, temp_size;
int a;

if (g_blackcat_argv_head != NULL) {
blackcat_clear_options();
g_blackcat_argv_head = NULL;
}

if (argv == NULL) {
g_blackcat_cmd = NULL;
g_blackcat_argv = NULL;
g_blackcat_argc = 0;
} else {
g_blackcat_cmd = argv[1];
g_blackcat_argv = &argv[2];
g_blackcat_argc = argc - 2;

if ((cfg = bcrepo_ld_config()) == NULL) {
g_blackcat_cmd = argv[1];
g_blackcat_argv = &argv[2];
g_blackcat_argc = argc - 2;
} else {
if (bcrepo_config_get_section(cfg, BCREPO_CONFIG_SECTION_DEFAULT_ARGS) != 0) {
cmdline_size = 0;
for (a = 1; a < argc; a++) {
cmdline_size += strlen(argv[a]) + 1;
}

while (bcrepo_config_get_next_word(cfg) != 0) {
cmdline_size = cfg->word_end - cfg->word + 1;
}

if ((cmdline = (char *) kryptos_newseg(cmdline_size)) == NULL) {
goto blackcat_set_argc_argv_epilogue;
}


memset(cmdline, 0, cmdline_size);
cp = cmdline;

for (a = 1; a < argc; a++) {
temp_size = strlen(argv[a]);
memcpy(cp, argv[a], temp_size);
*(cp + temp_size) = ' ';
cp += temp_size + 1;
}

bcrepo_config_get_section(cfg, BCREPO_CONFIG_SECTION_DEFAULT_ARGS);

while (bcrepo_config_get_next_word(cfg) != 0) {
temp_size = cfg->word_end - cfg->word;
memcpy(cp, cfg->word, temp_size);
*(cp + temp_size) = ' ';
cp += temp_size + 1;
}

// INFO(Rafael): Nasty trick for clearing original command line arguments easily.

g_blackcat_argv_head = NULL;

g_blackcat_cmd = argv[1];
g_blackcat_argv = &argv[2];
g_blackcat_argc = argc - 2;
blackcat_clear_options();

g_blackcat_argv_head = mkargv(g_blackcat_argv_head, cmdline, cmdline_size, &g_blackcat_argc);

g_blackcat_cmd = g_blackcat_argv_head[1];
g_blackcat_argv = &g_blackcat_argv_head[2];
g_blackcat_argc = g_blackcat_argc - 1;
}
}
}

blackcat_set_argc_argv_epilogue:

if (cfg != NULL) {
bcrepo_release_config(cfg);
}

if (cmdline != NULL) {
kryptos_freeseg(cmdline, cmdline_size);
}
}

Expand All @@ -81,16 +157,20 @@ void blackcat_clear_options(void) {
// WARN(Rafael): This is not an alibi to pass sensible data through command line.
size_t size;

if (g_blackcat_cmd != NULL) {
size = strlen(g_blackcat_cmd);
memset(g_blackcat_cmd, 0, size);
}
if (g_blackcat_argv_head == NULL) {
if (g_blackcat_cmd != NULL) {
size = strlen(g_blackcat_cmd);
memset(g_blackcat_cmd, 0, size);
}

if (g_blackcat_argv != NULL) {
while (g_blackcat_argc-- > -1) {
size = strlen(g_blackcat_argv[g_blackcat_argc]);
memset(g_blackcat_argv[g_blackcat_argc], 0, size);
if (g_blackcat_argv != NULL) {
while (g_blackcat_argc-- > -1) {
size = strlen(g_blackcat_argv[g_blackcat_argc]);
memset(g_blackcat_argv[g_blackcat_argc], 0, size);
}
}
} else {
freeargv(g_blackcat_argv_head, g_blackcat_argc + 1);
}

g_blackcat_cmd = NULL;
Expand Down Expand Up @@ -125,7 +205,9 @@ char **mkargv(char **argv, const char *buf, const size_t buf_size, int *argc) {
bp++;
}

argv = (char **) kryptos_newseg(sizeof(char *) * (*argc));
if ((argv = (char **) kryptos_newseg(sizeof(char *) * (*argc))) == NULL) {
return NULL;
}

argv[0] = NULL; // INFO(Rafael): Dummy entry.
a = 1;
Expand All @@ -138,7 +220,9 @@ char **mkargv(char **argv, const char *buf, const size_t buf_size, int *argc) {
bp++;
} else if (*bp == ' ' || *bp == 0) {
a_size = bp - bp_off + 1;
argv[a] = (char *) kryptos_newseg(a_size);
if ((argv[a] = (char *) kryptos_newseg(a_size)) == NULL) {
return NULL; // INFO(Rafael): Assuming it almost impossible, let leaking.
}
memset(argv[a], 0, a_size);
memcpy(argv[a], bp_off, a_size - 1);
a++;
Expand Down
9 changes: 9 additions & 0 deletions src/fs/bcrepo/bcrepo.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <fcntl.h>
#include <errno.h>
#include <utime.h>
#include <assert.h>

// INFO(Rafael): This version not always will match the cmd tool version. It does not mean that the
// tool will generate unsupported data for the fs module.
Expand Down Expand Up @@ -1573,6 +1574,8 @@ static int unl_handle_meta_proc(const char *rootpath, const size_t rootpath_size

in = bcrepo_read_file_data(rootpath, rootpath_size, path, path_size, &in_size);

assert(in != NULL);

if (in == NULL) {
no_error = 0;
goto unl_handle_meta_proc_epilogue;
Expand All @@ -1590,20 +1593,26 @@ static int unl_handle_meta_proc(const char *rootpath, const size_t rootpath_size
while ((no_error = bfs_data_wiping(rootpath, rootpath_size, path, path_size, in_size)) == 0 && ntry-- > 0)
;

assert(no_error != 0);

if (ntry == 0 && no_error == 0) {
goto unl_handle_meta_proc_epilogue;
}
}

out = dproc(protlayer, in, in_size, &out_size);

assert(out != NULL);

if (out == NULL) {
no_error = 0;
goto unl_handle_meta_proc_epilogue;
}

no_error = bcrepo_write_file_data(rootpath, rootpath_size, path, path_size, out, out_size);

assert(no_error != 0);

unl_handle_meta_proc_epilogue:

if (in != NULL) {
Expand Down
2 changes: 2 additions & 0 deletions src/fs/bcrepo/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#define BCREPO_CONFIG_FILE "CONFIG"
#define BCREPO_CONFIG_FILE_SIZE 6

#define BCREPO_CONFIG_SECTION_DEFAULT_ARGS "default-args:"

struct bcrepo_config_ctx {
struct bcrepo_config_priv_ctx *priv;
char *line, *line_end;
Expand Down

0 comments on commit e893ca2

Please sign in to comment.