2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef FILESYSTEM_FILESYSTEM_UTILS_H
18 #define FILESYSTEM_FILESYSTEM_UTILS_H
20 #include "common/picojson.h"
21 #include "common/tools.h"
24 #include <glib-object.h>
29 #include <system_error>
32 namespace filesystem {
34 enum class FilesystemError {
44 } // namespace filesystem
45 } // namespace extension
47 namespace FilesystemUtils {
50 * @brief Wrapper for POSIX mkdir function.
51 * @throw std::system_error
53 void Mkdir(const std::string& path);
56 * @brief Make directory using mkdir. If 'parents' is true, make parent directories as needed
57 * @throw std::system_error
59 void Mkdir(const std::string& path, bool parents);
62 * @brief Wrapper for POSIX unlink function
63 * @throw std::system_error
65 void Unlink(const std::string& path);
68 * @brief Returns last element of path (wrapper for POSIX basename function)
69 * @throw std::system_error
71 std::string PosixBasename(const std::string& path);
74 * @brief Returns parent directory of path (wrapper for POSIX dirname function)
75 * @throw std::system_error
77 std::string Dirname(const std::string& path);
80 * @brief Wrapper for GLib g_file_copy function.
81 * @throw std::system_error
83 void CopyFile(const std::string& src, const std::string& dest, bool overwrite);
86 * @brief Copies directory recursively
87 * @throw std::system_error
89 void CopyDirectory(const std::string& src, const std::string& dest, bool overwrite);
92 * @brief Calls 'next' function with name for every entry in given directory
93 * @throw std::system_error
95 void ListDirectory(const std::string& path, std::function<void(const std::string&, unsigned char)> next);
98 * @brief Removes directory recursively pointed by path.
99 * @throw std::system_error
101 void RemoveDirectoryRecursively(const std::string& path);
104 * @brief Removes directory pointed by path.
105 * @throw std::system_error
107 void RemoveDirectory(const std::string& path);
110 * @brief Returns the real path.
111 * @throw std::system_error
113 std::string RealPath(const std::string& path);
116 * @brief Checks if path points to file or directory.
117 * @throw std::system_error
119 bool CheckIfExists(const std::string& path, struct stat* buf);
122 * @brief Checks if path points to directory.
123 * @throw std::system_error
125 bool CheckIfDir(const struct stat& buf);
128 * @brief Checks if path points to file.
129 * @throw std::system_error
131 bool CheckIfFile(const struct stat& buf);
134 * @brief Renames file or directory.
135 * @throw std::system_error
137 void Rename(const std::string& path, const std::string& new_path);
140 * @brief Wrapper for GLib g_file_move function.
141 * @throw std::system_error
143 void MoveFile(const std::string& path, const std::string& new_path, bool overwrite);
146 * @brief Moves directory by recursively calling move_file.
147 * @throw std::system_error
149 void MoveDirectory(const std::string& path, const std::string& new_path, bool overwrite);
151 void TranslateException(const std::system_error& e, picojson::object& obj);
153 // This function is left only for compatibility with previous implementation in FilesystemManager
154 std::string GetDirname(const std::string& path);
157 #endif // FILESYSTEM_FILESYSTEM_UTILS_H