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:
parent
dcfc4752b5
commit
6689aa5210
@ -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();
|
||||
|
@ -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()
|
||||
{
|
||||
|
@ -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)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user