2013-01-29 04:07:54 +01:00
|
|
|
/*****************************************************************************
|
|
|
|
* Author: Valient Gough <vgough@pobox.com>
|
|
|
|
*
|
|
|
|
*****************************************************************************
|
|
|
|
* Copyright (c) 2004, Valient Gough
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU Lesser General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or (at your
|
2013-10-20 00:35:26 +02:00
|
|
|
* option) any later version.
|
2013-01-29 04:07:54 +01:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef linux
|
2013-10-20 00:35:26 +02:00
|
|
|
#define _XOPEN_SOURCE 500 // pick up pread , pwrite
|
2013-01-29 04:07:54 +01:00
|
|
|
#endif
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "base/Error.h"
|
|
|
|
#include "fs/RawFileIO.h"
|
|
|
|
|
|
|
|
#include <glog/logging.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
#include <cerrno>
|
|
|
|
|
2013-03-05 07:29:58 +01:00
|
|
|
namespace encfs {
|
|
|
|
|
2013-01-29 04:07:54 +01:00
|
|
|
static Interface RawFileIO_iface = makeInterface("FileIO/Raw", 1, 0, 0);
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
inline void swap(int &x, int &y) {
|
|
|
|
int tmp = x;
|
|
|
|
x = y;
|
|
|
|
y = tmp;
|
2013-01-29 04:07:54 +01:00
|
|
|
}
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
RawFileIO::RawFileIO()
|
|
|
|
: knownSize(false), fileSize(0), fd(-1), oldfd(-1), canWrite(false) {}
|
2013-01-29 04:07:54 +01:00
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
RawFileIO::RawFileIO(const std::string &fileName)
|
|
|
|
: name(fileName),
|
|
|
|
knownSize(false),
|
|
|
|
fileSize(0),
|
|
|
|
fd(-1),
|
|
|
|
oldfd(-1),
|
|
|
|
canWrite(false) {}
|
2013-01-29 04:07:54 +01:00
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
RawFileIO::~RawFileIO() {
|
2013-01-29 04:07:54 +01:00
|
|
|
int _fd = -1;
|
|
|
|
int _oldfd = -1;
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
swap(_fd, fd);
|
|
|
|
swap(_oldfd, oldfd);
|
2013-01-29 04:07:54 +01:00
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
if (_oldfd != -1) close(_oldfd);
|
2013-01-29 04:07:54 +01:00
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
if (_fd != -1) close(_fd);
|
2013-01-29 04:07:54 +01:00
|
|
|
}
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
Interface RawFileIO::interface() const { return RawFileIO_iface; }
|
2013-01-29 04:07:54 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
Workaround for opening a file for write when permissions don't allow.
|
|
|
|
Since the kernel has already checked permissions, we can assume it is ok to
|
|
|
|
provide access. So force it by changing permissions temporarily. Should
|
|
|
|
be called with a lock around it so that there won't be a race condition
|
|
|
|
with calls to lstat picking up the wrong permissions.
|
|
|
|
*/
|
2013-10-20 00:35:26 +02:00
|
|
|
static int open_readonly_workaround(const char *path, int flags) {
|
2013-01-29 04:07:54 +01:00
|
|
|
int fd = -1;
|
|
|
|
struct stat stbuf;
|
|
|
|
memset(&stbuf, 0, sizeof(struct stat));
|
2013-10-20 00:35:26 +02:00
|
|
|
if (lstat(path, &stbuf) != -1) {
|
2013-01-29 04:07:54 +01:00
|
|
|
// make sure user has read/write permission..
|
2013-10-20 00:35:26 +02:00
|
|
|
chmod(path, stbuf.st_mode | 0600);
|
|
|
|
fd = ::open(path, flags);
|
|
|
|
chmod(path, stbuf.st_mode);
|
|
|
|
} else {
|
2013-01-29 04:07:54 +01:00
|
|
|
LOG(INFO) << "can't stat file " << path;
|
|
|
|
}
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
We shouldn't have to support all possible open flags, so untaint the flags
|
|
|
|
argument by only taking ones we understand and accept.
|
|
|
|
- Since the kernel has already done permission tests before calling us, we
|
|
|
|
shouldn't have to worry about access control.
|
|
|
|
- Basically we just need to distinguish between read and write flags
|
|
|
|
- Also keep the O_LARGEFILE flag, in case the underlying filesystem needs
|
|
|
|
it..
|
|
|
|
*/
|
2013-10-20 00:35:26 +02:00
|
|
|
int RawFileIO::open(int flags) {
|
2013-01-29 04:07:54 +01:00
|
|
|
bool requestWrite = ((flags & O_RDWR) || (flags & O_WRONLY));
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
VLOG(1) << "open call for " << (requestWrite ? "writable" : "read only")
|
|
|
|
<< " file";
|
2013-01-29 04:07:54 +01:00
|
|
|
|
|
|
|
int result = 0;
|
|
|
|
|
|
|
|
// if we have a descriptor and it is writable, or we don't need writable..
|
2013-10-20 00:35:26 +02:00
|
|
|
if ((fd >= 0) && (canWrite || !requestWrite)) {
|
2013-01-29 04:07:54 +01:00
|
|
|
VLOG(1) << "using existing file descriptor";
|
2013-10-20 00:35:26 +02:00
|
|
|
result = fd; // success
|
|
|
|
} else {
|
2013-01-29 04:07:54 +01:00
|
|
|
int finalFlags = requestWrite ? O_RDWR : O_RDONLY;
|
|
|
|
|
|
|
|
#if defined(O_LARGEFILE)
|
2013-10-20 00:35:26 +02:00
|
|
|
if (flags & O_LARGEFILE) finalFlags |= O_LARGEFILE;
|
2013-01-29 04:07:54 +01:00
|
|
|
#else
|
|
|
|
#warning O_LARGEFILE not supported
|
|
|
|
#endif
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
int newFd = ::open(name.c_str(), finalFlags);
|
2013-01-29 04:07:54 +01:00
|
|
|
|
|
|
|
VLOG(1) << "open file with flags " << finalFlags << ", result = " << newFd;
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
if ((newFd == -1) && (errno == EACCES)) {
|
2013-01-29 04:07:54 +01:00
|
|
|
VLOG(1) << "using readonly workaround for open";
|
2013-10-20 00:35:26 +02:00
|
|
|
newFd = open_readonly_workaround(name.c_str(), finalFlags);
|
2013-01-29 04:07:54 +01:00
|
|
|
}
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
if (newFd >= 0) {
|
|
|
|
if (oldfd >= 0) {
|
|
|
|
LOG(ERROR) << "leaking FD?: oldfd = " << oldfd << ", fd = " << fd
|
|
|
|
<< ", newfd = " << newFd;
|
2013-01-29 04:07:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// the old fd might still be in use, so just keep it around for
|
|
|
|
// now.
|
|
|
|
canWrite = requestWrite;
|
|
|
|
oldfd = fd;
|
|
|
|
result = fd = newFd;
|
2013-10-20 00:35:26 +02:00
|
|
|
} else {
|
2013-01-29 04:07:54 +01:00
|
|
|
result = -errno;
|
|
|
|
LOG(INFO) << "::open error: " << strerror(errno);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG_IF(INFO, result < 0) << "file " << name << " open failure: " << -result;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
int RawFileIO::getAttr(struct stat *stbuf) const {
|
|
|
|
int res = lstat(name.c_str(), stbuf);
|
2013-01-29 04:07:54 +01:00
|
|
|
int eno = errno;
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
LOG_IF(INFO, res < 0) << "getAttr error on " << name << ": " << strerror(eno);
|
2013-01-29 04:07:54 +01:00
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
return (res < 0) ? -eno : 0;
|
2013-01-29 04:07:54 +01:00
|
|
|
}
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
void RawFileIO::setFileName(const char *fileName) { name = fileName; }
|
2013-01-29 04:07:54 +01:00
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
const char *RawFileIO::getFileName() const { return name.c_str(); }
|
2013-01-29 04:07:54 +01:00
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
off_t RawFileIO::getSize() const {
|
|
|
|
if (!knownSize) {
|
2013-01-29 04:07:54 +01:00
|
|
|
struct stat stbuf;
|
2013-10-20 00:35:26 +02:00
|
|
|
memset(&stbuf, 0, sizeof(struct stat));
|
|
|
|
int res = lstat(name.c_str(), &stbuf);
|
2013-01-29 04:07:54 +01:00
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
if (res == 0) {
|
2013-01-29 04:07:54 +01:00
|
|
|
fileSize = stbuf.st_size;
|
|
|
|
knownSize = true;
|
|
|
|
return fileSize;
|
|
|
|
} else
|
|
|
|
return -1;
|
2013-10-20 00:35:26 +02:00
|
|
|
} else {
|
2013-01-29 04:07:54 +01:00
|
|
|
return fileSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
ssize_t RawFileIO::read(const IORequest &req) const {
|
|
|
|
rAssert(fd >= 0);
|
2013-01-29 04:07:54 +01:00
|
|
|
|
|
|
|
VLOG(2) << "Read " << req.dataLen << " bytes from offset " << req.offset;
|
2013-10-20 00:35:26 +02:00
|
|
|
ssize_t readSize = pread(fd, req.data, req.dataLen, req.offset);
|
2013-01-29 04:07:54 +01:00
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
if (readSize < 0) {
|
|
|
|
LOG(INFO) << "read failed at offset " << req.offset << " for "
|
|
|
|
<< req.dataLen << " bytes: " << strerror(errno);
|
2013-01-29 04:07:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return readSize;
|
|
|
|
}
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
bool RawFileIO::write(const IORequest &req) {
|
|
|
|
rAssert(fd >= 0);
|
|
|
|
rAssert(true == canWrite);
|
2013-01-29 04:07:54 +01:00
|
|
|
|
|
|
|
VLOG(2) << "Write " << req.dataLen << " bytes to offset " << req.offset;
|
|
|
|
|
|
|
|
int retrys = 10;
|
|
|
|
void *buf = req.data;
|
|
|
|
ssize_t bytes = req.dataLen;
|
|
|
|
off_t offset = req.offset;
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
while (bytes && retrys > 0) {
|
|
|
|
ssize_t writeSize = ::pwrite(fd, buf, bytes, offset);
|
2013-01-29 04:07:54 +01:00
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
if (writeSize < 0) {
|
2013-01-29 04:07:54 +01:00
|
|
|
knownSize = false;
|
2013-10-20 00:35:26 +02:00
|
|
|
LOG(INFO) << "write failed at offset " << offset << " for " << bytes
|
|
|
|
<< " bytes: " << strerror(errno);
|
2013-01-29 04:07:54 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bytes -= writeSize;
|
|
|
|
offset += writeSize;
|
2013-10-20 00:35:26 +02:00
|
|
|
buf = (void *)((char *)buf + writeSize);
|
2013-01-29 04:07:54 +01:00
|
|
|
--retrys;
|
|
|
|
}
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
if (bytes != 0) {
|
|
|
|
LOG(ERROR) << "Write error: wrote " << (req.dataLen - bytes) << " bytes of "
|
|
|
|
<< req.dataLen << ", max retries reached";
|
2013-01-29 04:07:54 +01:00
|
|
|
knownSize = false;
|
|
|
|
return false;
|
2013-10-20 00:35:26 +02:00
|
|
|
} else {
|
|
|
|
if (knownSize) {
|
2013-01-29 04:07:54 +01:00
|
|
|
off_t last = req.offset + req.dataLen;
|
2013-10-20 00:35:26 +02:00
|
|
|
if (last > fileSize) fileSize = last;
|
2013-01-29 04:07:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
int RawFileIO::truncate(off_t size) {
|
2013-01-29 04:07:54 +01:00
|
|
|
int res;
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
if (fd >= 0 && canWrite) {
|
|
|
|
res = ::ftruncate(fd, size);
|
2013-01-29 04:07:54 +01:00
|
|
|
#ifndef __FreeBSD__
|
2013-10-20 00:35:26 +02:00
|
|
|
::fdatasync(fd);
|
2013-01-29 04:07:54 +01:00
|
|
|
#endif
|
|
|
|
} else
|
2013-10-20 00:35:26 +02:00
|
|
|
res = ::truncate(name.c_str(), size);
|
2013-01-29 04:07:54 +01:00
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
if (res < 0) {
|
2013-01-29 04:07:54 +01:00
|
|
|
int eno = errno;
|
2013-10-20 00:35:26 +02:00
|
|
|
LOG(INFO) << "truncate failed for " << name << " (" << fd << ") size "
|
|
|
|
<< size << ", error " << strerror(eno);
|
2013-01-29 04:07:54 +01:00
|
|
|
res = -eno;
|
|
|
|
knownSize = false;
|
2013-10-20 00:35:26 +02:00
|
|
|
} else {
|
2013-01-29 04:07:54 +01:00
|
|
|
res = 0;
|
|
|
|
fileSize = size;
|
|
|
|
knownSize = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
bool RawFileIO::isWritable() const { return canWrite; }
|
2013-03-05 07:29:58 +01:00
|
|
|
|
|
|
|
} // namespace encfs
|