refactor(cli): change config location to /etc/tfm/config

This commit is contained in:
Masahiko AMANO 2022-12-25 13:09:44 +03:00
parent bb5e5f209e
commit 828e4b5e81

View File

@ -1,8 +1,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <getopt.h> #include <getopt.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <sys/stat.h>
#include <libgen.h>
#include "../include/tanabata.h" #include "../include/tanabata.h"
#include "../include/cli.h" #include "../include/cli.h"
@ -248,16 +247,6 @@ int cli(int argc, char **argv) {
fprintf(stderr, ERROR("No options provided\n")); fprintf(stderr, ERROR("No options provided\n"));
return 1; return 1;
} }
char *exe_dir = malloc(4096);
memset(exe_dir, 0, 4096);
if (readlink("/proc/self/exe", exe_dir, 4096) == -1) {
fprintf(stderr, ERROR("Failed to get executable directory\n"));
return 1;
}
exe_dir = dirname(exe_dir);
char *config_path = malloc(strlen(exe_dir) + 12);
strcpy(config_path, exe_dir);
strcat(config_path, "/tfm-config");
const char *shortopts = "hI:O:suaftkwV"; const char *shortopts = "hI:O:suaftkwV";
int status = 0; int status = 0;
char *abspath = NULL; char *abspath = NULL;
@ -269,17 +258,37 @@ int cli(int argc, char **argv) {
_Bool opt_t = 0; _Bool opt_t = 0;
_Bool opt_k = 0; _Bool opt_k = 0;
_Bool opt_w = 0; _Bool opt_w = 0;
FILE *config = fopen(config_path, "r"); char *tanabata_path;
FILE *config = fopen("/etc/tfm/config", "r");
if (config == NULL) { if (config == NULL) {
fprintf(stderr, ERROR("Config file not found\n")); tanabata_path = NULL;
return 1; struct stat st;
} if (stat("/etc/tfm", &st) == -1) {
fseek(config, 0L, SEEK_END); if (mkdir("/etc/tfm", 0755) != 0) {
char *tanabata_path = malloc(ftell(config) + 1); fprintf(stderr, ERROR("Failed to create '/etc/tfm' directory. "
rewind(config); "Try again with 'sudo' or check your permissions\n"));
if (fgets(tanabata_path, INT32_MAX, config) == NULL) { return 1;
fprintf(stderr, ERROR("Failed to read config file\n")); }
return 1; }
config = fopen("/etc/tfm/config", "w");
if (config == NULL) {
fprintf(stderr, ERROR("Failed to create config file. "
"Try again with 'sudo' or check your permissions\n"));
return 1;
}
} else {
fseek(config, 0L, SEEK_END);
long fsize = ftell(config);
rewind(config);
if (fsize == 0) {
tanabata_path = NULL;
} else {
tanabata_path = malloc(fsize + 1);
if (fgets(tanabata_path, INT32_MAX, config) == NULL) {
fprintf(stderr, ERROR("Failed to read config file\n"));
return 1;
}
}
} }
while ((opt = getopt(argc, argv, shortopts)) != -1) { while ((opt = getopt(argc, argv, shortopts)) != -1) {
switch (opt) { switch (opt) {
@ -321,7 +330,8 @@ int cli(int argc, char **argv) {
if (status == 0) { if (status == 0) {
config = freopen(NULL, "w", config); config = freopen(NULL, "w", config);
if (config == NULL) { if (config == NULL) {
fprintf(stderr, ERROR("Failed to write to config file\n")); fprintf(stderr, ERROR("Failed to write to config file. "
"Try again with 'sudo' or check your permissions\n"));
return 1; return 1;
} }
fputs(abspath, config); fputs(abspath, config);
@ -340,7 +350,8 @@ int cli(int argc, char **argv) {
if (tanabata_open(&tanabata, abspath) == 0) { if (tanabata_open(&tanabata, abspath) == 0) {
config = freopen(NULL, "w", config); config = freopen(NULL, "w", config);
if (config == NULL) { if (config == NULL) {
fprintf(stderr, ERROR("Failed to write to config file\n")); fprintf(stderr, ERROR("Failed to write to config file. "
"Try again with 'sudo' or check your permissions\n"));
return 1; return 1;
} }
fputs(abspath, config); fputs(abspath, config);
@ -377,14 +388,18 @@ int cli(int argc, char **argv) {
break; break;
} }
} }
if (opt_s && opt_u) { if (tanabata_path == NULL) {
opt_s = 0; fprintf(stderr, ERROR("No connected database\n"));
opt_u = 0; return 1;
} }
if (tanabata_open(&tanabata, tanabata_path) != 0) { if (tanabata_open(&tanabata, tanabata_path) != 0) {
fprintf(stderr, ERROR("Failed to load database\n")); fprintf(stderr, ERROR("Failed to load database\n"));
return 1; return 1;
} }
if (opt_s && opt_u) {
opt_s = 0;
opt_u = 0;
}
free(tanabata_path); free(tanabata_path);
fclose(config); fclose(config);
if (opt_w) { if (opt_w) {