Fixed listDirectory()
[platform/core/api/webapi-plugins.git] / src / filesystem / filesystem_utils.h
1 /*
2  * Copyright (c) 2015 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 #ifndef FILESYSTEM_FILESYSTEM_UTILS_H
18 #define FILESYSTEM_FILESYSTEM_UTILS_H
19
20 #include "common/picojson.h"
21 #include "common/tools.h"
22
23 #include <gio/gio.h>
24 #include <glib-object.h>
25 #include <sys/stat.h>
26 #include <functional>
27 #include <memory>
28 #include <string>
29 #include <system_error>
30
31 namespace extension {
32 namespace filesystem {
33
34 enum class FilesystemError {
35   None,
36   NotFound,
37   FileExists,
38   DirectoryExists,
39   PermissionDenied,
40   IOError,
41   InvalidValue,
42   Other
43 };
44 }  // namespace filesystem
45 }  // namespace extension
46
47 namespace FilesystemUtils {
48
49 /**
50  * @brief Wrapper for POSIX mkdir function.
51  * @throw std::system_error
52  */
53 void Mkdir(const std::string& path);
54
55 /**
56  * @brief Make directory using mkdir. If 'parents' is true, make parent directories as needed
57  * @throw std::system_error
58  */
59 void Mkdir(const std::string& path, bool parents);
60
61 /**
62  * @brief Wrapper for POSIX unlink function
63  * @throw std::system_error
64  */
65 void Unlink(const std::string& path);
66
67 /**
68  * @brief Returns last element of path (wrapper for POSIX basename function)
69  * @throw std::system_error
70  */
71 std::string PosixBasename(const std::string& path);
72
73 /**
74  * @brief Returns parent directory of path (wrapper for POSIX dirname function)
75  * @throw std::system_error
76  */
77 std::string Dirname(const std::string& path);
78
79 /**
80  * @brief Wrapper for GLib g_file_copy function.
81  * @throw std::system_error
82  */
83 void CopyFile(const std::string& src, const std::string& dest, bool overwrite);
84
85 /**
86  * @brief Copies directory recursively
87  * @throw std::system_error
88  */
89 void CopyDirectory(const std::string& src, const std::string& dest, bool overwrite);
90
91 /**
92  * @brief Calls 'next' function with name for every entry in given directory
93  * @throw std::system_error
94  */
95 void ListDirectory(const std::string& path, std::function<void(const std::string&, unsigned char)> next);
96
97 /**
98  * @brief Removes directory recursively pointed by path.
99  * @throw std::system_error
100  */
101 void RemoveDirectoryRecursively(const std::string& path);
102
103 /**
104  * @brief Removes directory pointed by path.
105  * @throw std::system_error
106  */
107 void RemoveDirectory(const std::string& path);
108
109 /**
110  * @brief Returns the real path.
111  * @throw std::system_error
112  */
113 std::string RealPath(const std::string& path);
114
115 /**
116  * @brief Checks if path points to file or directory.
117  * @throw std::system_error
118  */
119 bool CheckIfExists(const std::string& path, struct stat* buf);
120
121 /**
122  * @brief Checks if path points to directory.
123  * @throw std::system_error
124  */
125 bool CheckIfDir(const struct stat& buf);
126
127 /**
128  * @brief Checks if path points to file.
129  * @throw std::system_error
130  */
131 bool CheckIfFile(const struct stat& buf);
132
133 /**
134  * @brief Renames file or directory.
135  * @throw std::system_error
136  */
137 void Rename(const std::string& path, const std::string& new_path);
138
139 /**
140  * @brief Wrapper for GLib g_file_move function.
141  * @throw std::system_error
142  */
143 void MoveFile(const std::string& path, const std::string& new_path, bool overwrite);
144
145 /**
146  * @brief Moves directory by recursively calling move_file.
147  * @throw std::system_error
148  */
149 void MoveDirectory(const std::string& path, const std::string& new_path, bool overwrite);
150
151 void TranslateException(const std::system_error& e, picojson::object& obj);
152
153 // This function is left only for compatibility with previous implementation in FilesystemManager
154 std::string GetDirname(const std::string& path);
155 }
156
157 #endif  // FILESYSTEM_FILESYSTEM_UTILS_H