Skip to content

Commit cae5c5c

Browse files
committed
Switch to using std::filesystem
Originally, the minimum required GCC version was version 6, which didn't have support for the C++17 Filesystem library. However, with our minimum requirement being GCC version 9, we now can use std::filesystem and drop a few of our hand-rolled implementations. Ideally, I'd even go so far as to even cut down blackhole.cc, which uses access() to check whether the temporary directory is writable and uses the best possible but writable one. Obviously, this isn't portable, but given that ip2unix for example will probably never work natively on Windows, I think it's fine to leave it as is, since using std::filesystem APIs won't actually assure that the underlying syscall would be access() anymore. Nevertheless however, this should pave the way to port ip2unix to Darwin and possibly BSD one day. Signed-off-by: aszlig <[email protected]>
1 parent c61f5cd commit cae5c5c

File tree

2 files changed

+11
-22
lines changed

2 files changed

+11
-22
lines changed

src/blackhole.cc

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66
#include <climits>
77
#include <cstdlib>
88
#include <cstring>
9+
#include <filesystem>
910
#include <initializer_list>
1011
#include <string>
1112

1213
#include <errno.h>
1314
#include <sys/stat.h>
1415
#include <unistd.h>
1516

16-
static bool is_writable_dir(const std::string &dir)
17+
static bool is_writable_dir(const std::filesystem::path &dir)
1718
{
1819
int old_errno = errno;
1920

@@ -40,7 +41,7 @@ static bool is_writable_dir(const std::string &dir)
4041
return S_ISDIR(st.st_mode);
4142
}
4243

43-
static std::string get_tmpdir(void)
44+
static std::filesystem::path get_tmpdir(void)
4445
{
4546
for (const char *tryenv : {"TMPDIR", "TMP", "TEMP", "TEMPDIR"}) {
4647
const char *tmpdir = getenv(tryenv);
@@ -62,15 +63,10 @@ static std::string get_tmpdir(void)
6263
if (is_writable_dir("/var/tmp"))
6364
return "/var/tmp";
6465

65-
int old_errno = errno;
66-
char *workdir = get_current_dir_name();
67-
errno = old_errno;
68-
if (workdir != nullptr) {
69-
std::string wdir_str(workdir);
70-
free(workdir);
71-
if (is_writable_dir(wdir_str))
72-
return wdir_str;
73-
}
66+
std::string workdir = std::filesystem::current_path();
67+
68+
if (is_writable_dir(workdir))
69+
return workdir;
7470

7571
LOG(FATAL) << "Unable to get temporary directory.";
7672
std::abort();
@@ -91,9 +87,9 @@ BlackHole::BlackHole()
9187
: tmpdir(std::nullopt)
9288
, filepath(std::nullopt)
9389
{
94-
static std::string tempdir = get_tmpdir();
90+
static std::filesystem::path tempdir = get_tmpdir();
9591

96-
std::string bh_template = tempdir + "/ip2unix.XXXXXX";
92+
std::string bh_template = tempdir / "ip2unix.XXXXXX";
9793

9894
char *c_bh_template = strdup(bh_template.c_str());
9995

src/rules/parse.cc

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include <algorithm>
77
#include <cstddef>
8+
#include <filesystem>
89
#include <iostream>
910
#include <optional>
1011
#include <string>
@@ -323,14 +324,6 @@ static void print_arg_error(size_t rulepos, const std::string &arg, size_t pos,
323324
<< ' ' << msg << std::endl;
324325
}
325326

326-
std::string make_absolute(const std::string &path)
327-
{
328-
if (path.empty() || path[0] == '/')
329-
return path;
330-
331-
return std::string(get_current_dir_name()) + '/' + path;
332-
}
333-
334327
std::optional<Rule> parse_rule_arg(size_t rulepos, const std::string &arg)
335328
{
336329
std::string buf;
@@ -346,7 +339,7 @@ std::optional<Rule> parse_rule_arg(size_t rulepos, const std::string &arg)
346339
if (i == arglen || arg[i] == ',') {
347340
/* Handle key=value options. */
348341
if (key.value() == "path") {
349-
rule.socket_path = make_absolute(buf);
342+
rule.socket_path = std::filesystem::absolute(buf);
350343
#ifdef SYSTEMD_SUPPORT
351344
} else if (key.value() == "systemd") {
352345
rule.socket_activation = true;

0 commit comments

Comments
 (0)