handle file size vs. upload_offset during load

parent 1b4b025d
......@@ -590,6 +590,7 @@ tus_file_add(struct VPFX(tus_server) *srv, int basefd, const char *filename)
struct stat st;
int fd = -1;
const char *err;
ssize_t sz;
if (fstatat(basefd, filename, &st, AT_SYMLINK_NOFOLLOW)) {
fprintf(stderr, "tus add stat %s: %d\n", filename, errno);
......@@ -634,6 +635,25 @@ tus_file_add(struct VPFX(tus_server) *srv, int basefd, const char *filename)
goto err;
}
sz = (uintmax_t)st.st_size - header_size;
assert(sz >= 0);
/*
* if data was written before we updated the offset,
* we truncate to avoid potentially corrupted data
*/
if (fdisk->location_length > 0) {
// .done() called & truncated
}
else if (fdisk->upload_offset < sz) {
AZ(ftruncate(fd, header_size + fdisk->upload_offset));
fprintf(stderr, "tus add %s: truncated to %zd\n", filename, sz);
}
else if (fdisk->upload_offset > sz) {
fprintf(stderr, "tus add %s: updated offset from %zd to %zd\n",
filename, fdisk->upload_offset, sz);
fdisk->upload_offset = sz;
}
/* note: the close can cause a SIGSEGV if a file is unlinked
* concurrently. But as we should be the only user of the base
* directory, that should not happen
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment