Skip to content

Commit

Permalink
fs:: -> std::filesystem::
Browse files Browse the repository at this point in the history
  • Loading branch information
LunarWatcher committed Dec 22, 2024
1 parent d4b632a commit 3db0097
Show file tree
Hide file tree
Showing 13 changed files with 75 additions and 79 deletions.
18 changes: 9 additions & 9 deletions src/upm/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ See GitHub for the full license.
return -1;
}

if (!fs::is_directory(Constants::UPM_ROOT)) {
fs::create_directories(Constants::UPM_ROOT);
if (!std::filesystem::is_directory(Constants::UPM_ROOT)) {
std::filesystem::create_directories(Constants::UPM_ROOT);
}

resolvePackageContext(input[0]);
Expand Down Expand Up @@ -284,10 +284,10 @@ void Context::disable() {
}
for (auto& sourceDest : *it) {
auto symlinkDest = sourceDest.at("target");
fs::path castPath = fs::path(symlinkDest.get<std::string>());
std::filesystem::path castPath = std::filesystem::path(symlinkDest.get<std::string>());
if (isGoodSymlink(castPath)) {
spdlog::info("Unlinking {}", castPath.string());
fs::remove(castPath);
std::filesystem::remove(castPath);
} else {
spdlog::error("Skipped bad entry: {}", castPath.string());
}
Expand Down Expand Up @@ -325,7 +325,7 @@ std::string Context::locateFile(const std::string& packageName) {
for (auto& dir : dirs) {
// TODO: this is where alias cache lookup goes
auto path = dir / (packageName + ".lua");
if (fs::is_regular_file(path)) {
if (std::filesystem::is_regular_file(path)) {
return path;
}
}
Expand All @@ -334,22 +334,22 @@ std::string Context::locateFile(const std::string& packageName) {

bool Context::checkInstalled() {
auto prefix = getPrefix();
bool isInstalled = fs::is_directory(prefix);
bool isInstalled = std::filesystem::is_directory(prefix);
// TODO: use isInstalled + config to check whether or not the existing directory should be removed
return isInstalled;
}

std::string Context::getPrefix() {
fs::path root = Constants::UPM_ROOT / "packages";
std::filesystem::path root = Constants::UPM_ROOT / "packages";

root /= package + "-" + (
resolvedPackageVersion.empty() ? packageVersion : resolvedPackageVersion
);
return root.string();
}

std::vector<fs::path> Context::getLuaLookupDirectory() {
std::vector<fs::path> res;
std::vector<std::filesystem::path> Context::getLuaLookupDirectory() {
std::vector<std::filesystem::path> res;
#ifdef UPM_DEBUG
// If built as debug, make sure ./lua is included first in the search path.
res.push_back("./lua/upm");
Expand Down
2 changes: 1 addition & 1 deletion src/upm/Context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class Context {
bool checkInstalled();

std::string getPrefix();
std::vector<fs::path> getLuaLookupDirectory();
std::vector<std::filesystem::path> getLuaLookupDirectory();

std::string locateFile(const std::string& packageName);

Expand Down
25 changes: 12 additions & 13 deletions src/upm/api/Filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

#include <cstdlib>
#include <filesystem>
#include <stc/FS.hpp>
#include <stc/Environment.hpp>
#include <stc/StringUtil.hpp>

Expand All @@ -23,34 +22,34 @@ int upmfilesystem_exists(lua_State* state) {

auto type = luaL_optinteger(state, 2, upm::filesystem::TYPE_ANY);

auto path = fs::path(luaL_checklstring(state, 1, nullptr));
auto path = std::filesystem::path(luaL_checklstring(state, 1, nullptr));
switch (type) {
case upm::filesystem::TYPE_ANY:
lua_pushboolean(state,
static_cast<int>(fs::exists(path)));
static_cast<int>(std::filesystem::exists(path)));
break;
case upm::filesystem::TYPE_DIRECTORY:
lua_pushboolean(state,
static_cast<int>(fs::is_directory(path)));
static_cast<int>(std::filesystem::is_directory(path)));
break;
case upm::filesystem::TYPE_FILE:
// Not sure if is_regular_file is good enough. Using exists && !is_directory _might_ be better, but
// I'm not sure
lua_pushboolean(state,
static_cast<int>(fs::is_regular_file(path)));
static_cast<int>(std::filesystem::is_regular_file(path)));
break;
default:
return luaL_error(state, "You supplied an invalid value for the type. Please use fs.TYPE_ANY, fs.TYPE_DIRECTORY, or fs.TYPE_FILE");
}
lua_pushboolean(state,
static_cast<int>(fs::exists(fs::path(luaL_checklstring(state, 1, nullptr))))
static_cast<int>(std::filesystem::exists(std::filesystem::path(luaL_checklstring(state, 1, nullptr))))
);

return 1;
}

int upmfilesystem_pwd(lua_State* L) {
lua_pushstring(L, fs::current_path().string().c_str());;
lua_pushstring(L, std::filesystem::current_path().string().c_str());;

return 1;
}
Expand Down Expand Up @@ -94,13 +93,13 @@ int upmfilesystem_sharedLibInstalled(lua_State* state) {
}

int upmfilesystem_installCopy(lua_State* state) {
fs::path source = luaL_checkstring(state, 1);
fs::path dest = upm::Context::inst->getPrefix();
if (!fs::is_directory(dest)) { fs::create_directories(dest);
std::filesystem::path source = luaL_checkstring(state, 1);
std::filesystem::path dest = upm::Context::inst->getPrefix();
if (!std::filesystem::is_directory(dest)) { std::filesystem::create_directories(dest);
}

// At least recursive copying is easy now
fs::copy(source, dest, fs::copy_options::recursive | fs::copy_options::overwrite_existing | fs::copy_options::copy_symlinks);
std::filesystem::copy(source, dest, std::filesystem::copy_options::recursive | std::filesystem::copy_options::overwrite_existing | std::filesystem::copy_options::copy_symlinks);
return 0;
}

Expand Down Expand Up @@ -235,7 +234,7 @@ int upmfilesystem_makeInstallOnly(lua_State *state) {

// TODO: Replace with libarchive
int upmfilesystem_untar(lua_State* state) {
fs::path source = luaL_checkstring(state, 1);
std::filesystem::path source = luaL_checkstring(state, 1);
if (auto pos = source.string().find("/tmp/"); pos != 0) {
throw std::runtime_error("Invalid path; must be a path to /tmp/");
}
Expand All @@ -255,7 +254,7 @@ int upmfilesystem_untar(lua_State* state) {
tarArg += " -C " + dest.string();
upm::filesystem::logger->info("Unpacking {} to {}...", source.string(), dest.string());

fs::create_directories(dest);
std::filesystem::create_directories(dest);
auto res = std::system(("tar " + tarArg).c_str());
if (res != 0) {
return luaL_error(state, "Failed to untar");
Expand Down
7 changes: 3 additions & 4 deletions src/upm/api/Git.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "Git.hpp"

#include <stc/FS.hpp>
#include <stc/Environment.hpp>
#include <spdlog/spdlog.h>

Expand All @@ -15,12 +14,12 @@ int git_clone(lua_State* state) {
std::string dest = luaL_checklstring(state, 2, nullptr);
bool clean = (lua_gettop(state) >= 3 ? lua_toboolean(state, 3) : 0) != 0;
bool absPath = (lua_gettop(state) >= 4 ? lua_toboolean(state, 4) : 0) != 0;
fs::path p = absPath ? dest : ("/tmp/upm/" + dest);
std::filesystem::path p = absPath ? dest : ("/tmp/upm/" + dest);

if (fs::exists(p)) {
if (std::filesystem::exists(p)) {
if (clean) {
spdlog::info("Already cloned. Reset policy forces cache deletion...");
if (fs::remove_all(p) == 0u) {
if (std::filesystem::remove_all(p) == 0u) {
return luaL_error(state, ("Failed to delete " + p.string()).c_str());
}
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/upm/api/LuaHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "stdfunc/Activators.hpp"
#include "stdfunc/VersionResolvers.hpp"

#include <stc/FS.hpp>
#include <filesystem>

#include <iostream>

Expand All @@ -26,7 +26,7 @@ LuaHelper::LuaHelper() {
// but I'll eventually make a lock-based system for execution, similar
// to how apt does it, to prevent this from happening.
// Not sure how I'd go about it, however.
fs::create_directory("/tmp/upm");
std::filesystem::create_directory("/tmp/upm");

state = luaL_newstate();
}
Expand Down
9 changes: 4 additions & 5 deletions src/upm/api/Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
#include "lua.h"
#include "lua.hpp"

#include <iostream>
#include <stc/FS.hpp>
#include <filesystem>
#include <cpr/cpr.h>

// Do I really need this twice?
Expand Down Expand Up @@ -44,9 +43,9 @@ int upmnetwork_download(lua_State* state) {


auto name = url.substr(url.rfind('/') + 1);
fs::path tmpDir = fs::temp_directory_path();
fs::path p = tmpDir / ("upm/" + name);
if (fs::exists(p)) {
std::filesystem::path tmpDir = std::filesystem::temp_directory_path();
std::filesystem::path p = tmpDir / ("upm/" + name);
if (std::filesystem::exists(p)) {
spdlog::info("Cache entry found at {}.", p.string());
lua_pushinteger(state, 200);
lua_pushstring(state, p.string().c_str());
Expand Down
8 changes: 4 additions & 4 deletions src/upm/api/stdfunc/Activators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ int activators_activateSingle(lua_State *L) {
std::string qualifiedDest = dest[0] != '/' ? "/usr/local/" + dest : dest;


if (!fs::exists(src)) {
if (!std::filesystem::exists(src)) {
return luaL_error(L, (src + "doesn't exist").c_str());
}
spdlog::info("Linking {} -> {}", src, qualifiedDest);
Expand All @@ -50,11 +50,11 @@ int activators_activateSingle(lua_State *L) {
}
);

if (fs::exists(qualifiedDest)) {
fs::remove_all(qualifiedDest);
if (std::filesystem::exists(qualifiedDest)) {
std::filesystem::remove_all(qualifiedDest);
}

fs::create_symlink(src, qualifiedDest);
std::filesystem::create_symlink(src, qualifiedDest);

ctx.cfg.save();

Expand Down
4 changes: 2 additions & 2 deletions src/upm/conf/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace upm {
Config::Config(Context* ctx) : ctx(ctx) {
confPath = Constants::UPM_ROOT / ".upmrc";

if (!fs::exists(confPath)) {
if (!std::filesystem::exists(confPath)) {
spdlog::debug("No config found at " + confPath.string());
return;
}
Expand All @@ -28,7 +28,7 @@ Config::~Config() {
}

void Config::save() {
if (this->confPath.empty() || fs::is_directory(this->confPath)) {
if (this->confPath.empty() || std::filesystem::is_directory(this->confPath)) {
spdlog::warn("No confPath set; cannot save");
return;
}
Expand Down
4 changes: 2 additions & 2 deletions src/upm/conf/Config.hpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#pragma once

#include "nlohmann/json.hpp"
#include "stc/Environment.hpp"
#include <filesystem>

namespace upm {

class Context;
class Config {
private:
Context* ctx;
fs::path confPath;
std::filesystem::path confPath;

void setup();
public:
Expand Down
6 changes: 3 additions & 3 deletions src/upm/conf/Constants.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#pragma once

#include <filesystem>
#include <memory>
#include <vector>
#include <spdlog/spdlog.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/sinks/basic_file_sink.h>
#include "stc/FS.hpp"

namespace upm::Constants {
static inline std::vector<spdlog::sink_ptr> sinks = {
Expand All @@ -15,7 +15,7 @@ static inline std::vector<spdlog::sink_ptr> sinks = {

const static auto inline logger = std::make_shared<spdlog::logger>("upm", sinks.begin(), sinks.end());

const static fs::path UPM_ROOT("/opt/upm/");
const static fs::path APPLY_ROOT("/usr/local/");
const static std::filesystem::path UPM_ROOT("/opt/upm/");
const static std::filesystem::path APPLY_ROOT("/usr/local/");

}
Loading

0 comments on commit 3db0097

Please sign in to comment.