Update mautrix-go and move crypto store to XDG_DATA_HOME

This commit is contained in:
Tulir Asokan 2020-04-28 22:00:37 +03:00
parent 92d4279b21
commit fa04323daf
6 changed files with 79 additions and 36 deletions

View File

@ -65,6 +65,7 @@ type Config struct {
NotifySound bool `yaml:"notify_sound"`
Dir string `yaml:"-"`
DataDir string `yaml:"data_dir"`
CacheDir string `yaml:"cache_dir"`
HistoryPath string `yaml:"history_path"`
RoomListPath string `yaml:"room_list_path"`
@ -81,9 +82,10 @@ type Config struct {
}
// NewConfig creates a config that loads data from the given directory.
func NewConfig(configDir, cacheDir, downloadDir string) *Config {
func NewConfig(configDir, dataDir, cacheDir, downloadDir string) *Config {
return &Config{
Dir: configDir,
DataDir: dataDir,
CacheDir: cacheDir,
DownloadDir: downloadDir,
HistoryPath: filepath.Join(cacheDir, "history.db"),
@ -108,8 +110,14 @@ func (config *Config) Clear() {
config.nosave = true
}
// ClearData clears non-temporary session data.
func (config *Config) ClearData() {
_ = os.RemoveAll(config.DataDir)
}
func (config *Config) CreateCacheDirs() {
_ = os.MkdirAll(config.CacheDir, 0700)
_ = os.MkdirAll(config.DataDir, 0700)
_ = os.MkdirAll(config.StateDir, 0700)
_ = os.MkdirAll(config.MediaDir, 0700)
}
@ -122,6 +130,7 @@ func (config *Config) DeleteSession() {
config.Rooms = rooms.NewRoomCache(config.RoomListPath, config.StateDir, config.RoomCacheSize, config.RoomCacheAge, config.GetUserID)
config.PushRules = nil
config.ClearData()
config.Clear()
config.nosave = false
config.CreateCacheDirs()

2
go.mod
View File

@ -21,7 +21,7 @@ require (
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e
gopkg.in/toast.v1 v1.0.0-20180812000517-0a84660828b2
gopkg.in/yaml.v2 v2.2.8
maunium.net/go/mautrix v0.2.0-beta.4.0.20200427215704-fe82e2b914c8
maunium.net/go/mautrix v0.2.0-beta.4.0.20200428185931-73915bafb538
maunium.net/go/mauview v0.1.0
maunium.net/go/tcell v0.1.0
)

2
go.sum
View File

@ -86,6 +86,8 @@ maunium.net/go/mautrix v0.2.0-beta.4.0.20200426213554-b07cb6dc1f6b h1:s3+wlMmmtp
maunium.net/go/mautrix v0.2.0-beta.4.0.20200426213554-b07cb6dc1f6b/go.mod h1:SkGZzch8CvU2qKtNpYxtzZ0sQxfVEJ3IsVVLSUBUx9Y=
maunium.net/go/mautrix v0.2.0-beta.4.0.20200427215704-fe82e2b914c8 h1:xflYDdpEonVTaw1Diq4z3ZK72Y8/TutiQgKOrHgfOCA=
maunium.net/go/mautrix v0.2.0-beta.4.0.20200427215704-fe82e2b914c8/go.mod h1:SkGZzch8CvU2qKtNpYxtzZ0sQxfVEJ3IsVVLSUBUx9Y=
maunium.net/go/mautrix v0.2.0-beta.4.0.20200428185931-73915bafb538 h1:E/6URkgRmxhNid8mA+PV25Bi8F/4Yg5nmsxIi/MWDrU=
maunium.net/go/mautrix v0.2.0-beta.4.0.20200428185931-73915bafb538/go.mod h1:SkGZzch8CvU2qKtNpYxtzZ0sQxfVEJ3IsVVLSUBUx9Y=
maunium.net/go/mauview v0.1.0 h1:x2WdkKI2zdriJuPAB0CKlwmnHGE7W9xfM5z6RgG+IIg=
maunium.net/go/mauview v0.1.0/go.mod h1:og9WbzmWe9SNYNyOFlCv8qa9zMcOvG2nzRJ5vYyud9U=
maunium.net/go/tcell v0.1.0 h1:XzsEoGCvOw5nac+tlkSLzQcliLYTN4PrtA7ar2ptjSM=

View File

@ -38,12 +38,12 @@ type Gomuks struct {
// NewGomuks creates a new Gomuks instance with everything initialized,
// but does not start it.
func NewGomuks(uiProvider ifc.UIProvider, configDir, cacheDir, downloadDir string) *Gomuks {
func NewGomuks(uiProvider ifc.UIProvider, configDir, dataDir, cacheDir, downloadDir string) *Gomuks {
gmx := &Gomuks{
stop: make(chan bool, 1),
}
gmx.config = config.NewConfig(configDir, cacheDir, downloadDir)
gmx.config = config.NewConfig(configDir, dataDir, cacheDir, downloadDir)
gmx.ui = uiProvider(gmx)
gmx.matrix = matrix.NewContainer(gmx)

86
main.go
View File

@ -48,24 +48,32 @@ func main() {
debug.Initialize()
defer debug.Recover()
configDir, err := UserConfigDir()
var configDir, dataDir, cacheDir, downloadDir string
var err error
configDir, err = UserConfigDir()
if err != nil {
fmt.Fprintln(os.Stderr, "Failed to get config directory:", err)
_, _ = fmt.Fprintln(os.Stderr, "Failed to get config directory:", err)
os.Exit(3)
}
cacheDir, err := UserCacheDir()
dataDir, err = UserDataDir()
if err != nil {
fmt.Fprintln(os.Stderr, "Failed to get cache directory:", err)
_, _ = fmt.Fprintln(os.Stderr, "Failed to get data directory:", err)
os.Exit(3)
}
downloadDir, err := UserDownloadDir()
cacheDir, err = UserCacheDir()
if err != nil {
fmt.Fprintln(os.Stderr, "Failed to get download directory:", err)
_, _ = fmt.Fprintln(os.Stderr, "Failed to get cache directory:", err)
os.Exit(3)
}
downloadDir, err = UserDownloadDir()
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, "Failed to get download directory:", err)
os.Exit(3)
}
gmx := NewGomuks(MainUIProvider, configDir, cacheDir, downloadDir)
gmx := NewGomuks(MainUIProvider, configDir, dataDir, cacheDir, downloadDir)
gmx.Start()
// We use os.Exit() everywhere, so exiting by returning from Start() shouldn't happen.
@ -74,8 +82,19 @@ func main() {
os.Exit(2)
}
func getRootDir(subdir string) string {
rootDir := os.Getenv("GOMUKS_ROOT")
if rootDir == "" {
return ""
}
return filepath.Join(rootDir, subdir)
}
func UserCacheDir() (dir string, err error) {
dir = os.Getenv("GOMUKS_CACHE_HOME")
if dir == "" {
dir = getRootDir("cache")
}
if dir == "" {
dir, err = os.UserCacheDir()
dir = filepath.Join(dir, "gomuks")
@ -83,34 +102,43 @@ func UserCacheDir() (dir string, err error) {
return
}
func UserDownloadDir() (dir string, err error) {
dir = os.Getenv("HOME")
return filepath.Join(dir, "Downloads"), nil
}
func UserConfigDir() (dir string, err error) {
dir = os.Getenv("GOMUKS_CONFIG_HOME")
func UserDataDir() (dir string, err error) {
dir = os.Getenv("GOMUKS_DATA_HOME")
if dir != "" {
return
}
if runtime.GOOS == "windows" {
dir = os.Getenv("AppData")
if runtime.GOOS == "windows" || runtime.GOOS == "darwin" {
return UserConfigDir()
}
dir = os.Getenv("XDG_DATA_HOME")
if dir == "" {
dir = getRootDir("data")
}
if dir == "" {
dir = os.Getenv("HOME")
if dir == "" {
err = errors.New("%AppData% is not defined")
}
} else {
dir = os.Getenv("XDG_CONFIG_HOME")
if dir == "" {
dir = os.Getenv("HOME")
if dir == "" {
err = errors.New("neither $XDG_CONFIG_HOME nor $HOME are defined")
} else if runtime.GOOS == "darwin" {
dir = filepath.Join(dir, "Library", "Application Support")
} else {
dir = filepath.Join(dir, ".config")
}
return "", errors.New("neither $XDG_CACHE_HOME nor $HOME are defined")
}
dir = filepath.Join(dir, ".local", "share")
}
dir = filepath.Join(dir, "gomuks")
return
}
func UserDownloadDir() (dir string, err error) {
dir, err = os.UserHomeDir()
dir = filepath.Join(dir, "Downloads")
return
}
func UserConfigDir() (dir string, err error) {
dir = os.Getenv("GOMUKS_CONFIG_HOME")
if dir == "" {
dir = getRootDir("cache")
}
if dir == "" {
dir, err = os.UserConfigDir()
dir = filepath.Join(dir, "gomuks")
}
return
}

View File

@ -135,7 +135,7 @@ func (c *Container) InitClient() error {
c.client.Logger = mxLogger{}
c.client.DeviceID = c.config.DeviceID
cryptoStore, err := crypto.NewGobStore(filepath.Join(c.config.CacheDir, "crypto.gob"))
cryptoStore, err := crypto.NewGobStore(filepath.Join(c.config.DataDir, "crypto.gob"))
if err != nil {
return err
}
@ -281,8 +281,8 @@ func (c *Container) Login(user, password string) error {
// Logout revokes the access token, stops the syncer and calls the OnLogout() method of the UI.
func (c *Container) Logout() {
c.client.Logout()
c.config.DeleteSession()
c.Stop()
c.config.DeleteSession()
c.client = nil
c.crypto = nil
c.ui.OnLogout()
@ -300,6 +300,11 @@ func (c *Container) Stop() {
debug.Print("Error closing history manager:", err)
}
c.history = nil
debug.Print("Flushing crypto store")
err = c.crypto.Store.Flush()
if err != nil {
debug.Print("Error flushing crypto store:", err)
}
}
}
@ -558,7 +563,6 @@ func (c *Container) HandleEncrypted(source EventSource, mxEvent *event.Event) {
debug.Print("Failed to decrypt event:", err)
return
}
debug.Print("!!!!!", evt)
c.HandleMessage(source, evt)
}