aboutsummaryrefslogtreecommitdiff
path: root/src/config_global.c
blob: 9032fda7fc82e7219d40772372ae718aa78099e4 (plain) (blame)
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/**
 * @file config_global.c
 */
#include "spm.h"

/**
 * Get path to user's local configuration directory
 * (The path will be created if it doesn't exist)
 * @return
 */
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;
}

/**
 * Get path to user's local configuration file
 * @return
 */
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);
}

/**
 * Determine location of temporary storage location
 * @return
 */
char *get_user_tmp_dir(void) {
    char *template = NULL;
    char *ucd = get_user_conf_dir();
    template = join_ex(DIRSEPS, ucd, "tmp", NULL);

    if (access(template, F_OK) != 0) {
        if (mkdirs(template, 0755) != 0) {
            return NULL;
        }
    }

    free(ucd);
    return template;
}

/**
 * Determine location of package directory
 * @return
 */
char *get_user_package_dir(void) {
    char *template = NULL;
    char *ucd = get_user_conf_dir();

    template = join_ex(DIRSEPS, ucd, "pkgs", SPM_GLOBAL.repo_target, NULL);

    if (access(template, F_OK) != 0) {
        if (mkdirs(template, 0755) != 0) {
            return NULL;
        }
    }

    free(ucd);
    return template;
}

/**
 * Determine location of the package manifest
 * @return
 */
char *get_package_manifest(void) {
    Manifest *manifest = NULL;
    char *template = NULL;
    char *ucd = get_user_conf_dir();

    //free(ucd);
    //return strdup(template);

    template = join_ex(DIRSEPS, SPM_GLOBAL.package_dir, SPM_MANIFEST_FILENAME, NULL);
    if (access(template, F_OK) != 0) {
        fprintf(stderr, "Package manifest not found: %s\n", template);
        manifest = manifest_from(PKG_DIR);
        if (manifest == NULL) {
            perror("manifest generator");
            fprintf(SYSERROR);
            return NULL;
        }
        manifest_write(manifest, PKG_DIR);
        manifest_free(manifest);
    }

    free(ucd);
    return template;
}


/**
 * Check whether SPM has access to external programs it needs
 */
void check_runtime_environment(void) {
    int bad_rt = 0;
    char *required[] = {
            "file",
            "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);
    }
}

/**
 *
 * @param basepath
 * @return
 */
SPM_Hierarchy *spm_hierarchy_init(char *basepath) {
    SPM_Hierarchy *fs = calloc(1, sizeof(SPM_Hierarchy));
    fs->rootdir = strdup(basepath);
    fs->bindir = join((char *[]) {fs->rootdir, "bin", NULL}, DIRSEPS);
    fs->includedir = join((char *[]) {fs->rootdir, "include", NULL}, DIRSEPS);
    fs->libdir = join((char *[]) {fs->rootdir, "lib", NULL}, DIRSEPS);
    fs->datadir = join((char *[]) {fs->rootdir, "share", NULL}, DIRSEPS);
    fs->localstatedir = join((char *[]) {fs->rootdir, "var", NULL}, DIRSEPS);
    fs->sysconfdir = join((char *[]) {fs->rootdir, "etc", NULL}, DIRSEPS);
    fs->mandir = join((char *[]) {fs->datadir, "man", NULL}, DIRSEPS);
    fs->tmpdir = join((char *[]) {fs->rootdir, "tmp", NULL}, DIRSEPS);
    fs->dbdir = join((char *[]) {fs->localstatedir, "db", NULL}, DIRSEPS);
    fs->dbrecdir = join((char *[]) {fs->dbdir, "records", NULL}, DIRSEPS);

    return fs;
}

/**
 *
 * @param fs
 */
void spm_hierarchy_free(SPM_Hierarchy *fs) {
    free(fs->rootdir);
    free(fs->bindir);
    free(fs->includedir);
    free(fs->libdir);
    free(fs->datadir);
    free(fs->localstatedir);
    free(fs->sysconfdir);
    free(fs->mandir);
    free(fs->tmpdir);
    free(fs->dbdir);
    free(fs->dbrecdir);
    free(fs);
}

/**
 * Populate global configuration structure
 */
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.package_manifest = NULL;
    SPM_GLOBAL.config = NULL;
    SPM_GLOBAL.verbose = 0;
    SPM_GLOBAL.repo_target = NULL;
    SPM_GLOBAL.mirror_list = NULL;
    SPM_GLOBAL.prompt_user = 1;

    if (uname(&SPM_GLOBAL.sysinfo) != 0) {
        fprintf(SYSERROR);
        exit(errno);
    }

    // Initialize filesystem paths structure
    SPM_GLOBAL.fs.bindir = calloc(strlen(SPM_PROGRAM_BIN) + 1, sizeof(char));
    SPM_GLOBAL.fs.includedir = calloc(strlen(SPM_PROGRAM_INCLUDE) + 1, sizeof(char));
    SPM_GLOBAL.fs.libdir = calloc(strlen(SPM_PROGRAM_LIB) + 1, sizeof(char));
    SPM_GLOBAL.fs.datadir = calloc(strlen(SPM_PROGRAM_DATA) + 1, sizeof(char));

    if (!SPM_GLOBAL.fs.bindir || !SPM_GLOBAL.fs.includedir
        || !SPM_GLOBAL.fs.libdir) {
        perror("Unable to allocate memory for global filesystem paths");
        fprintf(SYSERROR);
        exit(errno);
    }

    strcpy(SPM_GLOBAL.fs.bindir, SPM_PROGRAM_BIN);
    strcpy(SPM_GLOBAL.fs.includedir, SPM_PROGRAM_INCLUDE);
    strcpy(SPM_GLOBAL.fs.libdir, SPM_PROGRAM_LIB);
    strcpy(SPM_GLOBAL.fs.datadir, SPM_PROGRAM_DATA);
    SPM_GLOBAL.fs.mandir = join((char *[]) {SPM_PROGRAM_DATA, "man", NULL}, DIRSEPS);

    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 repository target (i.e. repository path suffix)
    SPM_GLOBAL.repo_target = join((char *[]) {SPM_GLOBAL.sysinfo.sysname, SPM_GLOBAL.sysinfo.machine, NULL}, DIRSEPS);
    item = config_get(SPM_GLOBAL.config, "repo_target");
    if (item) {
        free(SPM_GLOBAL.repo_target);
        SPM_GLOBAL.repo_target = normpath(item->value);
    }

    // Initialize mirror list filename
    SPM_GLOBAL.mirror_config = join((char *[]) {SPM_GLOBAL.user_config_basedir, SPM_MIRROR_FILENAME, NULL}, DIRSEPS);
    item = config_get(SPM_GLOBAL.config, "mirror_config");
    if (item) {
        free(SPM_GLOBAL.mirror_config);
        SPM_GLOBAL.mirror_config = normpath(item->value);
    }

    if (SPM_GLOBAL.mirror_config != NULL) {
        SPM_GLOBAL.mirror_list = mirror_list(SPM_GLOBAL.mirror_config);
    }

    // Initialize temp directory
    item = config_get(SPM_GLOBAL.config, "tmp_dir");
    if (item) {
        SPM_GLOBAL.tmp_dir = strdup(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 = calloc(PATH_MAX, sizeof(char)); //strdup(item->value);
        strncpy(SPM_GLOBAL.package_dir, item->value, PATH_MAX - 1);
        strcat(SPM_GLOBAL.package_dir, DIRSEPS);
        strcat(SPM_GLOBAL.package_dir, SPM_GLOBAL.repo_target);

        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();
    }

    // Initialize package manifest
    item = config_get(SPM_GLOBAL.config, "package_manifest");
    if (item) {
        SPM_GLOBAL.package_manifest = strdup(item->value);
        if (access(SPM_GLOBAL.package_manifest, F_OK) != 0) {
            fprintf(stderr, "Package manifest not found: %s\n", SPM_GLOBAL.package_manifest);
            Manifest *manifest = manifest_from(PKG_DIR);
            manifest_write(manifest, SPM_GLOBAL.package_manifest);
            manifest_free(manifest);
        }
    }
    else {
        SPM_GLOBAL.package_manifest = get_package_manifest();
    }
}

/**
 * Free memory allocated for global configuration
 */
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.package_manifest) {
        free(SPM_GLOBAL.package_manifest);
    }
    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.repo_target) {
        free(SPM_GLOBAL.repo_target);
    }
    if (SPM_GLOBAL.mirror_config) {
        free(SPM_GLOBAL.mirror_config);
    }
    if (SPM_GLOBAL.mirror_list) {
        mirror_list_free(SPM_GLOBAL.mirror_list);
    }

    free(SPM_GLOBAL.fs.bindir);
    free(SPM_GLOBAL.fs.includedir);
    free(SPM_GLOBAL.fs.libdir);
    free(SPM_GLOBAL.fs.datadir);
    free(SPM_GLOBAL.fs.mandir);
    if (SPM_GLOBAL.config) {
        config_free(SPM_GLOBAL.config);
    }
}

/**
 * Display global configuration data
 */
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("# package manifest: %s\n", SPM_GLOBAL.package_manifest);
    printf("\n");
}