aboutsummaryrefslogtreecommitdiffstats
path: root/lib/util.c
blob: ef5238f19469461ea5a0448d101c3027f88803d5 (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

#include "main.h"
#include <libgen.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
int check_file_exist(const char *storage) {
    return access(storage, F_OK) != -1;
}

int check_file_size(const char *storage) {
    struct stat st;
    stat(storage, &st);
    return st.st_size;
}

int check_basedir_exist(const char *storage) {
    char *_storage = strdup(storage);
    char *basedir = dirname(_storage);
    free(_storage);

    struct stat d;
    if (stat(basedir, &d) != 0 || !S_ISDIR(d.st_mode)) {
        return -1;
    }
    return 0;
}

enum CompressionType get_compression(const char *flag) {
    if (flag == NULL) {
        return COMPRESS_NONE;
    } else if (!strcmp(flag, "zstd") || !strcmp(flag, "zstandard")) {
        return COMPRESS_ZSTD;
    } else if (!strcmp(flag, "lz4")) {
        return COMPRESS_LZ4;
    } else {
        FATAL("Unknown compression algorithm: %s\n", flag);
        exit(1);
    }

    return 0;
}