Open file for write for every truncate

solves #382
This commit is contained in:
benrubson 2017-08-15 16:15:58 +02:00
parent acbeb64765
commit 41d5ec4cba

View File

@ -456,34 +456,40 @@ bool CipherFileIO::streamRead(unsigned char *buf, int size,
int CipherFileIO::truncate(off_t size) { int CipherFileIO::truncate(off_t size) {
int res = 0; int res = 0;
int reopen = 0;
// well, we will truncate, so we need a write access to the file
if (!base->isWritable()) {
int newFlags = lastFlags | O_RDWR;
int res = base->open(newFlags);
if (res < 0) {
VLOG(1) << "truncate failed to re-open for write";
base->open(lastFlags);
return res;
}
reopen = 1;
}
if (!haveHeader) { if (!haveHeader) {
res = BlockFileIO::truncateBase(size, base.get()); res = BlockFileIO::truncateBase(size, base.get());
} else { } else {
if (0 == fileIV) { if (0 == fileIV) {
// empty file.. create the header.. // empty file.. create the header..
if (!base->isWritable()) { res = initHeader();
// open for write..
int newFlags = lastFlags | O_RDWR;
int res = base->open(newFlags);
if (res < 0) {
VLOG(1) << "truncate failed to re-open for write";
return res;
}
}
int res = initHeader();
if (res < 0) {
return res;
}
} }
// can't let BlockFileIO call base->truncate(), since it would be using // can't let BlockFileIO call base->truncate(), since it would be using
// the wrong size.. // the wrong size..
res = BlockFileIO::truncateBase(size, nullptr); if (res == 0) {
res = BlockFileIO::truncateBase(size, nullptr);
}
if (res == 0) { if (res == 0) {
res = base->truncate(size + HEADER_SIZE); res = base->truncate(size + HEADER_SIZE);
} }
} }
if (reopen == 1) {
reopen = base->open(lastFlags);
if (res < 0) {
res = reopen;
}
}
return res; return res;
} }