<commit_before>/*
 * Copyright (C) 2019 ScyllaDB
 */

/*
 * This file is part of Scylla.
 *
 * Scylla is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Scylla is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Scylla.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "init.hh"
#include "supervisor.hh"
#include "directories.hh"
#include "distributed_loader.hh"
#include "disk-error-handler.hh"

namespace utils {

static future<> disk_sanity(fs::path path, bool developer_mode) {
    return check_direct_io_support(path.native()).then([] {
        return make_ready_future<>();
    }).handle_exception([path](auto ep) {
        startlog.error("Could not access {}: {}", path, ep);
        return make_exception_future<>(ep);
    });
};

future<> directories::touch_and_lock(fs::path path) {
    return io_check([path] { return recursive_touch_directory(path.native()); }).then_wrapped([this, path] (future<> f) {
        try {
            f.get();
            return file_lock::acquire(path / ".lock").then([this](file_lock lock) {
               _locks.emplace_back(std::move(lock));
            }).handle_exception([path](auto ep) {
                // only do this because "normal" unhandled exception exit in seastar
                // _drops_ system_error message ("what()") and thus does not quite deliver
                // the relevant info to the user
                try {
                    std::rethrow_exception(ep);
                } catch (std::exception& e) {
                    startlog.error("Could not initialize {}: {}", path, e.what());
                    throw;
                } catch (...) {
                    throw;
                }
            });
        } catch (...) {
            startlog.error("Directory '{}' cannot be initialized. Tried to do it but failed with: {}", path, std::current_exception());
            throw;
        }
    });
}

static void add(fs::path path, std::vector<fs::path>& to) {
    to.push_back(path);
}

static void add(sstring path, std::vector<fs::path>& to) {
    add(fs::path(path), to);
}

static void add(std::vector<sstring> paths, std::vector<fs::path>& to) {
    for (auto& path : paths) {
        add(path, to);
    }
}

static void add_sharded(sstring p, std::vector<fs::path>& to) {
    fs::path path(p);

    add(path, to);
    for (unsigned i = 0; i < smp::count; i++) {
         add(path / seastar::to_sstring(i).c_str(), to);
    }
}

future<> directories::init(db::config& cfg, bool hinted_handoff_enabled) {
    std::vector<fs::path> paths;

    add(cfg.data_file_directories(), paths);
    add(cfg.commitlog_directory(), paths);
    if (hinted_handoff_enabled) {
        add_sharded(cfg.hints_directory(), paths);<commit_msg>directories: Do not mess with per-shard base dir<commit_after>/*
 * Copyright (C) 2019 ScyllaDB
 */

/*
 * This file is part of Scylla.
 *
 * Scylla is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Scylla is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Scylla.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "init.hh"
#include "supervisor.hh"
#include "directories.hh"
#include "distributed_loader.hh"
#include "disk-error-handler.hh"

namespace utils {

static future<> disk_sanity(fs::path path, bool developer_mode) {
    return check_direct_io_support(path.native()).then([] {
        return make_ready_future<>();
    }).handle_exception([path](auto ep) {
        startlog.error("Could not access {}: {}", path, ep);
        return make_exception_future<>(ep);
    });
};

future<> directories::touch_and_lock(fs::path path) {
    return io_check([path] { return recursive_touch_directory(path.native()); }).then_wrapped([this, path] (future<> f) {
        try {
            f.get();
            return file_lock::acquire(path / ".lock").then([this](file_lock lock) {
               _locks.emplace_back(std::move(lock));
            }).handle_exception([path](auto ep) {
                // only do this because "normal" unhandled exception exit in seastar
                // _drops_ system_error message ("what()") and thus does not quite deliver
                // the relevant info to the user
                try {
                    std::rethrow_exception(ep);
                } catch (std::exception& e) {
                    startlog.error("Could not initialize {}: {}", path, e.what());
                    throw;
                } catch (...) {
                    throw;
                }
            });
        } catch (...) {
            startlog.error("Directory '{}' cannot be initialized. Tried to do it but failed with: {}", path, std::current_exception());
            throw;
        }
    });
}

static void add(fs::path path, std::vector<fs::path>& to) {
    to.push_back(path);
}

static void add(sstring path, std::vector<fs::path>& to) {
    add(fs::path(path), to);
}

static void add(std::vector<sstring> paths, std::vector<fs::path>& to) {
    for (auto& path : paths) {
        add(path, to);
    }
}

static void add_sharded(sstring p, std::vector<fs::path>& to) {
    fs::path path(p);

    for (unsigned i = 0; i < smp::count; i++) {
         add(path / seastar::to_sstring(i).c_str(), to);
    }
}

future<> directories::init(db::config& cfg, bool hinted_handoff_enabled) {
    std::vector<fs::path> paths;

    add(cfg.data_file_directories(), paths);
    add(cfg.commitlog_directory(), paths);
    if (hinted_handoff_enabled) {
        add_sharded(cfg.hints_directory(), paths);
{'fraction_non_alphanumeric': 0.13707653701380176, 'fraction_numerical': 0.0018820577164366374, 'mean_word_length': 2.7227086981903095, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4079962', 'n_tokens_mistral': 1913, 'n_tokens_neox': 1870, 'n_words': 556}