aboutsummaryrefslogtreecommitdiffstats
path: root/src/blogc-make/exec-native.c
blob: 1b517ff6d5448f03a3ad2017dc4faf636a01b529 (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
/*
 * blogc: A blog compiler.
 * Copyright (C) 2014-2017 Rafael G. Martins <rafael@rafaelmartins.eng.br>
 *
 * This program can be distributed under the terms of the BSD License.
 * See the file LICENSE.
 */

#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>
#include <libgen.h>
#include <errno.h>
#include <squareball.h>

#include "exec-native.h"
#include "ctx.h"

#define BM_FILE_CHUNK_SIZE 1024


int
bm_exec_native_cp(bm_filectx_t *source, bm_filectx_t *dest, bool verbose)
{
    if (verbose)
        printf("Copying '%s' to '%s'\n", source->path, dest->path);
    else
        printf("  COPY     %s\n", dest->short_path);
    fflush(stdout);

    char *fname = sb_strdup(dest->path);
    for (char *tmp = fname; *tmp != '\0'; tmp++) {
        if (*tmp != '/' && *tmp != '\\')
            continue;
        char bkp = *tmp;
        *tmp = '\0';
        if ((strlen(fname) > 0) &&
            (-1 == mkdir(fname, 0777)) &&
            (errno != EEXIST))
        {
            fprintf(stderr, "blogc-make: error: failed to create output "
                "directory (%s): %s\n", fname, strerror(errno));
            free(fname);
            exit(2);
        }
        *tmp = bkp;
    }
    free(fname);

    int fd_from = open(source->path, O_RDONLY);
    if (fd_from < 0) {
        fprintf(stderr, "blogc-make: error: failed to open source file to copy "
            " (%s): %s\n", source->path, strerror(errno));
        return 3;
    }

    int fd_to = open(dest->path, O_WRONLY | O_CREAT | O_TRUNC, 0666);
    if (fd_to < 0) {
        fprintf(stderr, "blogc-make: error: failed to open destination file to "
            "copy (%s): %s\n", dest->path, strerror(errno));
        close(fd_from);
        return 3;
    }

    ssize_t nread;
    char buffer[BM_FILE_CHUNK_SIZE];
    while (0 < (nread = read(fd_from, buffer, BM_FILE_CHUNK_SIZE))) {
        char *out_ptr = buffer;
        do {
            ssize_t nwritten = write(fd_to, out_ptr, nread);
            if (nwritten == -1) {
                fprintf(stderr, "blogc-make: error: failed to write to "
                    "destination file (%s): %s\n", dest->path, strerror(errno));
                close(fd_from);
                close(fd_to);
                return 3;
            }
            nread -= nwritten;
            out_ptr += nwritten;
        } while (nread > 0);
    }

    return 0;
}


bool
bm_exec_native_is_empty_dir(const char *dir, sb_error_t **err)
{
    DIR *d = opendir(dir);
    if (d == NULL) {
        if (errno == ENOENT) {
            return true;
        }
        if (err != NULL) {
            *err = sb_error_new_printf(0, "failed to open directory (%s): %s\n",
                dir, strerror(errno));
        }
        return true;
    }

    struct dirent *e;
    size_t count = 0;
    while (NULL != (e = readdir(d))) {
        if ((0 == strcmp(e->d_name, ".")) || (0 == strcmp(e->d_name, "..")))
            continue;
        count++;
        break;
    }

    if (0 != closedir(d)) {
        if (err != NULL) {
            *err = sb_error_new_printf(0, "failed to close directory (%s): %s\n",
                dir, strerror(errno));
        }
        return true;
    }

    return count == 0;
}


int
bm_exec_native_rm(const char *output_dir, bm_filectx_t *dest, bool verbose)
{
    if (verbose)
        printf("Removing file '%s'\n", dest->path);
    else
        printf("  CLEAN    %s\n", dest->short_path);
    fflush(stdout);

    if (0 != unlink(dest->path)) {
        fprintf(stderr, "blogc-make: error: failed to remove file (%s): %s\n",
            dest->path, strerror(errno));
        return 3;
    }

    int rv = 0;

    // blame freebsd's libc for all of those memory allocations around dirname
    // calls!
    char *short_dir = sb_strdup(dirname(dest->short_path));
    char *dir = sb_strdup(dirname(dest->path));

    sb_error_t *err = NULL;

    while ((0 != strcmp(short_dir, ".")) && (0 != strcmp(short_dir, "/"))) {
        bool empty = bm_exec_native_is_empty_dir(dir, &err);
        if (err != NULL) {
            fprintf(stderr, "blogc-make: error: %s\n", err->msg);
            sb_error_free(err);
            rv = 3;
            break;
        }
        if (!empty) {
            break;
        }
        if (verbose) {
            printf("Removing directory '%s'\n", dir);
            fflush(stdout);
        }
        if (0 != rmdir(dir)) {
            fprintf(stderr,
                "blogc-make: error: failed to remove directory(%s): %s\n",
                dir, strerror(errno));
            rv = 3;
            break;
        }
        if (0 == strcmp(dir, output_dir)) {
            break;
        }

        char *tmp = short_dir;
        short_dir = sb_strdup(dirname(short_dir));
        free(tmp);
        tmp = dir;
        dir = sb_strdup(dirname(dir));
        free(tmp);
    }

    free(short_dir);
    free(dir);

    return rv;
}