From aa44eb2e02ca78db3dc5cf2b51b587f5257c01f2 Mon Sep 17 00:00:00 2001 From: Sungbae Yoo Date: Mon, 27 Jun 2016 19:24:00 +0900 Subject: [PATCH] Add recursive option to chmod(), chown() Signed-off-by: Sungbae Yoo Change-Id: Id600f3dac8af2ae40b7b27040b770c69c3108748 --- common/filesystem.cpp | 20 ++++++++++++++++++-- common/filesystem.h | 4 ++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/common/filesystem.cpp b/common/filesystem.cpp index 2a3b575..748f30f 100755 --- a/common/filesystem.cpp +++ b/common/filesystem.cpp @@ -481,18 +481,34 @@ void File::makeDirectory(bool recursive, uid_t uid, gid_t gid) } } -void File::chown(uid_t uid, gid_t gid) +void File::chown(uid_t uid, gid_t gid, bool recursive) { if (::chown(path.getPathname().c_str(), uid, gid) != 0) { throw runtime::Exception(runtime::GetSystemErrorMessage()); } + + if (recursive && isDirectory()) { + DirectoryIterator iter(path), end; + while (iter != end) { + iter->chown(uid, gid, true); + ++iter; + } + } } -void File::chmod(mode_t mode) +void File::chmod(mode_t mode, bool recursive) { if (::chmod(path.getPathname().c_str(), mode) != 0) { throw runtime::Exception(runtime::GetSystemErrorMessage()); } + + if (recursive && isDirectory()) { + DirectoryIterator iter(path), end; + while (iter != end) { + iter->chmod(mode, true); + ++iter; + } + } } void File::lock() const diff --git a/common/filesystem.h b/common/filesystem.h index 3211372..48e6f7b 100644 --- a/common/filesystem.h +++ b/common/filesystem.h @@ -152,8 +152,8 @@ public: void lock() const; void unlock() const; - void chown(uid_t uid, gid_t gid); - void chmod(mode_t mode); + void chown(uid_t uid, gid_t gid, bool recursive = false); + void chmod(mode_t mode, bool recursive = false); std::string toString() const; -- 2.7.4