2 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
4 * Contact: Lukasz Pawelczyk <l.pawelczyk@partner.samsung.com>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License
21 * @author Lukasz Pawelczyk (l.pawelczyk@partner.samsung.com)
22 * @brief File utility functions declaration
25 #ifndef COMMON_UTILS_FS_HPP
26 #define COMMON_UTILS_FS_HPP
29 #include <sys/types.h>
31 #include <boost/filesystem.hpp>
37 * Reads the content of a file; Throws exception on error
39 std::string readFileContent(const std::string& path);
42 * Reads the content of a file
44 bool readFileContent(const std::string& path, std::string& content);
47 * Save the content to the file
49 bool saveFileContent(const std::string& path, const std::string& content);
52 * Read a line from file
53 * Its goal is to read a kernel config files (eg. from /proc, /sys/)
55 bool readFirstLineOfFile(const std::string& path, std::string& ret);
60 bool removeFile(const std::string& path);
63 * Checks if a char device exists
65 bool isCharDevice(const std::string& path);
68 * Checks if a path exists and points to a directory
70 void assertIsDir(const std::string& path);
73 * List all (including '.' and '..' entries) dir entries
75 bool listDir(const std::string& path, std::vector<std::string>& files);
78 * Mounts run as a tmpfs on a given path
80 bool mountRun(const std::string& path);
85 bool mount(const std::string& source,
86 const std::string& target,
87 const std::string& filesystemtype,
88 unsigned long mountflags,
89 const std::string& data);
92 * Umounts a filesystem
94 bool umount(const std::string& path);
97 * Check if given path is a mount point
99 bool isMountPoint(const std::string& path, bool& result);
102 * Checks whether the given paths are under the same mount point
104 bool hasSameMountPoint(const std::string& path1, const std::string& path2, bool& result);
107 * Moves the file either by rename if under the same mount point
108 * or by copy&delete if under a different one.
109 * The destination has to be a full path including file name.
111 bool moveFile(const std::string& src, const std::string& dst);
114 * Recursively copy contents of src dir to dst dir.
116 bool copyDirContents(const std::string& src, const std::string& dst);
119 * Creates a directory with specific UID, GID and permissions set.
121 bool createDir(const std::string& path, uid_t uid, uid_t gid, boost::filesystem::perms mode);
124 * Recursively creates a directory with specific permissions set.
126 bool createDirs(const std::string& path, mode_t mode);
129 * Creates an empty directory, ready to serve as mount point.
130 * Succeeds either if path did not exist and was created successfully, or if already existing dir
131 * under the same path is empty and is not a mount point.
133 bool createEmptyDir(const std::string& path);
136 * Creates an empty file
138 bool createFile(const std::string& path, int flags, mode_t mode);
141 * Creates an FIFO special file
143 bool createFifo(const std::string& path, mode_t mode);
148 bool copyFile(const std::string& src, const std::string& dest);
153 bool createLink(const std::string& src, const std::string& dest);
158 #endif // COMMON_UTILS_FS_HPP