Fix test check_file_t.file_init

* Add filepath_t enum key
* file_init writes path according to filepath_t
* file_init does NOT correctly write path FILEPATH_ABSOLUTE
* file_init parameters changed to include filepath_t and no longer includes "ENV" string
* file_close only now frees hash_str when non null ptr
* calibrate verify function updated to use correct file_init parameters
This commit is contained in:
Pradana Aumars 2021-05-31 22:07:06 +02:00
parent 4c29f89c9d
commit 8f72eabf1f
4 changed files with 27 additions and 36 deletions

View File

@ -192,7 +192,7 @@ verify(exception_t *e)
file_t *f; file_t *f;
xmlNode *x_name = b_node->children; xmlNode *x_name = b_node->children;
xmlNode *x_sum = x_name->next; xmlNode *x_sum = x_name->next;
f = file_init((char*) x_name->content, e, "HOME"); f = file_init((char*) x_name->content, FILEPATH_ABSOLUTE, e);
if (strcmp(f->hash_str, (char*) x_sum->content) != 0) if (strcmp(f->hash_str, (char*) x_sum->content) != 0)
{ {
e->type = MD5SUM_VERIFY_FAIL; e->type = MD5SUM_VERIFY_FAIL;
@ -253,7 +253,7 @@ edit(exception_t *e)
strcat(fullpath, x_group_type); strcat(fullpath, x_group_type);
strcat(fullpath, "/"); strcat(fullpath, "/");
strcat(fullpath, x_path); strcat(fullpath, x_path);
f = file_init(&fullpath, e, "HOME"); f = file_init(&fullpath, FILEPATH_ABSOLUTE, e);
replace_text(f, x_before, x_after, x_type, e); replace_text(f, x_before, x_after, x_type, e);
file_close(f); file_close(f);
} }

View File

@ -7,7 +7,6 @@
#include <errno.h> #include <errno.h>
#include <unistd.h> #include <unistd.h>
#include <limits.h> #include <limits.h>
#include <glib.h>
#include "file_t.h" #include "file_t.h"
@ -28,38 +27,27 @@ get_size_by_fd(int fd)
} }
file_t* file_t*
file_init(const char *filename, exception_t *e, const char *env) file_init(const char *filename, filepath_t FILEPATH, exception_t *e)
{ {
GString *abspath = g_string_new (NULL); char *path;
if (env != NULL) switch (FILEPATH) {
{ case FILEPATH_ABSOLUTE: {
char *envpath; path = (char*) filename; break;
size_t sizehome, sizepath; }
envpath = getenv (env); case FILEPATH_RELATIVE: path = (char*) filename; break;
sizehome = strlen(envpath); }
sizepath = strlen(filename);
abspath = malloc(sizehome + sizepath + 2);
strcpy (abspath, envpath);
strcat (abspath, "/");
strcat (abspath, filename);
abspath[sizehome + sizepath + 2] = '\0';
}
else
{
abspath = filename;
}
/* Checks if the file path can be opened. */ /* Checks if the file path can be opened. */
if (open(abspath, O_RDONLY) < 0) if (open(path, O_RDONLY) < 0)
{ {
e->type = NO_FILE; e->type = NO_FILE;
e->msg = abspath; e->msg = path;
return NULL; return NULL;
} }
file_t *f; file_t *f;
f = malloc(sizeof(file_t*)); f = malloc(sizeof(file_t*));
f->name = (char*) abspath; f->name = (char*) path;
f->hash = NULL; f->hash = NULL;
f->hash_str = NULL; f->hash_str = NULL;
return f; return f;
@ -68,9 +56,7 @@ file_init(const char *filename, exception_t *e, const char *env)
void void
file_close(file_t *f) file_close(file_t *f)
{ {
free(f->name); if (f->hash_str != NULL) free(f->hash_str);
free(f->hash);
free(f->hash_str);
free(f); free(f);
} }

View File

@ -3,6 +3,11 @@
#include "exception_t.h" #include "exception_t.h"
typedef enum filepath_t {
FILEPATH_ABSOLUTE,
FILEPATH_RELATIVE
} filepath_t;
/** /**
* Wrapper to file * Wrapper to file
*/ */
@ -20,7 +25,7 @@ typedef struct file_t {
* @return Pointer to file_t * @return Pointer to file_t
*/ */
file_t* file_t*
file_init(const char *filename, exception_t *e, const char *env); file_init(const char *filename, filepath_t FILEPATH, exception_t *e);
/** /**
* Destroy file_t * Destroy file_t

View File

@ -6,9 +6,9 @@ START_TEST(test_file_init)
{ {
file_t *f; file_t *f;
exception_t *e = NULL; exception_t *e = NULL;
f = file_init ("samples/1.txt", e, NULL); f = file_init ("samples/1.txt", FILEPATH_RELATIVE, e);
ck_assert_ptr_ne (f, NULL); ck_assert_ptr_nonnull (f);
ck_assert_ptr_eq (e, NULL); ck_assert_ptr_null (e);
file_close (f); file_close (f);
} }
END_TEST END_TEST
@ -17,11 +17,11 @@ START_TEST(test_file_md5)
{ {
file_t *f; file_t *f;
exception_t *e = NULL; exception_t *e = NULL;
f = file_init("samples/1.txt", e, NULL); f = file_init("samples/1.txt", FILEPATH_RELATIVE, e);
file_md5_gen (f, e); file_md5_gen (f, e);
ck_assert_ptr_eq (e, NULL); ck_assert_ptr_null (e);
file_md5_str (f, e); file_md5_str (f, e);
ck_assert_ptr_eq (e, NULL); ck_assert_ptr_null (e);
ck_assert_str_eq (f->hash_str, "492ff087b26577237c576e7ad409064e"); ck_assert_str_eq (f->hash_str, "492ff087b26577237c576e7ad409064e");
file_close (f); file_close (f);
} }