Remove utils::file::exists

This commit is contained in:
badaix 2021-12-18 15:16:59 +01:00
parent da687c1585
commit 01e35e9004
5 changed files with 27 additions and 34 deletions

View file

@ -1,6 +1,6 @@
/*** /***
This file is part of snapcast This file is part of snapcast
Copyright (C) 2014-2020 Johannes Pohl Copyright (C) 2014-2021 Johannes Pohl
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
@ -16,15 +16,24 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
***/ ***/
#ifndef FILE_UTILS_H #ifndef FILE_UTILS_HPP
#define FILE_UTILS_H #define FILE_UTILS_HPP
// local headers
#include "string_utils.hpp" #include "string_utils.hpp"
#include <fstream>
// 3rd party headers
// standard headers
#ifndef WINDOWS #ifndef WINDOWS
#include <grp.h> #include <grp.h>
#include <pwd.h> #include <pwd.h>
#include <sys/stat.h>
#include <unistd.h>
#endif #endif
#include <filesystem>
#include <fstream>
#include <stdexcept> #include <stdexcept>
#include <vector> #include <vector>
@ -35,26 +44,13 @@ 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 #ifndef WINDOWS
static void do_chown(const std::string& file_path, const std::string& user_name, const std::string& group_name) 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()) if (user_name.empty() && group_name.empty())
return; return;
if (!exists(file_path)) if (!std::filesystem::exists(file_path))
return; return;
uid_t uid = -1; uid_t uid = -1;

View file

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

View file

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

View file

@ -28,6 +28,8 @@
// standard headers // standard headers
#include <climits> #include <climits>
#include <cstdio>
#include <filesystem>
using namespace std; using namespace std;
@ -48,17 +50,10 @@ ProcessStream::ProcessStream(PcmStream::Listener* pcmListener, boost::asio::io_c
} }
bool ProcessStream::fileExists(const std::string& filename) const
{
struct stat buffer;
return (stat(filename.c_str(), &buffer) == 0);
}
std::string ProcessStream::findExe(const std::string& filename) const std::string ProcessStream::findExe(const std::string& filename) const
{ {
/// check if filename exists /// check if filename exists
if (fileExists(filename)) if (std::filesystem::exists(filename))
return filename; return filename;
std::string exe = filename; std::string exe = filename;
@ -95,7 +90,7 @@ void ProcessStream::initExeAndPath(const std::string& filename)
exe_ = exe_.substr(exe_.find_last_of('/') + 1); exe_ = exe_.substr(exe_.find_last_of('/') + 1);
} }
if (!fileExists(path_ + exe_)) if (!std::filesystem::exists(path_ + exe_))
throw SnapException("file not found: \"" + filename + "\""); throw SnapException("file not found: \"" + filename + "\"");
} }

View file

@ -68,7 +68,6 @@ protected:
virtual void onStderrMsg(const std::string& line); virtual void onStderrMsg(const std::string& line);
virtual void initExeAndPath(const std::string& filename); virtual void initExeAndPath(const std::string& filename);
bool fileExists(const std::string& filename) const;
std::string findExe(const std::string& filename) const; std::string findExe(const std::string& filename) const;
size_t wd_timeout_sec_; size_t wd_timeout_sec_;