diff --git a/matroska/matroskacontainer.cpp b/matroska/matroskacontainer.cpp index 74485f6..003e138 100644 --- a/matroska/matroskacontainer.cpp +++ b/matroska/matroskacontainer.cpp @@ -13,10 +13,10 @@ #include #include - -#include +#include #include +#include #include #include #include @@ -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(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); diff --git a/mediafileinfo.cpp b/mediafileinfo.cpp index 7dace78..8dacce2 100644 --- a/mediafileinfo.cpp +++ b/mediafileinfo.cpp @@ -37,11 +37,11 @@ #include #include - -#include +#include #include #include +#include #include #include #include @@ -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(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(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 diff --git a/mp4/mp4container.cpp b/mp4/mp4container.cpp index 24d92b0..3c1554d 100644 --- a/mp4/mp4container.cpp +++ b/mp4/mp4container.cpp @@ -11,9 +11,9 @@ #include #include #include +#include -#include - +#include #include #include #include @@ -838,10 +838,12 @@ calculatePadding: // -> close stream before truncating outputStream.close(); // -> truncate file - if (truncate(BasicFileInfo::pathForOpen(fileInfo().path()).data(), static_cast(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);