Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file configuration.c |
| 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @brief yanglint configuration |
| 5 | * |
| 6 | * Copyright (c) 2017 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
| 14 | |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 15 | #include "configuration.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 16 | |
| 17 | #include <errno.h> |
| 18 | #include <pwd.h> |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | #include <sys/stat.h> |
| 23 | #include <unistd.h> |
| 24 | |
| 25 | #include "linenoise/linenoise.h" |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 26 | |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 27 | #include "common.h" |
| 28 | |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 29 | /* Yanglint home (appended to ~/) */ |
| 30 | #define YL_DIR ".yanglint" |
| 31 | |
| 32 | char * |
| 33 | get_yanglint_dir(void) |
| 34 | { |
| 35 | int ret; |
| 36 | struct passwd *pw; |
| 37 | char *user_home, *yl_dir; |
| 38 | |
| 39 | if (!(pw = getpwuid(getuid()))) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 40 | YLMSG_E("Determining home directory failed (%s).", strerror(errno)); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 41 | return NULL; |
| 42 | } |
| 43 | user_home = pw->pw_dir; |
| 44 | |
| 45 | yl_dir = malloc(strlen(user_home) + 1 + strlen(YL_DIR) + 1); |
| 46 | if (!yl_dir) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 47 | YLMSG_E("Memory allocation failed (%s).", strerror(errno)); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 48 | return NULL; |
| 49 | } |
| 50 | sprintf(yl_dir, "%s/%s", user_home, YL_DIR); |
| 51 | |
| 52 | ret = access(yl_dir, R_OK | X_OK); |
| 53 | if (ret == -1) { |
| 54 | if (errno == ENOENT) { |
| 55 | /* directory does not exist */ |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 56 | YLMSG_W("Configuration directory \"%s\" does not exist, creating it.", yl_dir); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 57 | if (mkdir(yl_dir, 00700)) { |
Jan Kundrát | 2df50b9 | 2022-08-22 19:00:28 +0200 | [diff] [blame] | 58 | if (errno != EEXIST) { |
| 59 | /* parallel execution, yay */ |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 60 | YLMSG_E("Configuration directory \"%s\" cannot be created (%s).", yl_dir, strerror(errno)); |
Jan Kundrát | 2df50b9 | 2022-08-22 19:00:28 +0200 | [diff] [blame] | 61 | free(yl_dir); |
| 62 | return NULL; |
| 63 | } |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 64 | } |
| 65 | } else { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 66 | YLMSG_E("Configuration directory \"%s\" exists but cannot be accessed (%s).", yl_dir, strerror(errno)); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 67 | free(yl_dir); |
| 68 | return NULL; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | return yl_dir; |
| 73 | } |
| 74 | |
| 75 | void |
| 76 | load_config(void) |
| 77 | { |
| 78 | char *yl_dir, *history_file; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 79 | |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 80 | if ((yl_dir = get_yanglint_dir()) == NULL) { |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | history_file = malloc(strlen(yl_dir) + 9); |
| 85 | if (!history_file) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 86 | YLMSG_E("Memory allocation failed (%s).", strerror(errno)); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 87 | free(yl_dir); |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | sprintf(history_file, "%s/history", yl_dir); |
| 92 | if (access(history_file, F_OK) && (errno == ENOENT)) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 93 | YLMSG_W("No saved history."); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 94 | } else if (linenoiseHistoryLoad(history_file)) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 95 | YLMSG_E("Failed to load history."); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | free(history_file); |
| 99 | free(yl_dir); |
| 100 | } |
| 101 | |
| 102 | void |
| 103 | store_config(void) |
| 104 | { |
| 105 | char *yl_dir, *history_file; |
| 106 | |
| 107 | if ((yl_dir = get_yanglint_dir()) == NULL) { |
| 108 | return; |
| 109 | } |
| 110 | |
| 111 | history_file = malloc(strlen(yl_dir) + 9); |
| 112 | if (!history_file) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 113 | YLMSG_E("Memory allocation failed (%s).", strerror(errno)); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 114 | free(yl_dir); |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | sprintf(history_file, "%s/history", yl_dir); |
| 119 | if (linenoiseHistorySave(history_file)) { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 120 | YLMSG_E("Failed to save history."); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | free(history_file); |
| 124 | free(yl_dir); |
| 125 | } |