bootstrap/src/config.c

61 lines
1.1 KiB
C
Raw Normal View History

2023-11-25 02:35:22 +00:00
#include "config.h"
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include "path.h"
2023-11-23 14:40:17 +00:00
enum ConfigError config_load(
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-23 15:07:22 +00:00
if (cwd == 0) {
return CE_ENV_CWD_INVALID;
}
2023-11-23 15:07:22 +00:00
if (root_dir == 0) {
2023-11-23 18:02:40 +00:00
return CE_ENV_ROOT_DIR_INVALID;
}
if (target == 0) {
2023-11-23 15:07:22 +00:00
return CE_TARGET_INVALID;
}
2023-11-25 02:35:22 +00:00
{ // Check if the specified directory exists.
struct stat sb;
const char *segments[] = {root_dir, target};
char *filepath =
join_path_segments(sizeof(segments) / sizeof(char *), segments);
int stat_res = stat(filepath, &sb);
free(filepath);
if (stat_res == -1) {
if (errno == ENOENT) {
return CE_TARGET_NOT_FOUND;
}
return CE_TARGET_INVALID;
}
if (!S_ISDIR(sb.st_mode)) {
return CE_TARGET_NOT_DIR;
}
}
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;
return 0;
}
2023-11-23 14:40:17 +00:00
void config_free(struct Config *config) {
if (!config) {
return;
}
free(config);
}