summaryrefslogtreecommitdiffstats
path: root/src/common.c
blob: 1040556ed99e21bb3f89d80586cd838013088906 (plain)
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
/*
 * Copyright (C) 2003-2019 Jason Woodward <woodwardj at jaos dot org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "main.h"

FILE *slapt_open_file(const char *file_name, const char *mode)
{
    FILE *fh = NULL;
    if ((fh = fopen(file_name, mode)) == NULL) {
        fprintf(stderr, gettext("Failed to open %s\n"), file_name);

        if (errno)
            perror(file_name);

        return NULL;
    }
    return fh;
}

/* initialize regex structure and compile the regular expression */
slapt_regex_t *slapt_init_regex(const char *regex_string)
{
    slapt_regex_t *r;

    if (regex_string == NULL)
        return NULL;

    r = slapt_malloc(sizeof *r);

    r->nmatch = SLAPT_MAX_REGEX_PARTS;
    r->reg_return = -1;

    /* compile our regex */
    r->reg_return = regcomp(&r->regex, regex_string,
                            REG_EXTENDED | REG_NEWLINE | REG_ICASE);
    if (r->reg_return != 0) {
        size_t regerror_size;
        char errbuf[1024];
        size_t errbuf_size = 1024;
        fprintf(stderr, gettext("Failed to compile regex\n"));

        if ((regerror_size = regerror(r->reg_return,
                                      &r->regex, errbuf, errbuf_size)) != 0) {
            printf(gettext("Regex Error: %s\n"), errbuf);
        }
        free(r);
        return NULL;
    }

    return r;
}

/*
  execute the regular expression and set the return code
  in the passed in structure
 */
void slapt_execute_regex(slapt_regex_t *r, const char *string)
{
    r->reg_return = regexec(&r->regex, string,
                            r->nmatch, r->pmatch, 0);
}

char *slapt_regex_extract_match(const slapt_regex_t *r, const char *src, const int i)
{
    regmatch_t m = r->pmatch[i];
    char *str = NULL;

    if (m.rm_so != -1) {
        uint32_t len = m.rm_eo - m.rm_so + 1;
        str = slapt_malloc(sizeof *str * len);

        str = strncpy(str, src + m.rm_so, len);
        if (len > 0)
            str[len - 1] = '\0';
    }

    return str;
}

void slapt_free_regex(slapt_regex_t *r)
{
    regfree(&r->regex);
    free(r);
}

void slapt_gen_md5_sum_of_file(FILE *f, char *result_sum)
{
    EVP_MD_CTX *mdctx = NULL;
    const EVP_MD *md;
    unsigned char md_value[EVP_MAX_MD_SIZE];
    uint32_t md_len = 0;
    ssize_t getline_read;
    size_t getline_size;
    char *result_sum_tmp = NULL;
    char *getline_buffer = NULL;

    md = EVP_md5();

#if OPENSSL_VERSION_NUMBER >= 0x10100000L
    mdctx = EVP_MD_CTX_new();
#else
    mdctx = slapt_malloc(sizeof *mdctx);
    EVP_MD_CTX_init(mdctx);
#endif
    EVP_DigestInit_ex(mdctx, md, NULL);

    rewind(f);

    while ((getline_read = getline(&getline_buffer, &getline_size, f)) != EOF)
        EVP_DigestUpdate(mdctx, getline_buffer, getline_read);

    free(getline_buffer);

    EVP_DigestFinal_ex(mdctx, md_value, &md_len);
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
    EVP_MD_CTX_free(mdctx);
#else
    free(mdctx);
#endif

    result_sum[0] = '\0';

    for (uint32_t i = 0; i < md_len; ++i) {
        char *p = slapt_malloc(sizeof *p * 3);

        if (snprintf(p, 3, "%02x", md_value[i]) > 0) {
            if ((result_sum_tmp = strncat(result_sum, p, 3)) != NULL)
                result_sum = result_sum_tmp;
        }

        free(p);
    }
}

/* recursively create dirs */
void slapt_create_dir_structure(const char *dir_name)
{
    char *cwd = NULL;
    int position = 0, len = 0;

    cwd = getcwd(NULL, 0);
    if (cwd == NULL) {
        fprintf(stderr, gettext("Failed to get cwd\n"));
        return;
    }

    len = strlen(dir_name);
    while (position < len) {
        char *pointer = NULL;
        char *dir_name_buffer = NULL;

        /* if no more directory delim, then this must be last dir */
        if (strstr(dir_name + position, "/") == NULL) {
            dir_name_buffer = strndup(
                dir_name + position,
                strlen(dir_name + position));

            if (strcmp(dir_name_buffer, ".") != 0) {
                if ((mkdir(dir_name_buffer, 0755)) == -1) {
                    if (errno != EEXIST) {
                        fprintf(stderr, gettext("Failed to mkdir: %s\n"), dir_name_buffer);
                        exit(EXIT_FAILURE);
                    }
                }
                if ((chdir(dir_name_buffer)) == -1) {
                    fprintf(stderr, gettext("Failed to chdir to %s\n"), dir_name_buffer);
                    exit(EXIT_FAILURE);
                }
            } /* don't create . */

            free(dir_name_buffer);
            break;
        } else {
            if (dir_name[position] == '/') {
                /* move on ahead */
                ++position;
            } else {
                /* figure our dir name and mk it */
                pointer = strchr(dir_name + position, '/');
                dir_name_buffer = strndup(
                    dir_name + position,
                    strlen(dir_name + position) - strlen(pointer));

                if (strcmp(dir_name_buffer, ".") != 0) {
                    if ((mkdir(dir_name_buffer, 0755)) == -1) {
                        if (errno != EEXIST) {
                            fprintf(stderr, gettext("Failed to mkdir: %s\n"), dir_name_buffer);
                            exit(EXIT_FAILURE);
                        }
                    }
                    if ((chdir(dir_name_buffer)) == -1) {
                        fprintf(stderr, gettext("Failed to chdir to %s\n"), dir_name_buffer);
                        exit(EXIT_FAILURE);
                    }
                } /* don't create . */

                free(dir_name_buffer);
                position += (strlen(dir_name + position) - strlen(pointer));
            }
        }
    } /* end while */

    if ((chdir(cwd)) == -1) {
        fprintf(stderr, gettext("Failed to chdir to %s\n"), cwd);
        return;
    }

    free(cwd);
}

int slapt_ask_yes_no(const char *format, ...)
{
    va_list arg_list;
    int answer, parsed_answer = 0;

    va_start(arg_list, format);
    vprintf(format, arg_list);
    va_end(arg_list);

    while ((answer = fgetc(stdin)) != EOF) {
        if (answer == '\n')
            break;

        if (((tolower(answer) == 'y') ||
             (tolower(answer) == 'n')) &&
            parsed_answer == 0)
            parsed_answer = tolower(answer);
    }

    if (parsed_answer == 'y')
        return 1;

    if (parsed_answer == 'n')
        return 0;

    return -1;
}

char *slapt_str_replace_chr(const char *string, const char find,
                            const char replace)
{
    uint32_t len = 0;
    char *clean = slapt_calloc(strlen(string) + 1, sizeof *clean);
    ;

    len = strlen(string);
    for (uint32_t i = 0; i < len; ++i) {
        if (string[i] == find) {
            clean[i] = replace;
        } else {
            clean[i] = string[i];
        }
    }
    clean[strlen(string)] = '\0';

    return clean;
}

void *slapt_malloc(size_t s)
{
    void *p;
    if (!(p = malloc(s))) {
        fprintf(stderr, gettext("Failed to malloc\n"));

        if (errno)
            perror("malloc");

        exit(EXIT_FAILURE);
    }
    return p;
}

void *slapt_calloc(size_t n, size_t s)
{
    void *p;
    if (!(p = calloc(n, s))) {
        fprintf(stderr, gettext("Failed to calloc\n"));

        if (errno)
            perror("calloc");

        exit(EXIT_FAILURE);
    }
    return p;
}

const char *slapt_strerror(slapt_code_t code)
{
    switch (code) {
    case SLAPT_OK:
        return "No error";
    case SLAPT_MD5_CHECKSUM_MISMATCH:
        return gettext("MD5 checksum mismatch, override with --no-md5");
    case SLAPT_MD5_CHECKSUM_MISSING:
        return gettext("Missing MD5 checksum, override with --no-md5");
    case SLAPT_DOWNLOAD_INCOMPLETE:
        return gettext("Incomplete download");
#ifdef SLAPT_HAS_GPGME
    case SLAPT_GPG_KEY_IMPORTED:
        return gettext("GPG key successfully imported");
    case SLAPT_GPG_KEY_NOT_IMPORTED:
        return gettext("GPG key could not be imported");
    case SLAPT_GPG_KEY_UNCHANGED:
        return gettext("GPG key already present");
    case SLAPT_CHECKSUMS_VERIFIED:
        return gettext("Checksums signature successfully verified");
    case SLAPT_CHECKSUMS_NOT_VERIFIED_NULL_CONTEXT:
        return gettext("Not verified: null context");
    case SLAPT_CHECKSUMS_NOT_VERIFIED_READ_CHECKSUMS:
        return gettext("Checksums not read");
    case SLAPT_CHECKSUMS_NOT_VERIFIED_READ_SIGNATURE:
        return gettext("Signature not read");
    case SLAPT_CHECKSUMS_NOT_VERIFIED_GPGME_KEY_REVOKED:
        return gettext("Not Verified: key revoked");
    case SLAPT_CHECKSUMS_NOT_VERIFIED_GPGME_KEY_EXPIRED:
        return gettext("Not Verified: key expired");
    case SLAPT_CHECKSUMS_NOT_VERIFIED_GPGME_SIG_EXPIRED:
        return gettext("Not Verified: signature expired");
    case SLAPT_CHECKSUMS_NOT_VERIFIED_GPGME_CRL_MISSING:
        return gettext("Not Verified: missing CRL");
    case SLAPT_CHECKSUMS_NOT_VERIFIED_GPGME_CRL_TOO_OLD:
        return gettext("Not Verified: CRL too old");
    case SLAPT_CHECKSUMS_NOT_VERIFIED_GPGME_BAD_POLICY:
        return gettext("Not Verified: bad policy");
    case SLAPT_CHECKSUMS_NOT_VERIFIED_GPGME_SYS_ERROR:
        return gettext("Not Verified: system error");
    case SLAPT_CHECKSUMS_NOT_VERIFIED:
        return gettext("Checksums signature could not be verified");
    case SLAPT_CHECKSUMS_MISSING_KEY:
        return gettext("No key for verification");
#endif
    default:
        return gettext("Unknown error");
    };
}

const char *slapt_priority_to_str(SLAPT_PRIORITY_T priority)
{
    switch (priority) {
    case SLAPT_PRIORITY_DEFAULT:
        return gettext("Default");
    case SLAPT_PRIORITY_DEFAULT_PATCH:
        return gettext("Default Patch");
    case SLAPT_PRIORITY_PREFERRED:
        return gettext("Preferred");
    case SLAPT_PRIORITY_PREFERRED_PATCH:
        return gettext("Preferred Patch");
    case SLAPT_PRIORITY_OFFICIAL:
        return gettext("Official");
    case SLAPT_PRIORITY_OFFICIAL_PATCH:
        return gettext("Official Patch");
    case SLAPT_PRIORITY_CUSTOM:
        return gettext("Custom");
    case SLAPT_PRIORITY_CUSTOM_PATCH:
        return gettext("Custom Patch");
    default:
        return NULL;
    };
}

bool slapt_disk_space_check(const char *path, double space_needed)
{
    struct statvfs statvfs_buf;

    if (space_needed < 0)
        return true;

    space_needed *= 1024;

    if (statvfs(path, &statvfs_buf) != 0) {
        if (errno)
            perror("statvfs");

        return false;
    } else {
        if (statvfs_buf.f_bavail < (space_needed / statvfs_buf.f_bsize))
            return false;
    }

    return true;
}

slapt_vector_t *slapt_parse_delimited_list(char *line, char delim)
{
    slapt_vector_t *list = slapt_vector_t_init(free);
    int count = 0, position = 0, len = strlen(line);

    while (isspace(line[position]) != 0)
        ++position;

    while (position < len) {
        char *start = line + position;
        char *end = NULL, *ptr = NULL;
        int start_len = strlen(start), end_len = 0;

        if (strchr(start, delim) != NULL) {
            if (line[position] == delim || isspace(line[position]) != 0) {
                ++position;
                continue;
            }
            end = strchr(start, delim);
        }

        if (end != NULL)
            end_len = strlen(end);

        ptr = strndup(start, start_len - end_len);

        slapt_vector_t_add(list, ptr);
        ++count;

        position += start_len - end_len;
    }

    return list;
}

char *slapt_strip_whitespace(const char *s)
{
    int len = strlen(s);
    while (isspace(s[len - 1]))
        --len;
    while (*s && isspace(*s))
        ++s, --len;
    return strndup(s, len);
}


slapt_vector_t *slapt_vector_t_init(slapt_vector_t_free_function f) {
    slapt_vector_t *v = NULL;
    v = slapt_malloc(sizeof *v);
    v->size = 0;
    v->capacity = 0;
    v->items = slapt_malloc(sizeof *v->items);
    v->free_function = f;
    v->sorted = false;
    return v;
}

void slapt_vector_t_free(slapt_vector_t *v) {
    if (v->free_function) {
        for (uint32_t i = 0; i < v->size; i++ ) {
            v->free_function(v->items[i]);
        }
    }
    free(v->items);
    free(v);
}

void slapt_vector_t_add(slapt_vector_t *v, void *a) {
    if (v->capacity == v->size) {
        uint32_t new_capacity = (v->capacity+1) * 2;
        v->items = realloc(v->items, sizeof *v->items * new_capacity);
        if (v->items == NULL) {
            exit(EXIT_FAILURE);
        }
        v->capacity = new_capacity;
    }
    v->items[v->size++] = a;
    v->sorted = false;
}

void slapt_vector_t_remove(slapt_vector_t *v, void *j) {
    bool found = false;
    for (uint32_t i = 0; i < v->size; i++) {
        if (j == v->items[i]) {
            if (v->free_function) {
                v->free_function(v->items[i]);
            }
            found = true;
        }
        if (found && ((i+1) < v->size)) {
            v->items[i] = v->items[i + 1];
        }
    }
    if (found) {
        v->size--;
    }
}

void slapt_vector_t_sort(slapt_vector_t *v, slapt_vector_t_qsort_cmp cmp) {
    if (v->size > 0) {
        qsort(v->items, v->size, sizeof(v->items[0]), cmp);
        v->sorted = true;
    }
}

int slapt_vector_t_index_of(slapt_vector_t *v, slapt_vector_t_cmp cmp, void *opt) {
    if (v->sorted) {
        int min = 0, max = v->size - 1;

        while (max >= min) {
            int pivot = (min + max) / 2;
            int cmpv = cmp(v->items[pivot], opt);
            if (cmpv == 0) {
                return pivot;
            } else {
                if (cmpv < 0)
                    min = pivot + 1;
                else
                    max = pivot - 1;
            }
        }
    } else {
        for (uint32_t i = 0; i < v->size; i++) {
            if (cmp(v->items[i], opt) == 0) {
                return i;
            }
        }
    }
    return -1;
}

slapt_vector_t *slapt_vector_t_search(slapt_vector_t *v, slapt_vector_t_cmp cmp, void *opt) {
    slapt_vector_t *matches = slapt_vector_t_init(NULL);

    if (v->sorted) {
        uint32_t min = 0, max = v->size - 1;
        int idx = slapt_vector_t_index_of(v, cmp, opt);
        if (idx < 0) {
            slapt_vector_t_free(matches);
            return NULL;
        }

        uint32_t start = idx, end = idx;

        while (start > min) {
            if (cmp(v->items[start - 1], opt) == 0) {
                start -= 1;
            } else {
                break;
            }
        }

        while (end < max) {
            if (cmp(v->items[end + 1], opt) == 0) {
                end += 1;
            } else {
                break;
            }
        }

        for (uint32_t i = start; i <= end; i++) {
            slapt_vector_t_add(matches, v->items[i]);
        }

    } else {
        for (uint32_t i = 0; i < v->size; i++) {
            if (cmp(v->items[i], opt) == 0) {
                slapt_vector_t_add(matches, v->items[i]);
            }
        }
    }

    if (!matches->size) {
        slapt_vector_t_free(matches);
        return NULL;
    }
    return matches;
}