snapshots.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package smbserver
  2. import (
  3. "encoding/binary"
  4. "sort"
  5. "strings"
  6. "time"
  7. "imuslab.com/arozos/mod/storage/smbserver/wire"
  8. )
  9. /*
  10. snapshots.go
  11. Previous Versions. Windows lists the versions of a file with
  12. FSCTL_SRV_ENUMERATE_SNAPSHOTS, gets them as "@GMT-YYYY.MM.DD-HH.MM.SS"
  13. tokens, and opens one with the timewarp create context (TWrp) or with the
  14. token as a path component. Where the versions are kept is the backend's
  15. business; a share that has them implements SnapshotShare.
  16. A version is read only: it lives in a protected folder of the drive, so
  17. any open asking to change it is refused like any write there.
  18. */
  19. // SnapshotShare is a Share that keeps earlier versions of files.
  20. type SnapshotShare interface {
  21. //Snapshots lists when versions of p were taken; for a folder, of the
  22. //files in it
  23. Snapshots(p string) []time.Time
  24. //SnapshotPath is the share path holding p as it was at t
  25. SnapshotPath(p string, t time.Time) (string, bool)
  26. }
  27. const gmtLayout = "@GMT-2006.01.02-15.04.05"
  28. // parseGMTToken reads an @GMT path component.
  29. func parseGMTToken(s string) (time.Time, bool) {
  30. if len(s) != len(gmtLayout) || !strings.HasPrefix(s, "@GMT-") {
  31. return time.Time{}, false
  32. }
  33. t, err := time.ParseInLocation(gmtLayout, s, time.UTC)
  34. return t, err == nil
  35. }
  36. // splitGMTToken takes an @GMT component out of a share path.
  37. func splitGMTToken(rel string) (string, time.Time, bool) {
  38. parts := strings.Split(rel, "/")
  39. for i, part := range parts {
  40. if t, ok := parseGMTToken(part); ok {
  41. rest := append(append([]string{}, parts[:i]...), parts[i+1:]...)
  42. return strings.Join(rest, "/"), t, true
  43. }
  44. }
  45. return rel, time.Time{}, false
  46. }
  47. // snapshotArray is SRV_SNAPSHOT_ARRAY. A buffer too small for the names
  48. // gets the counts alone, and Windows asks again with room for them.
  49. func snapshotArray(times []time.Time, maxOut uint32) []byte {
  50. sort.Slice(times, func(i, j int) bool { return times[i].After(times[j]) })
  51. multi := []byte{}
  52. for _, t := range times {
  53. multi = append(multi, wire.EncodeUTF16(t.UTC().Format(gmtLayout))...)
  54. multi = append(multi, 0, 0)
  55. }
  56. if len(times) > 0 {
  57. multi = append(multi, 0, 0)
  58. }
  59. w := wire.NewWriter(16 + len(multi))
  60. w.U32(uint32(len(times)))
  61. if len(times) > 0 && uint64(12+len(multi)) <= uint64(maxOut) {
  62. w.U32(uint32(len(times)))
  63. w.U32(uint32(len(multi)))
  64. w.Bytes(multi)
  65. return w.B
  66. }
  67. w.U32(0)
  68. w.U32(uint32(len(multi)))
  69. w.Zero(4)
  70. return w.B
  71. }
  72. func (c *conn) enumerateSnapshots(r *request, q *wire.IoctlRequest) *response {
  73. if q.MaxOutputResponse < 16 {
  74. return errorResponse(wire.StatusInvalidParameter)
  75. }
  76. o, status := r.open(q.FileID)
  77. if status != wire.StatusOK {
  78. return errorResponse(status)
  79. }
  80. var times []time.Time
  81. if o.pipe == nil {
  82. if ss, ok := o.tree.share.(SnapshotShare); ok && o.tree.share.CanRead(o.getPath()) {
  83. times = ss.Snapshots(o.getPath())
  84. }
  85. }
  86. return ioctlResponse(q, snapshotArray(times, q.MaxOutputResponse), wire.StatusOK)
  87. }
  88. // timewarp reads the TWrp context: a FILETIME in UTC.
  89. func timewarp(data []byte) (time.Time, bool) {
  90. if len(data) != 8 {
  91. return time.Time{}, false
  92. }
  93. return wire.TimeFromFiletime(binary.LittleEndian.Uint64(data)).UTC().Truncate(time.Second), true
  94. }
  95. // snapshotOf maps a path and a time to the version kept of it, or reports
  96. // that there is none.
  97. func (c *conn) snapshotOf(share Share, rel string, at time.Time) (string, uint32) {
  98. ss, ok := share.(SnapshotShare)
  99. if !ok {
  100. return "", wire.StatusObjectNameNotFound
  101. }
  102. //Find the real spelling, if the file still exists; a deleted file's
  103. //versions are kept under its old name
  104. base := rel
  105. if res, err := resolve(share, c.srv.names, rel); err == nil && res.exists {
  106. base = res.path
  107. }
  108. p, ok := ss.SnapshotPath(base, at)
  109. if !ok {
  110. return "", wire.StatusObjectNameNotFound
  111. }
  112. return p, wire.StatusOK
  113. }