-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
168 lines (137 loc) · 3.1 KB
/
utils.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
package main
import (
"github.com/nu7hatch/gouuid"
"github.com/clawio/service-auth/lib"
"golang.org/x/net/context"
metadata "google.golang.org/grpc/metadata"
"io"
"os"
"path"
"strings"
)
// getHome returns the user home directory.
// the logical home has this layout.
// local/users/<letter>/<pid>
// Example: /local/users/o/ourense
// idt.Pid must be always non-empty
func getHome(idt *lib.Identity) string {
pid := path.Clean(idt.Pid)
if pid == "" {
panic("idt.Pid must not be empty")
}
return path.Join("/local", "users", string(pid[0]), pid)
}
// isUnderHome checks is the path is under a user home dir or not.
func isUnderHome(p string, idt *lib.Identity) bool {
p = path.Clean(p)
if strings.HasPrefix(p, getHome(idt)) {
return true
}
return false
}
func isUnderOtherHome(p string, idt *lib.Identity) bool {
home := getHome(idt)
homeTokens := strings.Split(home, "/")
tokens := strings.Split(p, "/")
// /local/users/d/labrador/myfiles
if len(tokens) < 5 { // not a home dir
return false
}
if tokens[3] == homeTokens[3] && tokens[4] == homeTokens[4] {
return false // same user home
}
return true
}
// isCommonDomain checks if the path is the common domain
// i.e /local/users/{letter}
func isCommonDomain(p string) bool {
p = path.Clean(p)
tokens := strings.Split(p, "/")
if len(tokens) > 4 {
// is a home dir or an fake dir
return false
}
return true
}
// copyFile copies a file from src to dst.
// src and dst are physycal paths.
func copyFile(src, dst string, size int64) (err error) {
reader, err := os.Open(src)
if err != nil {
return err
}
defer reader.Close()
writer, err := os.Create(dst)
if err != nil {
return err
}
defer writer.Close()
_, err = io.CopyN(writer, reader, size)
if err != nil {
return err
}
return nil
}
// copyDir copies a dir from src to dst.
// src and dst are physycal paths.
func copyDir(src, dst string) (err error) {
err = os.Mkdir(dst, dirPerm)
if err != nil {
return err
}
directory, err := os.Open(src)
if err != nil {
return err
}
defer directory.Close()
objects, err := directory.Readdir(-1)
for _, obj := range objects {
_src := path.Join(src, obj.Name())
_dst := path.Join(dst, obj.Name())
if obj.IsDir() {
// create sub-directories - recursively
err = copyDir(_src, _dst)
if err != nil {
return err
}
} else {
// perform copy
err = copyFile(_src, _dst, obj.Size())
if err != nil {
return err
}
}
}
return
}
func newTraceContext(ctx context.Context, trace string) context.Context {
md := metadata.Pairs("trace", trace)
ctx = metadata.NewContext(ctx, md)
return ctx
}
func getTraceID(ctx context.Context) (string, error) {
md, ok := metadata.FromContext(ctx)
if !ok {
rawUUID, err := uuid.NewV4()
if err != nil {
return "", err
}
return rawUUID.String(), nil
}
tokens := md["trace"]
if len(tokens) == 0 {
rawUUID, err := uuid.NewV4()
if err != nil {
return "", err
}
return rawUUID.String(), nil
}
if tokens[0] != "" {
return tokens[0], nil
}
rawUUID, err := uuid.NewV4()
if err != nil {
return "", err
}
return rawUUID.String(), nil
}