-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpackets.go
More file actions
113 lines (93 loc) · 2.75 KB
/
packets.go
File metadata and controls
113 lines (93 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"bytes"
"encoding/binary"
"io"
"github.com/Redundancy/go-sync/chunks"
"github.com/Redundancy/go-sync/filechecksum"
"github.com/Redundancy/go-sync/index"
"github.com/fsnotify/fsnotify"
)
// Hey is first message packet through UDP multicast
type Hey struct {
Hostname string `json:"hostname"`
Ip string `json:"ip"`
Items []Item `json:"items"`
}
// ItemType is type for tree item
type ItemType int
const (
// File for file item
File ItemType = iota
// Dir for directory item
Dir
)
// Item is directory tree struct
type Item struct {
RelPath string `json:"path"`
Checksum string `json:"checksum"`
ModTime int64 `json:"modtime"`
}
// Notification is packet for notify diff
type Notification struct {
Hostname string `json:"hostname"`
Event fsnotify.Op `json:"event"`
Type ItemType `json:"type"`
Path string `json:"path"`
ModTime int64 `json:"modtime"`
Ip string `json:"ip"`
}
// EncodeChecksumIndex encode ChecksumIndex of gosync
func EncodeChecksumIndex(content io.Reader, fileSize int64, blockSize uint) (io.ReadSeeker, error) {
generator := filechecksum.NewFileChecksumGenerator(uint(blockSize))
weakSize := generator.WeakRollingHash.Size()
strongSize := generator.GetStrongHash().Size()
b := bytes.NewBuffer(nil)
b.Write(int64ToBytes(fileSize))
b.Write(intToBytes(weakSize))
b.Write(intToBytes(strongSize))
_, err := generator.GenerateChecksums(content, b)
if err != nil {
return nil, err
}
return bytes.NewReader(b.Bytes()), nil
}
// DecodeChecksumIndex decode EncodeChecksumIndex of gosync
func DecodeChecksumIndex(reader io.Reader) (fileSize int64, idx *index.ChecksumIndex, lookup filechecksum.ChecksumLookup, err error) {
fBlock := bytes.NewBuffer(nil)
wBlock := bytes.NewBuffer(nil)
sBlock := bytes.NewBuffer(nil)
_, err = io.CopyN(fBlock, reader, 8)
if err != nil {
return
}
_, err = io.CopyN(wBlock, reader, 4)
if err != nil {
return
}
_, err = io.CopyN(sBlock, reader, 4)
if err != nil {
return
}
fileSize = int64(binary.LittleEndian.Uint64(fBlock.Bytes()))
weakSize := int(binary.LittleEndian.Uint32(wBlock.Bytes()))
strongSize := int(binary.LittleEndian.Uint32(sBlock.Bytes()))
readChunks, err := chunks.LoadChecksumsFromReader(reader, weakSize, strongSize)
if err != nil {
return
}
idx = index.MakeChecksumIndex(readChunks)
lookup = chunks.StrongChecksumGetter(readChunks)
return
}
// helpers
func intToBytes(val int) []byte {
bs := make([]byte, 4)
binary.LittleEndian.PutUint32(bs, uint32(val))
return bs
}
func int64ToBytes(val int64) []byte {
bs := make([]byte, 8)
binary.LittleEndian.PutUint64(bs, uint64(val))
return bs
}