summaryrefslogtreecommitdiff
path: root/indra/llcommon/tests/llfile_test.cpp
diff options
context:
space:
mode:
authorRye <rye@alchemyviewer.org>2025-12-30 11:39:16 -0500
committerAndrey Kleshchev <117672381+akleshchev@users.noreply.github.com>2026-01-07 02:15:43 +0200
commit25dc8660efedecfd9f021142ac1e4ff3639e80d4 (patch)
tree3751a0f21917bbe3d460ae2b613c7e5070d55dae /indra/llcommon/tests/llfile_test.cpp
parentb74a3c25d16685b8dfad8880e20ac99c5769d2e4 (diff)
Clean up LLFile and LLFilesystem operations to reduce temporaries allocations and utf8->utf16 conversions
Fix LLFile::tmpdir failing when temp directory is located in a unicode path on windows Add Tracy profiler tagging to LLFile that's disabled by default Replace LLFile::utf8StringToWstring with our fsyspath std::filesystem::path adapter Introduce std::filesystem::path to llifstream/llofstream wrappers Add move operators to fsyspath to match copy operator Restore LLUniqueFile RAII class for direct c file io wrapping and reducing merge conflicts with downstream code that may depend upon it Signed-off-by: Rye <rye@alchemyviewer.org>
Diffstat (limited to 'indra/llcommon/tests/llfile_test.cpp')
-rw-r--r--indra/llcommon/tests/llfile_test.cpp115
1 files changed, 54 insertions, 61 deletions
diff --git a/indra/llcommon/tests/llfile_test.cpp b/indra/llcommon/tests/llfile_test.cpp
index f1cee06856..ab4616cfb2 100644
--- a/indra/llcommon/tests/llfile_test.cpp
+++ b/indra/llcommon/tests/llfile_test.cpp
@@ -33,14 +33,13 @@
namespace tut
{
- static void clear_entire_dir(const std::string &dir)
+ static void clear_entire_dir(const std::filesystem::path& dir_path)
{
std::error_code ec;
- std::filesystem::path dir_path = LLFile::utf8StringToPath(dir);
std::filesystem::remove_all(dir_path, ec);
}
- static std::filesystem::path append_filename(const std::filesystem::path& dir, const std::string& element)
+ static std::filesystem::path append_filename(const std::filesystem::path& dir, const std::u8string& element)
{
std::filesystem::path path = dir;
return path.append(element);
@@ -48,13 +47,13 @@ namespace tut
static std::filesystem::path get_testdir(const std::filesystem::path& tempdir)
{
- return append_filename(tempdir, std::string("test_dir"));
+ return append_filename(tempdir, std::u8string(u8"test_dir"));
}
static std::filesystem::path get_testdir_unicode(const std::filesystem::path& tempdir)
{
// Example Unicode directory name: "test_ユニコード_dir"
- return append_filename(tempdir, std::string("test_\xE3\x83\xA6\xE3\x83\x8B\xE3\x82\xB3\xE3\x83\xBC\xE3\x83\x89_dir"));
+ return append_filename(tempdir, std::u8string(u8"test_\xE3\x83\xA6\xE3\x83\x8B\xE3\x82\xB3\xE3\x83\xBC\xE3\x83\x89_dir"));
}
struct llfile_test
@@ -72,46 +71,46 @@ namespace tut
// Test creating directories and files and deleting them and checking if the
// relevant status functions work as expected
ensure("LLFile::tmpdir() empty", !tempdir.empty());
- ensure("LLFile::tmpdir() doesn't exist", LLFile::exists(tempdir.string()));
- ensure("LLFile::tmpdir() is not a directory", LLFile::isdir(tempdir.string()));
- ensure("LLFile::tmpdir() should not be a file", !LLFile::isfile(tempdir.string()));
+ ensure("LLFile::tmpdir() doesn't exist", LLFile::exists(tempdir));
+ ensure("LLFile::tmpdir() is not a directory", LLFile::isdir(tempdir));
+ ensure("LLFile::tmpdir() should not be a file", !LLFile::isfile(tempdir));
// Make sure there is nothing left from a previous test run
- clear_entire_dir(testdir.string());
- ensure("llfile_test should not exist anymore", !LLFile::exists(testdir.string()));
+ clear_entire_dir(testdir);
+ ensure("llfile_test should not exist anymore", !LLFile::exists(testdir));
- int rc = LLFile::mkdir(testdir.string());
+ int rc = LLFile::mkdir(testdir);
ensure("LLFile::mkdir() failed", rc == 0);
- ensure("llfile_test should be a directory", LLFile::isdir(testdir.string()));
- rc = LLFile::mkdir(testdir.string());
+ ensure("llfile_test should be a directory", LLFile::isdir(testdir));
+ rc = LLFile::mkdir(testdir);
ensure("LLFile::mkdir() should not fail when the directory already exists", rc == 0);
std::filesystem::path testfile1 = testdir;
testfile1.append("llfile_test.dat");
- ensure("llfile_test1.dat should not yet exist", !LLFile::exists(testfile1.string()));
+ ensure("llfile_test1.dat should not yet exist", !LLFile::exists(testfile1));
const char* testdata = "testdata";
- S64 bytes = LLFile::write(testfile1.string(), testdata, 0, sizeof(testdata));
+ S64 bytes = LLFile::write(testfile1, testdata, 0, sizeof(testdata));
ensure("LLFile::write() did not write correctly", bytes == sizeof(testdata));
- rc = LLFile::remove(testfile1.string());
+ rc = LLFile::remove(testfile1);
ensure("LLFile::remove() for file test_file.dat", rc == 0);
- ensure("llfile_test.dat should not exist anymore", !LLFile::exists(testfile1.string()));
- ensure("llfile_test.dat should not be a file", !LLFile::isfile(testfile1.string()));
- ensure("llfile_test.dat should not be a directory", !LLFile::isdir(testfile1.string()));
- ensure("llfile_test.dat should not be a symlink", !LLFile::islink(testfile1.string()));
+ ensure("llfile_test.dat should not exist anymore", !LLFile::exists(testfile1));
+ ensure("llfile_test.dat should not be a file", !LLFile::isfile(testfile1));
+ ensure("llfile_test.dat should not be a directory", !LLFile::isdir(testfile1));
+ ensure("llfile_test.dat should not be a symlink", !LLFile::islink(testfile1));
- rc = LLFile::remove(testdir.string());
+ rc = LLFile::remove(testdir);
ensure("LLFile::remove() for directory llfile_test failed", rc == 0);
- ensure("llfile_test should not exist anymore", !LLFile::exists(testdir.string()));
+ ensure("llfile_test should not exist anymore", !LLFile::exists(testdir));
}
template<> template<>
void llfile_test_object_t::test<2>()
{
// High level static file IO functions to read and write data files
- LLFile::mkdir(testdir.string());
- ensure("llfile_test should exist", LLFile::isdir(testdir.string()));
+ LLFile::mkdir(testdir);
+ ensure("llfile_test should exist", LLFile::isdir(testdir));
std::filesystem::path testfile1 = testdir;
testfile1.append("llfile_test.dat");
@@ -119,42 +118,36 @@ namespace tut
std::string testdata1("testdata");
std::string testdata2("datateststuff");
std::time_t current = time(nullptr);
- S64 bytes = LLFile::write(testfile1.string(), testdata1.c_str(), 0, testdata1.length());
+ S64 bytes = LLFile::write(testfile1, testdata1.c_str(), 0, testdata1.length());
ensure("LLFile::write() did not write correctly", bytes == testdata1.length());
- ensure("llfile_test.dat should exist", LLFile::exists(testfile1.string()));
- ensure("llfile_test.dat should be a file", LLFile::isfile(testfile1.string()));
- ensure("llfile_test.dat should not be a directory", !LLFile::isdir(testfile1.string()));
+ ensure("llfile_test.dat should exist", LLFile::exists(testfile1));
+ ensure("llfile_test.dat should be a file", LLFile::isfile(testfile1));
+ ensure("llfile_test.dat should not be a directory", !LLFile::isdir(testfile1));
- bytes = LLFile::size(testfile1.string());
+ bytes = LLFile::size(testfile1);
ensure("LLFile::size() did not return the correct size", bytes == testdata1.length());
- std::string data = LLFile::getContents(testfile1.string());
+ std::string data = LLFile::getContents(testfile1);
ensure("LLFile::getContents() did not return the correct size data", data.length() == testdata1.length());
ensure_memory_matches("LLFile::getContents() did not read correct data", testdata1.c_str(), (U32)testdata1.length(), data.c_str(), (U32)data.length());
- std::time_t ctime = LLFile::getCreationTime(testfile1.string());
- ensure_approximately_equals_range("LLFile::getCreationTime() did not return correct time", (F32)(ctime - current), 0.f, 1);
-
- std::time_t mtime = LLFile::getModificationTime(testfile1.string());
- ensure_approximately_equals_range("LLFile::getModificationTime() did not return correct time", (F32)(mtime - current), 0.f, 1);
-
char buffer[1024];
- bytes = LLFile::read(testfile1.string(), buffer, 0, testdata1.length());
+ bytes = LLFile::read(testfile1, buffer, 0, testdata1.length());
ensure("LLFile:read() did not return the correct size", bytes == testdata1.length());
ensure_memory_matches("LLFile::read() did not read correct data", testdata1.c_str(), (U32)bytes, buffer, (U32)bytes);
// What if we try to read more data than there is in the file?
- bytes = LLFile::read(testfile1.string(), buffer, 0, bytes + 10);
+ bytes = LLFile::read(testfile1, buffer, 0, bytes + 10);
ensure("LLFile:read() did not correctly stop on eof", bytes == testdata1.length());
ensure_memory_matches("LLFile::read() did not read correct data", testdata1.c_str(), (U32)bytes, buffer, (U32)bytes);
// Let's append more data
- bytes = LLFile::write(testfile1.string(), testdata2.c_str(), -1, testdata2.length());
+ bytes = LLFile::write(testfile1, testdata2.c_str(), -1, testdata2.length());
ensure("LLFile::write() did not write correctly", bytes == testdata2.length());
- bytes = LLFile::size(testfile1.string());
+ bytes = LLFile::size(testfile1);
ensure("LLFile::size() did not return the correct size", bytes == testdata1.length() + testdata2.length());
- bytes = LLFile::read(testfile1.string(), buffer, 0, bytes);
+ bytes = LLFile::read(testfile1, buffer, 0, bytes);
ensure("LLFile:read() did not read correct number of bytes", bytes == testdata1.length() + testdata2.length());
ensure_memory_matches("LLFile:read() did not read correct testdata1", testdata1.c_str(), (U32)testdata1.length(), buffer, (U32)testdata1.length());
ensure_memory_matches("LLFile:read() did not read correct testdata2", testdata2.c_str(), (U32)testdata2.length(), buffer + testdata1.length(), (U32)testdata2.length());
@@ -176,7 +169,7 @@ namespace tut
}
std::error_code ec;
- LLFile fileout(testfile.string(), LLFile::out, ec);
+ LLFile fileout(testfile, LLFile::out, ec);
ensure("LLFile constructor did not open correctly", (bool)fileout);
ensure("error_code from LLFile constructor should not indicate an error", !ec);
if (fileout)
@@ -196,7 +189,7 @@ namespace tut
fileout.close();
}
- LLFile filein(testfile.string(), LLFile::in, ec);
+ LLFile filein(testfile, LLFile::in, ec);
ensure("LLFile constructor did not open correctly", (bool)filein);
ensure("error_code from LLFile constructor should not indicate an error", !ec);
if (filein)
@@ -236,16 +229,16 @@ namespace tut
testfile.append("llfile_test.bin");
std::error_code ec;
- LLFile file(testfile.string(), LLFile::out | LLFile::noreplace, ec);
+ LLFile file(testfile, LLFile::out | LLFile::noreplace, ec);
ensure("LLFile constructor should not have opened the already existing file", !file);
ensure("error_code from LLFile constructor should indicate an error", (bool)ec);
- LLFile::remove(testfile.string());
- file = LLFile(testfile.string(), LLFile::out | LLFile::app | LLFile::trunc, ec);
+ LLFile::remove(testfile);
+ file = LLFile(testfile, LLFile::out | LLFile::app | LLFile::trunc, ec);
ensure("LLFile constructor should not have opened the file with conflicting flags", !file);
ensure("error_code from LLFile constructor should indicate an error", (bool)ec);
- file = LLFile(testfile.string(), LLFile::out | LLFile::app | LLFile::noreplace, ec);
+ file = LLFile(testfile, LLFile::out | LLFile::app | LLFile::noreplace, ec);
ensure("LLFile constructor should not have opened the file with conflicting flags", !file);
ensure("error_code from LLFile constructor should indicate an error", (bool)ec);
@@ -253,7 +246,7 @@ namespace tut
testfile.append("llfile_test");
testfile.append("llfile_test.bin");
- file = LLFile(testfile.string(), LLFile::in, ec);
+ file = LLFile(testfile, LLFile::in, ec);
ensure("LLFile constructor should not have been able to open the file in the non-existing directory", !file);
ensure("error_code from LLFile constructor should indicate an error", (bool)ec);
}
@@ -270,37 +263,37 @@ namespace tut
testfile_unicode.append(unicode_filename);
// Clean up any previous test artifacts
- clear_entire_dir(testdir_unicode.string());
- ensure("Unicode test directory should not exist", !LLFile::exists(testdir_unicode.string()));
+ clear_entire_dir(testdir_unicode);
+ ensure("Unicode test directory should not exist", !LLFile::exists(testdir_unicode));
// Create the Unicode directory
- int rc = LLFile::mkdir(testdir_unicode.string());
+ int rc = LLFile::mkdir(testdir_unicode);
ensure("LLFile::mkdir() failed for Unicode directory", rc == 0);
- ensure("Unicode test directory should exist", LLFile::isdir(testdir_unicode.string()));
+ ensure("Unicode test directory should exist", LLFile::isdir(testdir_unicode));
- ensure("Unicode test file should not exist", !LLFile::exists(testfile_unicode.string()));
+ ensure("Unicode test file should not exist", !LLFile::exists(testfile_unicode));
// Write to the Unicode file
const char* testdata = "unicode_testdata";
- S64 bytes = LLFile::write(testfile_unicode.string(), testdata, 0, strlen(testdata));
- ensure("Unicode test file should exist", LLFile::exists(testfile_unicode.string()));
- ensure("Unicode test file should be a file", LLFile::isfile(testfile_unicode.string()));
+ S64 bytes = LLFile::write(testfile_unicode, testdata, 0, strlen(testdata));
+ ensure("Unicode test file should exist", LLFile::exists(testfile_unicode));
+ ensure("Unicode test file should be a file", LLFile::isfile(testfile_unicode));
ensure("LLFile::write() did not write correctly to Unicode file", bytes == (S64)strlen(testdata));
// Read back the data
char buffer[64] = {};
- bytes = LLFile::read(testfile_unicode.string(), buffer, 0, sizeof(buffer));
+ bytes = LLFile::read(testfile_unicode, buffer, 0, sizeof(buffer));
ensure("LLFile::read() did not read correctly from Unicode file", bytes == (S64)strlen(testdata));
ensure_memory_matches("LLFile::read() did not read correct Unicode data", testdata, (U32)bytes, buffer, (U32)bytes);
// Remove the file and directory
- rc = LLFile::remove(testfile_unicode.string());
+ rc = LLFile::remove(testfile_unicode);
ensure("LLFile::remove() failed for Unicode file", rc == 0);
- ensure("Unicode test file should not exist after removal", !LLFile::exists(testfile_unicode.string()));
+ ensure("Unicode test file should not exist after removal", !LLFile::exists(testfile_unicode));
- rc = LLFile::remove(testdir_unicode.string());
+ rc = LLFile::remove(testdir_unicode);
ensure("LLFile::remove() failed for Unicode directory", rc == 0);
- ensure("Unicode test directory should not exist after removal", !LLFile::exists(testdir_unicode.string()));
+ ensure("Unicode test directory should not exist after removal", !LLFile::exists(testdir_unicode));
}
} // namespace tut