structs.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. //go:generate go run ../../script/protofmt.go structs.proto
  7. //go:generate protoc -I ../../../../../ -I ../../vendor/ -I ../../vendor/github.com/gogo/protobuf/protobuf -I . --gogofast_out=. structs.proto
  8. package db
  9. import (
  10. "fmt"
  11. "time"
  12. "github.com/syncthing/syncthing/lib/protocol"
  13. )
  14. func (f FileInfoTruncated) String() string {
  15. return fmt.Sprintf("File{Name:%q, Permissions:0%o, Modified:%v, Version:%v, Length:%d, Deleted:%v, Invalid:%v, NoPermissions:%v, BlockSize:%d}",
  16. f.Name, f.Permissions, f.ModTime(), f.Version, f.Size, f.Deleted, f.Invalid, f.NoPermissions, f.RawBlockSize)
  17. }
  18. func (f FileInfoTruncated) IsDeleted() bool {
  19. return f.Deleted
  20. }
  21. func (f FileInfoTruncated) IsInvalid() bool {
  22. return f.Invalid
  23. }
  24. func (f FileInfoTruncated) IsDirectory() bool {
  25. return f.Type == protocol.FileInfoTypeDirectory
  26. }
  27. func (f FileInfoTruncated) IsSymlink() bool {
  28. switch f.Type {
  29. case protocol.FileInfoTypeSymlink, protocol.FileInfoTypeDeprecatedSymlinkDirectory, protocol.FileInfoTypeDeprecatedSymlinkFile:
  30. return true
  31. default:
  32. return false
  33. }
  34. }
  35. func (f FileInfoTruncated) HasPermissionBits() bool {
  36. return !f.NoPermissions
  37. }
  38. func (f FileInfoTruncated) FileSize() int64 {
  39. if f.Deleted {
  40. return 0
  41. }
  42. if f.IsDirectory() || f.IsSymlink() {
  43. return protocol.SyntheticDirectorySize
  44. }
  45. return f.Size
  46. }
  47. func (f FileInfoTruncated) BlockSize() int {
  48. if f.RawBlockSize == 0 {
  49. return protocol.MinBlockSize
  50. }
  51. return int(f.RawBlockSize)
  52. }
  53. func (f FileInfoTruncated) FileName() string {
  54. return f.Name
  55. }
  56. func (f FileInfoTruncated) ModTime() time.Time {
  57. return time.Unix(f.ModifiedS, int64(f.ModifiedNs))
  58. }
  59. func (f FileInfoTruncated) SequenceNo() int64 {
  60. return f.Sequence
  61. }
  62. func (f FileInfoTruncated) FileVersion() protocol.Vector {
  63. return f.Version
  64. }
  65. func (f FileInfoTruncated) ConvertToInvalidFileInfo(invalidatedBy protocol.ShortID) protocol.FileInfo {
  66. return protocol.FileInfo{
  67. Name: f.Name,
  68. Type: f.Type,
  69. ModifiedS: f.ModifiedS,
  70. ModifiedNs: f.ModifiedNs,
  71. ModifiedBy: invalidatedBy,
  72. Invalid: true,
  73. Version: f.Version,
  74. RawBlockSize: f.RawBlockSize,
  75. }
  76. }