cli: Allow preserving the modification time

See https://github.com/Martchus/tageditor/issues/66
This commit is contained in:
Martchus 2021-06-01 21:56:23 +02:00
parent 43eaf7f8db
commit 7ada28b06e
3 changed files with 19 additions and 1 deletions

View File

@ -81,6 +81,7 @@ SetTagInfoArgs::SetTagInfoArgs(Argument &filesArg, Argument &verboseArg)
{ "path 1", "path 2" })
, backupDirArg("temp-dir", '\0', "specifies the directory for temporary/backup files", { "path" })
, layoutOnlyArg("layout-only", 'l', "confirms layout-only changes")
, preserveModificationTimeArg("preserve-modification-time", '\0', "preserves the file's modification time")
, setTagInfoArg("set", 's', "sets the specified tag information and attachments")
{
docTitleArg.setRequiredValueCount(Argument::varValueCount);
@ -133,7 +134,7 @@ SetTagInfoArgs::SetTagInfoArgs(Argument &filesArg, Argument &verboseArg)
{ &valuesArg, &filesArg, &docTitleArg, &removeOtherFieldsArg, &treatUnknownFilesAsMp3FilesArg, &id3v1UsageArg, &id3v2UsageArg,
&id3InitOnCreateArg, &id3TransferOnRemovalArg, &mergeMultipleSuccessiveTagsArg, &id3v2VersionArg, &encodingArg, &removeTargetArg,
&addAttachmentArg, &updateAttachmentArg, &removeAttachmentArg, &removeExistingAttachmentsArg, &minPaddingArg, &maxPaddingArg,
&prefPaddingArg, &tagPosArg, &indexPosArg, &forceRewriteArg, &backupDirArg, &layoutOnlyArg, &verboseArg, &quietArg, &outputFilesArg });
&prefPaddingArg, &tagPosArg, &indexPosArg, &forceRewriteArg, &backupDirArg, &layoutOnlyArg, &preserveModificationTimeArg, &verboseArg, &quietArg, &outputFilesArg });
}
} // namespace Cli

View File

@ -51,6 +51,7 @@
#include <algorithm>
#include <cstring>
#include <filesystem>
#include <iomanip>
#include <iostream>
#include <memory>
@ -850,7 +851,14 @@ void setTagInfo(const SetTagInfoArgs &args)
}
// apply changes
auto modificationDateError = std::error_code();
auto modificationDate = std::filesystem::file_time_type();
auto modifiedFilePath = std::filesystem::path();
fileInfo.setSaveFilePath(currentOutputFile != noMoreOutputFiles ? string(*currentOutputFile) : string());
if (args.preserveModificationTimeArg.isPresent()) {
modifiedFilePath = fileInfo.saveFilePath().empty() ? fileInfo.path() : fileInfo.saveFilePath();
modificationDate = std::filesystem::last_write_time(modifiedFilePath, modificationDateError);
}
try {
// create handler for progress updates and aborting
auto applyProgress = quiet ? AbortableProgressFeedback() : AbortableProgressFeedback(logNextStep, logStepPercentage);
@ -872,6 +880,14 @@ void setTagInfo(const SetTagInfoArgs &args)
finalizeLog();
cerr << " - " << Phrases::Error << "Failed to apply changes." << Phrases::EndFlush;
}
if (args.preserveModificationTimeArg.isPresent()) {
if (!modificationDateError) {
std::filesystem::last_write_time(modifiedFilePath, modificationDate, modificationDateError);
}
if (modificationDateError) {
diag.emplace_back(DiagLevel::Critical, "Unable to preserve modification time: " + modificationDateError.message(), context);
}
}
} catch (const TagParser::Failure &) {
finalizeLog();
cerr << " - " << Phrases::Error << "A parsing failure occured when reading/writing the file \"" << file << "\"." << Phrases::EndFlush;

View File

@ -43,6 +43,7 @@ struct SetTagInfoArgs {
CppUtilities::ConfigValueArgument outputFilesArg;
CppUtilities::ConfigValueArgument backupDirArg;
CppUtilities::ConfigValueArgument layoutOnlyArg;
CppUtilities::ConfigValueArgument preserveModificationTimeArg;
CppUtilities::OperationArgument setTagInfoArg;
};