-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.go
More file actions
155 lines (143 loc) · 3.25 KB
/
util.go
File metadata and controls
155 lines (143 loc) · 3.25 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package hashq_mod
import (
"archive/zip"
"encoding/hex"
"fmt"
"io"
"os"
"path/filepath"
"strings"
)
func EncodeToString(data []byte) string {
return strings.ToUpper(hex.EncodeToString(data))
}
func DecodeFromString(data string) []byte {
ret, err := hex.DecodeString(data)
if err != nil {
panic(err)
}
return ret
}
func (dk DeviceCrypto) Print(f *os.File) {
_, _ = f.WriteString("PublicKey : " + dk.PublicKey)
_, _ = f.Write([]byte{0x0A, 0x0D})
_, _ = f.WriteString("PrivateKey: " + dk.PrivateKey)
_, _ = f.Write([]byte{0x0A, 0x0D})
_ = f.Sync()
}
func (hash CanonicalHash) Stringify() string {
key := hash.Key
if len(key) == 0 {
key = EmptyKey()
}
return fmt.Sprintf("%05d %s %s %s", hash.Sequence, key, hash.Gen, hash.Owner)
}
func (hash CanonicalHash) StringifyWithDigest() string {
key := hash.Key
if len(key) == 0 {
key = EmptyKey()
}
return fmt.Sprintf("%05d %s %s %s %s", hash.Sequence, hash.Token, key, hash.Gen, hash.Owner)
}
func (hash CanonicalHash) Print() {
key := hash.Key
if len(key) == 0 {
key = EmptyKey()
}
verified := "NotVerified"
if hash.Verified {
verified = "Verified"
}
fmt.Println(
fmt.Sprintf("%05d", hash.Sequence),
" ", hash.Token,
" ", key,
" ", hash.Gen,
" ", hash.Owner,
" ", verified)
}
func (tok *Token) Print() {
for temp := tok.List.Back(); temp != nil; temp = temp.Prev() {
temp.Value.(*CanonicalHash).Print()
}
}
func (store *HashStore) Print(hash string) {
fmt.Println("Token: ", hash)
hashes := store.IndexToken[hash]
if hashes != nil {
for temp := hashes.Back(); temp != nil; temp = temp.Prev() {
temp.Value.(*CanonicalHash).Print()
}
}
}
func ZipFiles(filename string, files []string) error {
newZipFile, err := os.Create(filename)
if err != nil {
return err
}
defer newZipFile.Close()
zipWriter := zip.NewWriter(newZipFile)
defer zipWriter.Close()
for _, file := range files {
zipfile, err := os.Open(file)
if err != nil {
return err
}
defer zipfile.Close()
info, err := zipfile.Stat()
if err != nil {
return err
}
header, err := zip.FileInfoHeader(info)
if err != nil {
return err
}
header.Name = file
header.Method = zip.Deflate
writer, err := zipWriter.CreateHeader(header)
if err != nil {
return err
}
if _, err = io.Copy(writer, zipfile); err != nil {
return err
}
}
return nil
}
func Unzip(src string, dest string) ([]string, error) {
var filenames []string
r, err := zip.OpenReader(src)
if err != nil {
return filenames, err
}
defer r.Close()
for _, f := range r.File {
rc, err := f.Open()
if err != nil {
return filenames, err
}
rc.Close()
fpath := filepath.Join(dest, f.Name)
if !strings.HasPrefix(fpath, filepath.Clean(dest)+string(os.PathSeparator)) {
return filenames, fmt.Errorf("%s: illegal file path", fpath)
}
filenames = append(filenames, fpath)
if f.FileInfo().IsDir() {
_ = os.MkdirAll(fpath, os.ModePerm)
} else {
if err = os.MkdirAll(filepath.Dir(fpath), os.ModePerm); err != nil {
return filenames, err
}
outFile, err := os.OpenFile(fpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
return filenames, err
}
_, err = io.Copy(outFile, rc)
outFile.Close()
if err != nil {
return filenames, err
}
}
}
return filenames, nil
}