Skip to content

Commit

Permalink
feat: make the code works on Windows.
Browse files Browse the repository at this point in the history
  • Loading branch information
HoytRen committed Jul 31, 2023
1 parent 794360f commit f848326
Show file tree
Hide file tree
Showing 14 changed files with 90 additions and 172 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ vendor
.bak
.idea/
.vscode/
.fleet/
/celestia
/cel-shed
/cel-key
Expand Down
6 changes: 6 additions & 0 deletions build.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
del -Recurse -Force .\build\*
gofmt -e -s -w .
go mod tidy
$LDFLAGS="-ldflags=-X 'main.buildTime=$(date)' -X 'main.lastCommit=$(git rev-parse HEAD)' -X 'main.semanticVersion=$(git describe --tags --dirty=-dev)'"
go build -o build/ ${LDFLAGS} ./cmd/celestia
go build -o build/ ./cmd/cel-key
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
github.com/cosmos/cosmos-sdk v0.46.13
github.com/cosmos/cosmos-sdk/api v0.1.0
github.com/cristalhq/jwt v1.2.0
github.com/danjacques/gofslock v0.0.0-20230227034627-eebdc482a3f5
github.com/dgraph-io/badger/v2 v2.2007.4
github.com/etclabscore/go-openrpc-reflect v0.0.37
github.com/filecoin-project/dagstore v0.5.6
Expand Down Expand Up @@ -77,6 +78,7 @@ require (
golang.org/x/crypto v0.11.0
golang.org/x/exp v0.0.0-20230321023759-10a507213a29
golang.org/x/sync v0.2.0
golang.org/x/sys v0.10.0
golang.org/x/text v0.11.0
google.golang.org/grpc v1.56.2
google.golang.org/protobuf v1.31.0
Expand Down Expand Up @@ -313,7 +315,6 @@ require (
golang.org/x/mod v0.10.0 // indirect
golang.org/x/net v0.12.0 // indirect
golang.org/x/oauth2 v0.8.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/term v0.10.0 // indirect
golang.org/x/tools v0.9.1 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,8 @@ github.com/cskr/pubsub v1.0.2/go.mod h1:/8MzYXk/NJAz782G8RPkFzXTZVu63VotefPnR9TI
github.com/cyberdelia/templates v0.0.0-20141128023046-ca7fffd4298c/go.mod h1:GyV+0YP4qX0UQ7r2MoYZ+AvYDp12OF5yg4q8rGnyNh4=
github.com/danieljoos/wincred v1.1.2 h1:QLdCxFs1/Yl4zduvBdcHB8goaYk9RARS2SgLLRuAyr0=
github.com/danieljoos/wincred v1.1.2/go.mod h1:GijpziifJoIBfYh+S7BbkdUTU4LfM+QnGqR5Vl2tAx0=
github.com/danjacques/gofslock v0.0.0-20230227034627-eebdc482a3f5 h1:/KFLY9ggaJT/mANX95q8ByhRR+9A3gWFrTz6ifkhOV8=
github.com/danjacques/gofslock v0.0.0-20230227034627-eebdc482a3f5/go.mod h1:9LABMmUSkKzt6FVQNEWdUTM0bz8Bt8MPyEcuZe0Sr8c=
github.com/dave/jennifer v1.2.0/go.mod h1:fIb+770HOpJ2fmN9EPPKOqm1vMGhB+TwXKMZhrIygKg=
github.com/davecgh/go-spew v0.0.0-20171005155431-ecdeabc65495/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
48 changes: 0 additions & 48 deletions libs/fslock/lock_unix.go

This file was deleted.

49 changes: 0 additions & 49 deletions libs/fslock/locker.go

This file was deleted.

40 changes: 0 additions & 40 deletions libs/fslock/locker_test.go

This file was deleted.

13 changes: 3 additions & 10 deletions libs/keystore/fs_keystore.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (f *fsKeystore) Put(n KeyName, pk PrivKey) error {
func (f *fsKeystore) Get(n KeyName) (PrivKey, error) {
path := f.pathTo(n.Base32())

st, err := os.Stat(path)
_, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
return PrivKey{}, fmt.Errorf("%w: %s", ErrNotFound, n)
Expand All @@ -68,7 +68,7 @@ func (f *fsKeystore) Get(n KeyName) (PrivKey, error) {
return PrivKey{}, fmt.Errorf("keystore: check before reading key '%s' failed: %w", n, err)
}

if err := checkPerms(st.Mode()); err != nil {
if err := keyAccess(path); err != nil {
return PrivKey{}, fmt.Errorf("keystore: permissions of key '%s' are too relaxed: %w", n, err)
}

Expand Down Expand Up @@ -115,7 +115,7 @@ func (f *fsKeystore) List() ([]KeyName, error) {
return nil, err
}

if err := checkPerms(e.Type()); err != nil {
if err := keyAccess(f.pathTo(kn.Base32())); err != nil {
return nil, fmt.Errorf("keystore: permissions of key '%s' are too relaxed: %w", kn, err)
}

Expand All @@ -136,10 +136,3 @@ func (f *fsKeystore) Keyring() keyring.Keyring {
func (f *fsKeystore) pathTo(file string) string {
return filepath.Join(f.path, file)
}

func checkPerms(perms os.FileMode) error {
if perms&0077 != 0 {
return fmt.Errorf("required: 0600, got: %#o", perms)
}
return nil
}
19 changes: 19 additions & 0 deletions libs/keystore/key_access_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//go:build darwin || freebsd || linux

package keystore

import (
"fmt"
"os"
)

// keyAccess checks whether file is not accessible by all users.
func keyAccess(path string) error {
st, _ := os.Stat(path)
mode := st.Mode()
if mode&0077 != 0 {
return fmt.Errorf("required: 0600, got: %#o", mode)
}

return nil
}
30 changes: 30 additions & 0 deletions libs/keystore/key_access_win.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//go:build windows

package keystore

import (
"fmt"
"golang.org/x/sys/windows"
"unsafe"
)

// keyAccess checks whether file is not accessible by all users.
func keyAccess(path string) error {

// Check the file's DACL
securityDescriptor, err := windows.GetNamedSecurityInfo(path, windows.SE_FILE_OBJECT, windows.DACL_SECURITY_INFORMATION)
if err != nil {
return fmt.Errorf("Error getting file's DACL: %v\n", err)
}
defer func() {
if err != nil {
_, _ = windows.LocalFree((windows.Handle)(unsafe.Pointer(securityDescriptor)))
}
}()
_, _, err = securityDescriptor.DACL()
if err != nil {
return fmt.Errorf("File is too Open with no DACL: %v\n", err)
}

return nil
}
9 changes: 5 additions & 4 deletions nodebuilder/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/BurntSushi/toml"
"github.com/imdario/mergo"

"github.com/celestiaorg/celestia-node/libs/fslock"
"github.com/celestiaorg/celestia-node/nodebuilder/core"
"github.com/celestiaorg/celestia-node/nodebuilder/das"
"github.com/celestiaorg/celestia-node/nodebuilder/gateway"
Expand All @@ -17,6 +16,8 @@ import (
"github.com/celestiaorg/celestia-node/nodebuilder/rpc"
"github.com/celestiaorg/celestia-node/nodebuilder/share"
"github.com/celestiaorg/celestia-node/nodebuilder/state"

"github.com/danjacques/gofslock/fslock"
)

// ConfigLoader defines a function that loads a config from any source.
Expand Down Expand Up @@ -67,7 +68,7 @@ func SaveConfig(path string, cfg *Config) error {
if err != nil {
return err
}
defer f.Close()
defer f.Close() //nolint: errcheck

return cfg.Encode(f)
}
Expand All @@ -93,7 +94,7 @@ func RemoveConfig(path string) (err error) {

flock, err := fslock.Lock(lockPath(path))
if err != nil {
if err == fslock.ErrLocked {
if err == fslock.ErrLockHeld {
err = ErrOpened
}
return
Expand All @@ -119,7 +120,7 @@ func UpdateConfig(tp node.Type, path string) (err error) {

flock, err := fslock.Lock(lockPath(path))
if err != nil {
if err == fslock.ErrLocked {
if err == fslock.ErrLockHeld {
err = ErrOpened
}
return
Expand Down
7 changes: 4 additions & 3 deletions nodebuilder/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import (
"github.com/celestiaorg/celestia-app/app"
"github.com/celestiaorg/celestia-app/app/encoding"

"github.com/celestiaorg/celestia-node/libs/fslock"
"github.com/celestiaorg/celestia-node/libs/utils"
"github.com/celestiaorg/celestia-node/nodebuilder/node"
"github.com/celestiaorg/celestia-node/nodebuilder/state"

"github.com/danjacques/gofslock/fslock"
)

// Init initializes the Node FileSystem Store for the given Node Type 'tp' in the directory under
Expand All @@ -34,7 +35,7 @@ func Init(cfg Config, path string, tp node.Type) error {

flock, err := fslock.Lock(lockPath(path))
if err != nil {
if err == fslock.ErrLocked {
if err == fslock.ErrLockHeld {
return ErrOpened
}
return err
Expand Down Expand Up @@ -81,7 +82,7 @@ func Reset(path string, tp node.Type) error {

flock, err := fslock.Lock(lockPath(path))
if err != nil {
if err == fslock.ErrLocked {
if err == fslock.ErrLockHeld {
return ErrOpened
}
return err
Expand Down
Loading

0 comments on commit f848326

Please sign in to comment.