Skip to content

Commit

Permalink
After importing a previously deleted key, be able to delete it again.
Browse files Browse the repository at this point in the history
  • Loading branch information
codefather-filestar committed Jun 28, 2021
1 parent 7e7f64c commit b742b52
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion node/repo/fsrepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,17 +522,31 @@ func (fsr *fsLockedRepo) Get(name string) (types.KeyInfo, error) {
return res, nil
}

const KTrashPrefix = "trash-"

// Put saves key info under given name
func (fsr *fsLockedRepo) Put(name string, info types.KeyInfo) error {
return fsr.put(name, info, 0)
}

func (fsr *fsLockedRepo) put(rawName string, info types.KeyInfo, retries int) error {
if err := fsr.stillValid(); err != nil {
return err
}

name := rawName
if retries > 0 {
name = fmt.Sprintf("%s-%d", rawName, retries)
}

encName := base32.RawStdEncoding.EncodeToString([]byte(name))
keyPath := fsr.join(fsKeystore, encName)

_, err := os.Stat(keyPath)
if err == nil {
if err == nil && strings.HasPrefix(name, KTrashPrefix) {
// retry writing the trash-prefixed file with a number suffix
return fsr.put(rawName, info, retries+1)
}else if err == nil {
return xerrors.Errorf("checking key before put '%s': %w", name, types.ErrKeyExists)
} else if !os.IsNotExist(err) {
return xerrors.Errorf("checking key before put '%s': %w", name, err)
Expand Down

0 comments on commit b742b52

Please sign in to comment.