Skip to content

Commit 7df03f1

Browse files
committed
util: add perm string helper functions
PermsToSymbolicString will convert from fs::perms to string type 'rwxrwxrwx'. InterpretPermString will convert from a user-supplied "perm string" such as 'owner', 'group' or 'all, into appropriate fs::perms.
1 parent 8efd03a commit 7df03f1

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

src/util/fs_helpers.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <fstream>
1717
#include <map>
1818
#include <memory>
19+
#include <optional>
1920
#include <string>
2021
#include <system_error>
2122
#include <utility>
@@ -269,3 +270,42 @@ bool TryCreateDirectories(const fs::path& p)
269270
// create_directories didn't create the directory, it had to have existed already
270271
return false;
271272
}
273+
274+
std::string PermsToSymbolicString(fs::perms p)
275+
{
276+
std::string perm_str(9, '-');
277+
278+
auto set_perm = [&](size_t pos, fs::perms required_perm, char letter) {
279+
if ((p & required_perm) != fs::perms::none) {
280+
perm_str[pos] = letter;
281+
}
282+
};
283+
284+
set_perm(0, fs::perms::owner_read, 'r');
285+
set_perm(1, fs::perms::owner_write, 'w');
286+
set_perm(2, fs::perms::owner_exec, 'x');
287+
set_perm(3, fs::perms::group_read, 'r');
288+
set_perm(4, fs::perms::group_write, 'w');
289+
set_perm(5, fs::perms::group_exec, 'x');
290+
set_perm(6, fs::perms::others_read, 'r');
291+
set_perm(7, fs::perms::others_write, 'w');
292+
set_perm(8, fs::perms::others_exec, 'x');
293+
294+
return perm_str;
295+
}
296+
297+
std::optional<fs::perms> InterpretPermString(const std::string& s)
298+
{
299+
if (s == "owner") {
300+
return fs::perms::owner_read | fs::perms::owner_write;
301+
} else if (s == "group") {
302+
return fs::perms::owner_read | fs::perms::owner_write |
303+
fs::perms::group_read;
304+
} else if (s == "all") {
305+
return fs::perms::owner_read | fs::perms::owner_write |
306+
fs::perms::group_read |
307+
fs::perms::others_read;
308+
} else {
309+
return std::nullopt;
310+
}
311+
}

src/util/fs_helpers.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <cstdio>
1313
#include <iosfwd>
1414
#include <limits>
15+
#include <optional>
1516

1617
/**
1718
* Ensure file contents are fully committed to disk, using a platform-specific
@@ -62,6 +63,19 @@ void ReleaseDirectoryLocks();
6263
bool TryCreateDirectories(const fs::path& p);
6364
fs::path GetDefaultDataDir();
6465

66+
/** Convert fs::perms to symbolic string of the form 'rwxrwxrwx'
67+
*
68+
* @param[in] p the perms to be converted
69+
* @return Symbolic permissions string
70+
*/
71+
std::string PermsToSymbolicString(fs::perms p);
72+
/** Interpret a custom permissions level string as fs::perms
73+
*
74+
* @param[in] s Permission level string
75+
* @return Permissions as fs::perms
76+
*/
77+
std::optional<fs::perms> InterpretPermString(const std::string& s);
78+
6579
#ifdef WIN32
6680
fs::path GetSpecialFolderPath(int nFolder, bool fCreate = true);
6781
#endif

0 commit comments

Comments
 (0)