Use `std::filesystem::resize_file` instead of POSIX function

* Avoid platform-specific code
* Fix build with MSVC
This commit is contained in:
Martchus 2023-02-01 14:05:32 +01:00
parent 937631e5c4
commit dc4e4082e0
3 changed files with 22 additions and 14 deletions

View File

@ -13,10 +13,10 @@
#include <c++utilities/conversion/stringbuilder.h>
#include <c++utilities/conversion/stringconversion.h>
#include <unistd.h>
#include <c++utilities/io/path.h>
#include <chrono>
#include <filesystem>
#include <functional>
#include <initializer_list>
#include <limits>
@ -1837,10 +1837,12 @@ void MatroskaContainer::internalMakeFile(Diagnostics &diag, AbortableProgressFee
// -> close stream before truncating
outputStream.close();
// -> truncate file
if (truncate(fileInfo().path().c_str(), static_cast<iostream::off_type>(newSize)) == 0) {
auto ec = std::error_code();
std::filesystem::resize_file(makeNativePath(fileInfo().path()), newSize, ec);
if (!ec) {
fileInfo().reportSizeChanged(newSize);
} else {
diag.emplace_back(DiagLevel::Critical, "Unable to truncate the file.", context);
diag.emplace_back(DiagLevel::Critical, "Unable to truncate the file: " + ec.message(), context);
}
// -> reopen the stream again
outputStream.open(fileInfo().path(), ios_base::in | ios_base::out | ios_base::binary);

View File

@ -37,11 +37,11 @@
#include <c++utilities/chrono/timespan.h>
#include <c++utilities/conversion/stringconversion.h>
#include <unistd.h>
#include <c++utilities/io/path.h>
#include <algorithm>
#include <cstdio>
#include <filesystem>
#include <functional>
#include <iomanip>
#include <ios>
@ -1599,10 +1599,12 @@ void MediaFileInfo::makeMp3File(Diagnostics &diag, AbortableProgressFeedback &pr
}
progress.updateStep("Removing ID3v1 tag ...");
stream().close();
if (truncate(BasicFileInfo::pathForOpen(path()).data(), static_cast<std::streamoff>(size() - 128)) == 0) {
auto ec = std::error_code();
std::filesystem::resize_file(makeNativePath(BasicFileInfo::pathForOpen(path())), size() - 128, ec);
if (!ec) {
reportSizeChanged(size() - 128);
} else {
diag.emplace_back(DiagLevel::Critical, "Unable to truncate file to remove ID3v1 tag.", context);
diag.emplace_back(DiagLevel::Critical, "Unable to truncate file to remove ID3v1 tag: " + ec.message(), context);
throw std::ios_base::failure("Unable to truncate file to remove ID3v1 tag.");
}
return;
@ -1844,10 +1846,12 @@ void MediaFileInfo::makeMp3File(Diagnostics &diag, AbortableProgressFeedback &pr
// -> prevent deferring final write operations
outputStream.close();
// -> truncate file
if (truncate(BasicFileInfo::pathForOpen(path()).data(), static_cast<streamoff>(newSize)) == 0) {
auto ec = std::error_code();
std::filesystem::resize_file(makeNativePath(BasicFileInfo::pathForOpen(path())), newSize, ec);
if (!ec) {
reportSizeChanged(newSize);
} else {
diag.emplace_back(DiagLevel::Critical, "Unable to truncate the file.", context);
diag.emplace_back(DiagLevel::Critical, "Unable to truncate the file: " + ec.message(), context);
}
} else {
// file is longer after the modification

View File

@ -11,9 +11,9 @@
#include <c++utilities/io/binaryreader.h>
#include <c++utilities/io/binarywriter.h>
#include <c++utilities/io/copy.h>
#include <c++utilities/io/path.h>
#include <unistd.h>
#include <filesystem>
#include <memory>
#include <numeric>
#include <tuple>
@ -838,10 +838,12 @@ calculatePadding:
// -> close stream before truncating
outputStream.close();
// -> truncate file
if (truncate(BasicFileInfo::pathForOpen(fileInfo().path()).data(), static_cast<iostream::off_type>(newSize)) == 0) {
auto ec = std::error_code();
std::filesystem::resize_file(makeNativePath(BasicFileInfo::pathForOpen(fileInfo().path())), newSize, ec);
if (!ec) {
fileInfo().reportSizeChanged(newSize);
} else {
diag.emplace_back(DiagLevel::Critical, "Unable to truncate the file.", context);
diag.emplace_back(DiagLevel::Critical, "Unable to truncate the file: " + ec.message(), context);
}
// -> reopen the stream again
outputStream.open(BasicFileInfo::pathForOpen(fileInfo().path()).data(), ios_base::in | ios_base::out | ios_base::binary);