-
Notifications
You must be signed in to change notification settings - Fork 231
/
Copy pathhash_data_adapter_hidden_files.go
95 lines (74 loc) · 2.2 KB
/
hash_data_adapter_hidden_files.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
package common
import (
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
)
type HiddenFileDataAdapter struct {
hashBasePath string // "" == dataBasePath
dataBasePath string
}
func (a *HiddenFileDataAdapter) GetMode() HashStorageMode {
return EHashStorageMode.HiddenFiles()
}
func (a *HiddenFileDataAdapter) getHashPath(relativePath string) string {
basePath := a.hashBasePath
if basePath == "" {
basePath = a.dataBasePath
}
dir, fName := filepath.Split(relativePath)
fName = fmt.Sprintf(".%s%s", fName, AzCopyHashDataStream)
// Try to create the directory
err := os.Mkdir(filepath.Join(basePath, dir), 0775)
if err != nil && !os.IsExist(err) {
lcm.Warn("Failed to create hash data directory")
}
return filepath.Join(basePath, dir, fName)
}
func (a *HiddenFileDataAdapter) getDataPath(relativePath string) string {
return filepath.Join(a.dataBasePath, relativePath)
}
func (a *HiddenFileDataAdapter) GetHashData(relativePath string) (*SyncHashData, error) {
metaFile := a.getHashPath(relativePath)
f, err := os.OpenFile(metaFile, os.O_RDONLY, 0644)
if err != nil {
return nil, fmt.Errorf("failed to open hash meta file: %w", err)
}
defer f.Close()
buf, err := io.ReadAll(f)
if err != nil {
return nil, fmt.Errorf("failed to read hash meta file: %w", err)
}
var out SyncHashData
err = json.Unmarshal(buf, &out)
return &out, err
}
func (a *HiddenFileDataAdapter) SetHashData(relativePath string, data *SyncHashData) error {
if data == nil {
return nil // no-op
}
metaFile := a.getHashPath(relativePath)
f, err := os.OpenFile(metaFile, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0644)
if err != nil {
return fmt.Errorf("failed to open hash meta file: %w", err)
}
buf, err := json.Marshal(data)
if err != nil {
return fmt.Errorf("failed to marshal metadata: %w", err)
}
_, err = f.Write(buf)
if err != nil {
return fmt.Errorf("failed to write metadata to file: %w", err)
}
// Push types around to check for OS-specific hide file method
if adapter, canHide := any(a).(interface{ HideFile(string) error }); canHide {
dataFile := a.getDataPath(relativePath)
err := adapter.HideFile(dataFile)
if err != nil {
return fmt.Errorf("failed to hide file: %w", err)
}
}
return f.Close()
}