diff --git a/src/lib/stat/file.hpp b/src/lib/stat/file.hpp index d6ae19fcc..4585605d3 100644 --- a/src/lib/stat/file.hpp +++ b/src/lib/stat/file.hpp @@ -78,6 +78,33 @@ namespace std::filesystem { fs::canonical(rawPath)) : rawPath; } + + + /** check if the denoted path \a p has at least the given permissions */ + inline bool + has_perm (fs::path const& p, fs::perms permissionMask) + { + return (fs::status(p).permissions() & permissionMask) == permissionMask; + } + + /** check if the owner has read permissions on the denoted file or directory */ + inline bool + can_read (fs::path const& p) + { + return has_perm (p, fs::perms::owner_read); + } + + inline bool + can_write (fs::path const& p) + { + return has_perm (p, fs::perms::owner_write); + } + + inline bool + can_exec (fs::path const& p) + { + return has_perm (p, fs::perms::owner_exec); + } }//(End)namespace fs diff --git a/src/lib/test/temp-dir.hpp b/src/lib/test/temp-dir.hpp index 8bd387791..fb0ba9b20 100644 --- a/src/lib/test/temp-dir.hpp +++ b/src/lib/test/temp-dir.hpp @@ -37,9 +37,12 @@ #include "lib/stat/file.hpp" #include "include/limits.hpp" #include "lib/format-string.hpp" +#include "lib/util.hpp" + //#include //#include //#include +#include #include //#include @@ -50,30 +53,12 @@ namespace test{ namespace error = lumiera::error; using util::_Fmt; + using util::isnil; + using std::string; namespace { Literal TEMPFILE_PREFIX = "Lux"; } - inline bool - has_perm (fs::path const& p, fs::perms permissionMask) - { - return (fs::status(p).permissions() & permissionMask) == permissionMask; - } - inline bool - can_read (fs::path const& p) - { - return has_perm (p, fs::perms::owner_read); - } - inline bool - can_write (fs::path const& p) - { - return has_perm (p, fs::perms::owner_write); - } - inline bool - can_exec (fs::path const& p) - { - return has_perm (p, fs::perms::owner_exec); - } /** @@ -101,9 +86,20 @@ namespace test{ } fs::path - makeFile() + makeFile (string name ="") { - UNIMPLEMENTED ("make temporary file"); + if (isnil (name)) + return establishNewFile (string{TEMPFILE_PREFIX}); + + auto newFile = loc_ / name; + if (fs::exists (newFile)) + return establishNewFile (name); + + std::ofstream{newFile}; + if (fs::exists (newFile) and fs::is_empty(newFile)) + return newFile; + // + throw error::Fatal{_Fmt{"Failed to create unique new file %s in TempDir."} % newFile}; } @@ -127,6 +123,26 @@ namespace test{ % LUMIERA_MAX_COMPETITION ,error::LUMIERA_ERROR_SAFETY_LIMIT }; } + + fs::path + establishNewFile (string prefix) + { + for (uint attempt=0; attempt : No diff --git a/tests/library/stat/file-support-test.cpp b/tests/library/stat/file-support-test.cpp new file mode 100644 index 000000000..b52e51eab --- /dev/null +++ b/tests/library/stat/file-support-test.cpp @@ -0,0 +1,124 @@ +/* + FileSupport(Test) - verify additional filesystem helpers + + Copyright (C) Lumiera.org + 2024, Hermann Vosseler + + 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 the Free Software Foundation; either version 2 of + the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +* *****************************************************/ + +/** @file file-support-test.cpp + ** unit test \ref FileSupport_test + */ + + +#include "lib/test/run.hpp" +#include "lib/test/test-helper.hpp" +#include "lib/test/temp-dir.hpp" +#include "lib/stat/file.hpp" +//#include "lib/time/timevalue.hpp" +//#include "lib/error.hpp" +//#include "lib/util-foreach.hpp" +#include "lib/format-cout.hpp" +#include "lib/test/diagnostic-output.hpp" + +//#include +#include +//#include +//#include + +//using util::for_each; +//using lumiera::Error; +//using lumiera::LUMIERA_ERROR_EXCEPTION; +//using lumiera::error::LUMIERA_ERROR_ASSERTION; +//using lib::time::TimeVar; +//using lib::time::Time; + +//using boost::algorithm::is_lower; +//using boost::algorithm::is_digit; +//using std::function; +//using std::string; + + +namespace lib { +namespace stat{ +namespace test{ + + using lib::test::TempDir; + + + + + /***************************************************************//** + * @test verify supplemental helper functions for file-handling support, + * provided to complement the C++ `` library. + * @see file.hpp + * @see TempDir_test + */ + class FileSupport_test : public Test + { + void + run (Arg) + { + simplifiedPermissionAccess(); + homedirectoryExpansion(); + } + + + void + simplifiedPermissionAccess() + { + TempDir temp; +SHOW_EXPR(temp) +SHOW_EXPR(fs::path{temp}) + fs::path f = temp.makeFile("Lumiera.nix"); +SHOW_EXPR(f); +SHOW_EXPR(fs::exists(f)); + std::ofstream out{f}; +SHOW_EXPR(fs::exists(f)); +SHOW_EXPR(fs::status(f).permissions()) +SHOW_EXPR(fs::has_perm(temp, fs::perms::owner_read)); +SHOW_EXPR(fs::has_perm(temp, fs::perms::owner_write)); +SHOW_EXPR(fs::has_perm(temp, fs::perms::owner_exec)); +SHOW_EXPR(fs::has_perm(temp, fs::perms::owner_all)); +SHOW_EXPR(fs::has_perm(temp, fs::perms::group_read)); +SHOW_EXPR(fs::has_perm(temp, fs::perms::group_write)); +SHOW_EXPR(fs::has_perm(temp, fs::perms::group_exec)); +SHOW_EXPR(fs::has_perm(temp, fs::perms::group_all)); +SHOW_EXPR(fs::has_perm(temp, fs::perms::others_read)); +SHOW_EXPR(fs::has_perm(temp, fs::perms::others_write)); +SHOW_EXPR(fs::has_perm(temp, fs::perms::others_exec)); +SHOW_EXPR(fs::has_perm(temp, fs::perms::others_all)); +SHOW_EXPR(fs::has_perm(temp, fs::perms::all)); +SHOW_EXPR(fs::can_read(temp)); +SHOW_EXPR(fs::can_write(temp)); +SHOW_EXPR(fs::can_exec(temp)); + } + + + + /** @test prints "sizeof()" including some type name. */ + void + homedirectoryExpansion () + { + } + }; + + LAUNCHER (FileSupport_test, "unit common"); + + +}}} // namespace lib::stat::test + diff --git a/tests/library/test/temp-dir-test.cpp b/tests/library/test/temp-dir-test.cpp index 604afd322..65df513f9 100644 --- a/tests/library/test/temp-dir-test.cpp +++ b/tests/library/test/temp-dir-test.cpp @@ -2,7 +2,7 @@ TempDir(Test) - verify automated temporary working directory Copyright (C) Lumiera.org - 2009, Hermann Vosseler + 2024, Hermann Vosseler This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as @@ -79,24 +79,6 @@ namespace test{ simpleUsage() { TempDir temp; -SHOW_EXPR(temp) -SHOW_EXPR(fs::path{temp}) -SHOW_EXPR(has_perm(temp, fs::perms::owner_read)); -SHOW_EXPR(has_perm(temp, fs::perms::owner_write)); -SHOW_EXPR(has_perm(temp, fs::perms::owner_exec)); -SHOW_EXPR(has_perm(temp, fs::perms::owner_all)); -SHOW_EXPR(has_perm(temp, fs::perms::group_read)); -SHOW_EXPR(has_perm(temp, fs::perms::group_write)); -SHOW_EXPR(has_perm(temp, fs::perms::group_exec)); -SHOW_EXPR(has_perm(temp, fs::perms::group_all)); -SHOW_EXPR(has_perm(temp, fs::perms::others_read)); -SHOW_EXPR(has_perm(temp, fs::perms::others_write)); -SHOW_EXPR(has_perm(temp, fs::perms::others_exec)); -SHOW_EXPR(has_perm(temp, fs::perms::others_all)); -SHOW_EXPR(has_perm(temp, fs::perms::all)); -SHOW_EXPR(can_read(temp)); -SHOW_EXPR(can_write(temp)); -SHOW_EXPR(can_exec(temp)); auto ff = temp.makeFile(); CHECK (fs::exists (ff)); CHECK (fs::is_empty (ff)); diff --git a/wiki/thinkPad.ichthyo.mm b/wiki/thinkPad.ichthyo.mm index 4b80f72cf..29a85b08a 100644 --- a/wiki/thinkPad.ichthyo.mm +++ b/wiki/thinkPad.ichthyo.mm @@ -57246,14 +57246,15 @@ - + + - - + + @@ -57266,8 +57267,7 @@ Verstehe die Doku so: wenn directory existiert ⟹ kein Fehler

- - +
@@ -57279,8 +57279,7 @@ if -1 is returned, no directory shall be created. - - + @@ -57331,8 +57330,7 @@ ⟹ JAGNI

- - +
@@ -57347,8 +57345,7 @@ die gestern implementierte Zufalls-Sequenz entropyGen.u64()

- - +
@@ -57379,6 +57376,9 @@ + + +