mirror of
https://github.com/restic/restic.git
synced 2026-09-29 15:30:29 +00:00
Refactor
This commit is contained in:
@@ -1,78 +1,60 @@
|
||||
package khepri
|
||||
|
||||
import "os"
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
type Object struct {
|
||||
type createObject struct {
|
||||
repo *Repository
|
||||
|
||||
id ID
|
||||
tpe Type
|
||||
|
||||
hw HashingWriter
|
||||
file *os.File
|
||||
|
||||
ch chan ID
|
||||
}
|
||||
|
||||
func (repo *Repository) NewObject(t Type) (*Object, error) {
|
||||
obj := &Object{
|
||||
func (repo *Repository) Create(t Type) (io.WriteCloser, <-chan ID, error) {
|
||||
obj := &createObject{
|
||||
repo: repo,
|
||||
tpe: t,
|
||||
ch: make(chan ID, 1),
|
||||
}
|
||||
|
||||
return obj, obj.open()
|
||||
// save contents to tempfile in repository, hash while writing
|
||||
var err error
|
||||
obj.file, err = obj.repo.tempFile()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
// create hashing writer
|
||||
obj.hw = NewHashingWriter(obj.file, obj.repo.hash)
|
||||
|
||||
return obj, obj.ch, nil
|
||||
}
|
||||
|
||||
func (obj *Object) open() error {
|
||||
if obj.isFinal() {
|
||||
panic("object is finalized")
|
||||
}
|
||||
|
||||
if obj.isOpen() {
|
||||
panic("object already open")
|
||||
}
|
||||
|
||||
// create tempfile in repository
|
||||
func (obj *createObject) Write(data []byte) (int, error) {
|
||||
if obj.hw == nil {
|
||||
// save contents to tempfile, hash while writing
|
||||
var err error
|
||||
obj.file, err = obj.repo.tempFile()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// create hashing writer
|
||||
obj.hw = NewHashingWriter(obj.file, obj.repo.hash)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (obj *Object) isOpen() bool {
|
||||
return obj.file != nil && obj.hw != nil
|
||||
}
|
||||
|
||||
func (obj *Object) isFinal() bool {
|
||||
return obj.id != nil
|
||||
}
|
||||
|
||||
func (obj *Object) Write(data []byte) (int, error) {
|
||||
if !obj.isOpen() {
|
||||
panic("object not open")
|
||||
panic("createObject: already closed!")
|
||||
}
|
||||
|
||||
return obj.hw.Write(data)
|
||||
}
|
||||
|
||||
func (obj *Object) Close() error {
|
||||
if obj.file == nil || obj.hw == nil {
|
||||
panic("object is not open")
|
||||
func (obj *createObject) Close() error {
|
||||
if obj.hw == nil {
|
||||
panic("createObject: already closed!")
|
||||
}
|
||||
|
||||
obj.file.Close()
|
||||
|
||||
hash := obj.hw.Hash()
|
||||
id := ID(obj.hw.Hash())
|
||||
obj.ch <- id
|
||||
|
||||
// move file to final name using hash of contents
|
||||
id := ID(hash)
|
||||
err := obj.repo.renameFile(obj.file, obj.tpe, id)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -80,40 +62,5 @@ func (obj *Object) Close() error {
|
||||
|
||||
obj.hw = nil
|
||||
obj.file = nil
|
||||
|
||||
obj.id = id
|
||||
return nil
|
||||
}
|
||||
|
||||
func (obj *Object) ID() ID {
|
||||
if !obj.isFinal() {
|
||||
panic("object not finalized")
|
||||
}
|
||||
|
||||
return obj.id
|
||||
}
|
||||
|
||||
func (obj *Object) Type() Type {
|
||||
return obj.tpe
|
||||
}
|
||||
|
||||
func (obj *Object) Remove() error {
|
||||
if obj.id != nil {
|
||||
return obj.repo.Remove(obj.tpe, obj.id)
|
||||
}
|
||||
|
||||
if obj.file != nil {
|
||||
file := obj.file
|
||||
obj.hw = nil
|
||||
obj.file = nil
|
||||
|
||||
err := file.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return os.Remove(file.Name())
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user