From 67a7e3876c5909828c3cd7d76a86cfcf54d037c8 Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Tue, 24 Nov 2026 17:53:46 +0100 Subject: [PATCH] fix(files): a new layer isn't made over a file there in another case, and the review's smaller points - node_layers::create() refuses a name a file is there with in any case: on Linux path() wrote over it. - A name is marked as logged only once the file is known to be there. - The log line says the file is on disk in another case; the folders FileOnDiskToWrite() looks up aren't logged. - The editor start reads the layers, and the heightmap terrain its terrain.yaml and pack files, as they're named on the disk. - The write test compares the names exactly; a test of the log line once per name, with the lines kept by the test log stub. --- application/editormode.cpp | 3 ++- editor/editorTerrainStreamer.cpp | 8 +++--- scene/scenelayers.cpp | 3 ++- tests/unit/logcapture.h | 25 +++++++++++++++++ tests/unit/test_utilities.cpp | 38 +++++++++++++++++++++++--- utilities/utilities.cpp | 46 +++++++++++++++++++++++--------- 6 files changed, 102 insertions(+), 21 deletions(-) create mode 100644 tests/unit/logcapture.h diff --git a/application/editormode.cpp b/application/editormode.cpp index 7c9231d2b..e5144b410 100644 --- a/application/editormode.cpp +++ b/application/editormode.cpp @@ -8684,7 +8684,8 @@ void editor_mode::starter_trainset(std::vector &Trailing) auto const &layer{scene::Layers.layer(static_cast(handle))}; if (layer.created) continue; - std::ifstream input{Global.asCurrentSceneryPath + layer.name, std::ios_base::binary}; + // the file as it's named on the disk, as node_layers::path() reads it + std::ifstream input{FileOnDisk(Global.asCurrentSceneryPath + layer.name), std::ios_base::binary}; std::string const content{std::istreambuf_iterator(input), std::istreambuf_iterator()}; if (std::regex_search(content, trainset)) return; diff --git a/editor/editorTerrainStreamer.cpp b/editor/editorTerrainStreamer.cpp index 647d44906..d887b14b9 100644 --- a/editor/editorTerrainStreamer.cpp +++ b/editor/editorTerrainStreamer.cpp @@ -100,7 +100,8 @@ terrain_streamer::~terrain_streamer() std::string terrain_streamer::pack_path(heightmap::pack_key const &Key) const { - return m_directory + heightmap::pack_file_name(Key); + // read and written as it's named on the disk + return FileOnDisk(m_directory + heightmap::pack_file_name(Key)); } bool terrain_streamer::open(std::string const &Name) @@ -111,7 +112,7 @@ bool terrain_streamer::open(std::string const &Name) m_directory = FileOnDiskToWrite("terrain/" + Name) + "/"; m_manifest = heightmap::manifest(); m_manifestchanged = false; - auto const manifestpath{m_directory + "terrain.yaml"}; + auto const manifestpath{FileOnDisk(m_directory + "terrain.yaml")}; std::error_code ec; if (std::filesystem::exists(manifestpath, ec)) { @@ -2308,7 +2309,8 @@ bool terrain_streamer::save(std::string *Error) return false; }; std::error_code ec; - auto const manifestpath{m_directory + "terrain.yaml"}; + // written over as it's named on the disk, not joined by a second one next to it + auto const manifestpath{FileOnDisk(m_directory + "terrain.yaml")}; if (m_manifestchanged || modified() || false == std::filesystem::exists(manifestpath, ec)) { std::string error; diff --git a/scene/scenelayers.cpp b/scene/scenelayers.cpp index 95cedb315..e7a72f3a9 100644 --- a/scene/scenelayers.cpp +++ b/scene/scenelayers.cpp @@ -618,7 +618,8 @@ layer_handle node_layers::create(std::string Name, layer_handle Parent, std::str return null_handle; } auto const taken{std::any_of(std::begin(m_layers), std::end(m_layers), [&](basic_layer const &Layer) { return Layer.name == Name && false == Layer.dead; })}; - if (taken || FileExists(Global.asCurrentSceneryPath + Name)) + // a file there in another case counts as well, path() would write over it + if (taken || false == FileCased(Global.asCurrentSceneryPath + Name).empty()) { Reason = format(STR_C("the file \"%s\" already exists"), Name.c_str()); return null_handle; diff --git a/tests/unit/logcapture.h b/tests/unit/logcapture.h new file mode 100644 index 000000000..393f74e98 --- /dev/null +++ b/tests/unit/logcapture.h @@ -0,0 +1,25 @@ +#pragma once + +#include +#include + +// engine_stubs.cpp: keeps every line logged from now on (true), or stops keeping them and forgets them (false), and +// the lines kept so far +void unit_tests_log_capture(bool Enabled); +std::vector unit_tests_logged_lines(); + +namespace test { + +// keeps the lines written to the log while it's alive, so a test can tell what was logged. one at a time +class log_capture { + public: + log_capture() { unit_tests_log_capture(true); } + ~log_capture() { unit_tests_log_capture(false); } + log_capture(log_capture const &) = delete; + log_capture &operator=(log_capture const &) = delete; + + // the lines written so far + std::vector lines() const { return unit_tests_logged_lines(); } +}; + +} // namespace test diff --git a/tests/unit/test_utilities.cpp b/tests/unit/test_utilities.cpp index a6cac5410..13924cf25 100644 --- a/tests/unit/test_utilities.cpp +++ b/tests/unit/test_utilities.cpp @@ -1,5 +1,6 @@ #include "stdafx.h" #include "doctest.h" +#include "logcapture.h" #include "scratch.h" #include @@ -719,19 +720,25 @@ TEST_CASE("utilities: a terrain file is found in another case, by its text or by TEST_CASE("utilities: a file is written over in another case, or made in its folders as they're named on the disk") { test::scratch_folder const folder{"utilities_cased_write"}; folder.write("Scenery/Braniewo/Base.scm", "x"); + // on Windows the names are given back as asked, the file system finding them in any case; elsewhere as they're on the disk +#ifdef _WIN32 + bool const cased{false}; +#else + bool const cased{true}; +#endif // a file there in another case: written under its name on the disk, not joined by a second one next to it auto const existing{FileOnDiskToWrite(folder.path("scenery/braniewo/base.scm"))}; - CHECK(ToLower(existing) == ToLower(folder.path("Scenery/Braniewo/Base.scm"))); + CHECK(existing == folder.path(cased ? "Scenery/Braniewo/Base.scm" : "scenery/braniewo/base.scm")); CHECK(std::filesystem::exists(existing)); // a new file: the name given, in the folders as they're on the disk, so no second folder is made next to the one there auto const added{FileOnDiskToWrite(folder.path("scenery/braniewo/nowa.scm"))}; - CHECK(ToLower(added) == ToLower(folder.path("Scenery/Braniewo/nowa.scm"))); + CHECK(added == folder.path(cased ? "Scenery/Braniewo/nowa.scm" : "scenery/braniewo/nowa.scm")); CHECK(added.ends_with("/nowa.scm")); CHECK(std::filesystem::is_directory(std::filesystem::path{added}.parent_path())); // folders missing in any case: named as given, to be made in the nearest one there is auto const deeper{FileOnDiskToWrite(folder.path("scenery/braniewo/teren/nowy/x.btf"))}; - CHECK(ToLower(deeper) == ToLower(folder.path("Scenery/Braniewo/teren/nowy/x.btf"))); + CHECK(deeper == folder.path(cased ? "Scenery/Braniewo/teren/nowy/x.btf" : "scenery/braniewo/teren/nowy/x.btf")); CHECK(deeper.ends_with("/teren/nowy/x.btf")); CHECK(std::filesystem::is_directory(std::filesystem::path{deeper}.parent_path().parent_path().parent_path())); // the folders written the Windows way as well @@ -742,6 +749,31 @@ TEST_CASE("utilities: a file is written over in another case, or made in its fol CHECK(FileOnDiskToWrite("eu07_unit_test_no_such_file.xyz") == "eu07_unit_test_no_such_file.xyz"); } +TEST_CASE("utilities: a file found only in another case is logged once, the folders of a file to write are not") { + test::scratch_folder const folder{"utilities_cased_log"}; + folder.write("Scenery/Braniewo/Base.scm", "x"); + test::log_capture const log; + + // asked for again, and to be written over + CHECK_FALSE(FileCased(folder.path("scenery/braniewo/base.scm")).empty()); + CHECK_FALSE(FileCased(folder.path("scenery/braniewo/base.scm")).empty()); + CHECK_FALSE(FileOnDiskToWrite(folder.path("scenery/braniewo/base.scm")).empty()); + // asked for in its case on the disk + CHECK_FALSE(FileCased(folder.path("Scenery/Braniewo/Base.scm")).empty()); + // a new file, in folders named in another case + CHECK_FALSE(FileOnDiskToWrite(folder.path("scenery/braniewo/nowa.scm")).empty()); + CHECK_FALSE(FileOnDiskToWrite(folder.path("scenery/braniewo/teren/nowa.scm")).empty()); + + auto const lines{log.lines()}; +#ifdef _WIN32 + // the file system finds the names in any case, the name asked for is the one given back: nothing to log + CHECK(lines.empty()); +#else + REQUIRE(lines.size() == 1); + CHECK(lines.front().find(folder.path("Scenery/Braniewo/Base.scm")) != std::string::npos); +#endif +} + TEST_CASE("utilities: the time a file was changed is given, zero for a file which isn't there") { test::scratch_folder const folder{"utilities_modified"}; auto const name{folder.write("fresh.txt", "x")}; diff --git a/utilities/utilities.cpp b/utilities/utilities.cpp index cc7012e30..233b7eb26 100644 --- a/utilities/utilities.cpp +++ b/utilities/utilities.cpp @@ -855,7 +855,11 @@ bool cased_folder_path(std::string const &Folder, std::string &Cased) } // namespace #endif -std::string FileCased(std::filesystem::path const &Filename) +namespace +{ + +// FileCased(), the file found only in another case written to the log when Report +std::string file_cased(std::filesystem::path const &Filename, [[maybe_unused]] bool const Report) { std::error_code error; if (std::filesystem::exists(Filename, error)) @@ -887,7 +891,6 @@ std::string FileCased(std::filesystem::path const &Filename) return {}; std::string cased; auto const asked{folder + file}; - bool report{false}; { std::scoped_lock const lock{cased_mutex}; if (false == cased_folder_path(folder, cased)) @@ -896,19 +899,42 @@ std::string FileCased(std::filesystem::path const &Filename) if (name.empty()) return {}; cased += name; - report = (cased != asked) && cased_reported().insert(asked).second; } if (false == std::filesystem::exists(cased, error)) return {}; - if (report) + if (Report && (cased != asked)) { - // the list for the authors of the data, to name the files as the sceneries and the vehicles ask for them - WriteLog("File \"" + asked + "\" found as \"" + cased + "\", named in another case", logtype::file); + // marked as reported once it's known to be there, and written to the log outside of the lock + std::unique_lock lock{cased_mutex}; + auto const first{cased_reported().insert(asked).second}; + lock.unlock(); + // the parser asks for the names in lowercase, so these are mostly the files not named in lowercase on the disk + if (first) + WriteLog("File \"" + asked + "\" is \"" + cased + "\" on disk, in another case", logtype::file); } return cased; #endif } +// FileOnDiskToWrite(), the file found only in another case written to the log when Report; the folders never are +std::string on_disk_to_write(std::string const &Filename, bool const Report) +{ + if (auto const cased{file_cased(Filename, Report)}; false == cased.empty()) + return cased; + // a new file: its folder as it's named on the disk, or the nearest one on the way to it which is there, the rest as given + auto const split{Filename.find_last_of("/\\")}; + if ((split == std::string::npos) || (split == 0)) + return Filename; + return on_disk_to_write(Filename.substr(0, split), false) + '/' + Filename.substr(split + 1); +} + +} // namespace + +std::string FileCased(std::filesystem::path const &Filename) +{ + return file_cased(Filename, true); +} + std::string FileOnDisk(std::string const &Filename) { auto cased{FileCased(Filename)}; @@ -917,13 +943,7 @@ std::string FileOnDisk(std::string const &Filename) std::string FileOnDiskToWrite(std::string const &Filename) { - if (auto const cased{FileCased(Filename)}; false == cased.empty()) - return cased; - // a new file: its folder as it's named on the disk, or the nearest one on the way to it which is there, the rest as given - auto const split{Filename.find_last_of("/\\")}; - if ((split == std::string::npos) || (split == 0)) - return Filename; - return FileOnDiskToWrite(Filename.substr(0, split)) + '/' + Filename.substr(split + 1); + return on_disk_to_write(Filename, true); } std::string FileStemOnDisk(std::string const &Folder, std::string const &Name, std::vector const &Extensions)