Fcitx
fs.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2016-2016 CSSlayer <wengxt@gmail.com>
3  *
4  * SPDX-License-Identifier: LGPL-2.1-or-later
5  *
6  */
7 #ifndef _FCITX_UTILS_FS_H_
8 #define _FCITX_UTILS_FS_H_
9 
10 #include <sys/types.h>
11 #include <cstddef>
12 #include <cstdint>
13 #include <filesystem>
14 #include <optional>
15 #include <string>
16 #include <string_view>
17 #include <fcitx-utils/fcitxutils_export.h>
18 #include <fcitx-utils/misc.h>
19 
20 /// \addtogroup FcitxUtils
21 /// \{
22 /// \file
23 /// \brief Simple file system related API for checking file status.
24 
25 namespace fcitx {
26 
27 class UnixFD;
28 class StandardPathFile;
29 
30 namespace fs {
31 
32 /// \brief check whether path is a directory.
33 FCITXUTILS_EXPORT bool isdir(const std::string &path);
34 /// \brief check whether path is a regular file.
35 FCITXUTILS_EXPORT bool isreg(const std::string &path);
36 /**
37  * \brief check whether path is an executable regular file.
38  * \since 5.0.18
39  */
40 FCITXUTILS_EXPORT bool isexe(const std::string &path);
41 /// \brief check whether path is a link.
42 FCITXUTILS_EXPORT bool islnk(const std::string &path);
43 
44 /// \brief Get the clean path by removing . , .. , and duplicate / in the path.
45 FCITXUTILS_EXPORT std::string cleanPath(const std::string &path);
46 /// \brief Create directory recursively.
47 FCITXUTILS_EXPORT bool makePath(const std::filesystem::path &path);
48 /// \brief Get directory name of path
49 FCITXUTILS_EXPORT std::string dirName(const std::string &path);
50 /// \brief Get base file name of path.
51 FCITXUTILS_EXPORT std::string baseName(std::string_view path);
52 
53 /// \brief a simple wrapper around read(), ignore EINTR.
54 FCITXUTILS_EXPORT ssize_t safeRead(int fd, void *data, size_t maxlen);
55 /// \brief a simple wrapper around write(), ignore EINTR.
56 FCITXUTILS_EXPORT ssize_t safeWrite(int fd, const void *data, size_t maxlen);
57 /// \brief read symlink.
58 FCITXUTILS_EXPORT std::optional<std::string> readlink(const std::string &path);
59 /**
60  * \brief Return modified time in seconds of given path.
61  *
62  * When error, it will return the time equal to std::chrono::system_clock::
63  * time_point::min().
64  *
65  * \param path the path to check
66  *
67  * \since 5.1.13
68  */
69 FCITXUTILS_EXPORT int64_t modifiedTime(const std::filesystem::path &path);
70 
71 /**
72  * \brief open the unix fd with fdopen.
73  *
74  * since fdopen'ed fd will be closed upon fclose, the ownership of fd will be
75  * transferred to the FILE if the file descriptor is opened. Otherwise fd is
76  * untouched. This helps user to avoid double close on the same file descriptor,
77  * since the file descriptor number might be reused.
78  *
79  * \param fd file descriptor
80  * \param modes modes passed to fdopen
81  * \return FILE pointer if fd is valid and successfully opened.
82  * \since 5.0.16
83  */
84 FCITXUTILS_EXPORT UniqueFilePtr openFD(UnixFD &fd, const char *modes);
85 
86 /**
87  * \brief open the standard path file fd with fdopen.
88  *
89  * \param fd file descriptor
90  * \param modes modes passed to fdopen
91  * \return FILE pointer if fd is valid and successfully opened.
92  * \see openUnixFD
93  * \since 5.0.16
94  */
95 FCITXUTILS_DEPRECATED_EXPORT UniqueFilePtr openFD(StandardPathFile &file,
96  const char *modes);
97 } // namespace fs
98 } // namespace fcitx
99 
100 #endif // _FCITX_UTILS_FS_H_
std::string baseName(std::string_view path)
Get base file name of path.
Definition: fs.cpp:215
bool islnk(const std::string &path)
check whether path is a link.
Definition: fs.cpp:94
bool isexe(const std::string &path)
check whether path is an executable regular file.
Definition: fs.cpp:88
bool isreg(const std::string &path)
check whether path is a regular file.
Definition: fs.cpp:82
Definition: action.cpp:17
bool isdir(const std::string &path)
check whether path is a directory.
Definition: fs.cpp:76
ssize_t safeWrite(int fd, const void *data, size_t maxlen)
a simple wrapper around write(), ignore EINTR.
Definition: fs.cpp:238
std::string dirName(const std::string &path)
Get directory name of path.
Definition: fs.cpp:192
std::optional< std::string > readlink(const std::string &path)
read symlink.
Definition: fs.cpp:246
FCITXUTILS_DEPRECATED_EXPORT UniqueFilePtr openFD(StandardPathFile &file, const char *modes)
open the standard path file fd with fdopen.
bool makePath(const std::filesystem::path &path)
Create directory recursively.
Definition: fs.cpp:179
int64_t modifiedTime(const std::filesystem::path &path)
Return modified time in seconds of given path.
Definition: fs.cpp:271
ssize_t safeRead(int fd, void *data, size_t maxlen)
a simple wrapper around read(), ignore EINTR.
Definition: fs.cpp:231
std::string cleanPath(const std::string &path)
Get the clean path by removing . , .. , and duplicate / in the path.
Definition: fs.cpp:104