stream: add file::exists and file::readonly

We are targeting C++14, while C++17 already has std::filesystem::exists. 😢

Signed-off-by: Simon Rozman <simon@rozman.si>
This commit is contained in:
Simon Rozman 2023-09-20 13:00:15 +02:00
parent dcfc4752b5
commit 6689aa5210
3 changed files with 64 additions and 1 deletions

View File

@ -1,4 +1,4 @@
/*
/*
SPDX-License-Identifier: MIT
Copyright © 2023 Amebis
*/
@ -26,6 +26,7 @@ int main(int argc, const char * argv[])
UnitTests::stream::async();
UnitTests::stream::replicator();
UnitTests::stream::open_close();
UnitTests::stream::file_stat();
UnitTests::unicode::str2wstr();
UnitTests::unicode::wstr2str();
UnitTests::unicode::charset_encoder();

View File

@ -139,6 +139,13 @@ namespace UnitTests
std::filesystem::remove(filename[i]);
}
TEST_METHOD(file_stat)
{
sstring path(temp_path());
Assert::IsTrue(stdex::stream::file::exists(path));
Assert::IsFalse(stdex::stream::file::readonly(path));
}
protected:
static sstring temp_path()
{

View File

@ -2777,6 +2777,61 @@ namespace stdex
#endif
throw std::runtime_error("failed to set file mtime");
}
///
/// Checks if file/folder/symlink likely exists
///
/// \param[in] filename Filename
///
static bool exists(_In_z_ const stdex::schar_t* filename)
{
#ifdef _WIN32
return GetFileAttributes(filename) != INVALID_FILE_ATTRIBUTES;
#else
struct stat s;
return stat(filename, &s) == 0;
#endif
}
///
/// Checks if file/folder/symlink likely exists
///
/// \param[in] filename Filename
///
static inline bool exists(_In_ const stdex::sstring& filename)
{
return exists(filename.c_str());
}
///
/// Checks if file/folder/symlink is read-only
///
/// For inexisting or inaccessible paths, writeability is assumed.
///
/// \param[in] filename Filename
///
static bool readonly(_In_z_ const stdex::schar_t* filename)
{
#ifdef _WIN32
DWORD dwAttr = GetFileAttributes(filename);
return dwAttr != INVALID_FILE_ATTRIBUTES && (dwAttr & FILE_ATTRIBUTE_READONLY) != 0;
#else
struct stat s;
return stat(filename, &s) == 0 && (s.st_mode & (S_IWUSR|S_IWGRP|S_IWOTH)) == 0;
#endif
}
///
/// Checks if file/folder/symlink is read-only
///
/// For inexisting or inaccessible paths, writeability is assumed.
///
/// \param[in] filename Filename
///
static inline bool readonly(_In_ const stdex::sstring& filename)
{
return readonly(filename.c_str());
}
};
#pragma warning(pop)