cleanup code, check errno usage

This commit is contained in:
Valient Gough 2017-08-25 22:52:59 -07:00
parent 9089b8555f
commit 64c3b4459c
No known key found for this signature in database
GPG Key ID: 33C65E29813C14DF
2 changed files with 39 additions and 39 deletions

View File

@ -90,6 +90,8 @@ Interface RawFileIO::interface() const { return RawFileIO_iface; }
This works around the problem described in
https://github.com/vgough/encfs/issues/181
Without this, "umask 0777 ; echo foo > bar" fails.
Sets errno when -1 is returned.
*/
static int open_readonly_workaround(const char *path, int flags) {
int fd = -1;
@ -118,13 +120,12 @@ int RawFileIO::open(int flags) {
bool requestWrite = (((flags & O_RDWR) != 0) || ((flags & O_WRONLY) != 0));
VLOG(1) << "open call, requestWrite = " << requestWrite;
int result = 0;
// if we have a descriptor and it is writable, or we don't need writable..
if ((fd >= 0) && (canWrite || !requestWrite)) {
VLOG(1) << "using existing file descriptor";
result = fd; // success
} else {
return fd; // success
}
int finalFlags = requestWrite ? O_RDWR : O_RDONLY;
#if defined(O_LARGEFILE)
@ -135,8 +136,11 @@ int RawFileIO::open(int flags) {
#warning O_LARGEFILE not supported
#endif
int eno;
int newFd = ::open(name.c_str(), finalFlags);
int eno = errno;
if (newFd < 0) {
eno = errno;
}
VLOG(1) << "open file with flags " << finalFlags << ", result = " << newFd;
@ -146,7 +150,11 @@ int RawFileIO::open(int flags) {
eno = errno;
}
if (newFd >= 0) {
if (newFd < 0) {
RLOG(DEBUG) << "::open error: " << strerror(eno);
return -eno;
}
if (oldfd >= 0) {
RLOG(ERROR) << "leaking FD?: oldfd = " << oldfd << ", fd = " << fd
<< ", newfd = " << newFd;
@ -156,14 +164,9 @@ int RawFileIO::open(int flags) {
// now.
canWrite = requestWrite;
oldfd = fd;
result = fd = newFd;
} else {
result = -eno;
RLOG(DEBUG) << "::open error: " << strerror(eno);
}
}
fd = newFd;
return result;
return fd;
}
int RawFileIO::getAttr(struct stat *stbuf) const {
@ -206,8 +209,6 @@ ssize_t RawFileIO::read(const IORequest &req) const {
if (readSize < 0) {
int eno = errno;
errno = 0; // just to be sure error seen in integration tests really comes
// from the function rc.
RLOG(WARNING) << "read failed at offset " << req.offset << " for "
<< req.dataLen << " bytes: " << strerror(eno);
return -eno;

View File

@ -532,10 +532,9 @@ void *encfs_init(fuse_conn_info *conn) {
int res =
pthread_create(&ctx->monitorThread, nullptr, idleMonitor, (void *)ctx);
if (res != 0) {
int eno = errno;
RLOG(ERROR) << "error starting idle monitor thread, "
"res = "
<< res << ", " << strerror(eno);
<< res << ", " << strerror(res);
}
}