aboutsummaryrefslogtreecommitdiff
path: root/src/fileutils.c
blob: 64c7a48ef3fd3ae1e66102e206616e5ca0b16219 (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
#include "fileutils.h"
#include "alloc.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <limits.h>
#include <pwd.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>

const char* get_home_dir() {
    const char *home_dir = getenv("HOME");
    if(!home_dir) {
        struct passwd *pw = getpwuid(getuid());
        home_dir = pw->pw_dir;
    }
    return home_dir;
}

int file_get_content(const char *filepath, char **data, long *size) {
    int result = 0;
    FILE *file = fopen(filepath, "rb");
    if(!file) {
        int err = -errno;
        perror(filepath);
        return err;
    }

    fseek(file, 0, SEEK_END);
    *size = ftell(file);
    if(*size == -1) {
        fprintf(stderr, "Failed to tell the size of file %s, is it not a file?\n", filepath);
        result = -1;
        goto cleanup;
    }
    fseek(file, 0, SEEK_SET);

    *data = alloc_or_crash(*size);
    if((long)fread(*data, 1, *size, file) != *size) {
        fprintf(stderr, "Failed to read all bytes in file %s\n", filepath);
        result = -1;
        goto cleanup;
    }

    cleanup:
    fclose(file);
    return result;
}

int create_directory_recursive(char *path) {
    int path_len = strlen(path);
    char *p = path;
    char *end = path + path_len;
    for(;;) {
        char *slash_p = strchr(p, '/');
        
        // Skips first '/', we don't want to try and create the root directory
        if(slash_p == path) {
            ++p;
            continue;
        }

        if(!slash_p)
            slash_p = end;

        char prev_char = *slash_p;
        *slash_p = '\0';
        int err = mkdir(path, S_IRWXU);
        *slash_p = prev_char;
        
        if(err == -1 && errno != EEXIST)
            return err;

        if(slash_p == end)
            break;
        else
            p = slash_p + 1;
    }
    return 0;
}

int file_exists(const char *path) {
    return access(path, F_OK);
}

int create_lock_file(const char *path) {
    int fd = open(path, O_CREAT | O_EXCL);
    if(fd == -1)
        return errno;
    fsync(fd);
    return close(fd);
}

int file_overwrite(const char *filepath, const char *data, size_t size) {
    FILE *file = fopen(filepath, "wb");
    if(!file) {
        int err = -errno;
        perror(filepath);
        return err;
    }

    unsigned long bytes_written = fwrite(data, 1, size, file);
    if(bytes_written != size) {
        fprintf(stderr, "Failed to write all bytes to file %s. Expected to write %zu bytes, only wrote %zu bytes\n", filepath, size, bytes_written);
        fclose(file);
        return -1;
    }

    fclose(file);
    return 0;
}

int file_overwrite_in_dir(const char *dir, const char *filename, const char *data, size_t size) {
    char filepath[PATH_MAX];
    const char *filepath_components[] = { dir, filename };
    path_join(filepath, filepath_components, 2);
    return file_overwrite(filepath, data, size);
}

void path_join(char *output, const char **components, int num_components) {
    int offset = 0;
    for(int i = 0; i < num_components; ++i) {
        if(i > 0) {
            output[offset] = '/';
            ++offset;
        }

        int component_len = strlen(components[i]);
        memcpy(output + offset, components[i], component_len);
        offset += component_len;
    }
    output[offset] = '\0';
}