mirror of
https://github.com/luanti-org/luanti.git
synced 2025-12-23 15:05:29 +01:00
Clean up some filesys code
This commit is contained in:
138
src/filesys.cpp
138
src/filesys.cpp
@@ -62,46 +62,39 @@ std::vector<DirListNode> GetDirListing(const std::string &pathstring)
|
||||
{
|
||||
std::vector<DirListNode> listing;
|
||||
|
||||
WIN32_FIND_DATA FindFileData;
|
||||
HANDLE hFind = INVALID_HANDLE_VALUE;
|
||||
WIN32_FIND_DATA FindFileData{};
|
||||
DWORD dwError;
|
||||
|
||||
std::string dirSpec = pathstring + "\\*";
|
||||
|
||||
// Find the first file in the directory.
|
||||
hFind = FindFirstFile(dirSpec.c_str(), &FindFileData);
|
||||
HANDLE hFind = FindFirstFile(dirSpec.c_str(), &FindFileData);
|
||||
|
||||
if (hFind == INVALID_HANDLE_VALUE) {
|
||||
dwError = GetLastError();
|
||||
if (dwError != ERROR_FILE_NOT_FOUND && dwError != ERROR_PATH_NOT_FOUND) {
|
||||
errorstream << "GetDirListing: FindFirstFile error."
|
||||
<< " Error is " << dwError << std::endl;
|
||||
errorstream << "GetDirListing: FindFirstFile error code "
|
||||
<< dwError << std::endl;
|
||||
}
|
||||
} else {
|
||||
// NOTE:
|
||||
// Be very sure to not include '..' in the results, it will
|
||||
// result in an epic failure when deleting stuff.
|
||||
|
||||
DirListNode node;
|
||||
node.name = FindFileData.cFileName;
|
||||
node.dir = FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
|
||||
if (node.name != "." && node.name != "..")
|
||||
listing.push_back(node);
|
||||
|
||||
// List all the other files in the directory.
|
||||
while (FindNextFile(hFind, &FindFileData) != 0) {
|
||||
// do-while because the first file is already in the struct
|
||||
do {
|
||||
DirListNode node;
|
||||
node.name = FindFileData.cFileName;
|
||||
node.dir = FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
|
||||
if(node.name != "." && node.name != "..")
|
||||
listing.push_back(node);
|
||||
}
|
||||
listing.push_back(std::move(node));
|
||||
} while (FindNextFile(hFind, &FindFileData) != 0);
|
||||
|
||||
dwError = GetLastError();
|
||||
FindClose(hFind);
|
||||
if (dwError != ERROR_NO_MORE_FILES) {
|
||||
errorstream << "GetDirListing: FindNextFile error."
|
||||
<< " Error is " << dwError << std::endl;
|
||||
errorstream << "GetDirListing: FindNextFile error code "
|
||||
<< dwError << std::endl;
|
||||
listing.clear();
|
||||
return listing;
|
||||
}
|
||||
@@ -111,10 +104,8 @@ std::vector<DirListNode> GetDirListing(const std::string &pathstring)
|
||||
|
||||
bool CreateDir(const std::string &path)
|
||||
{
|
||||
bool r = CreateDirectory(path.c_str(), NULL);
|
||||
if(r == true)
|
||||
return true;
|
||||
if(GetLastError() == ERROR_ALREADY_EXISTS)
|
||||
bool r = CreateDirectory(path.c_str(), nullptr);
|
||||
if (r || GetLastError() == ERROR_ALREADY_EXISTS)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
@@ -182,25 +173,31 @@ bool RecursiveDelete(const std::string &path)
|
||||
return true;
|
||||
}
|
||||
|
||||
bool DeleteSingleFileOrEmptyDirectory(const std::string &path)
|
||||
bool DeleteSingleFileOrEmptyDirectory(const std::string &path, bool log_error)
|
||||
{
|
||||
if (!IsDir(path))
|
||||
return DeleteFile(path.c_str());
|
||||
return RemoveDirectory(path.c_str());
|
||||
if (!IsDir(path)) {
|
||||
bool ok = DeleteFile(path.c_str()) != 0;
|
||||
if (!ok && log_error)
|
||||
errorstream << "DeleteFile failed: " << LAST_OS_ERROR() << std::endl;
|
||||
}
|
||||
bool ok = RemoveDirectory(path.c_str()) != 0;
|
||||
if (!ok && log_error)
|
||||
errorstream << "RemoveDirectory failed: " << LAST_OS_ERROR() << std::endl;
|
||||
return ok;
|
||||
}
|
||||
|
||||
std::string TempPath()
|
||||
{
|
||||
DWORD bufsize = GetTempPath(0, NULL);
|
||||
if(bufsize == 0){
|
||||
errorstream<<"GetTempPath failed, error = "<<GetLastError()<<std::endl;
|
||||
DWORD bufsize = GetTempPath(0, nullptr);
|
||||
if (bufsize == 0) {
|
||||
errorstream << "GetTempPath failed: " << LAST_OS_ERROR() << std::endl;
|
||||
return "";
|
||||
}
|
||||
std::string buf;
|
||||
buf.resize(bufsize);
|
||||
DWORD len = GetTempPath(bufsize, &buf[0]);
|
||||
if(len == 0 || len > bufsize){
|
||||
errorstream<<"GetTempPath failed, error = "<<GetLastError()<<std::endl;
|
||||
if (len == 0 || len > bufsize) {
|
||||
errorstream << "GetTempPath failed: " << LAST_OS_ERROR() << std::endl;
|
||||
return "";
|
||||
}
|
||||
buf.resize(len);
|
||||
@@ -235,7 +232,7 @@ bool CopyFileContents(const std::string &source, const std::string &target)
|
||||
nullptr, COPY_FILE_ALLOW_DECRYPTED_DESTINATION);
|
||||
if (!ok) {
|
||||
errorstream << "copying " << source << " to " << target
|
||||
<< " failed: " << GetLastError() << std::endl;
|
||||
<< " failed: " << LAST_OS_ERROR() << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -265,13 +262,12 @@ std::vector<DirListNode> GetDirListing(const std::string &pathstring)
|
||||
std::vector<DirListNode> listing;
|
||||
|
||||
DIR *dp;
|
||||
struct dirent *dirp;
|
||||
if((dp = opendir(pathstring.c_str())) == NULL) {
|
||||
//infostream<<"Error("<<errno<<") opening "<<pathstring<<std::endl;
|
||||
if((dp = opendir(pathstring.c_str())) == nullptr) {
|
||||
return listing;
|
||||
}
|
||||
|
||||
while ((dirp = readdir(dp)) != NULL) {
|
||||
struct dirent *dirp;
|
||||
while ((dirp = readdir(dp)) != nullptr) {
|
||||
// NOTE:
|
||||
// Be very sure to not include '..' in the results, it will
|
||||
// result in an epic failure when deleting stuff.
|
||||
@@ -301,12 +297,13 @@ std::vector<DirListNode> GetDirListing(const std::string &pathstring)
|
||||
*/
|
||||
if(isdir == -1) {
|
||||
struct stat statbuf{};
|
||||
if (stat((pathstring + "/" + node.name).c_str(), &statbuf))
|
||||
if (stat((pathstring + DIR_DELIM + node.name).c_str(), &statbuf))
|
||||
continue;
|
||||
isdir = ((statbuf.st_mode & S_IFDIR) == S_IFDIR);
|
||||
}
|
||||
node.dir = isdir;
|
||||
listing.push_back(node);
|
||||
|
||||
listing.push_back(std::move(node));
|
||||
}
|
||||
closedir(dp);
|
||||
|
||||
@@ -315,7 +312,7 @@ std::vector<DirListNode> GetDirListing(const std::string &pathstring)
|
||||
|
||||
bool CreateDir(const std::string &path)
|
||||
{
|
||||
int r = mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
|
||||
int r = mkdir(path.c_str(), 0775);
|
||||
if (r == 0) {
|
||||
return true;
|
||||
}
|
||||
@@ -407,40 +404,35 @@ bool RecursiveDelete(const std::string &path)
|
||||
}
|
||||
}
|
||||
|
||||
bool DeleteSingleFileOrEmptyDirectory(const std::string &path)
|
||||
bool DeleteSingleFileOrEmptyDirectory(const std::string &path, bool log_error)
|
||||
{
|
||||
if (IsDir(path)) {
|
||||
bool did = (rmdir(path.c_str()) == 0);
|
||||
if (!did)
|
||||
errorstream << "rmdir errno: " << errno << ": " << strerror(errno)
|
||||
<< std::endl;
|
||||
bool did = rmdir(path.c_str()) == 0;
|
||||
if (!did && log_error)
|
||||
errorstream << "rmdir error: " << LAST_OS_ERROR() << std::endl;
|
||||
return did;
|
||||
}
|
||||
|
||||
bool did = (unlink(path.c_str()) == 0);
|
||||
if (!did)
|
||||
errorstream << "unlink errno: " << errno << ": " << strerror(errno)
|
||||
<< std::endl;
|
||||
bool did = unlink(path.c_str()) == 0;
|
||||
if (!did && log_error)
|
||||
errorstream << "unlink error: " << LAST_OS_ERROR() << std::endl;
|
||||
return did;
|
||||
}
|
||||
|
||||
std::string TempPath()
|
||||
{
|
||||
/*
|
||||
Should the environment variables TMPDIR, TMP and TEMP
|
||||
and the macro P_tmpdir (if defined by stdio.h) be checked
|
||||
before falling back on /tmp?
|
||||
|
||||
Probably not, because this function is intended to be
|
||||
compatible with lua's os.tmpname which under the default
|
||||
configuration hardcodes mkstemp("/tmp/lua_XXXXXX").
|
||||
*/
|
||||
|
||||
#ifdef __ANDROID__
|
||||
return porting::path_cache;
|
||||
#else
|
||||
const char *env_tmpdir = getenv("TMPDIR");
|
||||
if (env_tmpdir && env_tmpdir[0] == DIR_DELIM_CHAR)
|
||||
return env_tmpdir;
|
||||
#ifdef P_tmpdir
|
||||
return P_tmpdir;
|
||||
#else
|
||||
return DIR_DELIM "tmp";
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
std::string CreateTempFile()
|
||||
@@ -560,7 +552,7 @@ bool CopyFileContents(const std::string &source, const std::string &target)
|
||||
|
||||
void GetRecursiveDirs(std::vector<std::string> &dirs, const std::string &dir)
|
||||
{
|
||||
static const std::set<char> chars_to_ignore = { '_', '.' };
|
||||
constexpr std::string_view chars_to_ignore = "_.";
|
||||
if (dir.empty() || !IsDir(dir))
|
||||
return;
|
||||
dirs.push_back(dir);
|
||||
@@ -575,39 +567,23 @@ std::vector<std::string> GetRecursiveDirs(const std::string &dir)
|
||||
}
|
||||
|
||||
void GetRecursiveSubPaths(const std::string &path,
|
||||
std::vector<std::string> &dst,
|
||||
bool list_files,
|
||||
const std::set<char> &ignore)
|
||||
std::vector<std::string> &dst,
|
||||
bool list_files,
|
||||
std::string_view ignore)
|
||||
{
|
||||
std::vector<DirListNode> content = GetDirListing(path);
|
||||
for (const auto &n : content) {
|
||||
std::string fullpath = path + DIR_DELIM + n.name;
|
||||
if (ignore.count(n.name[0]))
|
||||
if (ignore.find(n.name[0]) != std::string_view::npos)
|
||||
continue;
|
||||
if (list_files || n.dir)
|
||||
dst.push_back(fullpath);
|
||||
// Note: this is probably vulnerable to a symlink infinite loop trap
|
||||
if (n.dir)
|
||||
GetRecursiveSubPaths(fullpath, dst, list_files, ignore);
|
||||
}
|
||||
}
|
||||
|
||||
bool RecursiveDeleteContent(const std::string &path)
|
||||
{
|
||||
infostream<<"Removing content of \""<<path<<"\""<<std::endl;
|
||||
std::vector<DirListNode> list = GetDirListing(path);
|
||||
for (const DirListNode &dln : list) {
|
||||
if(trim(dln.name) == "." || trim(dln.name) == "..")
|
||||
continue;
|
||||
std::string childpath = path + DIR_DELIM + dln.name;
|
||||
bool r = RecursiveDelete(childpath);
|
||||
if(!r) {
|
||||
errorstream << "Removing \"" << childpath << "\" failed" << std::endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CreateAllDirs(const std::string &path)
|
||||
{
|
||||
std::vector<std::string> tocreate;
|
||||
@@ -618,8 +594,8 @@ bool CreateAllDirs(const std::string &path)
|
||||
if (removed.empty())
|
||||
break;
|
||||
}
|
||||
for(int i=tocreate.size()-1;i>=0;i--)
|
||||
if(!CreateDir(tocreate[i]))
|
||||
for (auto it = tocreate.rbegin(); it != tocreate.rend(); ++it)
|
||||
if(!CreateDir(*it))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user