forked from hdonnay/venti
/
venti.go
82 lines (69 loc) · 1.64 KB
/
venti.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
// Copyright 2016 The Venti Authors. All rights reserved.
// Use of this source code is governed by an ISC-style
// license that can be found in the LICENSE file.
// Package venti is a group of libraries for writing venti(7) servers.
package venti
import (
"fmt"
"strconv"
)
var vers = []string{"04"}
const verComment = "hdonnay/venti"
// MaxFileSize is the biggest file venti supports.
const MaxFileSize = (1 << 48) - 1
// Score is the hash of a block.
//
// Currently the score function clients recognize is SHA1.
type Score []byte
// String returns the hex representation of a score
func (s Score) String() string {
switch len(s) {
case 20:
return fmt.Sprintf("sha1!%20x", string(s))
case 0:
return "nil"
}
return fmt.Sprintf("???!%x", string(s))
}
// ParseScore returns a score from its hex representation
func ParseScore(s string) (Score, error) {
var score Score
if len(s) != 40 {
return score, fmt.Errorf("cannot parse score %q", s)
}
for i := 0; i < 20; i++ {
i, err := strconv.ParseInt(s[2*i:2*i+2], 16, 16)
if err != nil {
return score, fmt.Errorf("cannot parse score %q", s)
}
score = append(score, byte(i))
}
return score, nil
}
// Entry is a stub from libventi.h
type Entry struct {
Gen uint32
PoiterSize uint32
DataSize uint32
Type uint8
Flags uint8
Size uint64
Score Score
}
// Root is a stub from libventi.h
type Root struct {
Name string
Type []byte
Score Score
BlockSize uint32
Prev Score
}
// Type is a stub from libventi.h
type Type byte
// These are the Type constants.
const (
VtData Type = iota << 3
VtDir
VtRoot
)
//go:generate stringer -type Type