Refactor file & file visitor classes
[platform/upstream/csr-framework.git] / src / framework / service / file-system.h
1 /*
2  *  Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License
15  */
16 /*
17  * @file        file-system.h
18  * @author      Dongsun Lee (ds73.lee@samsung.com)
19  * @version     1.0
20  * @brief
21  */
22 #pragma once
23
24 #include <string>
25 #include <memory>
26 #include <queue>
27
28 #include <cstddef>
29 #include <ctime>
30 #include <dirent.h>
31 #include <sys/stat.h>
32
33 namespace Csr {
34
35 class File;
36 using FilePtr = std::unique_ptr<File>;
37
38 class File {
39 public:
40         File() = delete;
41
42         inline bool isInApp() const noexcept
43         {
44                 return this->isPackage() && !this->isPreloaded();
45         }
46
47         inline bool isPackage() const noexcept
48         {
49                 return this->m_type & static_cast<int>(Type::Package);
50         }
51
52         inline bool isPreloaded() const noexcept
53         {
54                 return this->m_type & static_cast<int>(Type::PreLoaded);
55         }
56
57         inline bool isModified() const noexcept
58         {
59                 return this->m_type & static_cast<int>(Type::Modified);
60         }
61
62         inline bool isModifiedSince(time_t since) const noexcept
63         {
64                 return this->m_statptr->st_ctime > since;
65         }
66
67         inline bool isDir() const noexcept
68         {
69                 return this->m_type & static_cast<int>(Type::Directory);
70         }
71
72         inline const std::string &getName() const noexcept
73         {
74                 return (this->isInApp()) ? this->m_appPkgPath : this->m_path;
75         }
76
77         inline const std::string &getPath() const noexcept
78         {
79                 return this->m_path;
80         }
81
82         inline const std::string &getAppPkgId() const noexcept
83         {
84                 return this->m_appPkgId;
85         }
86
87         inline const std::string &getAppUser() const noexcept
88         {
89                 return this->m_appUser;
90         }
91
92         inline const std::string &getAppPkgPath() const noexcept
93         {
94                 return this->m_appPkgPath;
95         }
96
97         void remove() const;
98
99         // throws FileNotExist and FileSystemError
100         static FilePtr create(const std::string &fpath, const FilePtr &parentdir,
101                                                   time_t modifiedSince = -1);
102         static FilePtr createIfModified(const std::string &fpath, const FilePtr &parentdir,
103                                                                         time_t modifiedSince = -1);
104
105         static std::string getPkgPath(const std::string &path);
106
107 private:
108         enum class Type : int {
109                 Modified  = (1 << 0),
110                 Package   = (1 << 1),
111                 PreLoaded = (1 << 2),
112                 File      = (1 << 3),
113                 Directory = (1 << 4)
114         };
115
116         static FilePtr createInternal(const std::string &fpath, const FilePtr &parentdir,
117                                                                   time_t modifiedSince, bool isModifiedOnly);
118         static int getPkgTypes(const std::string &user, const std::string &pkgid);
119
120         explicit File(const std::string &fpath, const FilePtr &parentdir, int type,
121                                   std::unique_ptr<struct stat> &&statptr);
122
123         std::string m_path;
124         int m_type;
125         std::unique_ptr<struct stat> m_statptr;
126         std::string m_appPkgId;    // meaningful only if inApp == true
127         std::string m_appUser;     // meaningful only if inApp == true
128         std::string m_appPkgPath;  // meaningful only if inApp == true
129 };
130
131 // visitor traverses file which is modified since the time of 'modifiedSince'
132 // if modifiedSince is -1, traverse regardless of time
133 class FsVisitor;
134 using FsVisitorPtr = std::unique_ptr<FsVisitor>;
135
136 class FsVisitor {
137 public:
138         ~FsVisitor();
139
140         FilePtr next();
141
142         // throws FileNotExist and FileSystemError
143         static FsVisitorPtr create(const std::string &dirpath, bool isBasedOnName,
144                                                            time_t modifiedSince = -1);
145
146 private:
147         using DirPtr = std::unique_ptr<DIR, int(*)(DIR *)>;
148
149         static DirPtr openDir(const std::string &);
150
151         FsVisitor(const std::string &dirpath, bool isBasedOnName, time_t modifiedSince = -1);
152
153         time_t m_since;
154         std::queue<FilePtr> m_dirs;
155         DirPtr m_dirptr;
156         struct dirent *m_entryBuf;
157         FilePtr m_currentdir;
158         bool m_isDone;
159         bool m_isBasedOnName;
160 };
161
162 } // namespace Csr