Commit cb282090 authored by Michal Humpula's avatar Michal Humpula 🔥
Browse files

don't forget to truncate file if we are overwriting

REVIEW: 114724
BUG: 323693
parent 1ab4b698
......@@ -732,8 +732,9 @@ bool TextBuffer::save (const QString &filename)
/**
* try to open or fail
*/
if (!saveFile.open())
if (!saveFile.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
return false;
}
/**
* construct correct filter device and try to open
......@@ -747,7 +748,7 @@ bool TextBuffer::save (const QString &filename)
* try to open, if new file
*/
if (deleteFile) {
if (!file->open (QIODevice::WriteOnly)) {
if (!file->open (QIODevice::WriteOnly | QIODevice::Truncate)) {
delete file;
return false;
}
......
......@@ -412,3 +412,38 @@ void KateTextBufferTest::nestedFoldingTest()
QVERIFY(folding.unfoldRange (0));
QVERIFY(folding.unfoldRange (1));
}
void KateTextBufferTest::saveFileInUnwritableFolder()
{
const QString folder_name = QString("katetest_%1").arg(QCoreApplication::applicationPid());
const QString file_path = QDir::tempPath() + '/' + folder_name + "/foo";
Q_ASSERT(QDir::temp().mkdir(folder_name));
QFile f(file_path);
Q_ASSERT(f.open(QIODevice::WriteOnly | QIODevice::Truncate));
f.write("1234567890");
Q_ASSERT(f.flush());
f.close();
QFile::setPermissions(QDir::tempPath() + '/' + folder_name, QFile::ExeOwner);
Kate::TextBuffer buffer(0, 1);
buffer.setTextCodec(QTextCodec::codecForName("UTF-8"));
buffer.setFallbackTextCodec(QTextCodec::codecForName("UTF-8"));
bool a, b;
buffer.load(file_path, a, b, true);
buffer.clear();
buffer.startEditing();
buffer.insertText (KTextEditor::Cursor (0, 0), "ABC");
buffer.finishEditing();
qDebug() << buffer.text();
buffer.save(file_path);
f.open(QIODevice::ReadOnly);
QCOMPARE(f.readAll(), QByteArray("ABC"));
f.close();
QFile::setPermissions(QDir::tempPath() + '/' + folder_name, QFile::WriteOwner | QFile::ExeOwner);
Q_ASSERT(f.remove());
Q_ASSERT(QDir::temp().rmdir(folder_name));
}
......@@ -39,6 +39,7 @@ class KateTextBufferTest : public QObject
void cursorTest();
void foldingTest();
void nestedFoldingTest();
void saveFileInUnwritableFolder();
};
#endif // KATEBUFFERTEST_H
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment