summaryrefslogtreecommitdiffstats
path: root/src/package.c
blob: 5a741eb3fb64ee85985051ca1f5c183380c66216 (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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <limits.h>
#include <unistd.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>

#include "package.h"

struct package_item;

struct package_item {
    char *name;
    char *path;
    struct package_item *next;
};

struct package {
    char *base;
    int count;
    struct package_item *items;
    struct package_item *last;
};

static int copy(FILE *f, FILE *t, uint32_t len) {
    static char buf[2048*4];
    
    while (len > 0) {
        uint32_t n;
        
        if (feof(f))
            return -1;

        n = len > sizeof(buf) ? sizeof(buf) : len;
        
        if (fread(buf, n, 1, f) != 1)
            return -1;
        
        if (fwrite(buf, n, 1, t) != 1)
            return -1;

        len -= n;
    }

    return 0;
}

static char *tmp(char *fn, int l) {
    char *t;

    if (!(t = getenv("TMPDIR")))
        if (!(t = getenv("TEMP")))
            if (!(t = getenv("TMP")))
                t = "/tmp";
    
    snprintf(fn, l, "%s/pkgXXXXXX", t);
    return fn;
}

static struct package_item *item_new(const char *name, const char *path) {
    struct package_item *i = NULL;

    if (!(i = malloc(sizeof(struct package_item))))
        return NULL;

    memset(i, 0, sizeof(struct package_item));

    if (!(i->name = strdup(name)))
        goto fail;

    if (!(i->path = strdup(path)))
        goto fail;

    return i;

fail:
    if(i) {
        if (i->path)
            free(i->path);
        if (i->name)
            free(i->name);
        free(i);
    }

    return NULL;
}

struct package* package_open(const char *fn) {
    struct package *p;
    FILE *f = NULL;
    char path[PATH_MAX];

    if (!(p = malloc(sizeof(struct package)))) {
        fprintf(stderr, "Failed to allocate package structure.\n");
        return NULL;
    }

    memset(p, 0, sizeof(struct package));

    tmp(path, sizeof(path));
    if (!mkdtemp(path)) {
        fprintf(stderr, "Failed to create temporary directory.\n");
        goto finish;
    }

    if (!(p->base = strdup(path))) {
        fprintf(stderr, "Failed to allocate memory.\n");
        goto finish;
    }
    
    if (fn) {
        if (!(f = fopen(fn, "r"))) {
            fprintf(stderr, "Failed to open <%s>: %s\n", fn, strerror(errno));
            goto finish;
        }
        
        for (;;) {
            uint32_t len;
            char name[PACKAGE_ITEM_NAME_LEN+1];
            FILE *d = NULL;
            struct package_item *pi;
            
            if (!fread(name, sizeof(name)-1, 1, f))
                break;

            name[sizeof(name)-1] = 0;
            
            if (fread(&len, sizeof(len), 1, f) != 1) {
                fprintf(stderr, "Short read while reading length: %s\n", strerror(errno));
                goto finish;
            }

            snprintf(path, sizeof(path), "%s/%i", p->base, p->count++);
            if (!(d = fopen(path, "w+"))) {
                fprintf(stderr, "Failed to open target file: %s\n", strerror(errno));
                goto finish;
            }
            
            if (copy(f, d, len) < 0) {
                fprintf(stderr, "Copy failed: %s\n", strerror(errno));
                fclose(d);
                unlink(path);
                goto finish;
            }

            fclose(d);
            if (!(pi = item_new(name, path))) {
                unlink(path);
                fprintf(stderr, "Failed to allocate memory.\n");
                goto finish;
            }

            assert(!!p->last == !!p->items);
            
            if (p->last) {
                p->last->next = pi;
                p->last = pi;
            } else
                p->items = p->last = pi;
        }

        fclose(f);
    }

    return p;

finish:
    if (f)
        fclose(f);

    if (p)
        package_remove(p);

    return NULL;
}

int package_save(struct package *p, const char *fn) {
    FILE *f, *s = NULL;
    int r = 0;
    struct package_item *i;
    
    assert(p && fn);

    if (!(f = fopen(fn, "w+")))
        return -1;

    for (i = p->items; i; i = i->next) {
        char name[PACKAGE_ITEM_NAME_LEN+1];
        uint32_t len;
        struct stat st;

        if (!i->name || !i->path)
            continue;

        if (!(s = fopen(i->path, "r")))
            continue;

        //fprintf(stderr, "Saving %s (%s)...\n", i->name, i->path);
        
        memset(name, 0, sizeof(name));
        strncpy(name, i->name, sizeof(name)-1);
        
        if (fwrite(name, sizeof(name)-1, 1, f) != 1) {
            r = 1;
            break;
        }
        
        if (fstat(fileno(s), &st) < 0) {
            fprintf(stderr, "Could not get file size: %s\n", strerror(errno));
            r = 1;
            break;
        }
        
        len = st.st_size;
        
        if (fwrite(&len, sizeof(len), 1, f) != 1) {
            r = 1;
            break;
        }
        
        if (copy(s, f, len) < 0) {
            fprintf(stderr, "Could not copy file to package: %s\n", strerror(errno));
            r = 1;
            break;
        }
        
        fclose(s);
        s = NULL;
    }

    if (s)
        fclose(s);
    
    fclose(f);

    if (r != 0)
        unlink(fn);

    return r;
}

const char *package_get_item(struct package* p, const char *name) {
    struct package_item *i;
    char path[PATH_MAX];
    assert(p && name);

    for (i = p->items; i; i = i->next) {
        if (strncmp(name, i->name, PACKAGE_ITEM_NAME_LEN) == 0)
            return i->path;
    }

    snprintf(path, sizeof(path), "%s/%i", p->base, p->count++);
    
    if (!(i = item_new(name, path))) {
        unlink(path);
        return NULL;
    }

    assert(!!p->last == !!p->items);
    
    if (p->last) {
        p->last->next = i;
        p->last = i;
    } else
        p->items = p->last = i;
    
    return i->path;
}

void package_remove(struct package *p) {
    struct package_item *i, *n;
    assert(p);

    for (i = p->items; i; i = n) {
        n = i->next;

        if (i->path)
            if (unlink(i->path))
                if (errno != ENOENT)
                    fprintf(stderr, "Failed to remove <%s>: %s\n", i->path, strerror(errno));
        
        free(i->path);
        free(i->name);
        free(i);
    }

    if (p->base) {
        if (rmdir(p->base))
            fprintf(stderr, "Failed to remove <%s>: %s\n", p->base, strerror(errno));
        
        free(p->base);
    }
    free(p);
}


int package_foreach(struct package *p, int (*cb) (struct package *p, const char *name, const char *path, void *u), void *u) {
    struct package_item *i;
    assert(p);
    
    for (i = p->items; i; i = i->next)
        if (cb(p, i->name, i->path, u) < 0)
            return -1;

    return 0;
}