From 1d789d9bce19179452ae6296e8081ce8dcfdf010 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Franti=C5=A1ek=20Dvo=C5=99=C3=A1k?= Date: Fri, 18 Oct 2013 17:32:07 +0200 Subject: [PATCH] Let authorization plugin working but only silently suffer (with LOG_NOTICE), when data files can't be created. Try to save them again on destroy. --- src/VfsAuthn.cpp | 38 +++++++++++++++++++++++++++++++------- src/VfsAuthn.h | 1 + 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/VfsAuthn.cpp b/src/VfsAuthn.cpp index 6c4d054..cb2b86d 100644 --- a/src/VfsAuthn.cpp +++ b/src/VfsAuthn.cpp @@ -22,6 +22,8 @@ VfsAuthn::VfsAuthn(const std::string& prefix) throw (DmException) this->nextGid_ = VFS_GID_START; this->noSync_ = false; + this->dirtyUsers_ = false; + this->dirtyGroups_ = false; vfsReload(); } @@ -30,6 +32,8 @@ VfsAuthn::VfsAuthn(const std::string& prefix) throw (DmException) VfsAuthn::~VfsAuthn() { + if (dirtyUsers_) vfsSaveUsers(); + if (dirtyGroups_) vfsSaveGroups(); } @@ -75,7 +79,7 @@ void VfsAuthn::getIdMap(const std::string &userName, const std::vectornoSync_ = false; - if (ngroups != this->groups_.size()) + if (ngroups != this->groups_.size() || this->dirtyGroups_) vfsSaveGroups(); } @@ -96,6 +100,7 @@ void VfsAuthn::vfsNewGroup(const std::string& groupName) throw (DmException) { this->groups_.push_back(gi); this->gids_.insert(gid); this->nextGid_ = gid + 1; + this->dirtyGroups_ = true; debug("new group '%s', gid %d", groupName.c_str(), gid); } @@ -117,6 +122,7 @@ void VfsAuthn::vfsNewUser(const std::string& userName) throw (DmException) { this->users_.push_back(ui); this->uids_.insert(uid); this->nextUid_ = uid + 1; + this->dirtyUsers_ = true; debug("new user '%s', uid %d", userName.c_str(), uid); } @@ -215,6 +221,7 @@ void VfsAuthn::deleteGroup(const std::string& groupName) throw (DmException) this->groups_.erase(this->groups_.begin() + i); this->gids_.erase(gid); //not needed immediately: if (this->nextGid_ > gid) this->nextGid_ = gid; + this->dirtyGroups_ = true; vfsSaveGroups(); } @@ -312,6 +319,7 @@ void VfsAuthn::deleteUser(const std::string& userName) throw (DmException) this->users_.erase(this->users_.begin() + i); this->uids_.erase(uid); //not needed immediately: if (this->nextUid_ > uid) this->nextUid_ = uid; + this->dirtyUsers_ = true; vfsSaveUsers(); } @@ -389,14 +397,20 @@ void VfsAuthn::vfsSaveUsers() throw (DmException) { std::ofstream f; std::string newName, name; - if (this->noSync_) return; + if (this->noSync_) { + this->dirtyUsers_ = true; + return; + } name = this->prefix_ + VFS_USERS_FILE; newName = this->prefix_ + ".new" + VFS_USERS_FILE; f.open(newName.c_str()); - if (!f) - vfsThrow(EIO, "could not create new version of users file"); + if (!f) { + log(LOG_NOTICE, "could not create new version of users file in '%s'", this->prefix_.c_str()); + this->dirtyUsers_ = true; + return; + } for (size_t i = 0; i < this->users_.size(); i++) { f << this->users_[i].getUnsigned("uid") << "\t"; @@ -406,6 +420,8 @@ void VfsAuthn::vfsSaveUsers() throw (DmException) { f.close(); wrapCall(rename(newName.c_str(), name.c_str()), "could not save users file"); + + this->dirtyUsers_ = false; } @@ -414,14 +430,20 @@ void VfsAuthn::vfsSaveGroups() throw (DmException) { std::ofstream f; std::string newName, name; - if (this->noSync_) return; + if (this->noSync_) { + this->dirtyGroups_ = true; + return; + } name = this->prefix_ + VFS_GROUPS_FILE; newName = this->prefix_ + ".new" + VFS_GROUPS_FILE; f.open(newName.c_str()); - if (!f) - vfsThrow(EIO, "could not create new version of groups file"); + if (!f) { + log(LOG_NOTICE, "could not create new version of groups file in '%s'", this->prefix_.c_str()); + this->dirtyGroups_ = true; + return; + } for (size_t i = 0; i < this->groups_.size(); i++) { f << this->groups_[i].getUnsigned("gid") << "\t"; @@ -431,4 +453,6 @@ void VfsAuthn::vfsSaveGroups() throw (DmException) { f.close(); wrapCall(rename(newName.c_str(), name.c_str()), "could not save groups file"); + + this->dirtyGroups_ = false; } diff --git a/src/VfsAuthn.h b/src/VfsAuthn.h index 396b519..8e7cf24 100644 --- a/src/VfsAuthn.h +++ b/src/VfsAuthn.h @@ -47,6 +47,7 @@ namespace dmlite { std::set uids_, gids_; /// helper sets with UID/GID unsigned int nextUid_, nextGid_; /// next UID/GID after the one last inserted bool noSync_; /// saving users/groups disabled + bool dirtyUsers_, dirtyGroups_; /// data which need to be saved }; }; -- 1.8.2.3