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
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "upload.h"
int
upload_chunk(const char *dir, size_t total, size_t received, size_t offset,
const void *data, size_t len, char *err, size_t errsz)
{
const unsigned char *bytes = data;
char part[800], source[800], buf[16384];
struct stat st;
size_t n, done = 0;
ssize_t got;
int fd = -1, dfd = -1, rv = -1;
if (total > UPLOAD_MAX_BYTES || total < 6 || received > total ||
offset > received || len == 0 || len > total || offset > total - len ||
(offset < received && len > received - offset)) {
snprintf(err, errsz, "chunk is out of order or outside the declared PDF");
return -2;
}
if (snprintf(part, sizeof(part), "%s/source.part", dir) >= (int)sizeof(part) ||
snprintf(source, sizeof(source), "%s/source.pdf", dir) >= (int)sizeof(source))
goto io;
if (received == total)
fd = open(source, O_RDONLY | O_CLOEXEC | O_NOFOLLOW);
else if (received == 0)
fd = open(part, O_RDWR | O_CREAT | O_EXCL | O_CLOEXEC | O_NOFOLLOW, 0640);
else {
fd = open(part, O_RDWR | O_CLOEXEC | O_NOFOLLOW);
/* A final rename can survive an uncertain SQL commit. */
if (fd == -1 && errno == ENOENT && rename(source, part) == 0)
fd = open(part, O_RDWR | O_CLOEXEC | O_NOFOLLOW);
}
if (fd == -1 || fstat(fd, &st) == -1 || !S_ISREG(st.st_mode) ||
st.st_nlink != 1 || st.st_size < 0 || (size_t)st.st_size < received ||
(received == total && (size_t)st.st_size != total) ||
(dfd = open(dir, O_RDONLY | O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW)) == -1)
goto io;
if (offset < received) {
/* A retry must never overwrite an acknowledged prefix. */
while (done < len) {
n = len - done < sizeof(buf) ? len - done : sizeof(buf);
got = pread(fd, buf, n, (off_t)(offset + done));
if (got == -1 && errno == EINTR)
continue;
if (got <= 0)
goto io;
if (memcmp(buf, bytes + done, (size_t)got) != 0) {
snprintf(err, errsz, "retry differs from an acknowledged chunk");
rv = -2;
goto out;
}
done += (size_t)got;
}
if (fsync(dfd) == -1)
goto io;
rv = (int)received;
goto out;
}
/* SQL owns the offset: discard only an uncommitted suffix. */
if (ftruncate(fd, (off_t)received) == -1)
goto io;
while (done < len) {
n = len - done < sizeof(buf) ? len - done : sizeof(buf);
got = pwrite(fd, bytes + done, n, (off_t)(received + done));
if (got == -1 && errno == EINTR)
continue;
if (got <= 0)
goto io;
done += (size_t)got;
}
if (fsync(fd) == -1)
goto io;
if (received + len == total && rename(part, source) == -1)
goto io;
/* Persist initial part creation as well as final/recovery renames. */
if (fsync(dfd) == -1)
goto io;
rv = (int)(received + len);
goto out;
io:
snprintf(err, errsz, "could not store PDF chunk; retry the same offset");
out:
if (dfd != -1)
close(dfd);
if (fd != -1)
close(fd);
return rv;
}