2018-10-05 03:18:34 +00:00
|
|
|
package storage
|
2018-10-06 03:50:55 +00:00
|
|
|
|
|
|
|
import (
|
2019-01-21 20:11:40 +00:00
|
|
|
"cwtch.im/cwtch/event"
|
2019-01-14 20:09:25 +00:00
|
|
|
"cwtch.im/cwtch/model"
|
2020-01-13 22:11:00 +00:00
|
|
|
"cwtch.im/cwtch/storage/v0"
|
|
|
|
"cwtch.im/cwtch/storage/v1"
|
2020-02-10 22:09:24 +00:00
|
|
|
"git.openprivacy.ca/openprivacy/log"
|
2020-01-13 22:11:00 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"path"
|
|
|
|
"strconv"
|
2018-10-06 03:50:55 +00:00
|
|
|
)
|
|
|
|
|
2019-01-29 20:56:59 +00:00
|
|
|
const profileFilename = "profile"
|
2020-01-13 22:11:00 +00:00
|
|
|
const versionFile = "VERSION"
|
|
|
|
const currentVersion = 1
|
2019-01-14 20:09:25 +00:00
|
|
|
|
2018-10-06 03:50:55 +00:00
|
|
|
// ProfileStore is an interface to managing the storage of Cwtch Profiles
|
|
|
|
type ProfileStore interface {
|
2019-01-21 20:11:40 +00:00
|
|
|
Shutdown()
|
2019-12-10 23:45:43 +00:00
|
|
|
Delete()
|
2019-07-19 17:27:50 +00:00
|
|
|
GetProfileCopy(timeline bool) *model.Profile
|
|
|
|
GetNewPeerMessage() *event.Event
|
|
|
|
GetStatusMessages() []*event.Event
|
2019-01-14 20:09:25 +00:00
|
|
|
}
|
|
|
|
|
2020-01-13 22:11:00 +00:00
|
|
|
// CreateProfileWriterStore creates a profile store backed by a filestore listening for events and saving them
|
2019-01-29 20:56:59 +00:00
|
|
|
// directory should be $appDir/profiles/$rand
|
2020-01-13 22:11:00 +00:00
|
|
|
func CreateProfileWriterStore(eventManager event.Manager, directory, password string, profile *model.Profile) ProfileStore {
|
|
|
|
return v1.CreateProfileWriterStore(eventManager, directory, password, profile)
|
2019-01-14 20:09:25 +00:00
|
|
|
}
|
|
|
|
|
2020-01-13 22:11:00 +00:00
|
|
|
// LoadProfileWriterStore loads a profile store from filestore listening for events and saving them
|
2019-05-15 20:12:11 +00:00
|
|
|
// directory should be $appDir/profiles/$rand
|
2020-01-13 22:11:00 +00:00
|
|
|
func LoadProfileWriterStore(eventManager event.Manager, directory, password string) (ProfileStore, error) {
|
|
|
|
versionCheckUpgrade(directory, password)
|
2019-06-05 20:40:55 +00:00
|
|
|
|
2020-01-13 22:11:00 +00:00
|
|
|
return v1.LoadProfileWriterStore(eventManager, directory, password)
|
|
|
|
}
|
2019-06-05 20:40:55 +00:00
|
|
|
|
2020-01-13 22:11:00 +00:00
|
|
|
// ReadProfile reads a profile from storage and returns the profile
|
|
|
|
// Should only be called for cache refresh of the profile after a ProfileWriterStore has opened
|
|
|
|
// (and upgraded) the store, and thus supplied the key/salt
|
|
|
|
func ReadProfile(directory string, key [32]byte, salt [128]byte) (*model.Profile, error) {
|
|
|
|
return v1.ReadProfile(directory, key, salt)
|
2019-05-15 20:12:11 +00:00
|
|
|
}
|
|
|
|
|
2019-02-04 18:25:58 +00:00
|
|
|
// NewProfile creates a new profile for use in the profile store.
|
|
|
|
func NewProfile(name string) *model.Profile {
|
|
|
|
profile := model.GenerateNewProfile(name)
|
|
|
|
return profile
|
2019-01-21 20:11:40 +00:00
|
|
|
}
|
|
|
|
|
2020-01-13 22:11:00 +00:00
|
|
|
// ********* Versioning and upgrade **********
|
2019-12-12 20:21:14 +00:00
|
|
|
|
2020-01-13 22:11:00 +00:00
|
|
|
func detectVersion(directory string) int {
|
|
|
|
vnumberStr, err := ioutil.ReadFile(path.Join(directory, versionFile))
|
|
|
|
if err != nil {
|
|
|
|
return 0
|
2019-12-12 20:21:14 +00:00
|
|
|
}
|
2020-01-13 22:11:00 +00:00
|
|
|
vnumber, err := strconv.Atoi(string(vnumberStr))
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Could not parse VERSION file contents: '%v' - %v\n", vnumber, err)
|
|
|
|
return -1
|
2019-05-15 20:12:11 +00:00
|
|
|
}
|
2020-01-13 22:11:00 +00:00
|
|
|
return vnumber
|
2019-01-14 20:09:25 +00:00
|
|
|
}
|
|
|
|
|
2020-01-13 22:11:00 +00:00
|
|
|
func upgradeV0ToV1(directory, password string) error {
|
|
|
|
log.Debugln("Attempting storage v0 to v1: Reading v0 profile...")
|
|
|
|
profile, err := v0.ReadProfile(directory, password)
|
2019-01-14 20:09:25 +00:00
|
|
|
if err != nil {
|
2019-01-21 20:11:40 +00:00
|
|
|
return err
|
2019-01-14 20:09:25 +00:00
|
|
|
}
|
2019-01-29 20:56:59 +00:00
|
|
|
|
2020-01-13 22:11:00 +00:00
|
|
|
log.Debugln("Attempting storage v0 to v1: Writing v1 profile...")
|
|
|
|
return v1.UpgradeV0Profile(profile, directory, password)
|
2019-01-21 20:11:40 +00:00
|
|
|
}
|
|
|
|
|
2020-01-13 22:11:00 +00:00
|
|
|
func versionCheckUpgrade(directory, password string) {
|
|
|
|
version := detectVersion(directory)
|
|
|
|
log.Infof("versionCheck: %v\n", version)
|
|
|
|
if version == -1 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if version == 0 {
|
|
|
|
err := upgradeV0ToV1(directory, password)
|
|
|
|
if err != nil {
|
2019-01-21 20:11:40 +00:00
|
|
|
return
|
|
|
|
}
|
2020-01-13 22:11:00 +00:00
|
|
|
//version = 1
|
2019-12-10 23:45:43 +00:00
|
|
|
}
|
|
|
|
}
|