blob: 86179fa10efd2bb63d33872162774a27565e72c6 [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)) {
Jan Kundrát2df50b92022-08-22 19:00:28 +020058 if (errno != EEXIST) {
59 /* parallel execution, yay */
60 YLMSG_E("Configuration directory \"%s\" cannot be created (%s).\n", yl_dir, strerror(errno));
61 free(yl_dir);
62 return NULL;
63 }
Radek Krejcied5acc52019-04-25 15:57:04 +020064 }
65 } else {
Radek Krejcie9f13b12020-11-09 17:42:04 +010066 YLMSG_E("Configuration directory \"%s\" exists but cannot be accessed (%s).\n", yl_dir, strerror(errno));
Radek Krejcied5acc52019-04-25 15:57:04 +020067 free(yl_dir);
68 return NULL;
69 }
70 }
71
72 return yl_dir;
73}
74
75void
76load_config(void)
77{
78 char *yl_dir, *history_file;
Radek Krejcie9f13b12020-11-09 17:42:04 +010079
Radek Krejcied5acc52019-04-25 15:57:04 +020080 if ((yl_dir = get_yanglint_dir()) == NULL) {
81 return;
82 }
83
84 history_file = malloc(strlen(yl_dir) + 9);
85 if (!history_file) {
Radek Krejcie9f13b12020-11-09 17:42:04 +010086 YLMSG_E("Memory allocation failed (%s).\n", strerror(errno));
Radek Krejcied5acc52019-04-25 15:57:04 +020087 free(yl_dir);
88 return;
89 }
90
91 sprintf(history_file, "%s/history", yl_dir);
92 if (access(history_file, F_OK) && (errno == ENOENT)) {
Radek Krejci0d7809b2020-11-26 12:50:12 +010093 YLMSG_W("No saved history.\n");
Radek Krejcied5acc52019-04-25 15:57:04 +020094 } else if (linenoiseHistoryLoad(history_file)) {
Radek Krejcie9f13b12020-11-09 17:42:04 +010095 YLMSG_E("Failed to load history.\n");
Radek Krejcied5acc52019-04-25 15:57:04 +020096 }
97
98 free(history_file);
99 free(yl_dir);
100}
101
102void
103store_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) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100113 YLMSG_E("Memory allocation failed (%s).\n", strerror(errno));
Radek Krejcied5acc52019-04-25 15:57:04 +0200114 free(yl_dir);
115 return;
116 }
117
118 sprintf(history_file, "%s/history", yl_dir);
119 if (linenoiseHistorySave(history_file)) {
Radek Krejcie9f13b12020-11-09 17:42:04 +0100120 YLMSG_E("Failed to save history.\n");
Radek Krejcied5acc52019-04-25 15:57:04 +0200121 }
122
123 free(history_file);
124 free(yl_dir);
125}