Skip to content

Commit

Permalink
Improve error message for state.json (#4251)
Browse files Browse the repository at this point in the history
Closes #4234.
  • Loading branch information
AlekSi authored Apr 28, 2024
1 parent 89f22e4 commit a84136f
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 4 deletions.
4 changes: 1 addition & 3 deletions cmd/ferretdb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,6 @@ func defaultLogLevel() zapcore.Level {
func setupState() *state.Provider {
var f string

// https://github.com/alecthomas/kong/issues/389
if cli.StateDir != "" && cli.StateDir != "-" {
var err error
if f, err = filepath.Abs(filepath.Join(cli.StateDir, "state.json")); err != nil {
Expand All @@ -228,7 +227,7 @@ func setupState() *state.Provider {

sp, err := state.NewProvider(f)
if err != nil {
log.Fatalf("Failed to create state provider: %s.", err)
log.Fatal(stateFileProblem(f, err))
}

return sp
Expand Down Expand Up @@ -369,7 +368,6 @@ func run() {
logger.Sugar().Fatal("--test-disable-pushdown and --test-enable-nested-pushdown should not be set at the same time")
}

// https://github.com/alecthomas/kong/issues/389
if cli.DebugAddr != "" && cli.DebugAddr != "-" {
wg.Add(1)

Expand Down
63 changes: 63 additions & 0 deletions cmd/ferretdb/main_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright 2021 FerretDB Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build unix

package main

import (
"fmt"
"os"
"os/user"
"strconv"
"syscall"
)

// stateFileProblem adds details to the state file access error.
func stateFileProblem(f string, err error) string {
res := fmt.Sprintf("Failed to create state provider: %s.\n", err)

if u, _ := user.Current(); u != nil {
var group string
if g, _ := user.LookupGroupId(u.Gid); g != nil {
group = g.Name
}

res += fmt.Sprintf("FerretDB is running as %s:%s (%s:%s). ", u.Username, group, u.Uid, u.Gid)
}

if fi, _ := os.Stat(f); fi != nil {
var username, group string
var uid, gid uint64

if s, _ := fi.Sys().(*syscall.Stat_t); s != nil {
uid = uint64(s.Uid)
if u, _ := user.LookupId(strconv.FormatUint(uid, 10)); u != nil {
username = u.Username
}

gid = uint64(s.Gid)
if g, _ := user.LookupGroupId(strconv.FormatUint(gid, 10)); g != nil {
group = g.Name
}
}

res += fmt.Sprintf("%s permissions are %s.", f, fi.Mode().String())
if username != "" {
res += fmt.Sprintf(" Owned by %s:%s (%d:%d).", username, group, uid, gid)
}
}

return res
}
22 changes: 22 additions & 0 deletions cmd/ferretdb/main_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2021 FerretDB Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import "fmt"

// stateFileProblem returns the state file access error.
func stateFileProblem(_ string, err error) string {
return fmt.Sprintf("Failed to create state provider: %s.", err)
}
3 changes: 2 additions & 1 deletion internal/util/state/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ func NewProvider(filename string) (*Provider, error) {
p.s.fill()

// Simply overwrite state to handle all errors and edge cases
// like missing directory, corrupted file, invalid UUID, etc.
// like missing directory, corrupted file, invalid UUID, etc.,
// and also to check permissions.
if err := persistState(p.s, p.filename); err != nil {
return p, fmt.Errorf("failed to persist state: %w", err)
}
Expand Down

0 comments on commit a84136f

Please sign in to comment.