-
Notifications
You must be signed in to change notification settings - Fork 3
/
walk.go
142 lines (122 loc) · 3.54 KB
/
walk.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
// FileLock - Read and write files with lock.
// Copyright (c) 2022-present, b3log.org
//
// FileLock is licensed under Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of Mulan PSL v2 at:
// http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
//
// See the Mulan PSL v2 for more details.
package filelock
import (
"errors"
"fmt"
"io/fs"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/siyuan-note/httpclient"
"github.com/siyuan-note/logging"
)
var AndroidServerPort = 6906 // Android HTTP 服务器端口
func Walk(root string, fn filepath.WalkFunc) error {
if strings.Contains(runtime.GOOS, "android") {
// Android 系统上统一使用 Android HTTP 服务器来遍历文件
// Data sync may cause data loss on Android 14 https://github.com/siyuan-note/siyuan/issues/10205
start := time.Now()
req := httpclient.NewCloudFileRequest2m()
req.SetBody(map[string]interface{}{"dir": root})
req.SetContentType("application/json; charset=utf-8")
resp, err := req.Post("http://[::1]:" + fmt.Sprintf("%d", AndroidServerPort) + "/api/walkDir")
logging.LogInfof("walk dir [%s] cost [%s]", root, time.Since(start))
if nil != err {
logging.LogErrorf("walk dir [%s] failed: %s", root, err)
return filepath.Walk(root, fn)
}
if 200 != resp.StatusCode {
logging.LogErrorf("walk dir [%s] failed: %d", root, resp.StatusCode)
return filepath.Walk(root, fn)
}
result := map[string]interface{}{}
if err = resp.UnmarshalJson(&result); nil != err {
logging.LogErrorf("walk dir [%s] failed: %s", root, err)
return errors.New("walk dir failed")
}
code := result["code"].(float64)
if 0 != code {
msgResult := result["msg"]
var msg string
if nil != msgResult {
msg = msgResult.(string)
}
logging.LogErrorf("walk dir [%s] failed: %f, %s", root, code, msg)
return errors.New("walk dir failed")
}
data := result["data"].(map[string]interface{})
filesData := data["files"].([]interface{})
var infos []*RemoteFile
for _, f := range filesData {
info := &RemoteFile{info: f.(map[string]interface{})}
infos = append(infos, info)
}
skipFiles := map[string]bool{}
for _, info := range infos {
p := info.Path()
skip := false
for skipFile, _ := range skipFiles {
if strings.HasPrefix(p, skipFile) {
skip = true
}
}
if skip {
//logging.LogInfof("skip walk [%s]", p)
continue
}
err = fn(p, info, nil)
if nil != err {
if errors.Is(err, fs.SkipDir) || errors.Is(err, fs.SkipAll) {
skipFiles[p] = true
//logging.LogInfof("skip walk [%s]", p)
continue
}
logging.LogErrorf("walk [%s] failed: %s", p, err)
return err
}
}
return nil
}
return filepath.Walk(root, fn)
}
type RemoteFile struct {
info map[string]interface{}
}
func (f *RemoteFile) Path() string {
return f.info["path"].(string)
}
func (f *RemoteFile) Name() string {
return f.info["name"].(string)
}
func (f *RemoteFile) Size() int64 {
return int64(f.info["size"].(float64))
}
func (f *RemoteFile) Mode() fs.FileMode {
if f.IsDir() {
return 0755
}
return 0644
}
func (f *RemoteFile) ModTime() time.Time {
ms := int64(f.info["updated"].(float64))
return time.UnixMilli(ms)
}
func (f *RemoteFile) IsDir() bool {
return f.info["isDir"].(bool)
}
func (f *RemoteFile) Sys() interface{} {
return nil
}