[network/libktorrent] src/diskio: Use QByteArrayView for CacheFile and DNDFile
Jack Hill <[email protected]>
| Newsgroups | gmane.comp.kde.cvs |
|---|---|
| Message-ID | <[email protected]> |
Git commit 98e2efeb9a7bf05c7fbb9aed148345ef5371b92e by Jack Hill.
Committed on 15/08/2026 at 10:44.
Pushed by jackh into branch 'master'.
Use QByteArrayView for CacheFile and DNDFile
M +6 -6 src/diskio/cachefile.cpp
M +2 -2 src/diskio/cachefile.h
M +4 -4 src/diskio/dndfile.cpp
M +3 -4 src/diskio/dndfile.h
M +11 -7 src/diskio/multifilecache.cpp
M +1 -1 src/diskio/singlefilecache.cpp
https://invent.kde.org/network/libktorrent/-/commit/98e2efeb9a7bf05c7fbb9aed148345ef5371b92e
diff --git a/src/diskio/cachefile.cpp b/src/diskio/cachefile.cpp
index 73665950..bb115305 100644
--- a/src/diskio/cachefile.cpp
+++ b/src/diskio/cachefile.cpp
@@ -336,7 +336,7 @@ void CacheFile::read(Uint8 *buf, Uint32 size, Uint64 off)
}
}
-void CacheFile::write(const Uint8 *buf, Uint32 size, Uint64 off)
+void CacheFile::write(QByteArrayView buf, Uint64 off)
{
const QMutexLocker lock(&mutex);
bool close_again = false;
@@ -352,9 +352,9 @@ void CacheFile::write(const Uint8 *buf, Uint32 size, Uint64 off)
throw Error(i18n("Cannot open %1 for writing: readonly filesystem", path));
}
- if (off + size > max_size) {
+ if (off + buf.size() > max_size) {
Out(SYS_DIO | LOG_DEBUG) << "Warning : writing past the end of " << path << endl;
- Out(SYS_DIO | LOG_DEBUG) << (off + size) << " " << max_size << endl;
+ Out(SYS_DIO | LOG_DEBUG) << (off + buf.size()) << " " << max_size << endl;
throw Error(i18n("Attempting to write beyond the maximum size of %1", path));
}
@@ -368,7 +368,7 @@ void CacheFile::write(const Uint8 *buf, Uint32 size, Uint64 off)
throw Error(i18n("Failed to seek file %1: %2", path, fptr.errorString()));
}
- if (fptr.write((const char *)buf, size) != size) {
+ if (fptr.write(buf.data(), buf.size()) != buf.size()) {
throw Error(i18n("Failed to write to file %1: %2", path, fptr.errorString()));
}
@@ -376,8 +376,8 @@ void CacheFile::write(const Uint8 *buf, Uint32 size, Uint64 off)
closeTemporary();
}
- if (off + size > file_size) {
- file_size = off + size;
+ if (off + buf.size() > file_size) {
+ file_size = off + buf.size();
}
}
diff --git a/src/diskio/cachefile.h b/src/diskio/cachefile.h
index 1d22f31b..aa13dc2f 100644
--- a/src/diskio/cachefile.h
+++ b/src/diskio/cachefile.h
@@ -6,6 +6,7 @@
#ifndef BTCACHEFILE_H
#define BTCACHEFILE_H
+#include <QByteArrayView>
#include <QFile>
#include <QHash>
#include <QRecursiveMutex>
@@ -112,10 +113,9 @@ public:
/*!
* Write to the file.
* \param buf Buffer to write
- * \param size Size to read
* \param off Offset to read from in file
*/
- void write(const Uint8 *buf, Uint32 size, Uint64 off);
+ void write(QByteArrayView buf, Uint64 off);
/*!
* Preallocate disk space
diff --git a/src/diskio/dndfile.cpp b/src/diskio/dndfile.cpp
index 450710a8..f4e7a4ac 100644
--- a/src/diskio/dndfile.cpp
+++ b/src/diskio/dndfile.cpp
@@ -112,7 +112,7 @@ Uint32 DNDFile::readLastChunk(Uint8 *buf, Uint32 off, Uint32 size)
return fptr.read(buf, size);
}
-void DNDFile::writeFirstChunk(const Uint8 *buf, Uint32 off, Uint32 size)
+void DNDFile::writeFirstChunk(QByteArrayView buf, Uint32 off)
{
File fptr;
if (!fptr.open(path, u"r+b"_s)) {
@@ -124,10 +124,10 @@ void DNDFile::writeFirstChunk(const Uint8 *buf, Uint32 off, Uint32 size)
// write data
fptr.seek(File::SeekPos::BEGIN, sizeof(DNDFileHeader) + off);
- fptr.write(buf, size);
+ fptr.write(buf.data(), buf.size());
}
-void DNDFile::writeLastChunk(const Uint8 *buf, Uint32 off, Uint32 size)
+void DNDFile::writeLastChunk(QByteArrayView buf, Uint32 off)
{
File fptr;
if (!fptr.open(path, u"r+b"_s)) {
@@ -138,7 +138,7 @@ void DNDFile::writeLastChunk(const Uint8 *buf, Uint32 off, Uint32 size)
}
fptr.seek(File::SeekPos::BEGIN, sizeof(DNDFileHeader) + first_size + off);
- fptr.write(buf, size);
+ fptr.write(buf.data(), buf.size());
}
}
diff --git a/src/diskio/dndfile.h b/src/diskio/dndfile.h
index 424365e4..ef68f886 100644
--- a/src/diskio/dndfile.h
+++ b/src/diskio/dndfile.h
@@ -6,6 +6,7 @@
#ifndef BTDNDFILE_H
#define BTDNDFILE_H
+#include <QByteArrayView>
#include <QString>
#include <QtClassHelperMacros>
#include <util/constants.h>
@@ -58,17 +59,15 @@ public:
* Write the partial first chunk.
* \param buf The buffer
* \param off Offset into partial chunk
- * \param size Size to write
*/
- void writeFirstChunk(const Uint8 *buf, Uint32 off, Uint32 size);
+ void writeFirstChunk(QByteArrayView buf, Uint32 off);
/*!
* Write the partial last chunk.
* \param buf The buffer
* \param off Offset into partial chunk
- * \param size Size to write
*/
- void writeLastChunk(const Uint8 *buf, Uint32 off, Uint32 size);
+ void writeLastChunk(QByteArrayView buf, Uint32 off);
private:
void create();
diff --git a/src/diskio/multifilecache.cpp b/src/diskio/multifilecache.cpp
index a5dd20dd..c96b1925 100644
--- a/src/diskio/multifilecache.cpp
+++ b/src/diskio/multifilecache.cpp
@@ -12,6 +12,7 @@
#include <QDir>
#include <QFileInfo>
#include <QSet>
+#include <QSpan>
#include <QStringList>
#include <QTextStream>
@@ -591,13 +592,14 @@ void MultiFileCache::savePiece(PieceData::Ptr piece)
const Uint8 *ptr = data + piece_off; // location to read from
piece_off += write_length;
+ const QByteArrayView piece{ptr, write_length};
if (fd) {
- fd->write(ptr, write_length, write_offset);
+ fd->write(piece, write_offset);
} else if (dfd) {
if (i == 0) {
- dfd->writeLastChunk(ptr, write_offset - file_off, write_length);
+ dfd->writeLastChunk(piece, write_offset - file_off);
} else {
- dfd->writeFirstChunk(ptr, write_offset, write_length);
+ dfd->writeFirstChunk(piece, write_offset);
}
}
@@ -668,14 +670,16 @@ void MultiFileCache::saveFirstAndLastChunk(TorrentFile *tf, const QString &src_f
Uint8 *tmp = new Uint8[tor.getChunkSize()];
try {
- fptr.read(tmp, cs - tf->getFirstChunkOffset());
- out.writeFirstChunk(tmp, 0, cs - tf->getFirstChunkOffset());
+ const QSpan first_chunk{tmp, static_cast<qsizetype>(cs - tf->getFirstChunkOffset())};
+ fptr.read(first_chunk.data(), first_chunk.size());
+ out.writeFirstChunk(first_chunk, 0);
if (tf->getFirstChunk() != tf->getLastChunk()) {
+ const auto last_chunk = QSpan{tmp, static_cast<qsizetype>(tf->getLastChunkSize())};
const Uint64 off = FileOffset(tf->getLastChunk(), *tf, tor.getChunkSize());
fptr.seek(File::SeekPos::BEGIN, off);
- fptr.read(tmp, tf->getLastChunkSize());
- out.writeLastChunk(tmp, 0, tf->getLastChunkSize());
+ fptr.read(last_chunk.data(), last_chunk.size());
+ out.writeLastChunk(last_chunk, 0);
}
delete[] tmp;
} catch (...) {
diff --git a/src/diskio/singlefilecache.cpp b/src/diskio/singlefilecache.cpp
index 3cedb4ef..5801194e 100644
--- a/src/diskio/singlefilecache.cpp
+++ b/src/diskio/singlefilecache.cpp
@@ -188,7 +188,7 @@ void SingleFileCache::savePiece(PieceData::Ptr piece)
if (!piece->mapped()) {
const Uint64 off = piece->parentChunk()->getIndex() * tor.getChunkSize() + piece->offset();
if (piece->ok()) {
- fd->write(piece->data(), piece->length(), off);
+ fd->write(QByteArrayView{piece->data(), piece->length()}, off);
}
}
}