Use `std::filesystem::resize_file` instead of POSIX function
* Avoid platform-specific code * Fix build with MSVC
This commit is contained in:
parent
937631e5c4
commit
dc4e4082e0
|
@ -13,10 +13,10 @@
|
||||||
|
|
||||||
#include <c++utilities/conversion/stringbuilder.h>
|
#include <c++utilities/conversion/stringbuilder.h>
|
||||||
#include <c++utilities/conversion/stringconversion.h>
|
#include <c++utilities/conversion/stringconversion.h>
|
||||||
|
#include <c++utilities/io/path.h>
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
#include <filesystem>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <initializer_list>
|
#include <initializer_list>
|
||||||
#include <limits>
|
#include <limits>
|
||||||
|
@ -1837,10 +1837,12 @@ void MatroskaContainer::internalMakeFile(Diagnostics &diag, AbortableProgressFee
|
||||||
// -> close stream before truncating
|
// -> close stream before truncating
|
||||||
outputStream.close();
|
outputStream.close();
|
||||||
// -> truncate file
|
// -> 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);
|
fileInfo().reportSizeChanged(newSize);
|
||||||
} else {
|
} 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
|
// -> reopen the stream again
|
||||||
outputStream.open(fileInfo().path(), ios_base::in | ios_base::out | ios_base::binary);
|
outputStream.open(fileInfo().path(), ios_base::in | ios_base::out | ios_base::binary);
|
||||||
|
|
|
@ -37,11 +37,11 @@
|
||||||
|
|
||||||
#include <c++utilities/chrono/timespan.h>
|
#include <c++utilities/chrono/timespan.h>
|
||||||
#include <c++utilities/conversion/stringconversion.h>
|
#include <c++utilities/conversion/stringconversion.h>
|
||||||
|
#include <c++utilities/io/path.h>
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
#include <filesystem>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include <ios>
|
#include <ios>
|
||||||
|
@ -1599,10 +1599,12 @@ void MediaFileInfo::makeMp3File(Diagnostics &diag, AbortableProgressFeedback &pr
|
||||||
}
|
}
|
||||||
progress.updateStep("Removing ID3v1 tag ...");
|
progress.updateStep("Removing ID3v1 tag ...");
|
||||||
stream().close();
|
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);
|
reportSizeChanged(size() - 128);
|
||||||
} else {
|
} 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.");
|
throw std::ios_base::failure("Unable to truncate file to remove ID3v1 tag.");
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
|
@ -1844,10 +1846,12 @@ void MediaFileInfo::makeMp3File(Diagnostics &diag, AbortableProgressFeedback &pr
|
||||||
// -> prevent deferring final write operations
|
// -> prevent deferring final write operations
|
||||||
outputStream.close();
|
outputStream.close();
|
||||||
// -> truncate file
|
// -> 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);
|
reportSizeChanged(newSize);
|
||||||
} else {
|
} 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 {
|
} else {
|
||||||
// file is longer after the modification
|
// file is longer after the modification
|
||||||
|
|
|
@ -11,9 +11,9 @@
|
||||||
#include <c++utilities/io/binaryreader.h>
|
#include <c++utilities/io/binaryreader.h>
|
||||||
#include <c++utilities/io/binarywriter.h>
|
#include <c++utilities/io/binarywriter.h>
|
||||||
#include <c++utilities/io/copy.h>
|
#include <c++utilities/io/copy.h>
|
||||||
|
#include <c++utilities/io/path.h>
|
||||||
|
|
||||||
#include <unistd.h>
|
#include <filesystem>
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <numeric>
|
#include <numeric>
|
||||||
#include <tuple>
|
#include <tuple>
|
||||||
|
@ -838,10 +838,12 @@ calculatePadding:
|
||||||
// -> close stream before truncating
|
// -> close stream before truncating
|
||||||
outputStream.close();
|
outputStream.close();
|
||||||
// -> truncate file
|
// -> 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);
|
fileInfo().reportSizeChanged(newSize);
|
||||||
} else {
|
} 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
|
// -> reopen the stream again
|
||||||
outputStream.open(BasicFileInfo::pathForOpen(fileInfo().path()).data(), ios_base::in | ios_base::out | ios_base::binary);
|
outputStream.open(BasicFileInfo::pathForOpen(fileInfo().path()).data(), ios_base::in | ios_base::out | ios_base::binary);
|
||||||
|
|
Loading…
Reference in New Issue