-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync.go
More file actions
110 lines (95 loc) · 2.09 KB
/
sync.go
File metadata and controls
110 lines (95 loc) · 2.09 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
package main
import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
)
func syncDocxNotText(diffs []diffEntry, rootA, rootB string) {
var synced int
for i, d := range diffs {
if d.kind != diffChanged {
continue
}
detail := detailString(d.details)
if detail != "docx:not-text" && detail != "docx:identical" {
continue
}
srcPath := filepath.Join(rootA, d.relPath)
dstPath := filepath.Join(rootB, d.relPath)
if err := copyFile(srcPath, dstPath); err != nil {
fmt.Fprintf(os.Stderr, " sync error: %s: %s\n", d.relPath, err)
continue
}
fmt.Fprintf(os.Stderr, " synced: %s\n", d.relPath)
synced++
diffs[i].kind = diffSynced
}
if synced > 0 {
fmt.Fprintf(os.Stderr, " %d files synced (A → B)\n\n", synced)
}
}
func syncModes(diffs []diffEntry, rootA, rootB string) {
var fixed int
for i, d := range diffs {
if d.kind != diffChanged {
continue
}
if d.entryA == nil {
continue
}
hasMode := false
for _, det := range d.details {
if strings.HasPrefix(det, "mode:") {
hasMode = true
break
}
}
if !hasMode {
continue
}
dstPath := filepath.Join(rootB, d.relPath)
modeA := d.entryA.info.Mode()
if err := os.Chmod(dstPath, modeA); err != nil {
fmt.Fprintf(os.Stderr, " chmod error: %s: %s\n", d.relPath, err)
continue
}
fmt.Fprintf(os.Stderr, " chmod: %s → %s\n", d.relPath, modeA)
fixed++
var remaining []string
for _, det := range diffs[i].details {
if !strings.HasPrefix(det, "mode:") {
remaining = append(remaining, det)
}
}
if len(remaining) == 0 {
diffs[i].kind = diffSynced
} else {
diffs[i].details = remaining
}
}
if fixed > 0 {
fmt.Fprintf(os.Stderr, " %d modes fixed (A → B)\n\n", fixed)
}
}
func copyFile(src, dst string) error {
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
info, err := in.Stat()
if err != nil {
return err
}
out, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, info.Mode())
if err != nil {
return err
}
defer out.Close()
if _, err := io.Copy(out, in); err != nil {
return err
}
return out.Close()
}