bep_fileinfo.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package protocol
  7. import (
  8. "bytes"
  9. "crypto/sha256"
  10. "encoding/binary"
  11. "fmt"
  12. "time"
  13. "github.com/syncthing/syncthing/internal/gen/bep"
  14. "github.com/syncthing/syncthing/lib/build"
  15. )
  16. // FileInfo.LocalFlags flags
  17. const (
  18. FlagLocalUnsupported = 1 << 0 // The kind is unsupported, e.g. symlinks on Windows
  19. FlagLocalIgnored = 1 << 1 // Matches local ignore patterns
  20. FlagLocalMustRescan = 1 << 2 // Doesn't match content on disk, must be rechecked fully
  21. FlagLocalReceiveOnly = 1 << 3 // Change detected on receive only folder
  22. // Flags that should result in the Invalid bit on outgoing updates
  23. LocalInvalidFlags = FlagLocalUnsupported | FlagLocalIgnored | FlagLocalMustRescan | FlagLocalReceiveOnly
  24. // Flags that should result in a file being in conflict with its
  25. // successor, due to us not having an up to date picture of its state on
  26. // disk.
  27. LocalConflictFlags = FlagLocalUnsupported | FlagLocalIgnored | FlagLocalReceiveOnly
  28. LocalAllFlags = FlagLocalUnsupported | FlagLocalIgnored | FlagLocalMustRescan | FlagLocalReceiveOnly
  29. )
  30. // BlockSizes is the list of valid block sizes, from min to max
  31. var BlockSizes []int
  32. func init() {
  33. for blockSize := MinBlockSize; blockSize <= MaxBlockSize; blockSize *= 2 {
  34. BlockSizes = append(BlockSizes, blockSize)
  35. if _, ok := sha256OfEmptyBlock[blockSize]; !ok {
  36. panic("missing hard coded value for sha256 of empty block")
  37. }
  38. }
  39. BufferPool = newBufferPool() // must happen after BlockSizes is initialized
  40. }
  41. type FileInfoType = bep.FileInfoType
  42. const (
  43. FileInfoTypeFile = bep.FileInfoType_FILE_INFO_TYPE_FILE
  44. FileInfoTypeDirectory = bep.FileInfoType_FILE_INFO_TYPE_DIRECTORY
  45. FileInfoTypeSymlinkFile = bep.FileInfoType_FILE_INFO_TYPE_SYMLINK_FILE
  46. FileInfoTypeSymlinkDirectory = bep.FileInfoType_FILE_INFO_TYPE_SYMLINK_DIRECTORY
  47. FileInfoTypeSymlink = bep.FileInfoType_FILE_INFO_TYPE_SYMLINK
  48. )
  49. type FileInfo struct {
  50. Name string
  51. Size int64
  52. ModifiedS int64
  53. ModifiedBy ShortID
  54. Version Vector
  55. Sequence int64
  56. Blocks []BlockInfo
  57. SymlinkTarget []byte
  58. BlocksHash []byte
  59. Encrypted []byte
  60. Platform PlatformData
  61. Type FileInfoType
  62. Permissions uint32
  63. ModifiedNs int32
  64. RawBlockSize int32
  65. // The local_flags fields stores flags that are relevant to the local
  66. // host only. It is not part of the protocol, doesn't get sent or
  67. // received (we make sure to zero it), nonetheless we need it on our
  68. // struct and to be able to serialize it to/from the database.
  69. LocalFlags uint32
  70. // The version_hash is an implementation detail and not part of the wire
  71. // format.
  72. VersionHash []byte
  73. // The time when the inode was last changed (i.e., permissions, xattrs
  74. // etc changed). This is host-local, not sent over the wire.
  75. InodeChangeNs int64
  76. // The size of the data appended to the encrypted file on disk. This is
  77. // host-local, not sent over the wire.
  78. EncryptionTrailerSize int
  79. Deleted bool
  80. RawInvalid bool
  81. NoPermissions bool
  82. truncated bool // was created from a truncated file info without blocks
  83. }
  84. func (f *FileInfo) ToWire(withInternalFields bool) *bep.FileInfo {
  85. if f.truncated && !(f.IsDeleted() || f.IsInvalid() || f.IsIgnored()) {
  86. panic("bug: must not serialize truncated file info")
  87. }
  88. blocks := make([]*bep.BlockInfo, len(f.Blocks))
  89. for j, b := range f.Blocks {
  90. blocks[j] = b.ToWire()
  91. }
  92. w := &bep.FileInfo{
  93. Name: f.Name,
  94. Size: f.Size,
  95. ModifiedS: f.ModifiedS,
  96. ModifiedBy: uint64(f.ModifiedBy),
  97. Version: f.Version.ToWire(),
  98. Sequence: f.Sequence,
  99. Blocks: blocks,
  100. SymlinkTarget: f.SymlinkTarget,
  101. BlocksHash: f.BlocksHash,
  102. Encrypted: f.Encrypted,
  103. Type: f.Type,
  104. Permissions: f.Permissions,
  105. ModifiedNs: f.ModifiedNs,
  106. BlockSize: f.RawBlockSize,
  107. Platform: f.Platform.toWire(),
  108. Deleted: f.Deleted,
  109. Invalid: f.RawInvalid,
  110. NoPermissions: f.NoPermissions,
  111. }
  112. if withInternalFields {
  113. w.LocalFlags = f.LocalFlags
  114. w.VersionHash = f.VersionHash
  115. w.InodeChangeNs = f.InodeChangeNs
  116. w.EncryptionTrailerSize = int32(f.EncryptionTrailerSize)
  117. }
  118. return w
  119. }
  120. // WinsConflict returns true if "f" is the one to choose when it is in
  121. // conflict with "other".
  122. func (f *FileInfo) WinsConflict(other FileInfo) bool {
  123. // If only one of the files is invalid, that one loses.
  124. if f.IsInvalid() != other.IsInvalid() {
  125. return !f.IsInvalid()
  126. }
  127. // If a modification is in conflict with a delete, we pick the
  128. // modification.
  129. if !f.IsDeleted() && other.IsDeleted() {
  130. return true
  131. }
  132. if f.IsDeleted() && !other.IsDeleted() {
  133. return false
  134. }
  135. // The one with the newer modification time wins.
  136. if f.ModTime().After(other.ModTime()) {
  137. return true
  138. }
  139. if f.ModTime().Before(other.ModTime()) {
  140. return false
  141. }
  142. // The modification times were equal. Use the device ID in the version
  143. // vector as tie breaker.
  144. return f.FileVersion().Compare(other.FileVersion()) == ConcurrentGreater
  145. }
  146. func FileInfoFromWire(w *bep.FileInfo) FileInfo {
  147. var blocks []BlockInfo
  148. if len(w.Blocks) > 0 {
  149. blocks = make([]BlockInfo, len(w.Blocks))
  150. for j, b := range w.Blocks {
  151. blocks[j] = BlockInfoFromWire(b)
  152. }
  153. }
  154. return fileInfoFromWireWithBlocks(w, blocks)
  155. }
  156. type FileInfoWithoutBlocks interface {
  157. GetName() string
  158. GetSize() int64
  159. GetModifiedS() int64
  160. GetModifiedBy() uint64
  161. GetVersion() *bep.Vector
  162. GetSequence() int64
  163. // GetBlocks() []*bep.BlockInfo // not included
  164. GetSymlinkTarget() []byte
  165. GetBlocksHash() []byte
  166. GetEncrypted() []byte
  167. GetType() FileInfoType
  168. GetPermissions() uint32
  169. GetModifiedNs() int32
  170. GetBlockSize() int32
  171. GetPlatform() *bep.PlatformData
  172. GetLocalFlags() uint32
  173. GetVersionHash() []byte
  174. GetInodeChangeNs() int64
  175. GetEncryptionTrailerSize() int32
  176. GetDeleted() bool
  177. GetInvalid() bool
  178. GetNoPermissions() bool
  179. }
  180. func fileInfoFromWireWithBlocks(w FileInfoWithoutBlocks, blocks []BlockInfo) FileInfo {
  181. return FileInfo{
  182. Name: w.GetName(),
  183. Size: w.GetSize(),
  184. ModifiedS: w.GetModifiedS(),
  185. ModifiedBy: ShortID(w.GetModifiedBy()),
  186. Version: VectorFromWire(w.GetVersion()),
  187. Sequence: w.GetSequence(),
  188. Blocks: blocks,
  189. SymlinkTarget: w.GetSymlinkTarget(),
  190. BlocksHash: w.GetBlocksHash(),
  191. Encrypted: w.GetEncrypted(),
  192. Type: w.GetType(),
  193. Permissions: w.GetPermissions(),
  194. ModifiedNs: w.GetModifiedNs(),
  195. RawBlockSize: w.GetBlockSize(),
  196. Platform: platformDataFromWire(w.GetPlatform()),
  197. Deleted: w.GetDeleted(),
  198. RawInvalid: w.GetInvalid(),
  199. NoPermissions: w.GetNoPermissions(),
  200. }
  201. }
  202. func FileInfoFromDB(w *bep.FileInfo) FileInfo {
  203. f := FileInfoFromWire(w)
  204. f.LocalFlags = w.LocalFlags
  205. f.VersionHash = w.VersionHash
  206. f.InodeChangeNs = w.InodeChangeNs
  207. f.EncryptionTrailerSize = int(w.EncryptionTrailerSize)
  208. return f
  209. }
  210. func FileInfoFromDBTruncated(w FileInfoWithoutBlocks) FileInfo {
  211. f := fileInfoFromWireWithBlocks(w, nil)
  212. f.LocalFlags = w.GetLocalFlags()
  213. f.VersionHash = w.GetVersionHash()
  214. f.InodeChangeNs = w.GetInodeChangeNs()
  215. f.EncryptionTrailerSize = int(w.GetEncryptionTrailerSize())
  216. f.truncated = true
  217. return f
  218. }
  219. func (f FileInfo) String() string {
  220. switch f.Type {
  221. case FileInfoTypeDirectory:
  222. return fmt.Sprintf("Directory{Name:%q, Sequence:%d, Permissions:0%o, ModTime:%v, Version:%v, VersionHash:%x, Deleted:%v, Invalid:%v, LocalFlags:0x%x, NoPermissions:%v, Platform:%v, InodeChangeTime:%v}",
  223. f.Name, f.Sequence, f.Permissions, f.ModTime(), f.Version, f.VersionHash, f.Deleted, f.RawInvalid, f.LocalFlags, f.NoPermissions, f.Platform, f.InodeChangeTime())
  224. case FileInfoTypeFile:
  225. return fmt.Sprintf("File{Name:%q, Sequence:%d, Permissions:0%o, ModTime:%v, Version:%v, VersionHash:%x, Length:%d, Deleted:%v, Invalid:%v, LocalFlags:0x%x, NoPermissions:%v, BlockSize:%d, NumBlocks:%d, BlocksHash:%x, Platform:%v, InodeChangeTime:%v}",
  226. f.Name, f.Sequence, f.Permissions, f.ModTime(), f.Version, f.VersionHash, f.Size, f.Deleted, f.RawInvalid, f.LocalFlags, f.NoPermissions, f.RawBlockSize, len(f.Blocks), f.BlocksHash, f.Platform, f.InodeChangeTime())
  227. case FileInfoTypeSymlink, FileInfoTypeSymlinkDirectory, FileInfoTypeSymlinkFile:
  228. return fmt.Sprintf("Symlink{Name:%q, Type:%v, Sequence:%d, Version:%v, VersionHash:%x, Deleted:%v, Invalid:%v, LocalFlags:0x%x, NoPermissions:%v, SymlinkTarget:%q, Platform:%v, InodeChangeTime:%v}",
  229. f.Name, f.Type, f.Sequence, f.Version, f.VersionHash, f.Deleted, f.RawInvalid, f.LocalFlags, f.NoPermissions, f.SymlinkTarget, f.Platform, f.InodeChangeTime())
  230. default:
  231. panic("mystery file type detected")
  232. }
  233. }
  234. func (f FileInfo) IsDeleted() bool {
  235. return f.Deleted
  236. }
  237. func (f FileInfo) IsInvalid() bool {
  238. return f.RawInvalid || f.LocalFlags&LocalInvalidFlags != 0
  239. }
  240. func (f FileInfo) IsUnsupported() bool {
  241. return f.LocalFlags&FlagLocalUnsupported != 0
  242. }
  243. func (f FileInfo) IsIgnored() bool {
  244. return f.LocalFlags&FlagLocalIgnored != 0
  245. }
  246. func (f FileInfo) MustRescan() bool {
  247. return f.LocalFlags&FlagLocalMustRescan != 0
  248. }
  249. func (f FileInfo) IsReceiveOnlyChanged() bool {
  250. return f.LocalFlags&FlagLocalReceiveOnly != 0
  251. }
  252. func (f FileInfo) IsDirectory() bool {
  253. return f.Type == FileInfoTypeDirectory
  254. }
  255. func (f FileInfo) ShouldConflict() bool {
  256. return f.LocalFlags&LocalConflictFlags != 0
  257. }
  258. func (f FileInfo) IsSymlink() bool {
  259. switch f.Type {
  260. case FileInfoTypeSymlink, FileInfoTypeSymlinkDirectory, FileInfoTypeSymlinkFile:
  261. return true
  262. default:
  263. return false
  264. }
  265. }
  266. func (f FileInfo) HasPermissionBits() bool {
  267. return !f.NoPermissions
  268. }
  269. func (f FileInfo) FileSize() int64 {
  270. if f.Deleted {
  271. return 0
  272. }
  273. if f.IsDirectory() || f.IsSymlink() {
  274. return SyntheticDirectorySize
  275. }
  276. return f.Size
  277. }
  278. func (f FileInfo) BlockSize() int {
  279. if f.RawBlockSize < MinBlockSize {
  280. return MinBlockSize
  281. }
  282. return int(f.RawBlockSize)
  283. }
  284. // BlockSize returns the block size to use for the given file size
  285. func BlockSize(fileSize int64) int {
  286. var blockSize int
  287. for _, blockSize = range BlockSizes {
  288. if fileSize < DesiredPerFileBlocks*int64(blockSize) {
  289. break
  290. }
  291. }
  292. return blockSize
  293. }
  294. func (f FileInfo) FileName() string {
  295. return f.Name
  296. }
  297. func (f FileInfo) FileLocalFlags() uint32 {
  298. return f.LocalFlags
  299. }
  300. func (f FileInfo) ModTime() time.Time {
  301. return time.Unix(f.ModifiedS, int64(f.ModifiedNs))
  302. }
  303. func (f FileInfo) SequenceNo() int64 {
  304. return f.Sequence
  305. }
  306. func (f FileInfo) FileVersion() Vector {
  307. return f.Version
  308. }
  309. func (f FileInfo) FileType() FileInfoType {
  310. return f.Type
  311. }
  312. func (f FileInfo) FilePermissions() uint32 {
  313. return f.Permissions
  314. }
  315. func (f FileInfo) FileModifiedBy() ShortID {
  316. return f.ModifiedBy
  317. }
  318. func (f FileInfo) PlatformData() PlatformData {
  319. return f.Platform
  320. }
  321. func (f FileInfo) InodeChangeTime() time.Time {
  322. return time.Unix(0, f.InodeChangeNs)
  323. }
  324. func (f FileInfo) FileBlocksHash() []byte {
  325. return f.BlocksHash
  326. }
  327. type FileInfoComparison struct {
  328. ModTimeWindow time.Duration
  329. IgnorePerms bool
  330. IgnoreBlocks bool
  331. IgnoreFlags uint32
  332. IgnoreOwnership bool
  333. IgnoreXattrs bool
  334. }
  335. func (f FileInfo) IsEquivalent(other FileInfo, modTimeWindow time.Duration) bool {
  336. return f.isEquivalent(other, FileInfoComparison{ModTimeWindow: modTimeWindow})
  337. }
  338. func (f FileInfo) IsEquivalentOptional(other FileInfo, comp FileInfoComparison) bool {
  339. return f.isEquivalent(other, comp)
  340. }
  341. // isEquivalent checks that the two file infos represent the same actual file content,
  342. // i.e. it does purposely not check only selected (see below) struct members.
  343. // Permissions (config) and blocks (scanning) can be excluded from the comparison.
  344. // Any file info is not "equivalent", if it has different
  345. // - type
  346. // - deleted flag
  347. // - invalid flag
  348. // - permissions, unless they are ignored
  349. //
  350. // A file is not "equivalent", if it has different
  351. // - modification time (difference bigger than modTimeWindow)
  352. // - size
  353. // - blocks, unless there are no blocks to compare (scanning)
  354. // - os data
  355. //
  356. // A symlink is not "equivalent", if it has different
  357. // - target
  358. //
  359. // A directory does not have anything specific to check.
  360. func (f FileInfo) isEquivalent(other FileInfo, comp FileInfoComparison) bool {
  361. if f.MustRescan() || other.MustRescan() {
  362. // These are per definition not equivalent because they don't
  363. // represent a valid state, even if both happen to have the
  364. // MustRescan bit set.
  365. return false
  366. }
  367. // If we care about either ownership or xattrs, are recording inode change
  368. // times and it changed, they are not equal.
  369. if !(comp.IgnoreOwnership && comp.IgnoreXattrs) && f.InodeChangeNs != 0 && other.InodeChangeNs != 0 && f.InodeChangeNs != other.InodeChangeNs {
  370. return false
  371. }
  372. // Mask out the ignored local flags before checking IsInvalid() below
  373. f.LocalFlags &^= comp.IgnoreFlags
  374. other.LocalFlags &^= comp.IgnoreFlags
  375. if f.Name != other.Name || f.Type != other.Type || f.Deleted != other.Deleted || f.IsInvalid() != other.IsInvalid() {
  376. return false
  377. }
  378. if !comp.IgnoreOwnership && f.Platform != other.Platform {
  379. if !unixOwnershipEqual(f.Platform.Unix, other.Platform.Unix) {
  380. return false
  381. }
  382. if !windowsOwnershipEqual(f.Platform.Windows, other.Platform.Windows) {
  383. return false
  384. }
  385. }
  386. if !comp.IgnoreXattrs && f.Platform != other.Platform {
  387. if !xattrsEqual(f.Platform.Linux, other.Platform.Linux) {
  388. return false
  389. }
  390. if !xattrsEqual(f.Platform.Darwin, other.Platform.Darwin) {
  391. return false
  392. }
  393. if !xattrsEqual(f.Platform.FreeBSD, other.Platform.FreeBSD) {
  394. return false
  395. }
  396. if !xattrsEqual(f.Platform.NetBSD, other.Platform.NetBSD) {
  397. return false
  398. }
  399. }
  400. if !comp.IgnorePerms && !f.NoPermissions && !other.NoPermissions && !PermsEqual(f.Permissions, other.Permissions) {
  401. return false
  402. }
  403. switch f.Type {
  404. case FileInfoTypeFile:
  405. return f.Size == other.Size && ModTimeEqual(f.ModTime(), other.ModTime(), comp.ModTimeWindow) && (comp.IgnoreBlocks || f.BlocksEqual(other))
  406. case FileInfoTypeSymlink:
  407. return bytes.Equal(f.SymlinkTarget, other.SymlinkTarget)
  408. case FileInfoTypeDirectory:
  409. return true
  410. }
  411. return false
  412. }
  413. func ModTimeEqual(a, b time.Time, modTimeWindow time.Duration) bool {
  414. if a.Equal(b) {
  415. return true
  416. }
  417. diff := a.Sub(b)
  418. if diff < 0 {
  419. diff *= -1
  420. }
  421. return diff < modTimeWindow
  422. }
  423. func PermsEqual(a, b uint32) bool {
  424. if build.IsWindows {
  425. // There is only writeable and read only, represented for user, group
  426. // and other equally. We only compare against user.
  427. return a&0o600 == b&0o600
  428. }
  429. // All bits count
  430. return a&0o777 == b&0o777
  431. }
  432. // BlocksEqual returns true when the two files have identical block lists.
  433. func (f FileInfo) BlocksEqual(other FileInfo) bool {
  434. // If both sides have blocks hashes and they match, we are good. If they
  435. // don't match still check individual block hashes to catch differences
  436. // in weak hashes only (e.g. after switching weak hash algo).
  437. if len(f.BlocksHash) > 0 && len(other.BlocksHash) > 0 && bytes.Equal(f.BlocksHash, other.BlocksHash) {
  438. return true
  439. }
  440. // Actually compare the block lists in full.
  441. return blocksEqual(f.Blocks, other.Blocks)
  442. }
  443. func (f *FileInfo) SetMustRescan() {
  444. f.setLocalFlags(FlagLocalMustRescan)
  445. }
  446. func (f *FileInfo) SetIgnored() {
  447. f.setLocalFlags(FlagLocalIgnored)
  448. }
  449. func (f *FileInfo) SetUnsupported() {
  450. f.setLocalFlags(FlagLocalUnsupported)
  451. }
  452. func (f *FileInfo) SetDeleted(by ShortID) {
  453. f.ModifiedBy = by
  454. f.Deleted = true
  455. f.Version = f.Version.Update(by)
  456. f.ModifiedS = time.Now().Unix()
  457. f.setNoContent()
  458. }
  459. func (f *FileInfo) setLocalFlags(flags uint32) {
  460. f.RawInvalid = false
  461. f.LocalFlags = flags
  462. f.setNoContent()
  463. }
  464. func (f *FileInfo) setNoContent() {
  465. f.Blocks = nil
  466. f.BlocksHash = nil
  467. f.Size = 0
  468. }
  469. type BlockInfo struct {
  470. Hash []byte
  471. Offset int64
  472. Size int
  473. WeakHash uint32
  474. }
  475. func (b BlockInfo) ToWire() *bep.BlockInfo {
  476. return &bep.BlockInfo{
  477. Hash: b.Hash,
  478. Offset: b.Offset,
  479. Size: int32(b.Size),
  480. WeakHash: b.WeakHash,
  481. }
  482. }
  483. func BlockInfoFromWire(w *bep.BlockInfo) BlockInfo {
  484. return BlockInfo{
  485. Hash: w.Hash,
  486. Offset: w.Offset,
  487. Size: int(w.Size),
  488. WeakHash: w.WeakHash,
  489. }
  490. }
  491. func (b BlockInfo) String() string {
  492. return fmt.Sprintf("Block{%d/%d/%d/%x}", b.Offset, b.Size, b.WeakHash, b.Hash)
  493. }
  494. // For each block size, the hash of a block of all zeroes
  495. var sha256OfEmptyBlock = map[int][sha256.Size]byte{
  496. 128 << KiB: {0xfa, 0x43, 0x23, 0x9b, 0xce, 0xe7, 0xb9, 0x7c, 0xa6, 0x2f, 0x0, 0x7c, 0xc6, 0x84, 0x87, 0x56, 0xa, 0x39, 0xe1, 0x9f, 0x74, 0xf3, 0xdd, 0xe7, 0x48, 0x6d, 0xb3, 0xf9, 0x8d, 0xf8, 0xe4, 0x71},
  497. 256 << KiB: {0x8a, 0x39, 0xd2, 0xab, 0xd3, 0x99, 0x9a, 0xb7, 0x3c, 0x34, 0xdb, 0x24, 0x76, 0x84, 0x9c, 0xdd, 0xf3, 0x3, 0xce, 0x38, 0x9b, 0x35, 0x82, 0x68, 0x50, 0xf9, 0xa7, 0x0, 0x58, 0x9b, 0x4a, 0x90},
  498. 512 << KiB: {0x7, 0x85, 0x4d, 0x2f, 0xef, 0x29, 0x7a, 0x6, 0xba, 0x81, 0x68, 0x5e, 0x66, 0xc, 0x33, 0x2d, 0xe3, 0x6d, 0x5d, 0x18, 0xd5, 0x46, 0x92, 0x7d, 0x30, 0xda, 0xad, 0x6d, 0x7f, 0xda, 0x15, 0x41},
  499. 1 << MiB: {0x30, 0xe1, 0x49, 0x55, 0xeb, 0xf1, 0x35, 0x22, 0x66, 0xdc, 0x2f, 0xf8, 0x6, 0x7e, 0x68, 0x10, 0x46, 0x7, 0xe7, 0x50, 0xab, 0xb9, 0xd3, 0xb3, 0x65, 0x82, 0xb8, 0xaf, 0x90, 0x9f, 0xcb, 0x58},
  500. 2 << MiB: {0x56, 0x47, 0xf0, 0x5e, 0xc1, 0x89, 0x58, 0x94, 0x7d, 0x32, 0x87, 0x4e, 0xeb, 0x78, 0x8f, 0xa3, 0x96, 0xa0, 0x5d, 0xb, 0xab, 0x7c, 0x1b, 0x71, 0xf1, 0x12, 0xce, 0xb7, 0xe9, 0xb3, 0x1e, 0xee},
  501. 4 << MiB: {0xbb, 0x9f, 0x8d, 0xf6, 0x14, 0x74, 0xd2, 0x5e, 0x71, 0xfa, 0x0, 0x72, 0x23, 0x18, 0xcd, 0x38, 0x73, 0x96, 0xca, 0x17, 0x36, 0x60, 0x5e, 0x12, 0x48, 0x82, 0x1c, 0xc0, 0xde, 0x3d, 0x3a, 0xf8},
  502. 8 << MiB: {0x2d, 0xae, 0xb1, 0xf3, 0x60, 0x95, 0xb4, 0x4b, 0x31, 0x84, 0x10, 0xb3, 0xf4, 0xe8, 0xb5, 0xd9, 0x89, 0xdc, 0xc7, 0xbb, 0x2, 0x3d, 0x14, 0x26, 0xc4, 0x92, 0xda, 0xb0, 0xa3, 0x5, 0x3e, 0x74},
  503. 16 << MiB: {0x8, 0xa, 0xcf, 0x35, 0xa5, 0x7, 0xac, 0x98, 0x49, 0xcf, 0xcb, 0xa4, 0x7d, 0xc2, 0xad, 0x83, 0xe0, 0x1b, 0x75, 0x66, 0x3a, 0x51, 0x62, 0x79, 0xc8, 0xb9, 0xd2, 0x43, 0xb7, 0x19, 0x64, 0x3e},
  504. }
  505. // IsEmpty returns true if the block is a full block of zeroes.
  506. func (b BlockInfo) IsEmpty() bool {
  507. if v, ok := sha256OfEmptyBlock[b.Size]; ok {
  508. return bytes.Equal(b.Hash, v[:])
  509. }
  510. return false
  511. }
  512. func BlocksHash(bs []BlockInfo) []byte {
  513. h := sha256.New()
  514. for _, b := range bs {
  515. _, _ = h.Write(b.Hash)
  516. _ = binary.Write(h, binary.BigEndian, b.WeakHash)
  517. }
  518. return h.Sum(nil)
  519. }
  520. func VectorHash(v Vector) []byte {
  521. h := sha256.New()
  522. for _, c := range v.Counters {
  523. if err := binary.Write(h, binary.BigEndian, c.ID); err != nil {
  524. panic("impossible: failed to write c.ID to hash function: " + err.Error())
  525. }
  526. if err := binary.Write(h, binary.BigEndian, c.Value); err != nil {
  527. panic("impossible: failed to write c.Value to hash function: " + err.Error())
  528. }
  529. }
  530. return h.Sum(nil)
  531. }
  532. // Xattrs is a convenience method to return the extended attributes of the
  533. // file for the current platform.
  534. func (p *PlatformData) Xattrs() []Xattr {
  535. switch {
  536. case build.IsLinux && p.Linux != nil:
  537. return p.Linux.Xattrs
  538. case build.IsDarwin && p.Darwin != nil:
  539. return p.Darwin.Xattrs
  540. case build.IsFreeBSD && p.FreeBSD != nil:
  541. return p.FreeBSD.Xattrs
  542. case build.IsNetBSD && p.NetBSD != nil:
  543. return p.NetBSD.Xattrs
  544. default:
  545. return nil
  546. }
  547. }
  548. // SetXattrs is a convenience method to set the extended attributes of the
  549. // file for the current platform.
  550. func (p *PlatformData) SetXattrs(xattrs []Xattr) {
  551. switch {
  552. case build.IsLinux:
  553. if p.Linux == nil {
  554. p.Linux = &XattrData{}
  555. }
  556. p.Linux.Xattrs = xattrs
  557. case build.IsDarwin:
  558. if p.Darwin == nil {
  559. p.Darwin = &XattrData{}
  560. }
  561. p.Darwin.Xattrs = xattrs
  562. case build.IsFreeBSD:
  563. if p.FreeBSD == nil {
  564. p.FreeBSD = &XattrData{}
  565. }
  566. p.FreeBSD.Xattrs = xattrs
  567. case build.IsNetBSD:
  568. if p.NetBSD == nil {
  569. p.NetBSD = &XattrData{}
  570. }
  571. p.NetBSD.Xattrs = xattrs
  572. }
  573. }
  574. // MergeWith copies platform data from other, for platforms where it's not
  575. // already set on p.
  576. func (p *PlatformData) MergeWith(other *PlatformData) {
  577. if p.Unix == nil {
  578. p.Unix = other.Unix
  579. }
  580. if p.Windows == nil {
  581. p.Windows = other.Windows
  582. }
  583. if p.Linux == nil {
  584. p.Linux = other.Linux
  585. }
  586. if p.Darwin == nil {
  587. p.Darwin = other.Darwin
  588. }
  589. if p.FreeBSD == nil {
  590. p.FreeBSD = other.FreeBSD
  591. }
  592. if p.NetBSD == nil {
  593. p.NetBSD = other.NetBSD
  594. }
  595. }
  596. // blocksEqual returns whether two slices of blocks are exactly the same hash
  597. // and index pair wise.
  598. func blocksEqual(a, b []BlockInfo) bool {
  599. if len(b) != len(a) {
  600. return false
  601. }
  602. for i, sblk := range a {
  603. if !bytes.Equal(sblk.Hash, b[i].Hash) {
  604. return false
  605. }
  606. }
  607. return true
  608. }
  609. type PlatformData struct {
  610. Unix *UnixData
  611. Windows *WindowsData
  612. Linux *XattrData
  613. Darwin *XattrData
  614. FreeBSD *XattrData
  615. NetBSD *XattrData
  616. }
  617. func (p *PlatformData) toWire() *bep.PlatformData {
  618. return &bep.PlatformData{
  619. Unix: p.Unix.toWire(),
  620. Windows: p.Windows,
  621. Linux: p.Linux.toWire(),
  622. Darwin: p.Darwin.toWire(),
  623. Freebsd: p.FreeBSD.toWire(),
  624. Netbsd: p.NetBSD.toWire(),
  625. }
  626. }
  627. func platformDataFromWire(w *bep.PlatformData) PlatformData {
  628. if w == nil {
  629. return PlatformData{}
  630. }
  631. return PlatformData{
  632. Unix: unixDataFromWire(w.Unix),
  633. Windows: w.Windows,
  634. Linux: xattrDataFromWire(w.Linux),
  635. Darwin: xattrDataFromWire(w.Darwin),
  636. FreeBSD: xattrDataFromWire(w.Freebsd),
  637. NetBSD: xattrDataFromWire(w.Netbsd),
  638. }
  639. }
  640. type UnixData struct {
  641. // The owner name and group name are set when known (i.e., could be
  642. // resolved on the source device), while the UID and GID are always set
  643. // as they come directly from the stat() call.
  644. OwnerName string
  645. GroupName string
  646. UID int
  647. GID int
  648. }
  649. func (u *UnixData) toWire() *bep.UnixData {
  650. if u == nil {
  651. return nil
  652. }
  653. return &bep.UnixData{
  654. OwnerName: u.OwnerName,
  655. GroupName: u.GroupName,
  656. Uid: int32(u.UID),
  657. Gid: int32(u.GID),
  658. }
  659. }
  660. func unixDataFromWire(w *bep.UnixData) *UnixData {
  661. if w == nil {
  662. return nil
  663. }
  664. return &UnixData{
  665. OwnerName: w.OwnerName,
  666. GroupName: w.GroupName,
  667. UID: int(w.Uid),
  668. GID: int(w.Gid),
  669. }
  670. }
  671. type WindowsData = bep.WindowsData
  672. type XattrData struct {
  673. Xattrs []Xattr
  674. }
  675. func (x *XattrData) toWire() *bep.XattrData {
  676. if x == nil {
  677. return nil
  678. }
  679. xattrs := make([]*bep.Xattr, len(x.Xattrs))
  680. for i, a := range x.Xattrs {
  681. xattrs[i] = a.toWire()
  682. }
  683. return &bep.XattrData{
  684. Xattrs: xattrs,
  685. }
  686. }
  687. func xattrDataFromWire(w *bep.XattrData) *XattrData {
  688. if w == nil {
  689. return nil
  690. }
  691. x := &XattrData{}
  692. x.Xattrs = make([]Xattr, len(w.Xattrs))
  693. for i, a := range w.Xattrs {
  694. x.Xattrs[i] = xattrFromWire(a)
  695. }
  696. return x
  697. }
  698. type Xattr struct {
  699. Name string
  700. Value []byte
  701. }
  702. func (a Xattr) toWire() *bep.Xattr {
  703. return &bep.Xattr{
  704. Name: a.Name,
  705. Value: a.Value,
  706. }
  707. }
  708. func xattrFromWire(w *bep.Xattr) Xattr {
  709. return Xattr{
  710. Name: w.Name,
  711. Value: w.Value,
  712. }
  713. }
  714. func xattrsEqual(a, b *XattrData) bool {
  715. aEmpty := a == nil || len(a.Xattrs) == 0
  716. bEmpty := b == nil || len(b.Xattrs) == 0
  717. if aEmpty && bEmpty {
  718. return true
  719. }
  720. if aEmpty || bEmpty {
  721. // Only one side is empty, so they can't be equal.
  722. return false
  723. }
  724. if len(a.Xattrs) != len(b.Xattrs) {
  725. return false
  726. }
  727. for i := range a.Xattrs {
  728. if a.Xattrs[i].Name != b.Xattrs[i].Name {
  729. return false
  730. }
  731. if !bytes.Equal(a.Xattrs[i].Value, b.Xattrs[i].Value) {
  732. return false
  733. }
  734. }
  735. return true
  736. }
  737. func unixOwnershipEqual(a, b *UnixData) bool {
  738. if a == nil && b == nil {
  739. return true
  740. }
  741. if a == nil || b == nil {
  742. return false
  743. }
  744. ownerEqual := a.OwnerName == "" || b.OwnerName == "" || a.OwnerName == b.OwnerName
  745. groupEqual := a.GroupName == "" || b.GroupName == "" || a.GroupName == b.GroupName
  746. return a.UID == b.UID && a.GID == b.GID && ownerEqual && groupEqual
  747. }
  748. func windowsOwnershipEqual(a, b *WindowsData) bool {
  749. if a == nil && b == nil {
  750. return true
  751. }
  752. if a == nil || b == nil {
  753. return false
  754. }
  755. return a.OwnerName == b.OwnerName && a.OwnerIsGroup == b.OwnerIsGroup
  756. }