/
githubmirror
/
client
Обзор
Документация
Войти
/
githubmirror
/
client
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
go/encrypteddb/file.go
50 строк
1 KB
zoom-ua
mark decryption failed errors as non-retryable; atomic writes for EncryptedDB (#29344)
22 июн 2026, 21:32
Не верифицирован
22 июн 2026, 21:32
a334649
Код
Авторство
О чём код?
package encrypteddb import ( "context" "os" "github.com/keybase/client/go/libkb" ) type EncryptedFile struct { libkb.Contextified getSecretBoxKey KeyFn path string } func NewFile(g *libkb.GlobalContext, path string, getSecretBoxKey KeyFn) *EncryptedFile { return &EncryptedFile{ Contextified: libkb.NewContextified(g), path: path, getSecretBoxKey: getSecretBoxKey, } } func (f *EncryptedFile) Get(ctx context.Context, res any) error { enc, err := os.ReadFile(f.path) if err != nil { return err } if err = DecodeBox(ctx, enc, f.getSecretBoxKey, res); err != nil { return err } return nil } func (f *EncryptedFile) Put(ctx context.Context, data any) error { b, err := EncodeBox(ctx, data, f.getSecretBoxKey) if err != nil { return err } // Write via a temp file + rename (libkb.SafeWriteToFile) so that an // interrupted write (app kill, crash, power loss) can never leave a // partially written file behind. A torn file would later fail to decrypt // and be indistinguishable from corruption. return libkb.NewFile(f.path, b, 0o600).Save(f.G().Log) } func (f *EncryptedFile) Remove(ctx context.Context) error { return os.Remove(f.path) }