github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/kbfs/libkbfs/conflict_renamer.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 libkbfs
     6  
     7  import (
     8  	"fmt"
     9  	"time"
    10  
    11  	"github.com/keybase/client/go/kbfs/data"
    12  	"github.com/keybase/client/go/kbfs/kbfscrypto"
    13  	"github.com/keybase/client/go/kbfs/kbfsmd"
    14  	"github.com/keybase/client/go/protocol/keybase1"
    15  	"golang.org/x/net/context"
    16  )
    17  
    18  // WriterDeviceDateConflictRenamer renames a file using
    19  // a username, device name, and date.
    20  type WriterDeviceDateConflictRenamer struct {
    21  	config Config
    22  }
    23  
    24  // ConflictRename implements the ConflictRename interface for
    25  // TimeAndWriterConflictRenamer.
    26  func (cr WriterDeviceDateConflictRenamer) ConflictRename(
    27  	ctx context.Context, op op, original string) (string, error) {
    28  	now := cr.config.Clock().Now()
    29  	winfo := op.getWriterInfo()
    30  	ui, err := cr.config.KeybaseService().LoadUserPlusKeys(
    31  		ctx, winfo.uid, "", winfo.offline)
    32  	if err != nil {
    33  		return "", err
    34  	}
    35  	deviceName := ui.KIDNames[winfo.key.KID()]
    36  	return cr.ConflictRenameHelper(
    37  		now, string(ui.Name), deviceName, original), nil
    38  }
    39  
    40  // ConflictRenameHelper is a helper for ConflictRename especially useful from
    41  // tests.
    42  func (WriterDeviceDateConflictRenamer) ConflictRenameHelper(t time.Time, user, device, original string) string {
    43  	if device == "" {
    44  		device = "unknown"
    45  	}
    46  	base, ext := data.SplitFileExtension(original)
    47  	date := t.Format("2006-01-02")
    48  	return fmt.Sprintf("%s.conflicted (%s's %s copy %s)%s",
    49  		base, user, device, date, ext)
    50  }
    51  
    52  func newWriterInfo(
    53  	uid keybase1.UID, key kbfscrypto.VerifyingKey, revision kbfsmd.Revision,
    54  	offline keybase1.OfflineAvailability) writerInfo {
    55  	return writerInfo{
    56  		uid:      uid,
    57  		key:      key,
    58  		revision: revision,
    59  		offline:  offline,
    60  	}
    61  }