1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
#include "spm.h"
char *get_user_conf_dir(void) {
char *result = NULL;
wordexp_t wexp;
wordexp("~/.spm", &wexp, 0);
if (wexp.we_wordc != 0) {
result = (char *)calloc(strlen(wexp.we_wordv[0]) + 1, sizeof(char));
if (!result) {
wordfree(&wexp);
return NULL;
}
strncpy(result, wexp.we_wordv[0], strlen(wexp.we_wordv[0]));
if (access(result, F_OK) != 0) {
mkdirs(result, 0755);
}
}
wordfree(&wexp);
return result;
}
char *get_user_config_file(void) {
const char *filename = "spm.conf";
char template[PATH_MAX];
char *ucd = get_user_conf_dir();
if (!ucd) {
return NULL;
}
// Initialize temporary path
template[0] = '\0';
sprintf(template, "%s%c%s", ucd, DIRSEP, filename);
if (access(template, F_OK) != 0) {
// No configuration exists, so fail
return NULL;
}
free(ucd);
// Allocate and return path to configuration file
return strdup(template);
}
char *get_user_tmp_dir(void) {
char template[PATH_MAX];
char *ucd = get_user_conf_dir();
sprintf(template, "%s%ctmp", ucd, DIRSEP);
if (access(template, F_OK) != 0) {
if (mkdirs(template, 0755) != 0) {
return NULL;
}
}
free(ucd);
return strdup(template);
}
char *get_user_package_dir(void) {
char template[PATH_MAX];
char *ucd = get_user_conf_dir();
sprintf(template, "%s%cpkgs", ucd, DIRSEP);
if (access(template, F_OK) != 0) {
if (mkdirs(template, 0755) != 0) {
return NULL;
}
}
free(ucd);
return strdup(template);
}
/**
* Check whether SPM has access to external programs it needs
*/
void check_runtime_environment(void) {
int bad_rt = 0;
char *required[] = {
"patchelf",
"rsync",
"tar",
"bash",
"reloc",
NULL,
};
for (int i = 0; required[i] != NULL; i++) {
char *result = find_executable(required[i]);
if (!result) {
fprintf(stderr, "Required program '%s' is not installed\n", required[i]);
bad_rt = 1;
}
free(result);
}
if (bad_rt) {
exit(1);
}
}
void init_config_global(void) {
SPM_GLOBAL.user_config_basedir = NULL;
SPM_GLOBAL.user_config_file = NULL;
SPM_GLOBAL.package_dir = NULL;
SPM_GLOBAL.tmp_dir = NULL;
SPM_GLOBAL.config = NULL;
SPM_GLOBAL.verbose = 0;
if (uname(&SPM_GLOBAL.sysinfo) != 0) {
fprintf(SYSERROR);
exit(1);
}
SPM_GLOBAL.user_config_basedir = get_user_conf_dir();
SPM_GLOBAL.user_config_file = get_user_config_file();
if (SPM_GLOBAL.user_config_file) {
SPM_GLOBAL.config = config_read(SPM_GLOBAL.user_config_file);
}
ConfigItem *item = NULL;
// Initialize temp directory
item = config_get(SPM_GLOBAL.config, "tmp_dir");
if (item) {
SPM_GLOBAL.tmp_dir = item->value;
if (access(SPM_GLOBAL.tmp_dir, F_OK) != 0) {
if (mkdirs(SPM_GLOBAL.tmp_dir, 0755) != 0) {
fprintf(stderr, "Unable to create global temporary directory: %s\n", SPM_GLOBAL.tmp_dir);
fprintf(SYSERROR);
exit(1);
}
}
}
else {
SPM_GLOBAL.tmp_dir = get_user_tmp_dir();
}
// Initialize package directory
item = config_get(SPM_GLOBAL.config, "package_dir");
if (item) {
SPM_GLOBAL.package_dir = item->value;
if (access(SPM_GLOBAL.package_dir, F_OK) != 0) {
if (mkdirs(SPM_GLOBAL.package_dir, 0755) != 0) {
fprintf(stderr, "Unable to create global package directory: %s\n", SPM_GLOBAL.package_dir);
fprintf(SYSERROR);
exit(1);
}
}
}
else {
SPM_GLOBAL.package_dir = get_user_package_dir();
}
}
void free_global_config(void) {
if (SPM_GLOBAL.package_dir) {
free(SPM_GLOBAL.package_dir);
}
if (SPM_GLOBAL.tmp_dir) {
free(SPM_GLOBAL.tmp_dir);
}
if (SPM_GLOBAL.user_config_basedir) {
free(SPM_GLOBAL.user_config_basedir);
}
if (SPM_GLOBAL.user_config_file) {
free(SPM_GLOBAL.user_config_file);
}
if (SPM_GLOBAL.config) {
config_free(SPM_GLOBAL.config);
}
}
void show_global_config(void) {
printf("#---------------------------\n");
printf("#---- SPM CONFIGURATION ----\n");
printf("#---------------------------\n");
printf("# base dir: %s\n", SPM_GLOBAL.user_config_basedir ? SPM_GLOBAL.user_config_basedir : "none (check write permission on home directory)");
printf("# config file: %s\n", SPM_GLOBAL.user_config_file ? SPM_GLOBAL.user_config_file : "none");
if (SPM_GLOBAL.user_config_file) {
printf("# config file contents:\n");
for (int i = 0; SPM_GLOBAL.config[i] != NULL; i++) {
printf("# -> %s: %s\n", SPM_GLOBAL.config[i]->key, SPM_GLOBAL.config[i]->value);
}
}
printf("# package storage: %s\n", SPM_GLOBAL.package_dir);
printf("# temp storage: %s\n", SPM_GLOBAL.tmp_dir);
printf("\n");
}
|