File indexing completed on 2024-06-02 05:05:19
0001 /* 0002 SPDX-FileCopyrightText: 2008 Joris Guisson <joris.guisson@gmail.com> 0003 SPDX-FileCopyrightText: 2008 Ivan Vasic <ivasic@gmail.com> 0004 0005 SPDX-License-Identifier: GPL-2.0-or-later 0006 */ 0007 #ifndef BTPIECEDATA_H 0008 #define BTPIECEDATA_H 0009 0010 #include <QSharedDataPointer> 0011 #include <ktorrent_export.h> 0012 #include <util/constants.h> 0013 #ifndef Q_WS_WIN 0014 #include <util/signalcatcher.h> 0015 #endif 0016 #include <diskio/cachefile.h> 0017 0018 namespace bt 0019 { 0020 class File; 0021 class Chunk; 0022 class SHA1Hash; 0023 class SHA1HashGen; 0024 0025 /** 0026 Class which holds the data of a piece of a chunk. 0027 It has a reference counter. 0028 */ 0029 class KTORRENT_EXPORT PieceData : public QSharedData, public MMappeable 0030 { 0031 public: 0032 PieceData(Chunk *chunk, Uint32 off, Uint32 len, Uint8 *ptr, CacheFile::Ptr cache_file, bool read_only); 0033 ~PieceData() override; 0034 0035 /// Unload the piece 0036 void unload(); 0037 0038 /// Is it a mapped into memory 0039 bool mapped() const 0040 { 0041 return cache_file != nullptr; 0042 } 0043 0044 /// Is this writeable 0045 bool writeable() const 0046 { 0047 return !read_only; 0048 } 0049 0050 /// Get the offset of the piece in the chunk 0051 Uint32 offset() const 0052 { 0053 return off; 0054 } 0055 0056 /// Get the length of the piece 0057 Uint32 length() const 0058 { 0059 return len; 0060 } 0061 0062 /// Get a pointer to the data 0063 Uint8 *data() 0064 { 0065 return ptr; 0066 } 0067 0068 /// Check if the data pointer is OK 0069 bool ok() const 0070 { 0071 return ptr != nullptr; 0072 } 0073 0074 /// Set the data pointer 0075 void setData(Uint8 *p) 0076 { 0077 ptr = p; 0078 } 0079 0080 /// Get the parent chunk of the piece 0081 Chunk *parentChunk() 0082 { 0083 return chunk; 0084 } 0085 0086 /** 0087 Write data into the PieceData. This function should always be used 0088 for writing into a PieceData object, as it protects against bus errors. 0089 @param buf The buffer to write 0090 @param size Size of the buffer 0091 @param off Offset to write 0092 @return The number of bytes written 0093 @throw BusError When writing results in a SIGBUS 0094 */ 0095 Uint32 write(const Uint8 *buf, Uint32 buf_size, Uint32 off = 0); 0096 0097 /** 0098 Read data from the PieceData. This function should always be used 0099 for reading from a PieceData object, as it protects against bus errors. 0100 @param buf The buffer to read into 0101 @param to_read Amount of bytes to read 0102 @param off Offset in the PieceData to start reading from 0103 @return The number of bytes read 0104 @throw BusError When reading results in a SIGBUS 0105 */ 0106 Uint32 read(Uint8 *buf, Uint32 to_read, Uint32 off = 0); 0107 0108 /** 0109 Save PieceData to a File. This function protects against bus errors. 0110 @param file The file to write to 0111 @param size Size to write 0112 @param off Offset in PieceData to write from 0113 @return The number of bytes written 0114 @throw BusError When writing results in a SIGBUS 0115 */ 0116 Uint32 writeToFile(File &file, Uint32 size, Uint32 off = 0); 0117 0118 /** 0119 Read PieceData from a File. This function protects against bus errors. 0120 @param file The file to read from 0121 @param size Size to read 0122 @param off Offset in PieceData to write into 0123 @return The number of bytes read 0124 @throw BusError When reading results in a SIGBUS 0125 */ 0126 Uint32 readFromFile(File &file, Uint32 size, Uint32 off = 0); 0127 0128 /** 0129 Update a SHA1HashGen with this PieceData. This function protects against bus errors. 0130 @param hg The SHA1HashGen to update 0131 @throw BusError When reading results in a SIGBUS 0132 */ 0133 void updateHash(SHA1HashGen &hg); 0134 0135 /** 0136 Generate a SHA1Hash of this PieceData. This function protects against bus errors. 0137 @return The SHA1 hash 0138 @throw BusError When reading results in a SIGBUS 0139 */ 0140 SHA1Hash generateHash() const; 0141 0142 typedef QExplicitlySharedDataPointer<PieceData> Ptr; 0143 0144 /// Is the piece in use by somebody else then the cache 0145 bool inUse() const 0146 { 0147 return ref > 1; 0148 } 0149 0150 private: 0151 void unmapped() override; 0152 0153 private: 0154 Chunk *chunk; 0155 Uint32 off; 0156 Uint32 len; 0157 Uint8 *ptr; 0158 CacheFile::Ptr cache_file; 0159 bool read_only; 0160 }; 0161 0162 } 0163 0164 #endif