flatten 20260225
This commit is contained in:
53
tjp/core/file/writeSegmentsToFile.cpp
Normal file
53
tjp/core/file/writeSegmentsToFile.cpp
Normal file
@@ -0,0 +1,53 @@
|
||||
// TJP COPYRIGHT HEADER
|
||||
|
||||
#include "writeSegmentsToFile.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <tjp/core/exception/Exception.hpp>
|
||||
#include <tjp/core/system/System.h>
|
||||
|
||||
#include <tjp/core/string/to_string.hpp>
|
||||
#include "../random/Random.h"
|
||||
|
||||
#include <utime.h>
|
||||
|
||||
namespace tjp {
|
||||
namespace core {
|
||||
namespace file {
|
||||
|
||||
bool writeSegmentsToFile(const StringView &path_, const StackArray<containers::MemorySegment<u8>> &segments, const WriteOptions &options)
|
||||
{
|
||||
auto path = String(path_);
|
||||
auto extension = "." + core::to_string(core::StandardRandom.next<u32>());
|
||||
auto temporary = path + extension;
|
||||
|
||||
std::ofstream file(temporary, std::ios::binary);
|
||||
if (!file)
|
||||
return false;
|
||||
|
||||
for (auto &contents: segments)
|
||||
file.write((char *)contents.data(), contents.size());
|
||||
|
||||
file.close();
|
||||
|
||||
remove(path.c_str());
|
||||
if (rename(temporary.c_str(), path.c_str()))
|
||||
return false;
|
||||
|
||||
if (options.lastModifiedTime)
|
||||
{
|
||||
setFileLastModifiedTime(path, options.lastModifiedTime);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
} // namespace
|
||||
} // namespace
|
||||
} // namespace
|
||||
Reference in New Issue
Block a user