blob: fe37f6da94baa1429d88efa519143c484a0c5869 [file] [log] [blame]
Radek Krejcied5acc52019-04-25 15:57:04 +02001/**
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 Krejcied5acc52019-04-25 15:57:04 +020015#include "configuration.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020016
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 Krejcied5acc52019-04-25 15:57:04 +020026
Radek Krejcie9f13b12020-11-09 17:42:04 +010027#include "common.h"
28
Radek Krejcied5acc52019-04-25 15:57:04 +020029/* Yanglint home (appended to ~/) */
30#define YL_DIR ".yanglint"
31
32char *
33get_yanglint_dir(void)
34{
35 int ret;
36 struct passwd *pw;
37 char *user_home, *yl_dir;
38
39 if (!(pw = getpwuid(getuid()))) {
Radek Krejcie9f13b12020-11-09 17:42:04 +010040 YLMSG_E("Determining home directory failed (%s).\n", strerror(errno));
Radek Krejcied5acc52019-04-25 15:57:04 +020041 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 Krejcie9f13b12020-11-09 17:42:04 +010047 YLMSG_E("Memory allocation failed (%s).\n", strerror(errno));
Radek Krejcied5acc52019-04-25 15:57:04 +020048 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 Krejci0d7809b2020-11-26 12:50:12 +010056 YLMSG_W("Configuration directory \"%s\" does not exist, creating it.\n", yl_dir);
Radek Krejcied5acc52019-04-25 15:57:04 +020057 if (mkdir(yl_dir, 00700)) {
Radek Krejcie9f13b12020-11-09 17:42:04 +010058 YLMSG_E("Configuration directory \"%s\" cannot be created (%s).\n", yl_dir, strerror(errno));
Radek Krejcied5acc52019-04-25 15:57:04 +020059 free(yl_dir);
60 return NULL;
61 }
62 } else {
Radek Krejcie9f13b12020-11-09 17:42:04 +010063 YLMSG_E("Configuration directory \"%s\" exists but cannot be accessed (%s).\n", yl_dir, strerror(errno));
Radek Krejcied5acc52019-04-25 15:57:04 +020064 free(yl_dir);
65 return NULL;
66 }
67 }
68
69 return yl_dir;
70}
71
72void
73load_config(void)
74{
75 char *yl_dir, *history_file;
Radek Krejcie9f13b12020-11-09 17:42:04 +010076
Radek Krejcied5acc52019-04-25 15:57:04 +020077 if ((yl_dir = get_yanglint_dir()) == NULL) {
78 return;
79 }
80
81 history_file = malloc(strlen(yl_dir) + 9);
82 if (!history_file) {
Radek Krejcie9f13b12020-11-09 17:42:04 +010083 YLMSG_E("Memory allocation failed (%s).\n", strerror(errno));
Radek Krejcied5acc52019-04-25 15:57:04 +020084 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 Krejci0d7809b2020-11-26 12:50:12 +010090 YLMSG_W("No saved history.\n");
Radek Krejcied5acc52019-04-25 15:57:04 +020091 } else if (linenoiseHistoryLoad(history_file)) {
Radek Krejcie9f13b12020-11-09 17:42:04 +010092 YLMSG_E("Failed to load history.\n");
Radek Krejcied5acc52019-04-25 15:57:04 +020093 }
94
95 free(history_file);
96 free(yl_dir);
97}
98
99void
100store_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 Krejcie9f13b12020-11-09 17:42:04 +0100110 YLMSG_E("Memory allocation failed (%s).\n", strerror(errno));
Radek Krejcied5acc52019-04-25 15:57:04 +0200111 free(yl_dir);
112 return;
113 }
114
115 sprintf(history_file, "%s/history", yl_dir);
116 if (linenoiseHistorySave(history_file)) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100117 YLMSG_E("Failed to save history.\n");
Radek Krejcied5acc52019-04-25 15:57:04 +0200118 }
119
120 free(history_file);
121 free(yl_dir);
122}