Add absolute paths to file_t

This commit is contained in:
Pradana Aumars 2021-06-03 00:33:26 +02:00
parent 0794691f5a
commit f9f8ce747a
2 changed files with 22 additions and 3 deletions

View File

@ -28,10 +28,10 @@ get_size_by_fd(int fd)
file_t*
file_init(const char *filename, filepath_t FILEPATH, exception_t *e)
{
char *path;
char *path = NULL;
switch (FILEPATH) {
case FILEPATH_ABSOLUTE: {
path = (char*) filename; break;
realpath(filename, path);
}
case FILEPATH_RELATIVE: path = (char*) filename; break;
}
@ -46,14 +46,16 @@ file_init(const char *filename, filepath_t FILEPATH, exception_t *e)
file_t *f;
f = malloc(sizeof(file_t));
f->name = (char*) path;
f->name = path;
f->hash_str = NULL;
f->fp = FILEPATH;
return f;
}
void
file_close(file_t *f)
{
if (f->fp == FILEPATH_ABSOLUTE) free(f->name);
if (f->hash_str != NULL) free(f->hash_str);
free(f);
}
@ -100,3 +102,16 @@ file_md5_str(file_t *f, exception_t *e)
f->hash_str[2*MD5_DIGEST_LENGTH] = '\0';
return f->hash_str;
}
char*
file_abs(const file_t *f) {
char *path = NULL;
switch (f->fp) {
case FILEPATH_ABSOLUTE: return f->name;
case FILEPATH_RELATIVE: {
realpath(f->name, path);
break;
}
}
return path;
}

View File

@ -17,6 +17,7 @@ typedef struct file_t {
char *name; /**< Absolute path to file */
unsigned char hash[MD5_DIGEST_LENGTH]; /**< MD5sum hash */
char *hash_str; /**< MD5sum hash as char* */
filepath_t fp;
} file_t;
/**
@ -51,4 +52,7 @@ file_md5_gen(file_t *f, exception_t *e);
char*
file_md5_str(file_t *f, exception_t *e);
char*
file_abs(const file_t *f);
#endif