Skip to content

Commit

Permalink
lib/protocol: Ensure correct blocksize on enc. fileinfo (ref syncthin…
Browse files Browse the repository at this point in the history
  • Loading branch information
imsodin authored Aug 4, 2021
1 parent e56e8b7 commit 50aacdf
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
4 changes: 2 additions & 2 deletions lib/protocol/bep_extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,10 @@ func (f FileInfo) FileSize() int64 {
}

func (f FileInfo) BlockSize() int {
if f.RawBlockSize == 0 {
if f.RawBlockSize < MinBlockSize {
return MinBlockSize
}
return int(f.RawBlockSize)
return f.RawBlockSize
}

func (f FileInfo) FileName() string {
Expand Down
26 changes: 14 additions & 12 deletions lib/protocol/encryption.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,18 +317,20 @@ func encryptFileInfo(fi FileInfo, folderKey *[keySize]byte) FileInfo {
typ = FileInfoTypeDirectory
}
enc := FileInfo{
Name: encryptName(fi.Name, folderKey),
Type: typ,
Size: offset, // new total file size
Permissions: 0644,
ModifiedS: 1234567890, // Sat Feb 14 00:31:30 CET 2009
Deleted: fi.Deleted,
RawInvalid: fi.IsInvalid(),
Version: version,
Sequence: fi.Sequence,
RawBlockSize: fi.RawBlockSize + blockOverhead,
Blocks: blocks,
Encrypted: encryptedFI,
Name: encryptName(fi.Name, folderKey),
Type: typ,
Permissions: 0644,
ModifiedS: 1234567890, // Sat Feb 14 00:31:30 CET 2009
Deleted: fi.Deleted,
RawInvalid: fi.IsInvalid(),
Version: version,
Sequence: fi.Sequence,
Encrypted: encryptedFI,
}
if typ == FileInfoTypeFile {
enc.Size = offset // new total file size
enc.Blocks = blocks
enc.RawBlockSize = fi.BlockSize() + blockOverhead
}

return enc
Expand Down
3 changes: 3 additions & 0 deletions lib/protocol/encryption_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ func TestEnDecryptFileInfo(t *testing.T) {
if bytes.Equal(enc.Blocks[0].Hash, enc.Blocks[1].Hash) {
t.Error("block hashes should not repeat when on different offsets")
}
if enc.RawBlockSize < MinBlockSize {
t.Error("Too small raw block size:", enc.RawBlockSize)
}
again := encryptFileInfo(fi, &key)
if !bytes.Equal(enc.Blocks[0].Hash, again.Blocks[0].Hash) {
t.Error("block hashes should remain stable (0)")
Expand Down

0 comments on commit 50aacdf

Please sign in to comment.