mirror of
https://github.com/restic/restic.git
synced 2024-08-25 13:47:22 +02:00
83 lines
1.4 KiB
Go
83 lines
1.4 KiB
Go
package khepri
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
"os/user"
|
|
"time"
|
|
)
|
|
|
|
type Snapshot struct {
|
|
Time time.Time `json:"time"`
|
|
TreeID ID `json:"tree"`
|
|
Dir string `json:"dir"`
|
|
Hostname string `json:"hostname,omitempty"`
|
|
Username string `json:"username,omitempty"`
|
|
UID string `json:"uid,omitempty"`
|
|
GID string `json:"gid,omitempty"`
|
|
}
|
|
|
|
func NewSnapshot(dir string) *Snapshot {
|
|
sn := &Snapshot{
|
|
Dir: dir,
|
|
Time: time.Now(),
|
|
}
|
|
|
|
hn, err := os.Hostname()
|
|
if err == nil {
|
|
sn.Hostname = hn
|
|
}
|
|
|
|
usr, err := user.Current()
|
|
if err == nil {
|
|
sn.Username = usr.Username
|
|
sn.UID = usr.Uid
|
|
sn.GID = usr.Gid
|
|
}
|
|
|
|
return sn
|
|
}
|
|
|
|
func (sn *Snapshot) Save(repo *Repository) (ID, error) {
|
|
if sn.TreeID == nil {
|
|
panic("Snapshot.Save() called with nil tree id")
|
|
}
|
|
|
|
obj, id_ch, err := repo.Create(TYPE_REF)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
enc := json.NewEncoder(obj)
|
|
err = enc.Encode(sn)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
err = obj.Close()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return <-id_ch, nil
|
|
}
|
|
|
|
func LoadSnapshot(repo *Repository, id ID) (*Snapshot, error) {
|
|
rd, err := repo.Get(TYPE_REF, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// TODO: maybe inject a hashing reader here and test if the given id is correct
|
|
|
|
dec := json.NewDecoder(rd)
|
|
sn := &Snapshot{}
|
|
err = dec.Decode(sn)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return sn, nil
|
|
}
|