diff options
author | Kyungwook Tak <k.tak@samsung.com> | 2017-01-31 17:51:29 +0900 |
---|---|---|
committer | Kyungwook Tak <k.tak@samsung.com> | 2017-02-01 17:35:50 +0900 |
commit | d52589e9d88c3d6399ddb7e493a944602125abd9 (patch) | |
tree | 09429cdf093f0492af244c44b4b2b8e76094ee46 | |
parent | 7e23c6e46c0207cb8973137aa365724d5555dc5d (diff) | |
download | csr-framework-d52589e9d88c3d6399ddb7e493a944602125abd9.tar.gz csr-framework-d52589e9d88c3d6399ddb7e493a944602125abd9.tar.bz2 csr-framework-d52589e9d88c3d6399ddb7e493a944602125abd9.zip |
Make File::Type class to encapsulate filetype op
Change-Id: I789fa225fdeaacb5d08c6d4235b1d8e259313885
Signed-off-by: Kyungwook Tak <k.tak@samsung.com>
-rw-r--r-- | src/framework/service/file-system.cpp | 53 | ||||
-rw-r--r-- | src/framework/service/file-system.h | 139 |
2 files changed, 133 insertions, 59 deletions
diff --git a/src/framework/service/file-system.cpp b/src/framework/service/file-system.cpp index 9ae2380..bcdf21d 100644 --- a/src/framework/service/file-system.cpp +++ b/src/framework/service/file-system.cpp @@ -39,7 +39,7 @@ namespace Csr { -int File::getPkgTypes(const std::string &user, const std::string &pkgid) +File::Type File::Type::get(const std::string &user, const std::string &pkgid) { pkgmgrinfo_pkginfo_h handle; @@ -49,13 +49,15 @@ int File::getPkgTypes(const std::string &user, const std::string &pkgid) else ret = ::pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid.c_str(), getUid(user), &handle); + Type type; + if (ret != PMINFO_R_OK) { INFO("Extracted pkgid[" << pkgid << "] from filepath isn't pkg id. " "It's not package."); - return 0; + return type; } - auto type = static_cast<int>(Type::Package); + type.m_attributes.set(Attribute::Package); bool isPreloaded = false; ret = ::pkgmgrinfo_pkginfo_is_preload(handle, &isPreloaded); @@ -64,10 +66,25 @@ int File::getPkgTypes(const std::string &user, const std::string &pkgid) ERROR("Failed to ::pkgmgrinfo_pkginfo_is_preload: " << ret); if (isPreloaded) - type |= static_cast<int>(Type::PreLoaded); + type.m_attributes.set(Attribute::PreLoaded); ::pkgmgrinfo_pkginfo_destroy_pkginfo(handle); + return Type(type); +} + +File::Type File::Type::get(const struct stat &s, time_t modifiedSince) +{ + Type type; + + if (S_ISREG(s.st_mode)) + type.m_attributes.set(Attribute::File); + else + type.m_attributes.set(Attribute::Directory); + + if (modifiedSince == -1 || s.st_ctime > modifiedSince) + type.m_attributes.set(Attribute::Modified); + return type; } @@ -94,34 +111,27 @@ std::string File::getPkgPath(const std::string &path) continue; } - auto type = File::getPkgTypes(pkgUser, pkgId); - - return ((type & static_cast<int>(Type::Package)) && - (!(type & static_cast<int>(Type::PreLoaded)))) ? pkgPath : path; + return Type::get(pkgUser, pkgId).isInApp() ? pkgPath : path; } return path; } -File::File(const std::string &fpath, const FilePtr &parentdir, int type, +File::File(const std::string &fpath, const FilePtr &parentdir, const Type &type, std::unique_ptr<struct stat> &&statptr) : m_path(fpath), m_type(type), m_statptr(std::move(statptr)) { if (parentdir != nullptr) { - if (parentdir->isPackage()) { + if (parentdir->m_type.isPackage()) { this->m_appPkgPath = parentdir->getAppPkgPath(); this->m_appPkgId = parentdir->getAppPkgId(); this->m_appUser = parentdir->getAppUser(); - this->m_type |= static_cast<int>(File::Type::Package); - - if (parentdir->isPreloaded()) - this->m_type |= static_cast<int>(File::Type::PreLoaded); + this->m_type.inheritParentType(parentdir->m_type); return; } else if (!this->isDir()) { - this->m_type &= ~(static_cast<int>(File::Type::Package) | - static_cast<int>(File::Type::PreLoaded)); + this->m_type.unsetDirAttributes(); } } @@ -143,7 +153,7 @@ File::File(const std::string &fpath, const FilePtr &parentdir, int type, continue; } - this->m_type |= File::getPkgTypes(this->m_appUser, this->m_appPkgId); + this->m_type |= Type::get(this->m_appUser, this->m_appPkgId); break; } @@ -183,14 +193,9 @@ FilePtr File::createInternal(const std::string &fpath, const FilePtr &parentdir, else if (!S_ISREG(statptr->st_mode) && !S_ISDIR(statptr->st_mode)) ThrowExc(CSR_ERROR_FILE_SYSTEM, "file type is not reguler or dir: " << fpath); - auto type = static_cast<int>(S_ISREG(statptr->st_mode) ? Type::File : Type::Directory); - - if (modifiedSince == -1 || statptr->st_ctime > modifiedSince) { - DEBUG("file[" << fpath << "] is changed since[" << modifiedSince << "]"); - type |= static_cast<int>(Type::Modified); - } + auto type = Type::get(*statptr, modifiedSince); - if (isModifiedOnly && !(type & static_cast<int>(Type::Modified))) + if (isModifiedOnly && !type.isModified()) return nullptr; else return FilePtr(new File(fpath, parentdir, type, std::move(statptr))); diff --git a/src/framework/service/file-system.h b/src/framework/service/file-system.h index 7166c47..1b92558 100644 --- a/src/framework/service/file-system.h +++ b/src/framework/service/file-system.h @@ -27,6 +27,7 @@ #include <functional> #include <mutex> #include <map> +#include <bitset> #include <cstddef> #include <ctime> @@ -42,36 +43,11 @@ class File { public: File() = delete; - inline bool isInApp() const noexcept - { - return this->isPackage() && !this->isPreloaded(); - } - - inline bool isPackage() const noexcept - { - return this->m_type & static_cast<int>(Type::Package); - } - - inline bool isPreloaded() const noexcept - { - return this->m_type & static_cast<int>(Type::PreLoaded); - } - - inline bool isModified() const noexcept - { - return this->m_type & static_cast<int>(Type::Modified); - } - inline bool isModifiedSince(time_t since) const noexcept { return this->m_statptr->st_ctime > since; } - inline bool isDir() const noexcept - { - return this->m_type & static_cast<int>(Type::Directory); - } - inline const std::string &getName() const noexcept { return (this->isInApp()) ? this->m_appPkgPath : this->m_path; @@ -97,6 +73,16 @@ public: return this->m_appPkgPath; } + inline bool isInApp() const noexcept + { + return this->m_type.isInApp(); + } + + inline bool isDir() const noexcept + { + return this->m_type.isDir(); + } + void remove() const; // throws FileNotExist and FileSystemError @@ -108,23 +94,106 @@ public: static std::string getPkgPath(const std::string &path); private: - enum class Type : int { - Modified = (1 << 0), - Package = (1 << 1), - PreLoaded = (1 << 2), - File = (1 << 3), - Directory = (1 << 4) + class Type { + public: + inline Type &operator|=(const Type &other) + { + this->m_attributes |= other.m_attributes; + return *this; + } + + inline bool isInApp() const noexcept + { + return Type::isInApp(this->m_attributes); + } + + inline bool isPackage() const noexcept + { + return Type::isPackage(this->m_attributes); + } + + inline bool isPreloaded() const noexcept + { + return Type::isPreloaded(this->m_attributes); + } + + inline bool isModified() const noexcept + { + return Type::isModified(this->m_attributes); + } + + inline bool isDir() const noexcept + { + return Type::isDir(this->m_attributes); + } + + inline void unsetDirAttributes() noexcept + { + this->m_attributes.reset(Attribute::Package); + this->m_attributes.reset(Attribute::PreLoaded); + } + + inline void inheritParentType(const Type &parent) noexcept + { + if (parent.isPackage()) + this->m_attributes.set(Attribute::Package); + if (parent.isPreloaded()) + this->m_attributes.set(Attribute::PreLoaded); + } + + static Type get(const std::string &user, const std::string &pkgid); + static Type get(const struct stat &s, time_t modifiedSince); + + private: + enum Attribute { + Modified = 0, + Package = 1, + PreLoaded = 2, + File = 3, + Directory = 4, + ENUM_COUNT = 5 + }; + + using Attributes = std::bitset<static_cast<std::underlying_type<Attribute>::type>(Attribute::ENUM_COUNT)>; + + static inline bool isInApp(const Attributes &attributes) noexcept + { + return isPackage(attributes) && !isPreloaded(attributes); + } + + static inline bool isPackage(const Attributes &attributes) noexcept + { + return attributes[Attribute::Package]; + } + + static inline bool isPreloaded(const Attributes &attributes) noexcept + { + return attributes[Attribute::PreLoaded]; + } + + static inline bool isModified(const Attributes &attributes) noexcept + { + return attributes[Attribute::Modified]; + } + + static inline bool isDir(const Attributes &attributes) noexcept + { + return attributes[Attribute::Directory]; + } + + Type() {} + + Attributes m_attributes; }; static FilePtr createInternal(const std::string &fpath, const FilePtr &parentdir, time_t modifiedSince, bool isModifiedOnly); - static int getPkgTypes(const std::string &user, const std::string &pkgid); - explicit File(const std::string &fpath, const FilePtr &parentdir, int type, - std::unique_ptr<struct stat> &&statptr); + explicit File(const std::string &fpath, const FilePtr &parentdir, + const Type &type, std::unique_ptr<struct stat> &&statptr); std::string m_path; - int m_type; + Type m_type; std::unique_ptr<struct stat> m_statptr; std::string m_appPkgId; // meaningful only if inApp == true std::string m_appUser; // meaningful only if inApp == true |