2018-04-21 17:47:44 +00:00
|
|
|
package client;
|
|
|
|
|
|
|
|
import (
|
2018-04-25 06:17:20 +00:00
|
|
|
"git.xdrm.io/schastsp/internal/client/keyset"
|
2018-04-21 17:47:44 +00:00
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
"errors"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type config struct {
|
|
|
|
path string // absolute configuration file path
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (1) Constructor
|
|
|
|
*
|
|
|
|
* @dir<string> Dir path
|
|
|
|
* @name<string> file name
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
func Config(dir string, name string) (*config, error) {
|
|
|
|
|
|
|
|
inst := new(config);
|
|
|
|
|
2018-04-21 22:58:38 +00:00
|
|
|
/* (1) Build full path */
|
2018-04-21 17:47:44 +00:00
|
|
|
absoluteDir, err := filepath.Abs(dir);
|
|
|
|
if err != nil { return nil, err; }
|
|
|
|
|
2018-04-21 22:58:38 +00:00
|
|
|
basename := filepath.Base(name);
|
|
|
|
fullpath := fmt.Sprintf("%s/%s", absoluteDir, basename);
|
2018-04-21 17:47:44 +00:00
|
|
|
|
|
|
|
|
2018-04-21 22:58:38 +00:00
|
|
|
/* (2) Check directory */
|
|
|
|
if info, err := os.Stat(absoluteDir); err != nil {
|
2018-04-21 17:47:44 +00:00
|
|
|
|
2018-04-21 22:58:38 +00:00
|
|
|
// Unknown error
|
|
|
|
if !os.IsNotExist(err) { return nil, err }
|
2018-04-21 17:47:44 +00:00
|
|
|
|
2018-04-21 22:58:38 +00:00
|
|
|
// not exists -> try to create dir
|
|
|
|
err2 := os.MkdirAll(absoluteDir, 0755);
|
|
|
|
if err2 != nil { return nil, err2 }
|
2018-04-21 17:47:44 +00:00
|
|
|
|
2018-04-21 22:58:38 +00:00
|
|
|
// fail if not a directory
|
|
|
|
} else if !info.Mode().IsDir() {
|
|
|
|
return nil, errors.New("Configuration dir is not a directory");
|
2018-04-21 17:47:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* (3) Check file */
|
|
|
|
info, err := os.Stat(fullpath);
|
|
|
|
if err != nil {
|
|
|
|
|
2018-04-21 22:58:38 +00:00
|
|
|
// Unknown error
|
|
|
|
if !os.IsNotExist(err) { return nil, err }
|
2018-04-21 17:47:44 +00:00
|
|
|
|
2018-04-21 22:58:38 +00:00
|
|
|
// File does not exist -> try to create file
|
|
|
|
_, err2 := os.Create(fullpath);
|
|
|
|
if err2 != nil { return nil, err2 }
|
2018-04-21 17:47:44 +00:00
|
|
|
|
|
|
|
|
2018-04-21 22:58:38 +00:00
|
|
|
// fail if exists but not regular file
|
|
|
|
} else if !info.Mode().IsRegular() {
|
|
|
|
return nil, errors.New("Configuration file is not a regular file");
|
2018-04-21 17:47:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inst.path = fullpath
|
|
|
|
|
|
|
|
return inst, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-21 22:58:38 +00:00
|
|
|
/* (2) Fetches a keyset from a file
|
2018-04-21 17:47:44 +00:00
|
|
|
*
|
2018-04-21 22:58:38 +00:00
|
|
|
* @ks<*keyset.T> Set to fetch into
|
|
|
|
*
|
|
|
|
* @return err<error> Fetching error
|
2018-04-21 17:47:44 +00:00
|
|
|
* NIL on error
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
2018-04-21 22:58:38 +00:00
|
|
|
func (c config) Fetch(ks *keyset.T) (err error) {
|
|
|
|
|
|
|
|
/* (1) Open file */
|
|
|
|
file, err := os.Open(c.path)
|
|
|
|
if err != nil { return err }
|
2018-04-21 17:47:44 +00:00
|
|
|
|
2018-04-21 22:58:38 +00:00
|
|
|
/* (2) Defer close */
|
|
|
|
defer file.Close()
|
2018-04-21 17:47:44 +00:00
|
|
|
|
2018-04-21 22:58:38 +00:00
|
|
|
/* (3) Fetch from file */
|
|
|
|
err = ks.Fetch(file);
|
|
|
|
if err != nil { return err }
|
|
|
|
|
|
|
|
return nil
|
2018-04-21 17:47:44 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-21 22:58:38 +00:00
|
|
|
/* (3) Stores a keyset into file
|
|
|
|
*
|
|
|
|
* @ks<*keyset.T> Set to store
|
2018-04-21 17:47:44 +00:00
|
|
|
*
|
2018-04-21 22:58:38 +00:00
|
|
|
* @return err<error> Storage error
|
2018-04-21 17:47:44 +00:00
|
|
|
* NIL on error
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
2018-04-21 22:58:38 +00:00
|
|
|
func (c config) Store(ks *keyset.T) error {
|
2018-04-21 17:47:44 +00:00
|
|
|
|
2018-04-21 22:58:38 +00:00
|
|
|
/* (1) Open file */
|
2018-04-21 17:47:44 +00:00
|
|
|
file, err := os.OpenFile(c.path, os.O_RDWR|os.O_CREATE, 0755);
|
2018-04-21 22:58:38 +00:00
|
|
|
if err != nil { return err }
|
|
|
|
|
|
|
|
/* (2) Defer close */
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
/* (3) Store into file */
|
|
|
|
err = ks.Store(file)
|
|
|
|
if err != nil { return err }
|
2018-04-21 17:47:44 +00:00
|
|
|
|
2018-04-21 22:58:38 +00:00
|
|
|
return nil
|
2018-04-21 17:47:44 +00:00
|
|
|
|
|
|
|
}
|