forked from qt-creator/qt-creator
Utils: Introduce FilePath::fileSize()
And implement it locally and for docker. Change-Id: I362f4cae95560836d9300473a286429eb4569329 Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
@@ -1279,6 +1279,15 @@ bool FilePath::renameFile(const FilePath &target) const
|
||||
return QFile::rename(path(), target.path());
|
||||
}
|
||||
|
||||
qint64 FilePath::fileSize() const
|
||||
{
|
||||
if (needsDevice()) {
|
||||
QTC_ASSERT(s_deviceHooks.fileSize, return false);
|
||||
return s_deviceHooks.fileSize(*this);
|
||||
}
|
||||
return QFileInfo(m_data).size();
|
||||
}
|
||||
|
||||
QTextStream &operator<<(QTextStream &s, const FilePath &fn)
|
||||
{
|
||||
return s << fn.toString();
|
||||
|
||||
@@ -139,6 +139,7 @@ public:
|
||||
bool removeRecursively(QString *error = nullptr) const;
|
||||
bool copyFile(const FilePath &target) const;
|
||||
bool renameFile(const FilePath &target) const;
|
||||
qint64 fileSize() const;
|
||||
|
||||
Qt::CaseSensitivity caseSensitivity() const;
|
||||
|
||||
|
||||
@@ -86,6 +86,7 @@ public:
|
||||
std::function<QFile::Permissions(const FilePath &)> permissions;
|
||||
std::function<OsType(const FilePath &)> osType;
|
||||
std::function<Environment(const FilePath &)> environment;
|
||||
std::function<qint64(const FilePath &)> fileSize;
|
||||
};
|
||||
|
||||
class QTCREATOR_UTILS_EXPORT FileUtils {
|
||||
|
||||
@@ -1275,6 +1275,20 @@ FilePath DockerDevice::symLinkTarget(const FilePath &filePath) const
|
||||
return output.isEmpty() ? FilePath() : filePath.withNewPath(output);
|
||||
}
|
||||
|
||||
qint64 DockerDevice::fileSize(const FilePath &filePath) const
|
||||
{
|
||||
QTC_ASSERT(handlesFile(filePath), return -1);
|
||||
tryCreateLocalFileAccess();
|
||||
if (hasLocalFileAccess()) {
|
||||
const FilePath localAccess = mapToLocalAccess(filePath);
|
||||
LOG("File size? " << filePath.toUserOutput() << localAccess.toUserOutput() << localAccess.fileSize());
|
||||
return localAccess.fileSize();
|
||||
}
|
||||
|
||||
const QString output = d->outputForRunInShell({"stat", {"-c", "%s", filePath.path()}});
|
||||
return output.toLongLong();
|
||||
}
|
||||
|
||||
static FilePaths filterEntriesHelper(const FilePath &base,
|
||||
const QStringList &entries,
|
||||
const QStringList &nameFilters,
|
||||
|
||||
@@ -99,6 +99,7 @@ public:
|
||||
bool writeFileContents(const Utils::FilePath &filePath, const QByteArray &data) const override;
|
||||
QDateTime lastModified(const Utils::FilePath &filePath) const override;
|
||||
void runProcess(Utils::QtcProcess &process) const override;
|
||||
qint64 fileSize(const Utils::FilePath &filePath) const override;
|
||||
|
||||
Utils::Environment systemEnvironment() const override;
|
||||
|
||||
|
||||
@@ -190,6 +190,12 @@ QList<FilePath> DesktopDevice::directoryEntries(const FilePath &filePath,
|
||||
return Utils::transform(entryInfoList, &FilePath::fromFileInfo);
|
||||
}
|
||||
|
||||
qint64 DesktopDevice::fileSize(const FilePath &filePath) const
|
||||
{
|
||||
QTC_ASSERT(handlesFile(filePath), return -1);
|
||||
return filePath.fileSize();
|
||||
}
|
||||
|
||||
Environment DesktopDevice::systemEnvironment() const
|
||||
{
|
||||
return Environment::systemEnvironment();
|
||||
|
||||
@@ -79,6 +79,7 @@ public:
|
||||
Utils::FilePath symLinkTarget(const Utils::FilePath &filePath) const override;
|
||||
QByteArray fileContents(const Utils::FilePath &filePath, qint64 limit, qint64 offset) const override;
|
||||
bool writeFileContents(const Utils::FilePath &filePath, const QByteArray &data) const override;
|
||||
qint64 fileSize(const Utils::FilePath &filePath) const override;
|
||||
|
||||
protected:
|
||||
DesktopDevice();
|
||||
|
||||
@@ -525,6 +525,12 @@ DeviceManager::DeviceManager(bool isInstance) : d(std::make_unique<DeviceManager
|
||||
return device->systemEnvironment();
|
||||
};
|
||||
|
||||
deviceHooks.fileSize = [](const FilePath &filePath) {
|
||||
auto device = DeviceManager::deviceForPath(filePath);
|
||||
QTC_ASSERT(device, return qint64(-1));
|
||||
return device->fileSize(filePath);
|
||||
};
|
||||
|
||||
FileUtils::setDeviceFileHooks(deviceHooks);
|
||||
|
||||
DeviceProcessHooks processHooks;
|
||||
|
||||
@@ -409,6 +409,12 @@ Environment IDevice::systemEnvironment() const
|
||||
return Environment::systemEnvironment();
|
||||
}
|
||||
|
||||
qint64 IDevice::fileSize(const FilePath &filePath) const
|
||||
{
|
||||
QTC_CHECK(false);
|
||||
return -1;
|
||||
}
|
||||
|
||||
IDevice::~IDevice() = default;
|
||||
|
||||
/*!
|
||||
|
||||
@@ -267,6 +267,7 @@ public:
|
||||
virtual QFile::Permissions permissions(const Utils::FilePath &filePath) const;
|
||||
virtual void runProcess(Utils::QtcProcess &process) const;
|
||||
virtual Utils::Environment systemEnvironment() const;
|
||||
virtual qint64 fileSize(const Utils::FilePath &filePath) const;
|
||||
|
||||
virtual void aboutToBeRemoved() const {}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user