From 25990a53f8c9c426ffbe7dea9d70d9de53a49560 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Sun, 5 Jun 2022 16:57:42 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E6=94=AF=E6=8C=81=E5=A4=9A=E8=AE=BE?= =?UTF-8?q?=E5=A4=87=E6=93=8D=E4=BD=9C=E4=B8=8D=E5=90=8C=E6=96=87=E6=A1=A3?= =?UTF-8?q?=E5=90=8E=E4=BA=91=E7=AB=AF=E5=90=8C=E6=AD=A5=E5=90=88=E5=B9=B6?= =?UTF-8?q?=20https://github.com/siyuan-note/siyuan/issues/5092?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/model/osssync.go | 14 +++++++------- kernel/model/sync.go | 32 ++++++++++++++++---------------- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/kernel/model/osssync.go b/kernel/model/osssync.go index aee98ffb5..9afab9b76 100644 --- a/kernel/model/osssync.go +++ b/kernel/model/osssync.go @@ -320,7 +320,7 @@ func ossDownload0(localDirPath, cloudDirPath, fetch string, fetchedFiles *int, t return } -func ossUpload(localDirPath, cloudDirPath, cloudDevice string, boot bool, removedSyncList, upsertedSyncList map[string]bool) (wroteFiles int, transferSize uint64, err error) { +func ossUpload(localDirPath, cloudDirPath, cloudDevice string, boot bool, removeList, upsertList map[string]bool) (wroteFiles int, transferSize uint64, err error) { if !gulu.File.IsExist(localDirPath) { return } @@ -328,16 +328,16 @@ func ossUpload(localDirPath, cloudDirPath, cloudDevice string, boot bool, remove localDevice := Conf.System.ID excludes := getSyncExcludedList(localDirPath) localFileList, genIndexErr := genCloudIndex(localDirPath, excludes) + if nil != genIndexErr { + err = genIndexErr + return + } + var localUpserts, cloudRemoves []string var cloudFileList map[string]*CloudIndex if "" != localDevice && localDevice == cloudDevice { //util.LogInfof("cloud device is the same as local device, get index from local") - if nil == genIndexErr { - localUpserts, cloudRemoves, err = cloudUpsertRemoveLocalListOSS(localDirPath, removedSyncList, upsertedSyncList, excludes) - } else { - util.LogInfof("get local index failed [%s], get index from cloud", err) - cloudFileList, err = getCloudFileListOSS(cloudDirPath) - } + localUpserts, cloudRemoves, err = cloudUpsertRemoveLocalListOSS(localDirPath, removeList, upsertList, excludes) } else { cloudFileList, err = getCloudFileListOSS(cloudDirPath) } diff --git a/kernel/model/sync.go b/kernel/model/sync.go index 225f186eb..2b951621f 100644 --- a/kernel/model/sync.go +++ b/kernel/model/sync.go @@ -135,9 +135,9 @@ func SyncData(boot, exit, byHand bool) { WaitForWritingFiles() writingDataLock.Lock() var err error - var removedSyncList, upsertedSyncList map[string]bool + var removeList, upsertList map[string]bool // 将 data 变更同步到 sync - if removedSyncList, upsertedSyncList, err = workspaceData2SyncDir(); nil != err { + if removeList, upsertList, err = workspaceData2SyncDir(); nil != err { msg := fmt.Sprintf(Conf.Language(80), formatErrorMsg(err)) Conf.Sync.Stat = msg util.PushErrMsg(msg, 7000) @@ -246,7 +246,7 @@ func SyncData(boot, exit, byHand bool) { return } - wroteFiles, transferSize, err := ossUpload(localSyncDirPath, "sync/"+Conf.Sync.CloudName, device, boot, removedSyncList, upsertedSyncList) + wroteFiles, transferSize, err := ossUpload(localSyncDirPath, "sync/"+Conf.Sync.CloudName, device, boot, removeList, upsertList) if nil != err { util.PushClearProgress() IncWorkspaceDataVer() // 上传失败的话提升本地版本,以备下次上传 @@ -591,17 +591,17 @@ func syncDir2WorkspaceData(boot bool) (upsertFiles, removeFiles []string, err er // workspaceData2SyncDir 将 data 的数据更新到 sync 中。 // 1. 删除 sync 中多余的文件 // 2. 将 data 中新增/修改的文件加密后拷贝到 sync 中 -func workspaceData2SyncDir() (removedSyncList, upsertedSyncList map[string]bool, err error) { +func workspaceData2SyncDir() (removeList, upsertList map[string]bool, err error) { start := time.Now() filesys.ReleaseAllFileLocks() passwd := Conf.E2EEPasswd - unchangedDataList, removedSyncList, err := calcUnchangedDataList(passwd) + unchangedDataList, removeList, err := calcUnchangedDataList(passwd) if nil != err { return } - encryptedDataDir, upsertedSyncList, err := prepareSyncData(passwd, unchangedDataList) + encryptedDataDir, upsertList, err := prepareSyncData(passwd, unchangedDataList) if nil != err { util.LogErrorf("encrypt data dir failed: %s", err) return @@ -758,7 +758,7 @@ func recoverSyncData(modified map[string]bool) (decryptedDataDir string, upsertF return } -func prepareSyncData(passwd string, unchangedDataList map[string]bool) (encryptedDataDir string, upsertedSyncList map[string]bool, err error) { +func prepareSyncData(passwd string, unchangedDataList map[string]bool) (encryptedDataDir string, upsertList map[string]bool, err error) { encryptedDataDir = filepath.Join(util.WorkspaceDir, "incremental", "sync-encrypt") if err = os.RemoveAll(encryptedDataDir); nil != err { return @@ -851,7 +851,7 @@ func prepareSyncData(passwd string, unchangedDataList map[string]bool) (encrypte } } - upsertedSyncList = map[string]bool{} + upsertList = map[string]bool{} // 检查文件是否全部已经编入索引 err = filepath.Walk(encryptedDataDir, func(path string, info fs.FileInfo, _ error) error { if encryptedDataDir == path { @@ -866,7 +866,7 @@ func prepareSyncData(passwd string, unchangedDataList map[string]bool) (encrypte } if !info.IsDir() { - upsertedSyncList["/"+path] = true + upsertList["/"+path] = true } return nil }) @@ -984,8 +984,8 @@ func calcUnchangedSyncList() (ret map[string]bool, removes []string, err error) return } -// calcUnchangedDataList 计算 sync 和 data 一致(没有修改过)的文件列表 unchangedDataList,并删除 sync 中不存在于 data 中的多余文件 removedSyncList。 -func calcUnchangedDataList(passwd string) (unchangedDataList map[string]bool, removedSyncList map[string]bool, err error) { +// calcUnchangedDataList 计算 sync 和 data 一致(没有修改过)的文件列表 unchangedDataList,并删除 sync 中不存在于 data 中的多余文件 removeList。 +func calcUnchangedDataList(passwd string) (unchangedDataList map[string]bool, removeList map[string]bool, err error) { syncDir := Conf.Sync.GetSaveDir() meta := filepath.Join(syncDir, pathJSON) if !gulu.File.IsExist(meta) { @@ -1007,7 +1007,7 @@ func calcUnchangedDataList(passwd string) (unchangedDataList map[string]bool, re } unchangedDataList = map[string]bool{} - removedSyncList = map[string]bool{} + removeList = map[string]bool{} filepath.Walk(syncDir, func(path string, info fs.FileInfo, _ error) error { if syncDir == path || pathJSON == info.Name() { return nil @@ -1017,7 +1017,7 @@ func calcUnchangedDataList(passwd string) (unchangedDataList map[string]bool, re encryptedP = filepath.ToSlash(encryptedP) decryptedP := metaJSON[encryptedP] if "" == decryptedP { // 理论上不会发生 - removedSyncList[path] = true + removeList[path] = true if gulu.File.IsDir(path) { return filepath.SkipDir } @@ -1026,7 +1026,7 @@ func calcUnchangedDataList(passwd string) (unchangedDataList map[string]bool, re dataP := filepath.Join(util.DataDir, decryptedP) dataP = filepath.FromSlash(dataP) if !gulu.File.IsExist(dataP) { // data 已经删除的文件 - removedSyncList[path] = true + removeList[path] = true if gulu.File.IsDir(path) { return filepath.SkipDir } @@ -1044,7 +1044,7 @@ func calcUnchangedDataList(passwd string) (unchangedDataList map[string]bool, re tmp := map[string]bool{} // 在 sync 中删除 data 中已经删除的文件 - for remove, _ := range removedSyncList { + for remove, _ := range removeList { if strings.HasSuffix(remove, "index.json") { continue } @@ -1057,7 +1057,7 @@ func calcUnchangedDataList(passwd string) (unchangedDataList map[string]bool, re util.LogErrorf("remove [%s] failed: %s", remove, err) } } - removedSyncList = tmp + removeList = tmp return }