-
Notifications
You must be signed in to change notification settings - Fork 7
/
encoding.go
61 lines (56 loc) · 1.54 KB
/
encoding.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
package filesystem
import (
"encoding/gob"
"io"
)
type encodedFileSystemType struct {
FileSystem
InodeTableLength uint64
}
type numberedInode struct {
InodeNumber uint64
GenericInode
}
func (fs *FileSystem) encode(writer io.Writer) error {
// Make a copy of the FileSystem so that the InodeTable can be ripped out
// and streamed afterwards.
var encodedFileSystem encodedFileSystemType
inodeTable := fs.InodeTable
encodedFileSystem.FileSystem = *fs
encodedFileSystem.FileSystem.InodeTable = nil
encodedFileSystem.InodeTableLength = uint64(len(inodeTable))
encoder := gob.NewEncoder(writer)
if err := encoder.Encode(encodedFileSystem); err != nil {
return err
}
// Stream out the InodeTable.
for inodeNumber, genericInode := range inodeTable {
var inode numberedInode
inode.InodeNumber = inodeNumber
inode.GenericInode = genericInode
if err := encoder.Encode(inode); err != nil {
return err
}
}
return nil
}
func decode(reader io.Reader) (*FileSystem, error) {
decoder := gob.NewDecoder(reader)
fs := new(FileSystem)
var decodedFileSystem encodedFileSystemType
if err := decoder.Decode(&decodedFileSystem); err != nil {
return nil, err
}
*fs = decodedFileSystem.FileSystem
// Stream in the InodeTable.
numInodes := decodedFileSystem.InodeTableLength
fs.InodeTable = make(InodeTable, numInodes)
for index := uint64(0); index < numInodes; index++ {
var inode numberedInode
if err := decoder.Decode(&inode); err != nil {
return nil, err
}
fs.InodeTable[inode.InodeNumber] = inode.GenericInode
}
return fs, nil
}