Scanframe Modular Application 0.1.0
Loading...
Searching...
No Matches
lnx_utils.h
Go to the documentation of this file.
1#pragma once
2#include <fcntl.h>
3#include <filesystem>
4#include <grp.h>
5#include <misc/gen/TStrings.h>
6#include <misc/gen/system.h>
7#include <misc/global.h>
8#include <pwd.h>
9
10namespace sf
11{
12
17
21_MISC_FUNC pid_t gettid() noexcept;
22
26_MISC_FUNC bool getFiles(strings& files, const std::string& directory, const std::string& wildcard);
27
36_MISC_FUNC bool file_write(const char* path, const void* buf, size_t sz, bool append = false);
37
46inline bool file_write(const std::string& path, const void* buf, size_t sz, bool append = false)
47{
48 return file_write(path.c_str(), buf, sz, append);
49}
50
58inline bool file_write(const std::string& path, const std::string& s, bool append = false)
59{
60 return file_write(path.c_str(), s.c_str(), s.length(), append);
61}
62
68_MISC_FUNC std::filesystem::path file_fd_path(int fd);
69
73typedef struct stat stat_t;
74
78_MISC_FUNC bool file_stat(const std::string& path, stat_t& _stat);
79
86_MISC_FUNC std::string time_format(const struct tm* time_info, const char* format = nullptr);
87
95_MISC_FUNC std::string time_format(time_t time = -1, const char* format = nullptr, bool gm_time = false);
96
104_MISC_FUNC time_t time_str2time(const std::string& str, const char* format = nullptr, bool gm_time = false);
105
112_MISC_FUNC time_t time_mktime(struct tm* tm, bool gm_time = false);
113
120_MISC_FUNC bool file_mkdir(const char* path, __mode_t mode = 0755);
121
128inline bool file_mkdir(const std::string& path, __mode_t mode = 0755)
129{
130 return file_mkdir(path.c_str(), mode);
131}
132
136typedef struct passwd passwd_type;
137
142struct passwd_t : public passwd_type
143{
144 public:
149
154
155 //
156 explicit inline operator bool() const
157 {
158 return valid;
159 }
160
161 private:
165 void reset();
166
170 ssize_t buf_sz{0};
174 char* buf{nullptr};
178 bool valid{};
179
180 friend bool proc_getpwnam(std::string name, passwd_t& pwd);
181
182 friend bool proc_getpwuid(uid_t uid, passwd_t& pwd);
183};
184
190bool proc_getpwnam(std::string name, passwd_t& pwd);
191
197bool proc_getpwuid(uid_t uid, passwd_t& pwd);
198
203void proc_setuid(uid_t uid);
204
209void proc_seteuid(uid_t uid);
210
214typedef struct group group_type;
215
220struct group_t : public group_type
221{
222 public:
227
232
236 explicit operator bool() const
237 {
238 return valid;
239 }
240
241 private:
245 void reset();
246
250 ssize_t buf_sz{0};
254 char* buf{nullptr};
258 bool valid{false};
259
260 friend bool proc_getgrnam(const std::string& name, group_t& grp);
261
262 friend bool proc_getgrgid(gid_t gid, group_t& grp);
263};
264
270_MISC_FUNC bool proc_getgrnam(const std::string& name, group_t& grp);
271
277_MISC_FUNC bool proc_getgrgid(gid_t gid, group_t& grp);
278
283_MISC_FUNC void proc_setgid(gid_t gid);
284
289_MISC_FUNC void proc_setegid(gid_t gid);
290
295_MISC_FUNC void proc_setfsuid(uid_t uid);
296
301_MISC_FUNC void proc_setfsgid(gid_t gid);
302
309int _MISC_FUNC siginterrupt(int sig, int flag);
310
311}// namespace sf
Counted vector of strings.
Definition TStrings.h:12
#define _MISC_FUNC
Definition misc/global.h:39
Definition Application.h:10
_MISC_FUNC bool getFiles(strings &files, const std::string &directory, const std::string &wildcard)
_MISC_FUNC void proc_setegid(gid_t gid)
_MISC_FUNC bool file_mkdir(const char *path, __mode_t mode=0755)
Makes all directories recursively in the path.
void proc_setuid(uid_t uid)
int _MISC_FUNC siginterrupt(int sig, int flag)
Replacement for deprecated 'siginterrupt' function. Returns EINVAL when the sig argument is not a val...
_MISC_FUNC std::string time_format(const struct tm *time_info, const char *format=nullptr)
Formats the time to the given format.
_MISC_FUNC void proc_setfsgid(gid_t gid)
struct group group_type
Definition lnx_utils.h:214
_MISC_FUNC time_t time_mktime(struct tm *tm, bool gm_time=false)
Same as mktime().
_MISC_FUNC std::filesystem::path file_fd_path(int fd)
_MISC_FUNC bool file_stat(const std::string &path, stat_t &_stat)
Same as stat() but using a std::string.
bool proc_getpwnam(std::string name, passwd_t &pwd)
_MISC_FUNC void proc_setfsuid(uid_t uid)
_MISC_FUNC pid_t gettid() noexcept
_MISC_FUNC bool proc_getgrnam(const std::string &name, group_t &grp)
void proc_seteuid(uid_t uid)
struct stat stat_t
Easier typename for 'struct stat'.
Definition lnx_utils.h:73
struct passwd passwd_type
Definition lnx_utils.h:136
_MISC_FUNC time_t time_str2time(const std::string &str, const char *format=nullptr, bool gm_time=false)
Gets the unix time from the passed string in the format that was passed.
_MISC_FUNC bool file_write(const char *path, const void *buf, size_t sz, bool append=false)
Writes a buffer to a filepath.
bool proc_getpwuid(uid_t uid, passwd_t &pwd)
_MISC_FUNC bool proc_getgrgid(gid_t gid, group_t &grp)
_MISC_FUNC bool kb_hit()
_MISC_FUNC void proc_setgid(gid_t gid)
Definition lnx_utils.h:221
friend bool proc_getgrgid(gid_t gid, group_t &grp)
friend bool proc_getgrnam(const std::string &name, group_t &grp)
Extends struct passwd and auto allocates needed memory. Used as a result type with auto memory cleanu...
Definition lnx_utils.h:143
friend bool proc_getpwuid(uid_t uid, passwd_t &pwd)
friend bool proc_getpwnam(std::string name, passwd_t &pwd)