From f024ae86f82bfb3c78033dcfc4fc1f9eae682fc4 Mon Sep 17 00:00:00 2001 From: benrubson Date: Mon, 20 Mar 2017 15:11:17 +0100 Subject: [PATCH] Make sure errno is correctly used --- encfs/DirNode.cpp | 11 ++++++----- encfs/FileNode.cpp | 12 +++++++----- encfs/RawFileIO.cpp | 32 ++++++++++++++++++-------------- encfs/encfs.h | 6 ++++-- encfs/main.cpp | 3 ++- 5 files changed, 37 insertions(+), 27 deletions(-) diff --git a/encfs/DirNode.cpp b/encfs/DirNode.cpp index 3612d6d..2e65f23 100644 --- a/encfs/DirNode.cpp +++ b/encfs/DirNode.cpp @@ -185,8 +185,9 @@ bool RenameOp::apply() { // rename on disk.. if (::rename(last->oldCName.c_str(), last->newCName.c_str()) == -1) { + int eno = errno; RLOG(WARNING) << "Error renaming " << last->oldCName << ": " - << strerror(errno); + << strerror(eno); dn->renameNode(last->newPName.c_str(), last->oldPName.c_str(), false); return false; } @@ -355,7 +356,8 @@ DirTraverse DirNode::openDir(const char *plaintextPath) { DIR *dir = ::opendir(cyName.c_str()); if (dir == NULL) { - VLOG(1) << "opendir error " << strerror(errno); + int eno = errno; + VLOG(1) << "opendir error " << strerror(eno); return DirTraverse(shared_ptr(), 0, std::shared_ptr()); } else { std::shared_ptr dp(dir, DirDeleter()); @@ -569,8 +571,7 @@ int DirNode::rename(const char *fromPlaintext, const char *toPlaintext) { } if (res != 0) { - VLOG(1) << "rename failed: " << strerror(errno); - res = -errno; + VLOG(1) << "rename failed: " << strerror(-res); } return res; @@ -692,7 +693,7 @@ int DirNode::unlink(const char *plaintextName) { res = ::unlink(fullName.c_str()); if (res == -1) { res = -errno; - VLOG(1) << "unlink error: " << strerror(errno); + VLOG(1) << "unlink error: " << strerror(-res); } } diff --git a/encfs/FileNode.cpp b/encfs/FileNode.cpp index 4b350ba..9d52763 100644 --- a/encfs/FileNode.cpp +++ b/encfs/FileNode.cpp @@ -140,14 +140,16 @@ int FileNode::mknod(mode_t mode, dev_t rdev, uid_t uid, gid_t gid) { if (uid != 0) { olduid = setfsuid(uid); if (olduid == -1) { - RLOG(DEBUG) << "setfsuid error: " << strerror(errno); + int eno = errno; + RLOG(DEBUG) << "setfsuid error: " << strerror(eno); return -EPERM; } } if (gid != 0) { oldgid = setfsgid(gid); if (oldgid == -1) { - RLOG(DEBUG) << "setfsgid error: " << strerror(errno); + int eno = errno; + RLOG(DEBUG) << "setfsgid error: " << strerror(eno); return -EPERM; } } @@ -165,15 +167,15 @@ int FileNode::mknod(mode_t mode, dev_t rdev, uid_t uid, gid_t gid) { else res = ::mknod(_cname.c_str(), mode, rdev); - if (olduid >= 0) setfsuid(olduid); - if (oldgid >= 0) setfsgid(oldgid); - if (res == -1) { int eno = errno; VLOG(1) << "mknod error: " << strerror(eno); res = -eno; } + if (olduid >= 0) setfsuid(olduid); + if (oldgid >= 0) setfsgid(oldgid); + return res; } diff --git a/encfs/RawFileIO.cpp b/encfs/RawFileIO.cpp index 78d1373..f28aa3f 100644 --- a/encfs/RawFileIO.cpp +++ b/encfs/RawFileIO.cpp @@ -92,13 +92,11 @@ static int open_readonly_workaround(const char *path, int flags) { memset(&stbuf, 0, sizeof(struct stat)); if (lstat(path, &stbuf) != -1) { // make sure user has read/write permission.. - chmod(path, stbuf.st_mode | 0600); - fd = ::open(path, flags); - chmod(path, stbuf.st_mode); - } else { - RLOG(INFO) << "can't stat file " << path; + if (chmod(path, stbuf.st_mode | 0600) != -1) { + fd = ::open(path, flags); + chmod(path, stbuf.st_mode); + } } - return fd; } @@ -131,10 +129,11 @@ int RawFileIO::open(int flags) { #endif int newFd = ::open(name.c_str(), finalFlags); + int eno = errno; VLOG(1) << "open file with flags " << finalFlags << ", result = " << newFd; - if ((newFd == -1) && (errno == EACCES)) { + if ((newFd == -1) && (eno == EACCES)) { VLOG(1) << "using readonly workaround for open"; newFd = open_readonly_workaround(name.c_str(), finalFlags); } @@ -152,7 +151,7 @@ int RawFileIO::open(int flags) { result = fd = newFd; } else { result = -errno; - RLOG(DEBUG) << "::open error: " << strerror(errno); + RLOG(DEBUG) << "::open error: " << strerror(-result); } } @@ -185,8 +184,9 @@ off_t RawFileIO::getSize() const { const_cast(this)->knownSize = true; return fileSize; } else { - RLOG(ERROR) << "getSize on " << name << " failed: " << strerror(errno); - return -1; + int eno = errno; + RLOG(ERROR) << "getSize on " << name << " failed: " << strerror(eno); + return -eno; } } else { return fileSize; @@ -199,8 +199,9 @@ ssize_t RawFileIO::read(const IORequest &req) const { ssize_t readSize = pread(fd, req.data, req.dataLen, req.offset); if (readSize < 0) { + readSize = -errno; RLOG(WARNING) << "read failed at offset " << req.offset << " for " - << req.dataLen << " bytes: " << strerror(errno); + << req.dataLen << " bytes: " << strerror(-readSize); } return readSize; @@ -254,9 +255,6 @@ int RawFileIO::truncate(off_t size) { if (fd >= 0 && canWrite) { res = ::ftruncate(fd, size); -#if !defined(__FreeBSD__) && !defined(__APPLE__) - ::fdatasync(fd); -#endif } else res = ::truncate(name.c_str(), size); @@ -272,6 +270,12 @@ int RawFileIO::truncate(off_t size) { knownSize = true; } +#if !defined(__FreeBSD__) && !defined(__APPLE__) + if (fd >= 0 && canWrite) { + ::fdatasync(fd); + } +#endif + return res; } diff --git a/encfs/encfs.h b/encfs/encfs.h index 2988af2..05c4fea 100644 --- a/encfs/encfs.h +++ b/encfs/encfs.h @@ -41,7 +41,8 @@ static __inline int setfsuid(uid_t uid) { uid_t olduid = geteuid(); if (seteuid(uid) != 0) { - VLOG(1) << "seteuid error: " << errno; + int eno = errno; + VLOG(1) << "seteuid error: " << strerror(eno); } return olduid; @@ -51,7 +52,8 @@ static __inline int setfsgid(gid_t gid) { gid_t oldgid = getegid(); if (setegid(gid) != 0) { - VLOG(1) << "setfsgid error: " << errno; + int eno = errno; + VLOG(1) << "setfsgid error: " << strerror(eno); } return oldgid; diff --git a/encfs/main.cpp b/encfs/main.cpp index e56b83a..ebb96bd 100644 --- a/encfs/main.cpp +++ b/encfs/main.cpp @@ -504,9 +504,10 @@ void *encfs_init(fuse_conn_info *conn) { int res = pthread_create(&ctx->monitorThread, 0, idleMonitor, (void *)ctx); if (res != 0) { + int eno = errno; RLOG(ERROR) << "error starting idle monitor thread, " "res = " - << res << ", errno = " << errno; + << res << ", " << strerror(eno); } }