aboutsummaryrefslogtreecommitdiffstats
path: root/src/blogc-make/exec-native.c
blob: 8b87d180c86b89c8c27c61d05e3ed87f4d857291 (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
/*
 * blogc: A blog compiler.
 * Copyright (C) 2016 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 <sys/types.h>
#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>
#include <libgen.h>
#include <errno.h>
#include "../common/file.h"
#include "../common/utils.h"
#include "exec-native.h"
#include "ctx.h"


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 = bc_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, 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[BC_FILE_CHUNK_SIZE];
    while (0 < (nread = read(fd_from, buffer, BC_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;
}


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;

    char *short_path = bc_strdup(dest->short_path);
    char *path = bc_strdup(dest->path);

    char *dir_short = dirname(short_path);
    char *dir = dirname(path);

    while ((0 != strcmp(dir_short, ".")) && (0 != strcmp(dir, output_dir))) {
        DIR *d = opendir(dir);
        if (d == NULL) {
            fprintf(stderr, "error: failed to open directory (%s): %s\n",
                dir, strerror(errno));
            rv = 3;
            break;
        }

        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)) {
            fprintf(stderr, "error: failed to close directory (%s): %s\n",
                dir, strerror(errno));
            rv = 3;
            break;
        }

        if (count == 0) {
            if (verbose) {
                printf("Removing directory '%s'\n", dir);
                fflush(stdout);
            }
            if (0 != rmdir(dir)) {
                fprintf(stderr, "error: failed to remove directory(%s): %s\n",
                    dir, strerror(errno));
                rv = 3;
                break;
            }
        }

        dir_short = dirname(dir_short);
        dir = dirname(dir);
    }

    // try to remove output dir
    // this is done on a best-effort basis, if we can't remove it, it probably
    // have files, so we don't really want to remove it.
    rmdir(output_dir);

    free(short_path);
    free(path);

    return rv;
}