Commit 2ea31d25 authored by David Auer's avatar David Auer
Browse files

Move load_hostfile to global

parent 82bae1b8
Loading
Loading
Loading
Loading
+0 −3
Original line number Diff line number Diff line
@@ -62,9 +62,6 @@ int
metadata_to_stat(const std::string& path, const gkfs::metadata::Metadata& md,
                 struct stat& attr);

void
load_hosts();

void
load_forwarding_map();

+2 −4
Original line number Diff line number Diff line
@@ -17,8 +17,7 @@
#include <vector>
#include <string>

namespace gkfs {
namespace utils {
namespace gkfs::utils {
void
populate_hosts_file();

@@ -27,7 +26,6 @@ destroy_hosts_file();

std::vector<std::pair<std::string, std::string>>
read_hosts_file();
} // namespace utils
} // namespace gkfs
} // namespace gkfs::utils

#endif // GEKKOFS_DAEMON_UTIL_HPP
+25 −0
Original line number Diff line number Diff line
/*
  Copyright 2018-2020, Barcelona Supercomputing Center (BSC), Spain
  Copyright 2015-2020, Johannes Gutenberg Universitaet Mainz, Germany

  This software was partially supported by the
  EC H2020 funded project NEXTGenIO (Project ID: 671951, www.nextgenio.eu).

  This software was partially supported by the
  ADA-FS project under the SPPEXA project funded by the DFG.

  SPDX-License-Identifier: MIT
*/

#ifndef GEKKOFS_HOSTSFILE_UTIL_HPP
#define GEKKOFS_HOSTSFILE_UTIL_HPP

#include <vector>
#include <string>

namespace gkfs::util {
std::vector<std::pair<std::string, std::string>>
load_hostfile(const std::string& path);
} // namespace gkfs::util

#endif // GEKKOFS_HOSTSFILE_UTIL_HPP
+1 −0
Original line number Diff line number Diff line
@@ -52,6 +52,7 @@ set(PRELOAD_LINK_LIBRARIES
    metadata
    distributor
    env_util
    hostsfile_util
    # external
    Syscall_intercept::Syscall_intercept
    dl
+2 −45
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@
#include <global/rpc/rpc_util.hpp>
#include <global/env_util.hpp>
#include <global/global_defs.hpp>
#include <global/hostsfile_util.hpp>

#include <hermes.hpp>

@@ -116,51 +117,6 @@ extract_protocol(const string& uri) {
    CTX->rpc_protocol(protocol);
}

/**
 * Reads the daemon generator hosts file by a given path, returning hosts and
 * URI addresses
 * @param path to hosts file
 * @return vector<pair<hosts, URI>>
 * @throws std::runtime_error
 */
vector<pair<string, string>>
load_hostfile(const std::string& path) {

    LOG(DEBUG, "Loading hosts file: \"{}\"", path);

    ifstream lf(path);
    if(!lf) {
        throw runtime_error(fmt::format("Failed to open hosts file '{}': {}",
                                        path, strerror(errno)));
    }
    vector<pair<string, string>> hosts;
    const regex line_re("^(\\S+)\\s+(\\S+)$",
                        regex::ECMAScript | regex::optimize);
    string line;
    string host;
    string uri;
    std::smatch match;
    while(getline(lf, line)) {
        if(!regex_match(line, match, line_re)) {

            LOG(ERROR, "Unrecognized line format: [path: '{}', line: '{}']",
                path, line);

            throw runtime_error(
                    fmt::format("unrecognized line format: '{}'", line));
        }
        host = match[1];
        uri = match[2];
        hosts.emplace_back(host, uri);
    }
    if(hosts.empty()) {
        throw runtime_error(
                "Hosts file found but no suitable addresses could be extracted");
    }
    extract_protocol(hosts[0].second);
    return hosts;
}

} // namespace

namespace gkfs::utils {
@@ -336,6 +292,7 @@ read_hosts_file() {
    vector<pair<string, string>> hosts;
    try {
        hosts = load_hostfile(hostfile);
        extract_protocol(hosts[0].second);
    } catch(const exception& e) {
        auto emsg = fmt::format("Failed to load hosts file: {}", e.what());
        throw runtime_error(emsg);
Loading