diff --git a/src/git.xdrm.io/schastsp/client/client.go b/src/git.xdrm.io/schastsp/client/client.go index 7920ea1..86ed78a 100644 --- a/src/git.xdrm.io/schastsp/client/client.go +++ b/src/git.xdrm.io/schastsp/client/client.go @@ -33,7 +33,6 @@ func New(ctx *context.T, saveDir string) (*T, error) { /* (1) Store context */ inst.ctx = ctx; - /* (2) Get file management for KEY */ inst.fkey, err = Config(saveDir, "key"); if err != nil { return nil, err } @@ -42,14 +41,8 @@ func New(ctx *context.T, saveDir string) (*T, error) { inst.fsync, err = Config(saveDir, "sync"); if err != nil { return nil, err } - /* (4) Init KEY */ - inst.key = nil - - /* (5) Init SYNC */ - inst.sync = nil - - /* (6) Restore from config */ - inst.Update(); + /* (4) Restore from config */ + inst.update(); return inst, nil; @@ -60,39 +53,33 @@ func New(ctx *context.T, saveDir string) (*T, error) { /* (1) Updates 'key' and 'sync' with files (reads or write) * ---------------------------------------------------------*/ -func (c T) Update(){ +func (c T) update(){ - /* (1) Restore (both are NIL) + var err error; + + /* (1) Restore if both are NIL ---------------------------------------------------------*/ if c.key == nil && c.sync == nil { - /* (1) Fetch key */ - keyReader := c.fkey.Reader(); + /* (1) Create default key */ + c.key, err = keyset.Create(c.ctx); - if keyReader != nil { + /* (2) Fetch key */ + err = c.fkey.Fetch(c.key) - defer keyReader.Close(); + /* (3) On error -> set key to NIL */ + if err != nil { c.key = nil } - c.key, _ = keyset.Create(c.ctx); + /* (4) Create default sync */ + c.sync, err = keyset.Create(c.ctx); - c.key.Fetch(keyReader); + /* (5) Fetch sync */ + err = c.fsync.Fetch(c.sync) - } + /* (6) On error -> set sync to NIL */ + if err != nil { c.sync = nil } - /* (2) Fetch sync */ - syncReader := c.fsync.Reader(); - - if syncReader != nil { - - defer syncReader.Close(); - - c.sync, _ = keyset.Create(c.ctx); - - c.sync.Fetch(syncReader); - - } - - /* (3) Exit if no error */ + /* (7) Exit if all keysets have been fetched */ if c.key != nil && c.sync != nil { return; } @@ -101,7 +88,7 @@ func (c T) Update(){ - /* (2) Manage if no config + /* (2) If cannot fetch -> create new keysets ---------------------------------------------------------*/ if c.key == nil{ c.key, _ = keyset.Create(c.ctx); @@ -116,31 +103,11 @@ func (c T) Update(){ /* (3) Store current value ---------------------------------------------------------*/ /* (1) Store key */ - c.key, _ = keyset.Create(c.ctx); - - keyWriter := c.fkey.Writer(); - - if keyWriter != nil { - - defer keyWriter.Close(); - - c.key, _ = keyset.Create(c.ctx); - - c.key.Store(keyWriter); - } + err = c.fkey.Store(c.key) + if err != nil { panic("Cannot store key") } /* (2) Store sync */ - c.sync, _ = keyset.Create(c.ctx); - - syncWriter := c.fsync.Writer(); - - if syncWriter != nil { - - defer syncWriter.Close(); - - c.sync, _ = keyset.Create(c.ctx); - - c.sync.Store(syncWriter); - } + err = c.fsync.Store(c.sync) + if err != nil { panic("Cannot store sync") } } \ No newline at end of file