- settings: get filter choices from storage for settings validation - updater: update servers to the storage - storage: minimal deep copying and data duplication - storage: add merged servers mutex for thread safety - connection: filter servers in storage - formatter: format servers to Markdown in storage - PIA: get server by name from storage directly - Updater: get servers count from storage directly - Updater: equality check done in storage, fix #882
42 lines
866 B
Go
42 lines
866 B
Go
package storage
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
)
|
|
|
|
// FlushToFile flushes the merged servers data to the file
|
|
// specified by path, as indented JSON.
|
|
func (s *Storage) FlushToFile(path string) error {
|
|
s.mergedMutex.RLock()
|
|
defer s.mergedMutex.RUnlock()
|
|
|
|
return s.flushToFile(path)
|
|
}
|
|
|
|
// flushToFile flushes the merged servers data to the file
|
|
// specified by path, as indented JSON. It is not thread-safe.
|
|
func (s *Storage) flushToFile(path string) error {
|
|
dirPath := filepath.Dir(path)
|
|
if err := os.MkdirAll(dirPath, 0644); err != nil {
|
|
return err
|
|
}
|
|
|
|
file, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
encoder := json.NewEncoder(file)
|
|
encoder.SetIndent("", " ")
|
|
|
|
err = encoder.Encode(&s.mergedServers)
|
|
if err != nil {
|
|
_ = file.Close()
|
|
return err
|
|
}
|
|
|
|
return file.Close()
|
|
}
|