Commit 95b0083e authored by Ramon Nou's avatar Ramon Nou
Browse files

create stoi alternative

parent ebac7a04
Loading
Loading
Loading
Loading
Loading
+43 −0
Original line number Diff line number Diff line
@@ -77,6 +77,49 @@ to_underlying(E e) {
    return static_cast<typename std::underlying_type<E>::type>(e);
}

inline bool
is_ascii_digit(char c) {
    return c >= '0' && c <= '9';
}

inline int
safe_stoi(const std::string& s, int def = 0) {
    if(s.empty())
        return def;
    int res = 0;
    int sign = 1;
    size_t i = 0;
    if(s[i] == '-') {
        sign = -1;
        ++i;
    } else if(s[i] == '+') {
        ++i;
    }
    bool found = false;
    for(; i < s.size() && is_ascii_digit(s[i]); ++i) {
        res = res * 10 + (s[i] - '0');
        found = true;
    }
    return found ? res * sign : def;
}

inline unsigned long
safe_stoul(const std::string& s, unsigned long def = 0) {
    if(s.empty())
        return def;
    unsigned long res = 0;
    size_t i = 0;
    if(s[i] == '+') {
        ++i;
    }
    bool found = false;
    for(; i < s.size() && is_ascii_digit(s[i]); ++i) {
        res = res * 10 + (s[i] - '0');
        found = true;
    }
    return found ? res : def;
}

std::optional<gkfs::metadata::Metadata>
get_metadata(const std::string& path, bool follow_links = false,
             bool include_inline = false);
+2 −1
Original line number Diff line number Diff line
@@ -78,6 +78,7 @@
#include <client/open_file_map.hpp>
#include <client/preload.hpp>
#include <client/preload_context.hpp>
#include <client/preload_util.hpp>
#include <client/user_functions.hpp>

// Forward declaration removed
@@ -796,7 +797,7 @@ get_open_fds() {
    while((entry = dlsym_readdir(dir))) {
        if(entry->d_type == DT_LNK) { // In /proc/self/fd, entries are symlinks
            try {
                int fd_val = std::stoi(entry->d_name);
                int fd_val = gkfs::utils::safe_stoi(entry->d_name);
                // Skip the directory FD itself for /proc/self/fd

                if(fd_val != dirfd(dir)) {
+2 −1
Original line number Diff line number Diff line
@@ -42,6 +42,7 @@
#include <client/hooks.hpp>
#include <client/logging.hpp>
#include <client/gkfs_functions.hpp>
#include <client/preload_util.hpp>
#include <optional>
#include <fmt/format.h>

@@ -136,7 +137,7 @@ get_open_fds() {
            }

            try {
                int fd = std::stoi(d_name);
                int fd = gkfs::utils::safe_stoi(d_name);
                // Skip the directory FD itself
                if(fd != dir_fd) {
                    fds.push_back(fd);
+7 −6
Original line number Diff line number Diff line
@@ -43,6 +43,7 @@
#include <client/open_file_map.hpp>
#include <client/open_dir.hpp>
#include <client/path.hpp>
#include <client/preload_util.hpp>

#include <common/env_util.hpp>
#include <common/path_util.hpp>
@@ -89,14 +90,14 @@ PreloadContext::PreloadContext()
            e.what());
        cwd_ = "/";
    }
    PreloadContext::set_replicas(
            std::stoi(gkfs::env::get_var(gkfs::env::NUM_REPL, "0")));
    PreloadContext::set_replicas(gkfs::utils::safe_stoi(
            gkfs::env::get_var(gkfs::env::NUM_REPL, "0")));

    const std::string env_dirents_buff_size =
            gkfs::env::get_var(gkfs::env::DIRENTS_BUFF_SIZE);
    if(!env_dirents_buff_size.empty()) {
        try {
            dirents_buff_size_ = std::stoul(env_dirents_buff_size);
            dirents_buff_size_ = gkfs::utils::safe_stoul(env_dirents_buff_size);
        } catch(...) {
            dirents_buff_size_ = gkfs::config::rpc::dirents_buff_size;
        }
@@ -126,8 +127,8 @@ PreloadContext::init_logging() {
    // atoi returns 0 if no int conversion can be performed, which works
    // for us since if the user provides a non-numeric value we can just treat
    // it as zero
    const int log_verbosity = std::atoi(
            gkfs::env::get_var(gkfs::env::LOG_DEBUG_VERBOSITY, "0").c_str());
    const int log_verbosity = gkfs::utils::safe_stoi(
            gkfs::env::get_var(gkfs::env::LOG_DEBUG_VERBOSITY, "0"));

    const std::string log_filter =
            gkfs::env::get_var(gkfs::env::LOG_SYSCALL_FILTER, "");
@@ -158,7 +159,7 @@ PreloadContext::destroy_metrics() {
bool
PreloadContext::init_metrics() {
#ifdef GKFS_ENABLE_CLIENT_METRICS
    auto flush_interval = std::stoi(gkfs::env::get_var(
    auto flush_interval = gkfs::utils::safe_stoi(gkfs::env::get_var(
            gkfs::env::METRICS_FLUSH_INTERVAL,
            std::to_string(gkfs::config::client_metrics::flush_interval)));
    if(gkfs::env::var_is_set(gkfs::env::METRICS_IP_PORT)) {
+3 −3
Original line number Diff line number Diff line
@@ -210,8 +210,8 @@ load_hostfile(const std::string& path) {
        CTX->fs_conf()->link_cnt_state = match[9] == '1';
        CTX->fs_conf()->blocks_state = match[10] == '1';
        // convert match[11] and match[12] to unsigned integers.
        CTX->fs_conf()->uid = std::stoi(match[11]);
        CTX->fs_conf()->gid = std::stoi(match[12]);
        CTX->fs_conf()->uid = gkfs::utils::safe_stoi(match[11]);
        CTX->fs_conf()->gid = gkfs::utils::safe_stoi(match[12]);
    }
    if(hosts.empty()) {
        throw runtime_error(
@@ -413,7 +413,7 @@ load_forwarding_map_file(const std::string& lfpath) {
                    fmt::format("unrecognized line format: '{}'", line));
        }
        string host = match[1];
        uint64_t forwarder = std::stoi(match[2].str());
        uint64_t forwarder = gkfs::utils::safe_stoi(match[2].str());
        forwarding_map[host] = forwarder;
    }
    return forwarding_map;