2023-11-25 02:35:22 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2023-11-25 15:55:02 +00:00
|
|
|
#include <assert.h>
|
2023-11-24 18:47:23 +00:00
|
|
|
#include <errno.h>
|
2023-11-22 21:39:27 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2023-11-24 18:47:23 +00:00
|
|
|
#include <sys/stat.h>
|
2023-11-22 21:39:27 +00:00
|
|
|
|
2023-11-25 22:29:35 +00:00
|
|
|
#include "string_utils.h"
|
2023-11-22 21:39:27 +00:00
|
|
|
|
2023-11-25 18:51:42 +00:00
|
|
|
struct Error *config_new(
|
2023-11-23 15:07:22 +00:00
|
|
|
const char *cwd,
|
2023-11-23 11:09:32 +00:00
|
|
|
const char *root_dir,
|
|
|
|
const char *target,
|
2023-11-23 14:40:17 +00:00
|
|
|
struct Config **config
|
2023-11-23 11:09:32 +00:00
|
|
|
) {
|
2023-11-25 17:37:41 +00:00
|
|
|
assert(target);
|
|
|
|
|
2023-11-23 15:07:22 +00:00
|
|
|
if (cwd == 0) {
|
2023-11-26 13:30:44 +00:00
|
|
|
return ERROR_NEW(
|
|
|
|
ERROR_CONFIG_ENV_CWD_INVALID,
|
|
|
|
ANSI_RED("ERROR"),
|
|
|
|
": Could not retrieve ",
|
|
|
|
ANSI_CYAN("CWD"),
|
|
|
|
"."
|
|
|
|
);
|
2023-11-22 21:39:27 +00:00
|
|
|
}
|
2023-11-23 15:07:22 +00:00
|
|
|
if (root_dir == 0) {
|
2023-11-25 15:55:02 +00:00
|
|
|
return ERROR_NEW(
|
2023-11-26 13:30:44 +00:00
|
|
|
ERROR_CONFIG_ENV_ROOT_DIR_INVALID,
|
|
|
|
ANSI_RED("ERROR"),
|
|
|
|
": Could not find root directory."
|
2023-11-25 15:55:02 +00:00
|
|
|
);
|
2023-11-22 21:39:27 +00:00
|
|
|
}
|
2023-11-24 18:47:23 +00:00
|
|
|
|
2023-11-25 15:55:02 +00:00
|
|
|
const char *segments[] = {root_dir, target};
|
2023-11-25 22:29:35 +00:00
|
|
|
const char *filepath = join(sizeof(segments) / sizeof(char *), segments, '/');
|
2023-11-24 18:47:23 +00:00
|
|
|
|
2023-11-25 15:55:02 +00:00
|
|
|
struct stat sb;
|
|
|
|
int stat_res = stat(filepath, &sb);
|
|
|
|
struct Error *error = 0;
|
2023-11-24 18:47:23 +00:00
|
|
|
|
2023-11-25 15:55:02 +00:00
|
|
|
if (stat_res == -1) {
|
|
|
|
if (errno == ENOENT) {
|
|
|
|
error = ERROR_NEW(
|
2023-11-26 13:30:44 +00:00
|
|
|
ERROR_CONFIG_TARGET_NOT_FOUND,
|
|
|
|
ANSI_RED("ERROR"),
|
|
|
|
": Could not find ",
|
|
|
|
ANSI_BLUE(target),
|
|
|
|
" spec."
|
2023-11-25 15:55:02 +00:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
error = ERROR_NEW(
|
2023-11-26 13:30:44 +00:00
|
|
|
ERROR_CONFIG_TARGET_INVALID,
|
|
|
|
ANSI_RED("ERROR"),
|
|
|
|
": ",
|
|
|
|
ANSI_BLUE(target),
|
|
|
|
" is invalid."
|
2023-11-25 15:55:02 +00:00
|
|
|
);
|
2023-11-24 18:47:23 +00:00
|
|
|
}
|
2023-11-25 15:55:02 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
if (!S_ISDIR(sb.st_mode)) {
|
|
|
|
error = ERROR_NEW(
|
2023-11-26 13:30:44 +00:00
|
|
|
ERROR_CONFIG_TARGET_NOT_DIR,
|
|
|
|
ANSI_RED("ERROR"),
|
|
|
|
": ",
|
|
|
|
ANSI_CYAN(filepath),
|
|
|
|
" is not a directory."
|
2023-11-25 15:55:02 +00:00
|
|
|
);
|
|
|
|
goto cleanup;
|
2023-11-24 18:47:23 +00:00
|
|
|
}
|
|
|
|
|
2023-11-23 14:40:17 +00:00
|
|
|
*config = malloc(sizeof(struct Config));
|
2023-11-23 15:07:22 +00:00
|
|
|
(*config)->cwd = cwd;
|
2023-11-23 14:40:17 +00:00
|
|
|
(*config)->root_dir = root_dir;
|
2023-11-23 15:07:22 +00:00
|
|
|
(*config)->target = target;
|
2023-11-22 21:39:27 +00:00
|
|
|
|
2023-11-25 15:55:02 +00:00
|
|
|
cleanup:
|
2023-11-25 17:37:41 +00:00
|
|
|
free((void *)filepath);
|
2023-11-25 15:55:02 +00:00
|
|
|
return error;
|
2023-11-22 21:39:27 +00:00
|
|
|
}
|
|
|
|
|
2023-11-23 14:40:17 +00:00
|
|
|
void config_free(struct Config *config) {
|
2023-11-22 21:39:27 +00:00
|
|
|
if (!config) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
free(config);
|
2023-11-25 03:29:24 +00:00
|
|
|
config = 0;
|
2023-11-22 21:39:27 +00:00
|
|
|
}
|