Make sure errno is correctly used

This commit is contained in:
benrubson 2017-03-20 15:11:17 +01:00
parent 13ae4de830
commit f024ae86f8
5 changed files with 37 additions and 27 deletions

View File

@ -185,8 +185,9 @@ bool RenameOp::apply() {
// rename on disk.. // rename on disk..
if (::rename(last->oldCName.c_str(), last->newCName.c_str()) == -1) { if (::rename(last->oldCName.c_str(), last->newCName.c_str()) == -1) {
int eno = errno;
RLOG(WARNING) << "Error renaming " << last->oldCName << ": " RLOG(WARNING) << "Error renaming " << last->oldCName << ": "
<< strerror(errno); << strerror(eno);
dn->renameNode(last->newPName.c_str(), last->oldPName.c_str(), false); dn->renameNode(last->newPName.c_str(), last->oldPName.c_str(), false);
return false; return false;
} }
@ -355,7 +356,8 @@ DirTraverse DirNode::openDir(const char *plaintextPath) {
DIR *dir = ::opendir(cyName.c_str()); DIR *dir = ::opendir(cyName.c_str());
if (dir == NULL) { if (dir == NULL) {
VLOG(1) << "opendir error " << strerror(errno); int eno = errno;
VLOG(1) << "opendir error " << strerror(eno);
return DirTraverse(shared_ptr<DIR>(), 0, std::shared_ptr<NameIO>()); return DirTraverse(shared_ptr<DIR>(), 0, std::shared_ptr<NameIO>());
} else { } else {
std::shared_ptr<DIR> dp(dir, DirDeleter()); std::shared_ptr<DIR> dp(dir, DirDeleter());
@ -569,8 +571,7 @@ int DirNode::rename(const char *fromPlaintext, const char *toPlaintext) {
} }
if (res != 0) { if (res != 0) {
VLOG(1) << "rename failed: " << strerror(errno); VLOG(1) << "rename failed: " << strerror(-res);
res = -errno;
} }
return res; return res;
@ -692,7 +693,7 @@ int DirNode::unlink(const char *plaintextName) {
res = ::unlink(fullName.c_str()); res = ::unlink(fullName.c_str());
if (res == -1) { if (res == -1) {
res = -errno; res = -errno;
VLOG(1) << "unlink error: " << strerror(errno); VLOG(1) << "unlink error: " << strerror(-res);
} }
} }

View File

@ -140,14 +140,16 @@ int FileNode::mknod(mode_t mode, dev_t rdev, uid_t uid, gid_t gid) {
if (uid != 0) { if (uid != 0) {
olduid = setfsuid(uid); olduid = setfsuid(uid);
if (olduid == -1) { if (olduid == -1) {
RLOG(DEBUG) << "setfsuid error: " << strerror(errno); int eno = errno;
RLOG(DEBUG) << "setfsuid error: " << strerror(eno);
return -EPERM; return -EPERM;
} }
} }
if (gid != 0) { if (gid != 0) {
oldgid = setfsgid(gid); oldgid = setfsgid(gid);
if (oldgid == -1) { if (oldgid == -1) {
RLOG(DEBUG) << "setfsgid error: " << strerror(errno); int eno = errno;
RLOG(DEBUG) << "setfsgid error: " << strerror(eno);
return -EPERM; return -EPERM;
} }
} }
@ -165,15 +167,15 @@ int FileNode::mknod(mode_t mode, dev_t rdev, uid_t uid, gid_t gid) {
else else
res = ::mknod(_cname.c_str(), mode, rdev); res = ::mknod(_cname.c_str(), mode, rdev);
if (olduid >= 0) setfsuid(olduid);
if (oldgid >= 0) setfsgid(oldgid);
if (res == -1) { if (res == -1) {
int eno = errno; int eno = errno;
VLOG(1) << "mknod error: " << strerror(eno); VLOG(1) << "mknod error: " << strerror(eno);
res = -eno; res = -eno;
} }
if (olduid >= 0) setfsuid(olduid);
if (oldgid >= 0) setfsgid(oldgid);
return res; return res;
} }

View File

@ -92,13 +92,11 @@ static int open_readonly_workaround(const char *path, int flags) {
memset(&stbuf, 0, sizeof(struct stat)); memset(&stbuf, 0, sizeof(struct stat));
if (lstat(path, &stbuf) != -1) { if (lstat(path, &stbuf) != -1) {
// make sure user has read/write permission.. // make sure user has read/write permission..
chmod(path, stbuf.st_mode | 0600); if (chmod(path, stbuf.st_mode | 0600) != -1) {
fd = ::open(path, flags); fd = ::open(path, flags);
chmod(path, stbuf.st_mode); chmod(path, stbuf.st_mode);
} else { }
RLOG(INFO) << "can't stat file " << path;
} }
return fd; return fd;
} }
@ -131,10 +129,11 @@ int RawFileIO::open(int flags) {
#endif #endif
int newFd = ::open(name.c_str(), finalFlags); int newFd = ::open(name.c_str(), finalFlags);
int eno = errno;
VLOG(1) << "open file with flags " << finalFlags << ", result = " << newFd; 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"; VLOG(1) << "using readonly workaround for open";
newFd = open_readonly_workaround(name.c_str(), finalFlags); newFd = open_readonly_workaround(name.c_str(), finalFlags);
} }
@ -152,7 +151,7 @@ int RawFileIO::open(int flags) {
result = fd = newFd; result = fd = newFd;
} else { } else {
result = -errno; 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<RawFileIO *>(this)->knownSize = true; const_cast<RawFileIO *>(this)->knownSize = true;
return fileSize; return fileSize;
} else { } else {
RLOG(ERROR) << "getSize on " << name << " failed: " << strerror(errno); int eno = errno;
return -1; RLOG(ERROR) << "getSize on " << name << " failed: " << strerror(eno);
return -eno;
} }
} else { } else {
return fileSize; 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); ssize_t readSize = pread(fd, req.data, req.dataLen, req.offset);
if (readSize < 0) { if (readSize < 0) {
readSize = -errno;
RLOG(WARNING) << "read failed at offset " << req.offset << " for " RLOG(WARNING) << "read failed at offset " << req.offset << " for "
<< req.dataLen << " bytes: " << strerror(errno); << req.dataLen << " bytes: " << strerror(-readSize);
} }
return readSize; return readSize;
@ -254,9 +255,6 @@ int RawFileIO::truncate(off_t size) {
if (fd >= 0 && canWrite) { if (fd >= 0 && canWrite) {
res = ::ftruncate(fd, size); res = ::ftruncate(fd, size);
#if !defined(__FreeBSD__) && !defined(__APPLE__)
::fdatasync(fd);
#endif
} else } else
res = ::truncate(name.c_str(), size); res = ::truncate(name.c_str(), size);
@ -272,6 +270,12 @@ int RawFileIO::truncate(off_t size) {
knownSize = true; knownSize = true;
} }
#if !defined(__FreeBSD__) && !defined(__APPLE__)
if (fd >= 0 && canWrite) {
::fdatasync(fd);
}
#endif
return res; return res;
} }

View File

@ -41,7 +41,8 @@ static __inline int setfsuid(uid_t uid) {
uid_t olduid = geteuid(); uid_t olduid = geteuid();
if (seteuid(uid) != 0) { if (seteuid(uid) != 0) {
VLOG(1) << "seteuid error: " << errno; int eno = errno;
VLOG(1) << "seteuid error: " << strerror(eno);
} }
return olduid; return olduid;
@ -51,7 +52,8 @@ static __inline int setfsgid(gid_t gid) {
gid_t oldgid = getegid(); gid_t oldgid = getegid();
if (setegid(gid) != 0) { if (setegid(gid) != 0) {
VLOG(1) << "setfsgid error: " << errno; int eno = errno;
VLOG(1) << "setfsgid error: " << strerror(eno);
} }
return oldgid; return oldgid;

View File

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