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()))) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 40 | YLMSG_E("Determining home directory failed (%s).\n", 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) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 47 | YLMSG_E("Memory allocation failed (%s).\n", 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 */ |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 56 | YLMSG_E("Configuration directory \"%s\" does not exist, creating it.\n", yl_dir); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 57 | if (mkdir(yl_dir, 00700)) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 58 | YLMSG_E("Configuration directory \"%s\" cannot be created (%s).\n", yl_dir, strerror(errno)); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 59 | free(yl_dir); |
| 60 | return NULL; |
| 61 | } |
| 62 | } else { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 63 | YLMSG_E("Configuration directory \"%s\" exists but cannot be accessed (%s).\n", yl_dir, strerror(errno)); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 64 | free(yl_dir); |
| 65 | return NULL; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | return yl_dir; |
| 70 | } |
| 71 | |
| 72 | void |
| 73 | load_config(void) |
| 74 | { |
| 75 | char *yl_dir, *history_file; |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 76 | |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 77 | if ((yl_dir = get_yanglint_dir()) == NULL) { |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | history_file = malloc(strlen(yl_dir) + 9); |
| 82 | if (!history_file) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 83 | YLMSG_E("Memory allocation failed (%s).\n", strerror(errno)); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 84 | free(yl_dir); |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | sprintf(history_file, "%s/history", yl_dir); |
| 89 | if (access(history_file, F_OK) && (errno == ENOENT)) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 90 | YLMSG_E("No saved history.\n"); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 91 | } else if (linenoiseHistoryLoad(history_file)) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 92 | YLMSG_E("Failed to load history.\n"); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | free(history_file); |
| 96 | free(yl_dir); |
| 97 | } |
| 98 | |
| 99 | void |
| 100 | store_config(void) |
| 101 | { |
| 102 | char *yl_dir, *history_file; |
| 103 | |
| 104 | if ((yl_dir = get_yanglint_dir()) == NULL) { |
| 105 | return; |
| 106 | } |
| 107 | |
| 108 | history_file = malloc(strlen(yl_dir) + 9); |
| 109 | if (!history_file) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 110 | YLMSG_E("Memory allocation failed (%s).\n", strerror(errno)); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 111 | free(yl_dir); |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | sprintf(history_file, "%s/history", yl_dir); |
| 116 | if (linenoiseHistorySave(history_file)) { |
Radek Krejci | e9f13b1 | 2020-11-09 17:42:04 +0100 | [diff] [blame] | 117 | YLMSG_E("Failed to save history.\n"); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | free(history_file); |
| 121 | free(yl_dir); |
| 122 | } |