Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

z0 - fix storage load/save #139

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions src/cmd/vpnUp.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (
"time"

"github.com/pkg/errors"
"github.com/zeropsio/zcli/src/uxBlock"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"

"github.com/zeropsio/zcli/src/uxBlock"

"github.com/zeropsio/zcli/src/cliStorage"
"github.com/zeropsio/zcli/src/cmd/scope"
"github.com/zeropsio/zcli/src/cmdBuilder"
Expand Down Expand Up @@ -92,13 +93,19 @@ func vpnUpCmd() *cmdBuilder.Cmd {
return err
}

f, err := file.Open(filePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, fileMode)
if err != nil {
return err
}
if err := func() error {
f, err := file.Open(filePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, fileMode)
if err != nil {
return err
}
defer f.Close()

err = wg.GenerateConfig(f, privateKey, vpnSettings)
if err != nil {
err = wg.GenerateConfig(f, privateKey, vpnSettings)
if err != nil {
return err
}
return nil
}(); err != nil {
return err
}

Expand Down
16 changes: 0 additions & 16 deletions src/storage/exists.go

This file was deleted.

44 changes: 20 additions & 24 deletions src/storage/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package storage
import (
"encoding/json"
"os"
"path/filepath"
"sync"

"github.com/pkg/errors"
Expand Down Expand Up @@ -34,43 +35,34 @@ func New[T any](config Config) (*Handler[T], error) {
}

func (h *Handler[T]) load() error {
storageFileExists, err := FileExists(h.config.FilePath)
if err != nil {
return errors.WithStack(err)
}
if !storageFileExists {
return nil
{
f, err := os.Stat(h.config.FilePath)
if os.IsNotExist(err) {
return nil
}
if err != nil {
return errors.WithStack(err)
}
if f.Size() == 0 {
if err := os.Remove(h.config.FilePath); err != nil {
return errors.WithStack(err)
}
Comment on lines +47 to +49
Copy link
Contributor

@janhajek-outreach janhajek-outreach Apr 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason to delete the file?

return nil
}
}

f, err := file.Open(h.config.FilePath, os.O_RDONLY, h.config.FileMode)
f, err := os.Open(h.config.FilePath)
Copy link
Contributor

@janhajek-outreach janhajek-outreach Apr 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you intentionally removed the h.config.FileMode part? The file can contain a private key, so we agreed on 0600 permissions.

#135 (comment)

if err != nil {
return errors.WithStack(err)
}
defer f.Close()

// If the file is empty, set the default value and save it.
fi, err := f.Stat()
if err != nil {
return errors.WithStack(err)
}
if fi.Size() == 0 {
return h.Clear()
}

if err := json.NewDecoder(f).Decode(&h.data); err != nil {
return errors.WithMessagef(err, i18n.T(i18n.UnableToDecodeJsonFile, h.config.FilePath))
}

return nil
}

func (h *Handler[T]) Clear() error {
h.lock.Lock()
defer h.lock.Unlock()
var data T
return h.save(data)
}

func (h *Handler[T]) Update(callback func(T) T) (T, error) {
h.lock.Lock()
defer h.lock.Unlock()
Expand All @@ -82,6 +74,9 @@ func (h *Handler[T]) save(data T) error {
h.data = data

if err := func() error {
if err := os.MkdirAll(filepath.Dir(h.config.FilePath), 0755); err != nil {
Copy link
Contributor

@janhajek-outreach janhajek-outreach Apr 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 0755 is intentional?

#135 (comment)

return errors.WithStack(err)
}
f, err := file.Open(h.config.FilePath+".new", os.O_RDWR|os.O_CREATE|os.O_TRUNC, h.config.FileMode)
if err != nil {
return errors.WithStack(err)
Expand All @@ -95,6 +90,7 @@ func (h *Handler[T]) save(data T) error {
}(); err != nil {
return err
}
os.Remove(h.config.FilePath)
if err := os.Rename(h.config.FilePath+".new", h.config.FilePath); err != nil {
return errors.WithStack(err)
}
Expand Down
Loading