Use utils::file::exists instead of std::filesystem

This commit is contained in:
badaix 2021-12-18 22:48:39 +01:00
parent 98ccc8eded
commit 3f208d260e
5 changed files with 29 additions and 23 deletions

View file

@ -32,7 +32,6 @@
#include <unistd.h>
#endif
#include <filesystem>
#include <fstream>
#include <stdexcept>
#include <vector>
@ -44,13 +43,26 @@ namespace file
{
static bool exists(const std::string& filename)
{
if (filename.empty())
return false;
#ifdef WINDOWS
DWORD dwAttrib = GetFileAttributes(filename.c_str());
return (dwAttrib != INVALID_FILE_ATTRIBUTES);
#else
struct stat buffer;
return (stat(filename.c_str(), &buffer) == 0);
#endif
}
#ifndef WINDOWS
static void do_chown(const std::string& file_path, const std::string& user_name, const std::string& group_name)
{
if (user_name.empty() && group_name.empty())
return;
if (!std::filesystem::exists(file_path))
if (!utils::file::exists(file_path))
return;
uid_t uid = -1;

View file

@ -24,11 +24,9 @@
#include "common/aixlog.hpp"
#include "common/snap_exception.hpp"
#include "common/utils.hpp"
#include "common/utils/file_utils.hpp"
#include "common/utils/string_utils.hpp"
// standard headers
#include <filesystem>
using namespace std;
namespace streamreader
@ -222,7 +220,7 @@ void AirplayStream::do_disconnect()
{
ProcessStream::do_disconnect();
// Shairpot-sync created but does not remove the pipe
if (std::filesystem::exists(pipePath_) && (remove(pipePath_.c_str()) != 0))
if (utils::file::exists(pipePath_) && (remove(pipePath_.c_str()) != 0))
LOG(INFO, LOG_TAG) << "Failed to remove metadata pipe \"" << pipePath_ << "\": " << errno << "\n";
}
@ -283,10 +281,10 @@ void AirplayStream::initExeAndPath(const string& filename)
{
path_ = "";
exe_ = findExe(filename);
if (!std::filesystem::exists(exe_) || (exe_ == "/"))
if (!utils::file::exists(exe_) || (exe_ == "/"))
{
exe_ = findExe("shairport-sync");
if (!std::filesystem::exists(exe_))
if (!utils::file::exists(exe_))
throw SnapException("shairport-sync not found");
}

View file

@ -23,10 +23,10 @@
#include "common/aixlog.hpp"
#include "common/snap_exception.hpp"
#include "common/utils.hpp"
#include "common/utils/file_utils.hpp"
#include "common/utils/string_utils.hpp"
// standard headers
#include <filesystem>
#include <regex>
@ -90,10 +90,10 @@ void LibrespotStream::initExeAndPath(const std::string& filename)
{
path_ = "";
exe_ = findExe(filename);
if (!std::filesystem::exists(exe_) || (exe_ == "/"))
if (!utils::file::exists(exe_) || (exe_ == "/"))
{
exe_ = findExe("librespot");
if (!std::filesystem::exists(exe_))
if (!utils::file::exists(exe_))
throw SnapException("librespot not found");
}

View file

@ -24,12 +24,12 @@
#include "common/aixlog.hpp"
#include "common/snap_exception.hpp"
#include "common/utils.hpp"
#include "common/utils/file_utils.hpp"
#include "common/utils/string_utils.hpp"
// standard headers
#include <climits>
#include <cstdio>
#include <filesystem>
using namespace std;
@ -53,7 +53,7 @@ ProcessStream::ProcessStream(PcmStream::Listener* pcmListener, boost::asio::io_c
std::string ProcessStream::findExe(const std::string& filename) const
{
/// check if filename exists
if (std::filesystem::exists(filename))
if (utils::file::exists(filename))
return filename;
std::string exe = filename;
@ -90,7 +90,7 @@ void ProcessStream::initExeAndPath(const std::string& filename)
exe_ = exe_.substr(exe_.find_last_of('/') + 1);
}
if (!std::filesystem::exists(path_ + exe_))
if (!utils::file::exists(path_ + exe_))
throw SnapException("file not found: \"" + filename + "\"");
}

View file

@ -23,13 +23,11 @@
#include "common/aixlog.hpp"
#include "common/snap_exception.hpp"
#include "common/str_compat.hpp"
#include "common/utils/file_utils.hpp"
#include "common/utils/string_utils.hpp"
#include "encoder/encoder_factory.hpp"
// 3rd party headers
// standard headers
#include <filesystem>
#include <memory>
@ -141,14 +139,12 @@ void StreamControl::onLog(std::string message)
ScriptStreamControl::ScriptStreamControl(const net::any_io_executor& executor, const std::string& script) : StreamControl(executor), script_(script)
{
namespace fs = std::filesystem;
namespace fs = utils::file;
if (!fs::exists(script_))
{
fs::path plugin_path = "/usr/share/snapserver/plug-ins";
if (fs::exists(plugin_path / script_))
script_ = (plugin_path / script_).native();
else if (fs::exists(plugin_path / fs::path(script_).filename()))
script_ = (plugin_path / fs::path(script_).filename()).native();
std::string plugin_path = "/usr/share/snapserver/plug-ins/";
if (fs::exists(plugin_path + script_))
script_ = plugin_path + script_;
else
throw SnapException("Control script not found: \"" + script_ + "\"");
}