add piece size and file length

pull/53/head
Zlatin Balevsky 2020-10-31 02:05:19 +00:00
parent 608de1c9b3
commit 5e0519cc06
No known key found for this signature in database
GPG Key ID: A72832072D525E41
3 changed files with 21 additions and 4 deletions

View File

@ -84,7 +84,7 @@ class FileCollectionBuilder {
String comment = sf.getComment() // TODO: check comment encoding String comment = sf.getComment() // TODO: check comment encoding
if (comment == null) if (comment == null)
comment = "" comment = ""
def item = new FileCollectionItem(new InfoHash(sf.root), comment, sfPathElements.get(sf)) def item = new FileCollectionItem(new InfoHash(sf.root), comment, sfPathElements.get(sf), (byte)sf.pieceSize, sf.getCachedLength())
files.add(item) files.add(item)
} }

View File

@ -3,19 +3,24 @@ package com.muwire.core.collections
import com.muwire.core.Constants import com.muwire.core.Constants
import com.muwire.core.InfoHash import com.muwire.core.InfoHash
import com.muwire.core.Name import com.muwire.core.Name
import com.muwire.core.files.FileHasher
class FileCollectionItem { class FileCollectionItem {
private final InfoHash infoHash private final InfoHash infoHash
private final String comment private final String comment
private final File file private final File file
private final byte pieceSizePow2
private final long length
private volatile byte[] payload private volatile byte[] payload
private final List<String> pathElements = new ArrayList<>() private final List<String> pathElements = new ArrayList<>()
public FileCollectionItem(InfoHash infoHash, String comment, List<String> pathElements) { public FileCollectionItem(InfoHash infoHash, String comment, List<String> pathElements, byte pieceSizePow2, long length) {
this.infoHash = infoHash this.infoHash = infoHash
this.comment = comment this.comment = comment
this.pathElements = pathElements this.pathElements = pathElements
this.pieceSizePow2 = pieceSizePow2
this.length = length
File f = null File f = null
pathElements.each { pathElements.each {
@ -38,6 +43,14 @@ class FileCollectionItem {
dis.readFully(hash) dis.readFully(hash)
infoHash = new InfoHash(hash) infoHash = new InfoHash(hash)
pieceSizePow2 = dis.readByte()
if (pieceSizePow2 < FileHasher.MIN_PIECE_SIZE_POW2 || pieceSizePow2 > FileHasher.MAX_PIECE_SIZE_POW2)
throw new InvalidCollectionException("invalid piece size $pieceSizePow2")
length = dis.readLong()
if (length < 1 || length > FileHasher.MAX_SIZE)
throw new InvalidCollectionException("invalid length $length")
int nPathElements = dis.readUnsignedByte() int nPathElements = dis.readUnsignedByte()
File f = null File f = null
nPathElements.times { nPathElements.times {
@ -62,6 +75,8 @@ class FileCollectionItem {
daos.writeByte(Constants.COLLECTION_ENTRY_VERSION) daos.writeByte(Constants.COLLECTION_ENTRY_VERSION)
daos.write(infoHash.getRoot()) daos.write(infoHash.getRoot())
daos.writeByte(pieceSizePow2)
daos.writeLong(length)
daos.writeByte((byte) pathElements.size()) daos.writeByte((byte) pathElements.size())
pathElements.each { pathElements.each {
def name = new Name(it) def name = new Name(it)

View File

@ -24,8 +24,10 @@ The header is followed by a file entry for each file in the collection. The for
``` ```
byte 0: File entry version, currently fixed at "1". byte 0: File entry version, currently fixed at "1".
byte 1-33: hash of the file byte 1-33: hash of the file
byte 34: Unsigned 8-bit number of path elements from root to where the file will ultimately be placed upon download. byte 34: piece size power of 2
bytes 35-N : UTF-8 encoded length-prefixed path elements. Each element can be at most 32kb long. The last element is the name of the file. bytes 35-43: length of the file
byte 44: Unsigned 8-bit number of path elements from root to where the file will ultimately be placed upon download.
bytes 45-N : UTF-8 encoded length-prefixed path elements. Each element can be at most 32kb long. The last element is the name of the file.
bytes N-M: length-prefixed free from description of the file (comment). Format is UTF-8, maximum size is 32kb. bytes N-M: length-prefixed free from description of the file (comment). Format is UTF-8, maximum size is 32kb.
``` ```