Skip to content

Commit

Permalink
Switch to using std::filesystem
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
aszlig committed Aug 6, 2023
1 parent c61f5cd commit cae5c5c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 22 deletions.
22 changes: 9 additions & 13 deletions src/blackhole.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
#include <climits>
#include <cstdlib>
#include <cstring>
#include <filesystem>
#include <initializer_list>
#include <string>

#include <errno.h>
#include <sys/stat.h>
#include <unistd.h>

static bool is_writable_dir(const std::string &dir)
static bool is_writable_dir(const std::filesystem::path &dir)
{
int old_errno = errno;

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

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

int old_errno = errno;
char *workdir = get_current_dir_name();
errno = old_errno;
if (workdir != nullptr) {
std::string wdir_str(workdir);
free(workdir);
if (is_writable_dir(wdir_str))
return wdir_str;
}
std::string workdir = std::filesystem::current_path();

if (is_writable_dir(workdir))
return workdir;

LOG(FATAL) << "Unable to get temporary directory.";
std::abort();
Expand All @@ -91,9 +87,9 @@ BlackHole::BlackHole()
: tmpdir(std::nullopt)
, filepath(std::nullopt)
{
static std::string tempdir = get_tmpdir();
static std::filesystem::path tempdir = get_tmpdir();

std::string bh_template = tempdir + "/ip2unix.XXXXXX";
std::string bh_template = tempdir / "ip2unix.XXXXXX";

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

Expand Down
11 changes: 2 additions & 9 deletions src/rules/parse.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <algorithm>
#include <cstddef>
#include <filesystem>
#include <iostream>
#include <optional>
#include <string>
Expand Down Expand Up @@ -323,14 +324,6 @@ static void print_arg_error(size_t rulepos, const std::string &arg, size_t pos,
<< ' ' << msg << std::endl;
}

std::string make_absolute(const std::string &path)
{
if (path.empty() || path[0] == '/')
return path;

return std::string(get_current_dir_name()) + '/' + path;
}

std::optional<Rule> parse_rule_arg(size_t rulepos, const std::string &arg)
{
std::string buf;
Expand All @@ -346,7 +339,7 @@ std::optional<Rule> parse_rule_arg(size_t rulepos, const std::string &arg)
if (i == arglen || arg[i] == ',') {
/* Handle key=value options. */
if (key.value() == "path") {
rule.socket_path = make_absolute(buf);
rule.socket_path = std::filesystem::absolute(buf);
#ifdef SYSTEMD_SUPPORT
} else if (key.value() == "systemd") {
rule.socket_activation = true;
Expand Down

0 comments on commit cae5c5c

Please sign in to comment.