github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/kbfs/libfs/sync_control_file.go (about)

     1  // Copyright 2017 Keybase Inc. All rights reserved.
     2  // Use of this source code is governed by a BSD
     3  // license that can be found in the LICENSE file.
     4  
     5  package libfs
     6  
     7  import (
     8  	"fmt"
     9  
    10  	"github.com/keybase/client/go/kbfs/data"
    11  	"github.com/keybase/client/go/kbfs/libkbfs"
    12  	"github.com/keybase/client/go/kbfs/tlfhandle"
    13  	"github.com/keybase/client/go/protocol/keybase1"
    14  	"golang.org/x/net/context"
    15  )
    16  
    17  // SyncAction enumerates all the possible actions to take on a
    18  // TLF's sync state.
    19  type SyncAction int
    20  
    21  const (
    22  	// SyncEnable is to enable syncing for a TLF.
    23  	SyncEnable SyncAction = iota
    24  	// SyncDisable is to disable syncing for a TLF.
    25  	SyncDisable
    26  )
    27  
    28  func (a SyncAction) String() string {
    29  	switch a {
    30  	case SyncEnable:
    31  		return "Enable syncing"
    32  	case SyncDisable:
    33  		return "Disable syncing"
    34  	}
    35  	return fmt.Sprintf("SyncAction(%d)", int(a))
    36  }
    37  
    38  // Execute performs the action on the given JournalManager for the
    39  // given TLF.
    40  func (a SyncAction) Execute(
    41  	ctx context.Context, c libkbfs.Config, fb data.FolderBranch,
    42  	h *tlfhandle.Handle) (err error) {
    43  	if fb == (data.FolderBranch{}) {
    44  		panic("zero fb in SyncAction.Execute")
    45  	}
    46  
    47  	// Ensure the TLF is initialized by getting the root node first.
    48  	_, _, err = c.KBFSOps().GetRootNode(ctx, h, data.MasterBranch)
    49  	if err != nil {
    50  		return err
    51  	}
    52  
    53  	switch a {
    54  	case SyncEnable:
    55  		_, err = c.KBFSOps().SetSyncConfig(
    56  			ctx, fb.Tlf, keybase1.FolderSyncConfig{
    57  				Mode: keybase1.FolderSyncMode_ENABLED,
    58  			})
    59  
    60  	case SyncDisable:
    61  		_, err = c.KBFSOps().SetSyncConfig(
    62  			ctx, fb.Tlf, keybase1.FolderSyncConfig{
    63  				Mode: keybase1.FolderSyncMode_DISABLED,
    64  			})
    65  
    66  	default:
    67  		return fmt.Errorf("Unknown action %s", a)
    68  	}
    69  	if err != nil {
    70  		return err
    71  	}
    72  	// Re-trigger prefetches.
    73  	_, _, err = c.KBFSOps().GetRootNode(ctx, h, fb.Branch)
    74  	return err
    75  }