From 6fcb97ad464a346663184b27d587668c59b54361 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Nicole?= Date: Fri, 27 Jul 2018 13:40:15 +0200 Subject: Use Key API in SinkSH MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: Depends on D14289 - Fixes the `sinksh inspect …` command - Introduces `isValid`, `isValidInternal` and `isValidDisplay` static functions in Key, Identifier and Revision I still have to do a more extensive search for induced bugs in other commands Reviewers: cmollekopf Reviewed By: cmollekopf Tags: #sink Differential Revision: https://phabricator.kde.org/D14404 --- common/storage/key.cpp | 76 +++++++++++++++++++++++++++++++++++++++++++++++++- common/storage/key.h | 18 ++++++++++-- 2 files changed, 90 insertions(+), 4 deletions(-) (limited to 'common') diff --git a/common/storage/key.cpp b/common/storage/key.cpp index cfeb016..215e155 100644 --- a/common/storage/key.cpp +++ b/common/storage/key.cpp @@ -84,6 +84,27 @@ bool Identifier::isNull() const return uid.isNull(); } +bool Identifier::isValidInternal(const QByteArray &bytes) +{ + return !QUuid::fromRfc4122(bytes).isNull(); +} + +bool Identifier::isValidDisplay(const QByteArray &bytes) +{ + return !QUuid(bytes).isNull(); +} + +bool Identifier::isValid(const QByteArray &bytes) +{ + switch (bytes.size()) { + case Identifier::INTERNAL_REPR_SIZE: + return isValidInternal(bytes); + case Identifier::DISPLAY_REPR_SIZE: + return isValidDisplay(bytes); + } + return false; +} + bool Identifier::operator==(const Identifier &other) const { return uid == other.uid; @@ -128,6 +149,27 @@ qint64 Revision::toQint64() const return rev; } +bool Revision::isValidInternal(const QByteArray &bytes) +{ + if (bytes.size() != Revision::INTERNAL_REPR_SIZE) { + return false; + } + + bool ok; + bytes.toLongLong(&ok); + return ok; +} + +bool Revision::isValidDisplay(const QByteArray &bytes) +{ + isValidInternal(bytes); +} + +bool Revision::isValid(const QByteArray &bytes) +{ + isValidInternal(bytes); +} + bool Revision::operator==(const Revision &other) const { return rev == other.rev; @@ -191,6 +233,39 @@ bool Key::isNull() const return id.isNull(); } +bool Key::isValidInternal(const QByteArray &bytes) +{ + if (bytes.size() != Key::INTERNAL_REPR_SIZE) { + return false; + } + + auto idBytes = bytes.mid(0, Identifier::INTERNAL_REPR_SIZE); + auto revBytes = bytes.mid(Identifier::INTERNAL_REPR_SIZE); + return Identifier::isValidInternal(idBytes) && Revision::isValidInternal(revBytes); +} + +bool Key::isValidDisplay(const QByteArray &bytes) +{ + if (bytes.size() != Key::DISPLAY_REPR_SIZE) { + return false; + } + + auto idBytes = bytes.mid(0, Identifier::DISPLAY_REPR_SIZE); + auto revBytes = bytes.mid(Identifier::DISPLAY_REPR_SIZE); + return Key::isValidDisplay(idBytes) && Revision::isValidDisplay(revBytes); +} + +bool Key::isValid(const QByteArray &bytes) +{ + switch (bytes.size()) { + case Key::INTERNAL_REPR_SIZE: + return isValidInternal(bytes); + case Key::DISPLAY_REPR_SIZE: + return isValidDisplay(bytes); + } + return false; +} + bool Key::operator==(const Key &other) const { return (id == other.id) && (rev == other.rev); @@ -200,4 +275,3 @@ bool Key::operator!=(const Key &other) const { return !(*this == other); } - diff --git a/common/storage/key.h b/common/storage/key.h index a5b92bb..211aea7 100644 --- a/common/storage/key.h +++ b/common/storage/key.h @@ -48,6 +48,10 @@ public: bool isNull() const; + static bool isValidInternal(const QByteArray &); + static bool isValidDisplay(const QByteArray &); + static bool isValid(const QByteArray &); + bool operator==(const Identifier &other) const; bool operator!=(const Identifier &other) const; @@ -72,6 +76,10 @@ public: static Revision fromDisplayByteArray(const QByteArray &bytes); qint64 toQint64() const; + static bool isValidInternal(const QByteArray &); + static bool isValidDisplay(const QByteArray &); + static bool isValid(const QByteArray &); + bool operator==(const Revision &other) const; bool operator!=(const Revision &other) const; @@ -99,6 +107,10 @@ public: bool isNull() const; + static bool isValidInternal(const QByteArray &); + static bool isValidDisplay(const QByteArray &); + static bool isValid(const QByteArray &); + bool operator==(const Key &other) const; bool operator!=(const Key &other) const; @@ -110,6 +122,6 @@ private: } // namespace Storage } // namespace Sink -SINK_EXPORT QDebug& operator<<(QDebug &dbg, const Sink::Storage::Identifier &); -SINK_EXPORT QDebug& operator<<(QDebug &dbg, const Sink::Storage::Revision &); -SINK_EXPORT QDebug& operator<<(QDebug &dbg, const Sink::Storage::Key &); +SINK_EXPORT QDebug &operator<<(QDebug &dbg, const Sink::Storage::Identifier &); +SINK_EXPORT QDebug &operator<<(QDebug &dbg, const Sink::Storage::Revision &); +SINK_EXPORT QDebug &operator<<(QDebug &dbg, const Sink::Storage::Key &); -- cgit v1.2.3