move config stuff to hdbd.c

This commit is contained in:
2025-09-19 11:06:32 +02:00
parent 34495bccbb
commit 2564df5cec
2 changed files with 73 additions and 79 deletions

View File

@@ -12,87 +12,10 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <errno.h> #include <errno.h>
typedef struct {
char *tmp_dir;
char *data_dir;
char *gallery_dl_conf;
char *yt_dlp_conf;
} Config;
int h_config_parser(Config *config) {
char *config_location = malloc(10000);
char *user_name = getenv("USER");
if (user_name == NULL) {
fprintf(stderr, "Couldn't read env variable USER\n");
return 1;
}
sprintf(config_location, "/home/%s/.config/HDB.conf", user_name);
FILE *fptr = fopen(config_location, "r");
if (fptr == NULL) {
fprintf(stderr, "Failed to open the config %s\n", config_location);
return 1;
}
char *line = malloc(10000);
char *left = malloc(10000);
char *right = malloc(10000);
while(fgets(line, 10000, fptr) != NULL) {
line[strcspn(line, "\n#")] = '\0';
if (strcmp(line, "") == 0) {
continue;
}
if (
strchr(line, '=') == NULL
|| line[strcspn(line, "=") + 1] == ' '
|| line[strcspn(line, "=") - 1] == ' '
) {
fprintf(stderr, "Config line doesn't contain a valid assignment. line = '%s'\n", line);
continue;
}
/* TODO: support env variables */
if (strchr(line, '$') != NULL) {
fprintf(stderr, "Config doesn't support env variables. line = '%s'\n", line);
continue;
}
left = strtok(line, "=");
right = strtok(NULL, "\0");
if (strcmp(left, "data_dir") == 0) {
config->data_dir = malloc(10000);
memcpy(config->data_dir, right, strlen(right));
} else if (strcmp(left, "gallery-dl_conf") == 0) {
config->gallery_dl_conf = malloc(10000);
memcpy(config->gallery_dl_conf, right, strlen(right));
} else if (strcmp(left, "yt-dlp_conf") == 0) {
config->yt_dlp_conf = malloc(10000);
memcpy(config->yt_dlp_conf, right, strlen(right));
} else {
fprintf(stderr, "Unknown config option. left = '%s'\n", left);
}
}
fclose(fptr);
free(line);
free(config_location);
return 0;
}
int main(int argc, char **argv) { int main(int argc, char **argv) {
int ret; int ret;
Config config = { 0 };
config.tmp_dir = "/tmp/HDB";
ret = h_config_parser(&config);
if (ret != 0) {
fprintf(stderr, "Failed to parse config\n");
return 1;
}
if(mkdir(config.tmp_dir, 0777) && errno != EEXIST) {
fprintf(stderr, "'%s' %s\n", config.tmp_dir, strerror(errno));
return 1;
}
if (argc < 2) { if (argc < 2) {
fprintf(stderr, "Incorrect amount of arguments\n"); fprintf(stderr, "Incorrect amount of arguments\n");

View File

@@ -13,6 +13,69 @@ pid_t pid, sid;
int server_sock; int server_sock;
struct sockaddr_un server; struct sockaddr_un server;
int sockaddr_len; int sockaddr_len;
struct {
char *tmp_dir;
char *data_dir;
char *gallery_dl_conf;
char *yt_dlp_conf;
} config;
int config_parser() {
char config_location[1000] = {0};
char *user_name = getenv("USER");
if (user_name == NULL) {
syslog(LOG_ERR, "config_parser(): Couldn't read env variable USER");
return 1;
}
sprintf(config_location, "/home/%s/.config/HDB.conf", user_name);
FILE *fptr = fopen(config_location, "r");
if (fptr == NULL) {
syslog(LOG_ERR, "config_parser(): Failed to open the config %s", config_location);
return 1;
}
char line[1000] = {0};
char left[1000] = {0};
char right[1000] = {0};
while(fgets(line, 10000, fptr) != NULL) {
line[strcspn(line, "\n#")] = '\0';
if (strcmp(line, "") == 0) {
continue;
}
if (
strchr(line, '=') == NULL
|| line[strcspn(line, "=") + 1] == ' '
|| line[strcspn(line, "=") - 1] == ' '
) {
syslog(LOG_WARNING, "config_parser(): Config line doesn't contain a valid assignment. line = '%s'", line);
continue;
}
/* TODO: support env variables */
if (strchr(line, '$') != NULL) {
syslog(LOG_WARNING, "config_parser(): Config doesn't support env variables. line = '%s'", line);
continue;
}
left = strtok(line, "=");
right = strtok(NULL, "\0");
if (strcmp(left, "data_dir") == 0) {
config->data_dir = malloc(10000);
memcpy(config->data_dir, right, strlen(right));
} else if (strcmp(left, "gallery-dl_conf") == 0) {
config->gallery_dl_conf = malloc(10000);
memcpy(config->gallery_dl_conf, right, strlen(right));
} else if (strcmp(left, "yt-dlp_conf") == 0) {
config->yt_dlp_conf = malloc(10000);
memcpy(config->yt_dlp_conf, right, strlen(right));
} else {
syslog(LOG_WARNING, "config_parser(): Unknown config option. key = '%s'", left);
}
}
fclose(fptr);
return 0;
}
/* /*
Message format: (Basically like a command line) Message format: (Basically like a command line)
@@ -158,15 +221,23 @@ int main() {
char operand2[1000] = {0}; char operand2[1000] = {0};
char operand3[1000] = {0}; char operand3[1000] = {0};
config.tmp_dir = "/tmp/hdbd";
if (config_parser() != 0) {
syslog(LOG_ERR, "Failed reading the users config. Shutting down...");
close(server_sock);
return 1;
}
while(1) { while(1) {
client_sock = accept(server_sock, NULL, NULL); client_sock = accept(server_sock, NULL, NULL);
if (client_sock == -1) { if (client_sock == -1) {
return 1; syslog(LOG_WARNING, "Failed to accept the connection.");
continue;
} }
syslog(LOG_INFO, "Accepted a connection."); syslog(LOG_INFO, "Accepted a connection.");
data_length = read(client_sock, receive_buf, sizeof receive_buf); data_length = read(client_sock, receive_buf, sizeof receive_buf);
if (data_length <= 0) { if (data_length <= 0) {
syslog(LOG_WARNING, "Received no data from client!"); syslog(LOG_WARNING, "Received no data from client.");
strcpy(send_buf, "Failed to receive any data."); strcpy(send_buf, "Failed to receive any data.");
send(client_sock, send_buf, strlen(send_buf), 0); send(client_sock, send_buf, strlen(send_buf), 0);
close(client_sock); close(client_sock);