BinEditor: Make it work with remote files

For testing, start Creator with  bin/qtcreator docker://<imageid>/bin/ls

Change-Id: I438dde457b6bfb860939ad928fb4e563a3766aa9
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
hjk
2021-07-23 14:24:11 +02:00
parent 40aeab6849
commit 95c5a12532

View File

@@ -241,48 +241,51 @@ public:
return false; return false;
} }
OpenResult open(QString *errorString, const Utils::FilePath &filePath, OpenResult open(QString *errorString, const FilePath &filePath,
const Utils::FilePath &realFilePath) override const FilePath &realFilePath) override
{ {
QTC_CHECK(filePath == realFilePath); // The bineditor can do no autosaving QTC_CHECK(filePath == realFilePath); // The bineditor can do no autosaving
return openImpl(errorString, filePath); return openImpl(errorString, filePath);
} }
OpenResult openImpl(QString *errorString, const Utils::FilePath &filePath, quint64 offset = 0) OpenResult openImpl(QString *errorString, const FilePath &filePath, quint64 offset = 0)
{ {
QFile file(filePath.toString()); const qint64 size = filePath.fileSize();
if (file.open(QIODevice::ReadOnly)) { if (size < 0) {
file.close(); QString msg = tr("Cannot open %1: %2").arg(filePath.toUserOutput(), tr("File Error"));
quint64 size = static_cast<quint64>(file.size()); // FIXME: Was: file.errorString(), but we don't have a file anymore.
if (size == 0) { if (errorString)
QString msg = tr("The Binary Editor cannot open empty files."); *errorString = msg;
if (errorString) else
*errorString = msg; QMessageBox::critical(ICore::dialogParent(), tr("File Error"), msg);
else return OpenResult::ReadError;
QMessageBox::critical(ICore::dialogParent(), tr("File Error"), msg);
return OpenResult::CannotHandle;
}
if (size / 16 >= qint64(1) << 31) {
// The limit is 2^31 lines (due to QText* interfaces) * 16 bytes per line.
QString msg = tr("The file is too big for the Binary Editor (max. 32GB).");
if (errorString)
*errorString = msg;
else
QMessageBox::critical(ICore::dialogParent(), tr("File Error"), msg);
return OpenResult::CannotHandle;
}
if (offset >= size)
return OpenResult::CannotHandle;
setFilePath(filePath);
m_widget->setSizes(offset, file.size());
return OpenResult::Success;
} }
QString errStr = tr("Cannot open %1: %2").arg(filePath.toUserOutput(), file.errorString());
if (errorString) if (size == 0) {
*errorString = errStr; QString msg = tr("The Binary Editor cannot open empty files.");
else if (errorString)
QMessageBox::critical(ICore::dialogParent(), tr("File Error"), errStr); *errorString = msg;
return OpenResult::ReadError; else
QMessageBox::critical(ICore::dialogParent(), tr("File Error"), msg);
return OpenResult::CannotHandle;
}
if (size / 16 >= qint64(1) << 31) {
// The limit is 2^31 lines (due to QText* interfaces) * 16 bytes per line.
QString msg = tr("The file is too big for the Binary Editor (max. 32GB).");
if (errorString)
*errorString = msg;
else
QMessageBox::critical(ICore::dialogParent(), tr("File Error"), msg);
return OpenResult::CannotHandle;
}
if (offset >= size)
return OpenResult::CannotHandle;
setFilePath(filePath);
m_widget->setSizes(offset, size);
return OpenResult::Success;
} }
void provideData(quint64 address) void provideData(quint64 address)
@@ -290,21 +293,15 @@ public:
const FilePath fn = filePath(); const FilePath fn = filePath();
if (fn.isEmpty()) if (fn.isEmpty())
return; return;
QFile file(fn.toString()); const int blockSize = m_widget->dataBlockSize();
if (file.open(QIODevice::ReadOnly)) { QByteArray data = fn.fileContents(blockSize, address);
int blockSize = m_widget->dataBlockSize(); const int dataSize = data.size();
file.seek(address); if (dataSize != blockSize)
QByteArray data = file.read(blockSize); data += QByteArray(blockSize - dataSize, 0);
file.close(); m_widget->addData(address, data);
const int dataSize = data.size(); // QMessageBox::critical(ICore::dialogParent(), tr("File Error"),
if (dataSize != blockSize) // tr("Cannot open %1: %2").arg(
data += QByteArray(blockSize - dataSize, 0); // fn.toUserOutput(), file.errorString()));
m_widget->addData(address, data);
} else {
QMessageBox::critical(ICore::dialogParent(), tr("File Error"),
tr("Cannot open %1: %2").arg(
fn.toUserOutput(), file.errorString()));
}
} }
void provideNewRange(quint64 offset) void provideNewRange(quint64 offset)