Browse Source

:art: Flashcards are not allowed to be modified during data sync to avoid data overwriting https://github.com/siyuan-note/siyuan/issues/10167

Daniel 1 year ago
parent
commit
651f245662
2 changed files with 16 additions and 3 deletions
  1. 2 2
      kernel/model/flashcard.go
  2. 14 1
      kernel/model/repository.go

+ 2 - 2
kernel/model/flashcard.go

@@ -641,7 +641,7 @@ func (tx *Transaction) doRemoveFlashcards(operation *Operation) (ret *TxErr) {
 	deckLock.Lock()
 	defer deckLock.Unlock()
 
-	if isSyncing.Load() {
+	if isSyncingStorages() {
 		ret = &TxErr{code: TxErrCodeDataIsSyncing}
 		return
 	}
@@ -753,7 +753,7 @@ func (tx *Transaction) doAddFlashcards(operation *Operation) (ret *TxErr) {
 	deckLock.Lock()
 	defer deckLock.Unlock()
 
-	if isSyncing.Load() {
+	if isSyncingStorages() {
 		ret = &TxErr{code: TxErrCodeDataIsSyncing}
 		return
 	}

+ 14 - 1
kernel/model/repository.go

@@ -955,11 +955,15 @@ var syncingFiles = sync.Map{}
 var syncingStorages = atomic.Bool{}
 
 func waitForSyncingStorages() {
-	for syncingStorages.Load() {
+	for isSyncingStorages() {
 		time.Sleep(time.Second)
 	}
 }
 
+func isSyncingStorages() bool {
+	return syncingStorages.Load() || isBootSyncing.Load()
+}
+
 func IsSyncingFile(rootID string) (ret bool) {
 	_, ret = syncingFiles.Load(rootID)
 	return
@@ -1105,6 +1109,8 @@ func syncRepoUpload() (err error) {
 	return
 }
 
+var isBootSyncing = atomic.Bool{}
+
 func bootSyncRepo() (err error) {
 	if 1 > len(Conf.Repo.Key) {
 		autoSyncErrCount++
@@ -1129,11 +1135,14 @@ func bootSyncRepo() (err error) {
 		return
 	}
 
+	isBootSyncing.Store(true)
+
 	start := time.Now()
 	_, _, err = indexRepoBeforeCloudSync(repo)
 	if nil != err {
 		autoSyncErrCount++
 		planSyncAfter(fixSyncInterval)
+		isBootSyncing.Store(false)
 		return
 	}
 
@@ -1180,17 +1189,21 @@ func bootSyncRepo() (err error) {
 		util.PushStatusBar(msg)
 		util.PushErrMsg(msg, 0)
 		BootSyncSucc = 1
+		isBootSyncing.Store(false)
 		return
 	}
 
 	if 0 < len(fetchedFiles) {
 		go func() {
 			_, syncErr := syncRepo(false, false)
+			isBootSyncing.Store(false)
 			if nil != err {
 				logging.LogErrorf("boot background sync repo failed: %s", syncErr)
 				return
 			}
 		}()
+	} else {
+		isBootSyncing.Store(false)
 	}
 	return
 }