Official cwtch.im peer and server implementations. https://cwtch.im
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

profile_store.go 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package storage
  2. import (
  3. "cwtch.im/cwtch/event"
  4. "cwtch.im/cwtch/model"
  5. "cwtch.im/cwtch/storage/v0"
  6. "cwtch.im/cwtch/storage/v1"
  7. "git.openprivacy.ca/openprivacy/libricochet-go/log"
  8. "io/ioutil"
  9. "path"
  10. "strconv"
  11. )
  12. const profileFilename = "profile"
  13. const versionFile = "VERSION"
  14. const currentVersion = 1
  15. // ProfileStore is an interface to managing the storage of Cwtch Profiles
  16. type ProfileStore interface {
  17. Shutdown()
  18. Delete()
  19. GetProfileCopy(timeline bool) *model.Profile
  20. GetNewPeerMessage() *event.Event
  21. GetStatusMessages() []*event.Event
  22. }
  23. // CreateProfileWriterStore creates a profile store backed by a filestore listening for events and saving them
  24. // directory should be $appDir/profiles/$rand
  25. func CreateProfileWriterStore(eventManager event.Manager, directory, password string, profile *model.Profile) ProfileStore {
  26. return v1.CreateProfileWriterStore(eventManager, directory, password, profile)
  27. }
  28. // LoadProfileWriterStore loads a profile store from filestore listening for events and saving them
  29. // directory should be $appDir/profiles/$rand
  30. func LoadProfileWriterStore(eventManager event.Manager, directory, password string) (ProfileStore, error) {
  31. versionCheckUpgrade(directory, password)
  32. return v1.LoadProfileWriterStore(eventManager, directory, password)
  33. }
  34. // ReadProfile reads a profile from storage and returns the profile
  35. // Should only be called for cache refresh of the profile after a ProfileWriterStore has opened
  36. // (and upgraded) the store, and thus supplied the key/salt
  37. func ReadProfile(directory string, key [32]byte, salt [128]byte) (*model.Profile, error) {
  38. return v1.ReadProfile(directory, key, salt)
  39. }
  40. // NewProfile creates a new profile for use in the profile store.
  41. func NewProfile(name string) *model.Profile {
  42. profile := model.GenerateNewProfile(name)
  43. return profile
  44. }
  45. // ********* Versioning and upgrade **********
  46. func detectVersion(directory string) int {
  47. vnumberStr, err := ioutil.ReadFile(path.Join(directory, versionFile))
  48. if err != nil {
  49. return 0
  50. }
  51. vnumber, err := strconv.Atoi(string(vnumberStr))
  52. if err != nil {
  53. log.Errorf("Could not parse VERSION file contents: '%v' - %v\n", vnumber, err)
  54. return -1
  55. }
  56. return vnumber
  57. }
  58. func upgradeV0ToV1(directory, password string) error {
  59. log.Debugln("Attempting storage v0 to v1: Reading v0 profile...")
  60. profile, err := v0.ReadProfile(directory, password)
  61. if err != nil {
  62. return err
  63. }
  64. log.Debugln("Attempting storage v0 to v1: Writing v1 profile...")
  65. return v1.UpgradeV0Profile(profile, directory, password)
  66. }
  67. func versionCheckUpgrade(directory, password string) {
  68. version := detectVersion(directory)
  69. log.Infof("versionCheck: %v\n", version)
  70. if version == -1 {
  71. return
  72. }
  73. if version == 0 {
  74. err := upgradeV0ToV1(directory, password)
  75. if err != nil {
  76. return
  77. }
  78. //version = 1
  79. }
  80. }