-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathguest_paths.go
More file actions
75 lines (66 loc) · 1.2 KB
/
guest_paths.go
File metadata and controls
75 lines (66 loc) · 1.2 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
package vbk
import (
"errors"
"io"
"path"
"strings"
"sync"
)
type lockedReadSeekerAt struct {
r io.ReadSeeker
mu sync.Mutex
}
func (l *lockedReadSeekerAt) ReadAt(p []byte, off int64) (int, error) {
l.mu.Lock()
defer l.mu.Unlock()
if _, err := l.r.Seek(off, io.SeekStart); err != nil {
return 0, err
}
read := 0
for read < len(p) {
n, err := l.r.Read(p[read:])
read += n
if err != nil {
if errors.Is(err, io.EOF) {
if read == len(p) {
return read, nil
}
return read, io.EOF
}
return read, err
}
if n == 0 {
break
}
}
if read < len(p) {
return read, io.EOF
}
return read, nil
}
func normalizeGuestPath(p, cwd string) string {
p = strings.ReplaceAll(strings.TrimSpace(p), "\\", "/")
if p == "" {
if cwd == "" {
return "/"
}
return cwd
}
if len(p) >= 2 && p[1] == ':' {
p = p[2:]
}
if !strings.HasPrefix(p, "/") {
p = joinGuestPath(cwd, p)
}
clean := path.Clean(p)
if !strings.HasPrefix(clean, "/") {
clean = "/" + clean
}
return clean
}
func joinGuestPath(base, name string) string {
if base == "" || base == "/" {
return "/" + strings.TrimLeft(name, "/")
}
return strings.TrimRight(base, "/") + "/" + strings.TrimLeft(name, "/")
}