common: Inotify implementation
[platform/core/security/vasum.git] / common / utils / fs.hpp
1 /*
2  *  Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Contact: Lukasz Pawelczyk <l.pawelczyk@partner.samsung.com>
5  *
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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
17  */
18
19 /**
20  * @file
21  * @author  Lukasz Pawelczyk (l.pawelczyk@partner.samsung.com)
22  * @brief   File utility functions declaration
23  */
24
25 #ifndef COMMON_UTILS_FS_HPP
26 #define COMMON_UTILS_FS_HPP
27
28 #include <string>
29 #include <sys/types.h>
30 #include <vector>
31 #include <boost/filesystem.hpp>
32
33
34 namespace utils {
35
36 /**
37  * Reads the content of a file; Throws exception on error
38  */
39 std::string readFileContent(const std::string& path);
40
41 /**
42  * Reads the content of a file
43  */
44 bool readFileContent(const std::string& path, std::string& content);
45
46 /**
47  * Save the content to the file
48  */
49 bool saveFileContent(const std::string& path, const std::string& content);
50
51 /**
52  * Read a line from file
53  * Its goal is to read a kernel config files (eg. from /proc, /sys/)
54  */
55 bool readFirstLineOfFile(const std::string& path, std::string& ret);
56
57 /**
58  * Remove file
59  */
60 bool removeFile(const std::string& path);
61
62 /**
63  * Checks if a char device exists
64  */
65 bool isCharDevice(const std::string& path);
66
67 /**
68  * Checks if a path exists and points to a directory
69  */
70 void assertIsDir(const std::string& path);
71
72 /**
73  * List all (including '.' and '..' entries) dir entries
74  */
75 bool listDir(const std::string& path, std::vector<std::string>& files);
76
77 /**
78  * Mounts run as a tmpfs on a given path
79  */
80 bool mountRun(const std::string& path);
81
82 /**
83  * Creates mount point
84  */
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);
90
91 /**
92  * Umounts a filesystem
93  */
94 bool umount(const std::string& path);
95
96 /**
97  * Check if given path is a mount point
98  */
99 bool isMountPoint(const std::string& path, bool& result);
100
101 /**
102  * Checks whether the given paths are under the same mount point
103  */
104 bool hasSameMountPoint(const std::string& path1, const std::string& path2, bool& result);
105
106 /**
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.
110  */
111 bool moveFile(const std::string& src, const std::string& dst);
112
113 /**
114  * Recursively copy contents of src dir to dst dir.
115  */
116 bool copyDirContents(const std::string& src, const std::string& dst);
117
118 /**
119  * Creates a directory with specific UID, GID and permissions set.
120  */
121 bool createDir(const std::string& path, uid_t uid, uid_t gid, boost::filesystem::perms mode);
122
123 /**
124  * Recursively creates a directory with specific permissions set.
125  */
126 bool createDirs(const std::string& path, mode_t mode);
127
128 /**
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.
132  */
133 bool createEmptyDir(const std::string& path);
134
135 /**
136  * Creates an empty file
137  */
138 bool createFile(const std::string& path, int flags, mode_t mode);
139
140 /**
141  * Creates an FIFO special file
142  */
143 bool createFifo(const std::string& path, mode_t mode);
144
145 /**
146  * Copy an file
147  */
148 bool copyFile(const std::string& src, const std::string& dest);
149
150 /**
151  * Create hard link
152  */
153 bool createLink(const std::string& src, const std::string& dest);
154
155 } // namespace utils
156
157
158 #endif // COMMON_UTILS_FS_HPP