2023-11-25 02:35:22 +00:00
|
|
|
#include "config.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-24 18:47:23 +00:00
|
|
|
#include "path.h"
|
2023-11-22 21:39:27 +00:00
|
|
|
|
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-22 21:39:27 +00:00
|
|
|
}
|
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;
|
2023-11-22 21:39:27 +00:00
|
|
|
}
|
|
|
|
if (target == 0) {
|
2023-11-23 15:07:22 +00:00
|
|
|
return CE_TARGET_INVALID;
|
2023-11-22 21:39:27 +00:00
|
|
|
}
|
|
|
|
|
2023-11-25 02:35:22 +00:00
|
|
|
{ // Check if the specified directory exists.
|
2023-11-24 18:47:23 +00:00
|
|
|
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;
|
2023-11-22 21:39:27 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|