mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-07-05 21:13:55 -07:00
Use QSaveFile in convertToCockatriceFormat() too
convertToCockatriceFormat() had the same data-loss pattern: QFile WriteOnly truncated the .cod, saveToFile_Native() always returns true, and the original file was then removed unconditionally -- so a full disk during conversion wrote a 0-byte .cod and then deleted the source deck. Switch to QSaveFile (write + atomic commit), remove the original only after a successful commit, and move the format check ahead of the file open so an already-Cockatrice or unsupported deck never truncates or deletes anything. Raised in review by ZeldaZach.
This commit is contained in:
parent
6ff8783265
commit
28740c0675
1 changed files with 35 additions and 32 deletions
|
|
@ -458,51 +458,54 @@ bool DeckLoader::convertToCockatriceFormat(LoadedDeck &deck)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Determine the format before touching any file, so an already-converted or
|
||||||
|
// unsupported deck never truncates or deletes anything.
|
||||||
|
switch (DeckFileFormat::getFormatFromName(fileName)) {
|
||||||
|
case DeckFileFormat::PlainText:
|
||||||
|
break;
|
||||||
|
case DeckFileFormat::Cockatrice:
|
||||||
|
qCInfo(DeckLoaderLog) << "File is already in Cockatrice format. No conversion needed.";
|
||||||
|
return true;
|
||||||
|
default:
|
||||||
|
qCWarning(DeckLoaderLog) << "Unsupported file format for conversion:" << fileName;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
// Change the file extension to .cod
|
// Change the file extension to .cod
|
||||||
QFileInfo fileInfo(fileName);
|
QFileInfo fileInfo(fileName);
|
||||||
QString newFileName = QDir::toNativeSeparators(fileInfo.path() + "/" + fileInfo.completeBaseName() + ".cod");
|
QString newFileName = QDir::toNativeSeparators(fileInfo.path() + "/" + fileInfo.completeBaseName() + ".cod");
|
||||||
|
|
||||||
// Open the new file for writing
|
// Use QSaveFile so a failed write (e.g. a full disk) cannot leave a 0-byte .cod
|
||||||
QFile file(newFileName);
|
// behind and then delete the original deck.
|
||||||
|
QSaveFile file(newFileName);
|
||||||
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
|
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
|
||||||
qCWarning(DeckLoaderLog) << "Failed to open file for writing:" << newFileName;
|
qCWarning(DeckLoaderLog) << "Failed to open file for writing:" << newFileName;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool result = false;
|
if (!deck.deckList.saveToFile_Native(&file)) {
|
||||||
|
file.cancelWriting();
|
||||||
// Perform file modifications based on the detected format
|
qCWarning(DeckLoaderLog) << "Failed to serialize deck for file:" << newFileName;
|
||||||
switch (DeckFileFormat::getFormatFromName(fileName)) {
|
return false;
|
||||||
case DeckFileFormat::PlainText:
|
|
||||||
// Save in Cockatrice's native format
|
|
||||||
result = deck.deckList.saveToFile_Native(&file);
|
|
||||||
break;
|
|
||||||
case DeckFileFormat::Cockatrice:
|
|
||||||
qCInfo(DeckLoaderLog) << "File is already in Cockatrice format. No conversion needed.";
|
|
||||||
result = true;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
qCWarning(DeckLoaderLog) << "Unsupported file format for conversion:" << fileName;
|
|
||||||
result = false;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
file.close();
|
if (!file.commit()) {
|
||||||
|
qCWarning(DeckLoaderLog) << "Failed to convert deck to " << newFileName << ":" << file.errorString();
|
||||||
// Delete the old file if conversion was successful
|
return false;
|
||||||
if (result) {
|
|
||||||
if (!QFile::remove(fileName)) {
|
|
||||||
qCWarning(DeckLoaderLog) << "Failed to delete original file:" << fileName;
|
|
||||||
} else {
|
|
||||||
qCInfo(DeckLoaderLog) << "Original file deleted successfully:" << fileName;
|
|
||||||
}
|
|
||||||
deck.lastLoadInfo = {
|
|
||||||
.fileName = newFileName,
|
|
||||||
.fileFormat = DeckFileFormat::Cockatrice,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
// Conversion succeeded: delete the original file.
|
||||||
|
if (!QFile::remove(fileName)) {
|
||||||
|
qCWarning(DeckLoaderLog) << "Failed to delete original file:" << fileName;
|
||||||
|
} else {
|
||||||
|
qCInfo(DeckLoaderLog) << "Original file deleted successfully:" << fileName;
|
||||||
|
}
|
||||||
|
deck.lastLoadInfo = {
|
||||||
|
.fileName = newFileName,
|
||||||
|
.fileFormat = DeckFileFormat::Cockatrice,
|
||||||
|
};
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void DeckLoader::printDeckListNode(QTextCursor *cursor, const InnerDecklistNode *node)
|
void DeckLoader::printDeckListNode(QTextCursor *cursor, const InnerDecklistNode *node)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue