checksums.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * Copyright (C) by Klaas Freitag <freitag@owncloud.com>
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #pragma once
  19. #include "ocsynclib.h"
  20. #include "config.h"
  21. #include <QObject>
  22. #include <QByteArray>
  23. #include <QFutureWatcher>
  24. #include <memory>
  25. class QFile;
  26. namespace OCC {
  27. class ChecksumCalculator;
  28. class SyncJournalDb;
  29. /**
  30. * Returns the highest-quality checksum in a 'checksums'
  31. * property retrieved from the server.
  32. *
  33. * Example: "ADLER32:1231 SHA1:ab124124 MD5:2131affa21"
  34. * -> "SHA1:ab124124"
  35. */
  36. OCSYNC_EXPORT QByteArray findBestChecksum(const QByteArray &checksums);
  37. /// Creates a checksum header from type and value.
  38. OCSYNC_EXPORT QByteArray makeChecksumHeader(const QByteArray &checksumType, const QByteArray &checksum);
  39. /// Parses a checksum header
  40. OCSYNC_EXPORT bool parseChecksumHeader(const QByteArray &header, QByteArray *type, QByteArray *checksum);
  41. /// Convenience for getting the type from a checksum header, null if none
  42. OCSYNC_EXPORT QByteArray parseChecksumHeaderType(const QByteArray &header);
  43. /// Checks OWNCLOUD_DISABLE_CHECKSUM_UPLOAD
  44. OCSYNC_EXPORT bool uploadChecksumEnabled();
  45. /**
  46. * Computes the checksum of a file.
  47. * \ingroup libsync
  48. */
  49. class OCSYNC_EXPORT ComputeChecksum : public QObject
  50. {
  51. Q_OBJECT
  52. public:
  53. explicit ComputeChecksum(QObject *parent = nullptr);
  54. ~ComputeChecksum() override;
  55. /**
  56. * Sets the checksum type to be used. The default is empty.
  57. */
  58. void setChecksumType(const QByteArray &type);
  59. QByteArray checksumType() const;
  60. /**
  61. * Computes the checksum for the given file path.
  62. *
  63. * done() is emitted when the calculation finishes.
  64. */
  65. void start(const QString &filePath);
  66. /**
  67. * Computes the checksum for the given device.
  68. *
  69. * done() is emitted when the calculation finishes.
  70. *
  71. * The device ownership transfers into the thread that
  72. * will compute the checksum. It must not have a parent.
  73. */
  74. void start(QSharedPointer<QIODevice> device);
  75. /**
  76. * Computes the checksum synchronously.
  77. */
  78. static QByteArray computeNow(QSharedPointer<QIODevice> device, const QByteArray &checksumType);
  79. /**
  80. * Computes the checksum synchronously on file. Convenience wrapper for computeNow().
  81. */
  82. static QByteArray computeNowOnFile(const QString &filePath, const QByteArray &checksumType);
  83. signals:
  84. void done(const QByteArray &checksumType, const QByteArray &checksum);
  85. private slots:
  86. void slotCalculationDone();
  87. private:
  88. void startImpl(QSharedPointer<QIODevice> device);
  89. QByteArray _checksumType;
  90. // watcher for the checksum calculation thread
  91. QFutureWatcher<QByteArray> _watcher;
  92. QScopedPointer<ChecksumCalculator> _checksumCalculator;
  93. };
  94. /**
  95. * Checks whether a file's checksum matches the expected value.
  96. * @ingroup libsync
  97. */
  98. class OCSYNC_EXPORT ValidateChecksumHeader : public QObject
  99. {
  100. Q_OBJECT
  101. public:
  102. enum FailureReason {
  103. Success,
  104. ChecksumHeaderMalformed,
  105. ChecksumTypeUnknown,
  106. ChecksumMismatch,
  107. };
  108. Q_ENUM(FailureReason)
  109. explicit ValidateChecksumHeader(QObject *parent = nullptr);
  110. /**
  111. * Check a file's actual checksum against the provided checksumHeader
  112. *
  113. * If no checksum is there, or if a correct checksum is there, the signal validated()
  114. * will be emitted. In case of any kind of error, the signal validationFailed() will
  115. * be emitted.
  116. */
  117. void start(const QString &filePath, const QByteArray &checksumHeader);
  118. /**
  119. * Check a device's actual checksum against the provided checksumHeader
  120. *
  121. * Like the other start() but works on an device.
  122. *
  123. * The device ownership transfers into the thread that
  124. * will compute the checksum. It must not have a parent.
  125. */
  126. void start(QSharedPointer<QIODevice> device, const QByteArray &checksumHeader);
  127. [[nodiscard]] QByteArray calculatedChecksumType() const;
  128. [[nodiscard]] QByteArray calculatedChecksum() const;
  129. signals:
  130. void validated(const QByteArray &checksumType, const QByteArray &checksum);
  131. void validationFailed(const QString &errMsg, const QByteArray &calculatedChecksumType,
  132. const QByteArray &calculatedChecksum, const ValidateChecksumHeader::FailureReason reason);
  133. private slots:
  134. void slotChecksumCalculated(const QByteArray &checksumType, const QByteArray &checksum);
  135. private:
  136. ComputeChecksum *prepareStart(const QByteArray &checksumHeader);
  137. QByteArray _expectedChecksumType;
  138. QByteArray _expectedChecksum;
  139. QByteArray _calculatedChecksumType;
  140. QByteArray _calculatedChecksum;
  141. };
  142. /**
  143. * Hooks checksum computations into csync.
  144. * @ingroup libsync
  145. */
  146. class OCSYNC_EXPORT CSyncChecksumHook : public QObject
  147. {
  148. Q_OBJECT
  149. public:
  150. explicit CSyncChecksumHook();
  151. /**
  152. * Returns the checksum value for \a path that is comparable to \a otherChecksum.
  153. *
  154. * Called from csync, where a instance of CSyncChecksumHook has
  155. * to be set as userdata.
  156. * The return value will be owned by csync.
  157. */
  158. static QByteArray hook(const QByteArray &path, const QByteArray &otherChecksumHeader, void *this_obj);
  159. };
  160. }