Skip to content

Commit

Permalink
Recreate lockfile on close and remove lockfile with new interface Clo…
Browse files Browse the repository at this point in the history
…se func
  • Loading branch information
sa6mwa committed Mar 21, 2023
1 parent 38dbe38 commit 34e4548
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 20 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
*~
\#*\#
/.emacs.desktop
/.emacs.desktop.lock
*.elc
auto-save-list
tramp
.\#*
121 changes: 101 additions & 20 deletions anystore.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Example:
if err != nil {
log.Fatal(err)
}
defer persisted.Close()
if err := ephemeral.Store("hello", "world"); err != nil {
log.Fatal(err)
Expand Down Expand Up @@ -57,12 +58,10 @@ Example:
}
log.Println(v)
}
AnyStore also feature a configuration mode with convenience-functions Stash,
Unstash and EditThing. Whether you choose to hard-code an encryption key in the
application or provide one via environment variables, using Stash, Unstash and
EditThing is simple...
AnyStore also feature a configuration mode with convenience-functions
Stash, Unstash and EditThing. Whether you choose to hard-code an
encryption key in the application or provide one via environment
variables, using Stash, Unstash and EditThing is simple...
package main
Expand Down Expand Up @@ -217,6 +216,9 @@ type AnyStore interface {
// function is returned by Run.
Run(atomicOperation func(s AnyStore) error) error

// If persistence is enabled, Close removes the lockfile.
Close() error

load() error

loadStoreAndSave(key any, value any, remove bool) error
Expand Down Expand Up @@ -450,6 +452,30 @@ func (a *anyStore) Run(atomicOperation func(s AnyStore) error) error {
return atomicOperation(anyStoreOverride)
}

func (a *anyStore) Close() error {
if a.persist.Load() {
// Lock the store
a.mutex.Lock()
defer a.mutex.Unlock()
file, ok := a.savefile.Load().(string)
if !ok {
return errors.New("persistence not set")
}
lockfile := file + ".lock"
_, err := os.Stat(lockfile)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return nil
}
return err
}
if err := os.Remove(lockfile); err != nil {
return err
}
}
return nil
}

func (a *anyStore) load() error {
file, ok := a.savefile.Load().(string)
if !ok {
Expand Down Expand Up @@ -521,13 +547,30 @@ func (a *anyStore) loadStoreAndSave(key any, value any, remove bool) error {
}
lockfile := file + ".lock"
unlink := true
lockfd, err := syscall.Open(lockfile, syscall.O_CREAT|syscall.O_RDWR, 0666)
if err != nil {
return err
}
defer syscall.Close(lockfd)
if err := syscall.Flock(lockfd, syscall.LOCK_EX); err != nil {
return err
var lockfd int
for {
var err error
lockfd, err = syscall.Open(lockfile, syscall.O_CREAT|syscall.O_RDWR, 0666)
if err != nil {
return err
}
if err := syscall.Flock(lockfd, syscall.LOCK_EX); err != nil {
syscall.Close(lockfd)
return err
}
var stat_t syscall.Stat_t
if err := syscall.Fstat(lockfd, &stat_t); err != nil {
syscall.Close(lockfd)
return err
}
if stat_t.Nlink == 0 {
// File deleted (no hard links), recreate it
syscall.Close(lockfd)
continue
}
// We should have a lockfd with an existing file at this point
defer syscall.Close(lockfd)
break
}
f, err := os.OpenFile(file, os.O_CREATE|os.O_RDWR, 0666)
if err != nil {
Expand Down Expand Up @@ -767,6 +810,27 @@ func (u *unsafeAnyStore) Run(atomicOperation func(s AnyStore) error) error {
return atomicOperation(u)
}

func (u *unsafeAnyStore) Close() error {
if u.persist.Load() {
file, ok := u.savefile.Load().(string)
if !ok {
return errors.New("persistence not set")
}
lockfile := file + ".lock"
_, err := os.Stat(lockfile)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return nil
}
return err
}
if err := os.Remove(lockfile); err != nil {
return err
}
}
return nil
}

func (u *unsafeAnyStore) load() error {
file, ok := u.savefile.Load().(string)
if !ok {
Expand Down Expand Up @@ -838,13 +902,30 @@ func (u *unsafeAnyStore) loadStoreAndSave(key any, value any, remove bool) error
}
lockfile := file + ".lock"
unlink := true
lockfd, err := syscall.Open(lockfile, syscall.O_CREAT|syscall.O_RDWR, 0666)
if err != nil {
return err
}
defer syscall.Close(lockfd)
if err := syscall.Flock(lockfd, syscall.LOCK_EX); err != nil {
return err
var lockfd int
for {
var err error
lockfd, err = syscall.Open(lockfile, syscall.O_CREAT|syscall.O_RDWR, 0666)
if err != nil {
return err
}
if err := syscall.Flock(lockfd, syscall.LOCK_EX); err != nil {
syscall.Close(lockfd)
return err
}
var stat_t syscall.Stat_t
if err := syscall.Fstat(lockfd, &stat_t); err != nil {
syscall.Close(lockfd)
return err
}
if stat_t.Nlink == 0 {
// File deleted (no hard links), recreate it
syscall.Close(lockfd)
continue
}
// We should have a lockfd with an existing file at this point
defer syscall.Close(lockfd)
break
}
f, err := os.OpenFile(file, os.O_CREATE|os.O_RDWR, 0666)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions examples/edit-stash-2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func main() {
}); err != nil {
log.Fatal(err)
}
fmt.Println("Saved configuration as ", file)
}

j, err := json.MarshalIndent(configuration, "", " ")
Expand Down
2 changes: 2 additions & 0 deletions stash.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func Unstash(conf *StashConfig) error {
if err != nil {
return err
}
defer a.Close()
var gobbedThing any
if conf.Reader != nil {
// Read encrypted anyMap
Expand Down Expand Up @@ -202,6 +203,7 @@ func Stash(conf *StashConfig) error {
if err != nil {
return err
}
defer a.Close()

// Use gob to store the struct (or other value) instead of re-inventing
// dereference of all pointers. It is also unlikely that the interface stored
Expand Down

0 comments on commit 34e4548

Please sign in to comment.