github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/kbfs/libfs/journal_control_file.go (about)

     1  // Copyright 2016 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/libkbfs"
    11  	"github.com/keybase/client/go/kbfs/tlf"
    12  	"github.com/keybase/client/go/kbfs/tlfhandle"
    13  	"golang.org/x/net/context"
    14  )
    15  
    16  // JournalAction enumerates all the possible actions to take on a
    17  // TLF's journal.
    18  type JournalAction int
    19  
    20  const (
    21  	// JournalEnable is to turn the journal on.
    22  	JournalEnable JournalAction = iota
    23  	// JournalFlush is to flush the journal.
    24  	JournalFlush
    25  	// JournalPauseBackgroundWork is to pause journal background
    26  	// work.
    27  	JournalPauseBackgroundWork
    28  	// JournalResumeBackgroundWork is to resume journal background
    29  	// work.
    30  	JournalResumeBackgroundWork
    31  	// JournalDisable is to disable the journal.
    32  	JournalDisable
    33  	// JournalEnableAuto is to turn on journals for all TLFs, persistently.
    34  	JournalEnableAuto
    35  	// JournalDisableAuto is to turn off automatic journaling for new TLFs.
    36  	JournalDisableAuto
    37  )
    38  
    39  func (a JournalAction) String() string {
    40  	switch a {
    41  	case JournalEnable:
    42  		return "Enable journal"
    43  	case JournalFlush:
    44  		return "Flush journal"
    45  	case JournalPauseBackgroundWork:
    46  		return "Pause journal background work"
    47  	case JournalResumeBackgroundWork:
    48  		return "Resume journal background work"
    49  	case JournalDisable:
    50  		return "Disable journal"
    51  	case JournalEnableAuto:
    52  		return "Enable auto-journals"
    53  	case JournalDisableAuto:
    54  		return "Disable auto-journals"
    55  	}
    56  	return fmt.Sprintf("JournalAction(%d)", int(a))
    57  }
    58  
    59  // Execute performs the action on the given JournalManager for the
    60  // given TLF.
    61  func (a JournalAction) Execute(
    62  	ctx context.Context, jManager *libkbfs.JournalManager,
    63  	tlfID tlf.ID, h *tlfhandle.Handle) error {
    64  	// These actions don't require TLF IDs.
    65  	switch a {
    66  	case JournalEnableAuto:
    67  		return jManager.EnableAuto(ctx)
    68  
    69  	case JournalDisableAuto:
    70  		return jManager.DisableAuto(ctx)
    71  	}
    72  
    73  	if tlfID == (tlf.ID{}) {
    74  		panic("zero TlfID in JournalAction.Execute")
    75  	}
    76  
    77  	switch a {
    78  	case JournalEnable:
    79  		err := jManager.Enable(
    80  			ctx, tlfID, h, libkbfs.TLFJournalBackgroundWorkEnabled)
    81  		if err != nil {
    82  			return err
    83  		}
    84  
    85  	case JournalFlush:
    86  		err := jManager.Flush(ctx, tlfID)
    87  		if err != nil {
    88  			return err
    89  		}
    90  
    91  	case JournalPauseBackgroundWork:
    92  		jManager.PauseBackgroundWork(ctx, tlfID)
    93  
    94  	case JournalResumeBackgroundWork:
    95  		jManager.ResumeBackgroundWork(ctx, tlfID)
    96  
    97  	case JournalDisable:
    98  		_, err := jManager.Disable(ctx, tlfID)
    99  		if err != nil {
   100  			return err
   101  		}
   102  
   103  	default:
   104  		return fmt.Errorf("Unknown action %s", a)
   105  	}
   106  
   107  	return nil
   108  }