Make File::Type class to encapsulate filetype op 42/112342/4
authorKyungwook Tak <k.tak@samsung.com>
Tue, 31 Jan 2017 08:51:29 +0000 (17:51 +0900)
committerKyungwook Tak <k.tak@samsung.com>
Wed, 1 Feb 2017 08:35:50 +0000 (17:35 +0900)
Change-Id: I789fa225fdeaacb5d08c6d4235b1d8e259313885
Signed-off-by: Kyungwook Tak <k.tak@samsung.com>
src/framework/service/file-system.cpp
src/framework/service/file-system.h

index 9ae2380..bcdf21d 100644 (file)
@@ -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)));
index 7166c47..1b92558 100644 (file)
@@ -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