Skip to content

Commit 48ea33c

Browse files
committed
fix: resolve file save failure on SMB filesystem
- Replace file.remove() with atomic rename operation to avoid SMB file handle conflicts - Use hidden backup file (.filename.backup.uuid) during save process - Add error recovery to restore original file if write fails - Improve error logging for debugging save failures Log: resolve file save failure on SMB filesystem pms: BUG-351271
1 parent b1746b3 commit 48ea33c

1 file changed

Lines changed: 42 additions & 7 deletions

File tree

3rdparty/deepin-pdfium/src/dpdfdoc.cpp

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -267,19 +267,54 @@ bool DPdfDoc::save()
267267

268268
tempFile.close();
269269

270-
QFile file(d_func()->m_filePath);
270+
QString targetPath = d_func()->m_filePath;
271271

272-
file.remove(); //不remove会出现第二次导出丢失数据问题 (保存动作完成之后,如果当前文档是当初打开那个,下一次导出会出错)
272+
// Extract directory and filename to create hidden backup file
273+
QFileInfo fileInfo(targetPath);
274+
QString backupPath = fileInfo.absolutePath() + "/." + fileInfo.fileName() + ".backup." + QUuid::createUuid().toString();
273275

274-
if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate))
276+
QFile targetFile(targetPath);
277+
278+
// Rename original file to backup instead of deleting (avoids SMB file handle issues)
279+
if (targetFile.exists()) {
280+
if (!targetFile.rename(backupPath)) {
281+
qWarning() << "Failed to rename original file to backup:" << targetPath;
282+
return false;
283+
}
284+
}
285+
286+
// Write new file
287+
QFile newFile(targetPath);
288+
if (!newFile.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
289+
qWarning() << "Failed to open file for writing:" << targetPath;
290+
// Restore original file on failure
291+
if (QFile::exists(backupPath)) {
292+
QFile::rename(backupPath, targetPath);
293+
}
275294
return false;
295+
}
276296

277-
if (array.size() != file.write(array))
297+
if (array.size() != newFile.write(array)) {
298+
qWarning() << "Failed to write data to file:" << targetPath;
278299
result = false;
300+
newFile.close();
301+
newFile.remove();
302+
// Restore original file on failure
303+
if (QFile::exists(backupPath)) {
304+
QFile::rename(backupPath, targetPath);
305+
}
306+
return false;
307+
}
308+
309+
newFile.flush(); // Flush user buffer to kernel buffer
310+
fsync(newFile.handle()); // Sync kernel buffer to disk
311+
newFile.close();
312+
313+
// Remove backup file after successful write
314+
if (QFile::exists(backupPath)) {
315+
QFile::remove(backupPath);
316+
}
279317

280-
file.flush();//函数将用户缓存中的内容写入内核缓冲区
281-
fsync(file.handle());//将内核缓冲写入文件(磁盘)
282-
file.close();
283318
return result;
284319
}
285320

0 commit comments

Comments
 (0)