File indexing completed on 2024-11-24 04:31:14

0001 /*
0002     SPDX-FileCopyrightText: 2005 Joris Guisson <joris.guisson@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 #ifndef BTCHUNK_H
0007 #define BTCHUNK_H
0008 
0009 #include <diskio/piecedata.h>
0010 #include <ktorrent_export.h>
0011 #include <util/constants.h>
0012 
0013 namespace bt
0014 {
0015 class SHA1Hash;
0016 class Cache;
0017 class PieceData;
0018 
0019 /**
0020  * @author Joris Guisson
0021  * @brief Keep track of a piece of the file
0022  *
0023  * Keeps track of a piece of the file. The Chunk has 3 possible states :
0024  * - MMAPPED : It is memory mapped
0025  * - BUFFERED : It is in a buffer in dynamically allocated memory
0026  *  (because the chunk is located in 2 or more separate files, so we cannot just set a pointer
0027  *   to a region of mmapped memory)
0028  * - ON_DISK : On disk
0029  * - NOT_DOWNLOADED : It hasn't been dowloaded yet, and there is no buffer allocated
0030  */
0031 class KTORRENT_EXPORT Chunk
0032 {
0033 public:
0034     Chunk(Uint32 index, Uint32 size, Cache *cache);
0035     ~Chunk();
0036 
0037     enum Status {
0038         ON_DISK,
0039         NOT_DOWNLOADED,
0040     };
0041 
0042     /**
0043      * Read a piece from the chunk
0044      * @param off The offset of the chunk
0045      * @param len The length of the chunk
0046      * @param data The data, should be big enough to hold len bytes
0047      */
0048     bool readPiece(Uint32 off, Uint32 len, Uint8 *data);
0049 
0050     /**
0051      * Get a pointer to the data of a piece.
0052      * If it isn't loaded, it will be loaded.
0053      * @param off Offset of the piece
0054      * @param len Length of the piece
0055      * @param read_only Is this for reading the piece or for writing
0056      * @return Pointer to the PieceData
0057      */
0058     PieceData::Ptr getPiece(Uint32 off, Uint32 len, bool read_only);
0059 
0060     /**
0061      * Save a piece
0062      * @param off Offset of the piece
0063      * @param len Length of the piece
0064      */
0065     void savePiece(PieceData::Ptr piece);
0066 
0067     /// Get the chunks status.
0068     Status getStatus() const
0069     {
0070         return status;
0071     }
0072 
0073     /**
0074      * Set the chunks status
0075      * @param s
0076      */
0077     void setStatus(Status s)
0078     {
0079         status = s;
0080     }
0081 
0082     /// Get the chunk's index
0083     Uint32 getIndex() const
0084     {
0085         return index;
0086     }
0087 
0088     /// Get the chunk's size
0089     Uint32 getSize() const
0090     {
0091         return size;
0092     }
0093 
0094     /// get chunk priority
0095     Priority getPriority() const
0096     {
0097         return priority;
0098     }
0099 
0100     /// set chunk priority
0101     void setPriority(Priority newpriority = NORMAL_PRIORITY)
0102     {
0103         priority = newpriority;
0104     }
0105 
0106     /// Is chunk part of a multimedia preview
0107     bool isPreview() const
0108     {
0109         return priority == FIRST_PREVIEW_PRIORITY ||
0110                 priority == NORMAL_PREVIEW_PRIORITY ||
0111                 priority == LAST_PREVIEW_PRIORITY;
0112     }
0113 
0114     /// Is chunk excluded
0115     bool isExcluded() const
0116     {
0117         return priority == EXCLUDED;
0118     }
0119 
0120     /// Is this a seed only chunk
0121     bool isExcludedForDownloading() const
0122     {
0123         return priority == ONLY_SEED_PRIORITY;
0124     }
0125 
0126     /// In/Exclude chunk
0127     void setExclude(bool yes)
0128     {
0129         priority = yes ? EXCLUDED : NORMAL_PRIORITY;
0130     }
0131 
0132     /**
0133      * Check wehter the chunk matches it's hash.
0134      * @param h The hash
0135      * @return true if the data matches the hash
0136      */
0137     bool checkHash(const SHA1Hash &h);
0138 
0139 private:
0140     Status status;
0141     Uint32 index;
0142     Uint32 size;
0143     Priority priority;
0144     Cache *cache;
0145 };
0146 }
0147 
0148 #endif