diff options
Diffstat (limited to 'src/file_processor.c')
| -rw-r--r-- | src/file_processor.c | 110 |
1 files changed, 105 insertions, 5 deletions
diff --git a/src/file_processor.c b/src/file_processor.c index 1cfed46..f4f9d05 100644 --- a/src/file_processor.c +++ b/src/file_processor.c @@ -21,7 +21,9 @@ #include "file_processor.h" #include "trace_macros.h" - +#include "hex_conversion.h" +#include "kv_manager.h" +#include "database_interaction.h" /*=========== DEFINES, CONSTANTS AND TYPES ===========*/ @@ -214,7 +216,7 @@ int process_file(struct df_fileinfo *info) { LOGERR("ERROR: Non-regular files are not processed.\n"); return -1; } - + if ((ctx_pkg = init_md_components()) == NULL) { LOGERR("ERROR: Failed to initialize/create md contexts to be used with %s\n", fullpath); @@ -270,23 +272,121 @@ int process_file(struct df_fileinfo *info) { destroy_md_components(ctx_pkg); return -1; } - memcpy(info->blake2, md_val, md_len); + convert_from_binary(md_val, md_len, info->hashes.blake2); if (EVP_DigestFinal_ex(ctx_pkg->mdctx_sha256, md_val, &md_len) != 1) { LOGERR("ERROR: Failed to finalize MD SHA256 of file '%s'\n", fullpath); destroy_md_components(ctx_pkg); return -1; } - memcpy(info->sha256, md_val, md_len); + convert_from_binary(md_val, md_len, info->hashes.sha256); if (EVP_DigestFinal_ex(ctx_pkg->mdctx_sha512, md_val, &md_len) != 1) { LOGERR("ERROR: Failed to finalize MD SHA512 of file '%s'\n", fullpath); destroy_md_components(ctx_pkg); return -1; } - memcpy(info->sha512, md_val, md_len); + convert_from_binary(md_val, md_len, info->hashes.sha512); return 0; } +/** + * Return a file info struct with path and filename fields filled. + * @param key the fullpath used as key in the gdbm. + * @return NULL on failure + */ +struct df_fileinfo *prepare_fileinfo(char *key) { + char *tmp; + char *fname = NULL; + size_t plen=0; + struct df_fileinfo *info = NULL; + + if (key == NULL || key[0] == '\0') { + return NULL; + } + if ((fname=strrchr(key, '/')) == NULL) { + LOGERR("ERROR: path<->filename separation failed with '%s'\n", key); + return NULL; + } + + /* At this point the address of fname will always be equal or larger than keys */ + plen = (size_t) (fname - key); + fname++; /* drop the / */ + + if ((info=calloc(1, sizeof(struct df_fileinfo))) == NULL) { + return NULL; + } + + if ((tmp = calloc(plen+1, sizeof(char))) == NULL) { + free(info); + return NULL; + } + memcpy(tmp, key, plen); + info->path = tmp; + + if ((tmp = calloc(strlen(fname)+1, sizeof(char))) == NULL) { + free(info->path); + free(info); + return NULL; + } + memcpy(tmp, fname, plen); + info->name = tmp; + + return info; +} + +/** + * Iterate over the whole gdbm content. If an entry is an unprocessed file, + * process it, place the information in the database and set it as processed + * in the storage. + * @return 0 on success + * <0 on failure + */ +int process_gdbm_content() { + char *key, *tmpkey; + struct df_fileinfo *info; + int dbrc = 0; + + key = kv_first_key(); + while (key != NULL) { + /* file? already processed? */ + if (kv_get_type(key) == 'D' || kv_get_bool(key)) { + tmpkey = key; + key = kv_next_key(tmpkey); + free(tmpkey); + continue; + } + + info = prepare_fileinfo(key); + if (info == NULL) { + LOGERR("ERROR: Preparing struct for key %s failed.\n", key); + return -1; + } + if (process_file(info) < 0) { + free(info->path); + free(info->name); + free(info); + return -1; + } + + dbrc = dbi_insert_fileinfo(info); + free(info->path); + free(info->name); + free(info); + if (dbrc < 0) { + LOGERR("ERROR: Aborting after database error.\n"); + return -1; + } + + + kv_set_bool(key, true); + + tmpkey = key; + key = kv_next_key(tmpkey); + free(tmpkey); + } + + return 0; +} |
