summaryrefslogtreecommitdiffstats
path: root/src/util.c
blob: ed93e6b6c0a8bfb9f37f5f462286bb209bc50fb7 (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
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <string.h>
#include <errno.h>
#include <limits.h>
#include <unistd.h>
#include <sys/mman.h>
#include <fcntl.h>

#include "util.h"

void statistics(DB *db) {
    DB_BTREE_STAT *statp;
    int ret;

    assert(db);
    
     if ((ret = db->stat(db, &statp, 0)) != 0) {
        db->err(db, ret, "DB->stat");
        return;
    }
    
    printf("Database contains %lu records\n", (long unsigned) statp->bt_ndata);
    free(statp);
}

char* normalize_path(char *s) {
    char *l, *p, *d;

    // deletes /./ and //

    if (*s == '/')
        l = p = d = s+1;
    else
        l = p = d = s;
    
    for (; *p; p++) {
        if (*p == '/') {

            if (l-p == 0) {
                l++;
                continue;
            }

            if (p-l == 1 && *l == '.') {
                l += 2;
                continue;
            }

            while (l <= p)
                *(d++) = *(l++);
        }
    }

    while (l <= p)
        *(d++) = *(l++);

    return s;
}

void rotdash(void) {
    static const char dashes[] = /* ".oOo"; */ "|/-\\";
    const static char *d = dashes;

    if (isatty(fileno(stderr))) {
        fprintf(stderr, "%c\b", *d);
        
        d++;
        if (!*d)
            d = dashes;
    }
}

const char* get_attached_filename(const char *path, const char *fn) {
    static char npath[PATH_MAX];
    struct stat st;

    if (stat(path, &st) < 0) {
        if (errno == ENOENT)
            return path;
        
        fprintf(stderr, "stat(%s) failed: %s\n", path, strerror(errno));
        return NULL;
    }

    if (S_ISREG(st.st_mode))
        return path;

    if (S_ISDIR(st.st_mode)) {
        snprintf(npath, sizeof(npath), "%s/.syrep", path);
        mkdir(npath, 0777);
        snprintf(npath, sizeof(npath), "%s/.syrep/%s", path, fn);
        return npath;
    }

    fprintf(stderr, "<%s> is not a valid syrep snapshot\n", path);
    return NULL;
}

int isdirectory(const char *path) {
    struct stat st;

    if (stat(path, &st) < 0)
        return -1;

    return !!S_ISDIR(st.st_mode);
}


#define MMAPSIZE (100*1024*1024)
#define BUFSIZE (1024*1024)

int copy_fd(int sfd, int dfd, uint32_t l) {
    off_t src_o, dst_o;
    void *src_p, *dst_p;
    void *buf = NULL;
    int r = -1;
    struct stat st;

    if ((src_o = lseek(sfd, 0, SEEK_CUR)) == (off_t) -1) {
        fprintf(stderr, "lseek(): %s\n", strerror(errno));
        goto finish;
    }

    if (fstat(sfd, &st) < 0) {
        fprintf(stderr, "fstat(): %s\n", strerror(errno));
        goto finish;
    }

    if (l == (uint32_t) -1 || l >= st.st_size - src_o)
        l = st.st_size - src_o;

    if ((dst_o = lseek(dfd, 0, SEEK_CUR)) == (off_t) -1) {
        fprintf(stderr, "lseek(): %s\n", strerror(errno));
        goto finish;
    }
    
    if (fstat(dfd, &st) < 0) {
        fprintf(stderr, "fstat(): %s\n", strerror(errno));
        goto finish;
    }
    
    if (dst_o+l > st.st_size)
        if (ftruncate(dfd, dst_o+l) < 0) {
            fprintf(stderr, "ftruncate(): %s\n", strerror(errno));
            goto finish;
        }

    if (l > BUFSIZE) {
        uint32_t m = l < MMAPSIZE ? l : MMAPSIZE;

        while (l > 0) {
            if ((src_p = mmap(NULL, m, PROT_READ, MAP_SHARED, sfd, src_o)) == MAP_FAILED)
                break;
            
            if ((dst_p = mmap(NULL, m, PROT_READ|PROT_WRITE, MAP_SHARED, dfd, dst_o)) == MAP_FAILED) {
                munmap(src_p, m);
                break;
            }
            
            memcpy(dst_p, src_p, m);
            
            munmap(src_p, m);
            munmap(dst_p, m);
            
            src_o += m;
            dst_o += m;
            l -= m;
            
            m = l < MMAPSIZE ? l : MMAPSIZE;
        }
        
    
        if (lseek(sfd, src_o, SEEK_SET) == (off_t) -1) {
            fprintf(stderr, "lseek(): %s\n", strerror(errno));
            goto finish;
        }
        
        if (lseek(dfd, dst_o, SEEK_SET) == (off_t) -1) {
            fprintf(stderr, "lseek(): %s\n", strerror(errno));
            goto finish;
        }
    }
    
    if (l > 0) {
        if (!(buf = malloc(BUFSIZE)))
            goto finish;

        for (;;) {
            ssize_t m;

            if ((m = read(sfd, buf, BUFSIZE)) < 0) {
                fprintf(stderr, "read(): %s\n", strerror(errno));
                goto finish;
            }

            if (!m)
                break;
                
            if (write(dfd, buf, m) < 0) {
                fprintf(stderr, "write(): %s\n", strerror(errno));
                goto finish;
            }
        }
    }

    r = 0;
    
finish:

    return r;
}

int copy_file(const char *src, const char *dst) {
    int sfd = -1, dfd = -1, r = -1;

    if ((sfd = open(src, O_RDONLY)) < 0) {
        fprintf(stderr, "open(%s, O_RDONLY): %s\n", src, strerror(errno));
        goto finish;
    }

    if ((dfd = open(dst, O_RDWR|O_TRUNC|O_CREAT, 0666)) < 0) {
        fprintf(stderr, "open(%s, O_RDWR|O_TRUNC|O_CREAT): %s\n", dst, strerror(errno));
        goto finish;
    }

    if (copy_fd(sfd, dfd, (uint32_t) -1) < 0)
        goto finish;

    r = 0;

finish:
    
    if (sfd >= 0)
        close(sfd);

    if (dfd >= 0)
        close(dfd);

    return r;
}

int copy_or_link_file(const char *src, const char *dst) {

    if (link(src, dst) < 0) {

        if (errno == EXDEV || errno == EPERM)
            return copy_file(src, dst);

        fprintf(stderr, "link(%s, %s): %s\n", src, dst, strerror(errno));
        return -1;
    }

    return 0;
}