瀏覽代碼

lib/protocol: Ensure correct blocksize on enc. fileinfo (ref #7861) (#7870)

Simon Frei 4 年之前
父節點
當前提交
50aacdf1f0
共有 3 個文件被更改,包括 19 次插入14 次删除
  1. 2 2
      lib/protocol/bep_extensions.go
  2. 14 12
      lib/protocol/encryption.go
  3. 3 0
      lib/protocol/encryption_test.go

+ 2 - 2
lib/protocol/bep_extensions.go

@@ -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 {

+ 14 - 12
lib/protocol/encryption.go

@@ -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

+ 3 - 0
lib/protocol/encryption_test.go

@@ -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)")