/
file.go
105 lines (88 loc) · 2.78 KB
/
file.go
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
package main
import (
"bytes"
"encoding/binary"
"fmt"
"github.com/spaolacci/murmur3"
"hash/crc32"
)
// Virtual file representation
type FileMeta struct {
Id []byte // Random uuid
FullName string // virtual path, folder/directory + filename (e.g. /images/robin/profile.JPG)
Created uint32 // Unix timestamp
Size uint32 // Length of file in bytes
StartOffset uint32 // Offset in bytes to start reading contents
Checksum uint32 // Crc 32 (Castagnoli)
}
// Serialize to bytes
func (this *FileMeta) Bytes() []byte {
buf := new(bytes.Buffer)
buf.Write(this.Id) // ID, 16 bytes
binary.Write(buf, binary.BigEndian, uint32(len([]byte(this.FullName)))) // Length of name
buf.Write([]byte(this.FullName)) // Name
binary.Write(buf, binary.BigEndian, this.Created) // Created
binary.Write(buf, binary.BigEndian, this.Size) // Size
binary.Write(buf, binary.BigEndian, this.StartOffset) // StartOffset
binary.Write(buf, binary.BigEndian, this.Checksum) // Checksum
return buf.Bytes()
}
// Recover from bytes
func (this *FileMeta) FromBytes(b []byte) {
buf := bytes.NewReader(b)
var err error
// Id
idBytes := make([]byte, 16)
idBytesRead, _ := buf.Read(idBytes)
if idBytesRead != 16 {
panic("ID not 16 bytes")
}
// Name length
var nameLen uint32
err = binary.Read(buf, binary.BigEndian, &nameLen) // Length of name
panicErr(err)
// Read actual name
nameBytes := allocByteArr(nameLen, 1024)
nameBytesRead, _ := buf.Read(nameBytes)
if uint32(nameBytesRead) != nameLen {
panic(fmt.Sprintf("Name bytes read mismatch: %v", b))
}
this.FullName = string(nameBytes)
// Created
err = binary.Read(buf, binary.BigEndian, &this.Created)
panicErr(err)
// Size
err = binary.Read(buf, binary.BigEndian, &this.Size)
panicErr(err)
// StartOffset
err = binary.Read(buf, binary.BigEndian, &this.StartOffset)
panicErr(err)
// Checksum
err = binary.Read(buf, binary.BigEndian, &this.Checksum)
panicErr(err)
}
// Update contents from data of the file (e.g. length)
func (this *FileMeta) UpdateFromData(b []byte) {
// Size
this.Size = uint32(len(b))
if this.Size > uint32(conf.MaxFileSize) {
panic("Exceeds maximum file size")
}
// File meta checksum
this.Checksum = crc32.Checksum(b, crcTable)
}
// Get murmur hash
func (this *FileMeta) GetHash() uint64 {
return murmur3.Sum64([]byte(this.FullName))
}
// New file meta
func newFileMeta(name string) *FileMeta {
return &FileMeta{
Id: randomUuid(),
FullName: name,
Created: unixTsUint32(),
Size: 0, // auto-calculated on write
StartOffset: 0, // auto-calculated on write
Checksum: 0, // auto-calculated on write
}
}