|
| 1 | +#include "datafile.h" |
| 2 | +#include <libwebsockets.h> |
| 3 | +#include <stdlib.h> |
| 4 | +#include <string.h> |
| 5 | +#include <sys/stat.h> |
| 6 | + |
| 7 | +static error_t load_api_key_file_path(char *result, size_t result_buf_len) { |
| 8 | + char *home_path = getenv("HOME"); |
| 9 | + char *base_path = getenv("XDG_DATA_HOME"); |
| 10 | + size_t buffer_used_len = 0; |
| 11 | + |
| 12 | + if (base_path == NULL || strlen(base_path) == 0) { |
| 13 | + buffer_used_len = lws_snprintf(result, result_buf_len, "%s/%s", home_path, |
| 14 | + ".local/share"); |
| 15 | + if (buffer_used_len >= result_buf_len - 1) { |
| 16 | + lwsl_err("data file path exceeds max length"); |
| 17 | + return ERR_DATA_PATH_EXCEED_MAX_PATH; |
| 18 | + } |
| 19 | + } else { |
| 20 | + buffer_used_len = lws_snprintf(result, result_buf_len, "%s", base_path); |
| 21 | + } |
| 22 | + |
| 23 | + buffer_used_len = |
| 24 | + lws_snprintf(result + buffer_used_len, result_buf_len - buffer_used_len, |
| 25 | + "%s", "/tshotkeysctl/api_key"); |
| 26 | + |
| 27 | + if (buffer_used_len >= result_buf_len - 1) { |
| 28 | + lwsl_err("api key data file path exceeds max length"); |
| 29 | + return ERR_DATA_FILE_EXCEED_MAX_PATH; |
| 30 | + } |
| 31 | + |
| 32 | + lwsl_notice("api key file path: %s", result); |
| 33 | + |
| 34 | + return NO_ERROR; |
| 35 | +} |
| 36 | + |
| 37 | +static void ensure_data_dir_present(const char *data_path) { |
| 38 | + const char separator = '/'; |
| 39 | + |
| 40 | + const char *ret = data_path; |
| 41 | + int index = 0; |
| 42 | + struct stat stat_struct = {0}; |
| 43 | + |
| 44 | + while (ret != NULL && ret[0] != '\0') { |
| 45 | + ret = strchr(&data_path[ret - data_path], separator); |
| 46 | + |
| 47 | + if (ret == NULL) { |
| 48 | + break; |
| 49 | + } |
| 50 | + |
| 51 | + char *dir = strdup(data_path); |
| 52 | + index = ret - data_path; |
| 53 | + |
| 54 | + dir[index + 1] = 0; |
| 55 | + |
| 56 | + if (stat(dir, &stat_struct)) { |
| 57 | + lwsl_notice("creating path: %s", dir); |
| 58 | + |
| 59 | + mkdir(dir, 0700); |
| 60 | + } |
| 61 | + |
| 62 | + free(dir); |
| 63 | + |
| 64 | + ret++; |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +static void load_api_key(const char *api_key_file_path, char *result, |
| 69 | + size_t result_buf_len) { |
| 70 | + FILE *file; |
| 71 | + |
| 72 | + lwsl_notice("Loading API key"); |
| 73 | + |
| 74 | + file = fopen(api_key_file_path, "r"); |
| 75 | + |
| 76 | + if (file) { |
| 77 | + fgets(result, result_buf_len, file); |
| 78 | + fclose(file); |
| 79 | + lwsl_notice("API key loaded"); |
| 80 | + } else { |
| 81 | + lwsl_notice("API key file not found"); |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +const char *df_get_api_key(df_data_t *data) { return data->api_key; } |
| 86 | + |
| 87 | +error_t df_init(df_data_t *data) { |
| 88 | + error_t result_code = |
| 89 | + load_api_key_file_path(data->api_key_file_path, PATH_BUF_SIZE); |
| 90 | + if (result_code != 0) { |
| 91 | + return result_code; |
| 92 | + } |
| 93 | + |
| 94 | + load_api_key(data->api_key_file_path, data->api_key, API_KEY_BUF_SIZE); |
| 95 | + return NO_ERROR; |
| 96 | +} |
| 97 | + |
| 98 | +error_t df_save_api_key(df_data_t *data, const char *new_api_key) { |
| 99 | + error_t result_code = NO_ERROR; |
| 100 | + FILE *file; |
| 101 | + size_t api_key_len; |
| 102 | + |
| 103 | + if (strcmp(data->api_key, new_api_key) == 0) { |
| 104 | + lwsl_notice("API key is not changed. Continue..."); |
| 105 | + return result_code; |
| 106 | + } |
| 107 | + |
| 108 | + api_key_len = strlen(new_api_key); |
| 109 | + if (api_key_len >= API_KEY_BUF_SIZE) { |
| 110 | + |
| 111 | + lwsl_warn("API key exceeds the maximum key size %d. Not saving to the file", |
| 112 | + API_KEY_BUF_SIZE - 1); |
| 113 | + return ERR_API_KEY_EXCEED_MAX_LEN; |
| 114 | + } |
| 115 | + |
| 116 | + lwsl_notice("New API key - save to: %s", data->api_key_file_path); |
| 117 | + |
| 118 | + ensure_data_dir_present(data->api_key_file_path); |
| 119 | + |
| 120 | + file = fopen(data->api_key_file_path, "w"); |
| 121 | + if (file == NULL) { |
| 122 | + lwsl_err("Error opening file: %s", data->api_key_file_path); |
| 123 | + result_code = ERR_DATA_FILE_OPEN; |
| 124 | + } else { |
| 125 | + if (fputs(new_api_key, file) < 0) { |
| 126 | + lwsl_err("Error writing api key to: %s", data->api_key_file_path); |
| 127 | + result_code = ERR_DATA_FILE_WRITE; |
| 128 | + } |
| 129 | + |
| 130 | + fclose(file); |
| 131 | + } |
| 132 | + |
| 133 | + return result_code; |
| 134 | +} |
0 commit comments