Skip to content
Open
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
22 changes: 22 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
name: Lint

on:
push:
pull_request:

jobs:
lint:
name: Run on Ubuntu
runs-on: ubuntu-latest
steps:
- name: Clone the code
uses: actions/checkout@v4
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version-file: go.mod
- name: Run linter
uses: golangci/golangci-lint-action@v9
with:
version: v2.13
2 changes: 1 addition & 1 deletion internal/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func PgToolVersion(logger *logger.LevelLog, binDir, tool string) int {
default:
// We have the special case of the development version, where the
// format is MAJdevel
fmt.Sscanf(string(vs), tool+" (PostgreSQL) %ddevel", &maj)
_, _ = fmt.Sscanf(string(vs), tool+" (PostgreSQL) %ddevel", &maj)
numver = maj * 10000
}

Expand Down
18 changes: 10 additions & 8 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,12 @@ func validateMode(s string) (int, error) {
if (strings.HasPrefix(s, "0") && len(s) <= 5) || (strings.HasPrefix(s, "-")) {
mode, err := strconv.ParseInt(s, 0, 32)
if err != nil {
return 0, fmt.Errorf("Invalid permission %q", s)
return 0, fmt.Errorf("invalid permission %q", s)
}
return int(mode), nil
}
return 0, fmt.Errorf(
"Invalid permission %q, must be octal (start by 0 and max 5 digits) number or negative",
"invalid permission %q, must be octal (start by 0 and max 5 digits) number or negative",
s,
)
}
Expand All @@ -235,11 +235,11 @@ func validatePurgeKeepValue(k string) (int, error) {
keep, err := strconv.ParseInt(k, 10, 0)
if err != nil {
// return -1 too when the input is not convertible to an int
return -1, fmt.Errorf("Invalid input for keep: %w", err)
return -1, fmt.Errorf("invalid input for keep: %w", err)
}

if keep < 0 {
return -1, fmt.Errorf("Invalid input for keep: negative value: %d", keep)
return -1, fmt.Errorf("invalid input for keep: negative value: %d", keep)
}

return int(keep), nil
Expand All @@ -248,7 +248,7 @@ func validatePurgeKeepValue(k string) (int, error) {
func validatePurgeTimeLimitValue(i string) (time.Duration, error) {
if days, err := strconv.ParseInt(i, 10, 0); err != nil {
if errors.Is(err, strconv.ErrRange) {
return 0, errors.New("Invalid input for purge interval, number too big")
return 0, errors.New("invalid input for purge interval, number too big")
}
} else {
return time.Duration(-days*24) * time.Hour, nil
Expand Down Expand Up @@ -582,12 +582,14 @@ func ParseCli(args []string, defaultCfg string) (Options, []string, error) {
// Do not use the default pflag.Parse() that use os.Args[1:],
// but pass it explicitly so that unit-tests can feed any set
// of flags
pflag.CommandLine.Parse(args)
changed := make([]string, 0)
if err := pflag.CommandLine.Parse(args); err != nil {
return opts, changed, fmt.Errorf("imposibile to parse command line argument: %w", err)
}

// Record the list of flags set on the command line to allow
// overriding the configuration later, if an alternate
// configuration file has been provided
changed := make([]string, 0)
pflag.Visit(func(f *pflag.Flag) {
changed = append(changed, f.Name)
})
Expand Down Expand Up @@ -837,7 +839,7 @@ func LoadConfigurationFile(path string, l *logger.LevelLog) (Options, error) {
return opts, nil
}

return opts, fmt.Errorf("Could load configuration file: %v", err)
return opts, fmt.Errorf("could load configuration file: %v", err)
}

if err := validateConfigurationFile(cfg); err != nil {
Expand Down
4 changes: 3 additions & 1 deletion internal/crypto/hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,9 @@ func TestChecksumFile(t *testing.T) {
if err != nil {
t.Fatal("could not create test file")
}
fmt.Fprintf(f, "abdc%d", i)
if _, err := fmt.Fprintf(f, "abdc%d", i); err != nil {
t.Fatalf("could not format test file: %v", err)
}
if err := f.Close(); err != nil {
t.Fatal("could not close test file")
}
Expand Down
21 changes: 16 additions & 5 deletions internal/storage/lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"runtime"
"testing"

"github.com/orgrim/pg_back/internal/helpers"
"github.com/orgrim/pg_back/internal/logger"
)

Expand All @@ -42,7 +43,11 @@ func TestLockPath(t *testing.T) {
if err != nil {
t.Fatal("could not create tempdir:", err)
}
defer os.RemoveAll(dir)
defer func() {
if err := os.RemoveAll(dir); err != nil {
t.Logf("cannot remove %s: %v", dir, err)
}
}()

// tempdir with perms for mkdirall failure
if err := os.MkdirAll(filepath.Join(dir, "subfail"), 0444); err != nil {
Expand All @@ -68,7 +73,7 @@ func TestLockPath(t *testing.T) {
if err != nil {
t.Errorf("expected <nil> got error %q\n", err)
}
defer f.Close()
defer helpers.WrappedClose(f, &err)
if !l {
t.Errorf("expected a true for locked, got false")
}
Expand All @@ -81,7 +86,7 @@ func TestLockPath(t *testing.T) {
if l1 {
t.Errorf("expected a false for failed locked, got true")
}
f1.Close()
defer helpers.WrappedClose(f1, &err)
}

func TestUnlockPath(t *testing.T) {
Expand All @@ -90,7 +95,11 @@ func TestUnlockPath(t *testing.T) {
if err != nil {
t.Fatal("could not create tempfile")
}
defer os.Remove(f.Name())
defer func() {
if err := os.Remove(f.Name()); err != nil {
t.Logf("cannot remove %s: %v", f.Name(), err)
}
}()

// unlock shall always work even if the file is not locked
err = UnlockPath(logger, f)
Expand All @@ -99,7 +108,9 @@ func TestUnlockPath(t *testing.T) {
}

// error when the locked file as already been removed
os.Remove(f.Name())
if err := os.Remove(f.Name()); err != nil {
t.Errorf("cannot remove %s: %v", f.Name(), err)
}
err = UnlockPath(logger, f)
if err == nil {
t.Errorf("got <nil> instead of \"bad file descriptor\" error")
Expand Down
63 changes: 48 additions & 15 deletions internal/storage/purge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ func TestPurgeDumps(t *testing.T) {
if err != nil {
t.Fatal("could not create tempdir:", err)
}
defer os.RemoveAll(dir)
defer func() {
if err := os.RemoveAll(dir); err != nil {
t.Fatal("could not remove tmpdir:", err)
}
}()

// empty path - on windows chmod does not work as expected
wd := filepath.Join(dir, "real", "bad")
Expand All @@ -54,12 +58,16 @@ func TestPurgeDumps(t *testing.T) {
}

if runtime.GOOS != "windows" {
os.Chmod(filepath.Dir(wd), 0444)
if err := os.Chmod(filepath.Dir(wd), 0444); err != nil {
t.Fatal("could not change permision on working dir:", wd, err)
}
err = PurgeDumps(logger, wd, "", 0, time.Time{})
if err == nil {
t.Errorf("empty path gave error <nil>\n")
}
os.Chmod(filepath.Dir(wd), 0755)
if err := os.Chmod(filepath.Dir(wd), 0755); err != nil {
t.Fatal("could not remove working dir:", err)
}
}

// empty dbname
Expand All @@ -70,8 +78,12 @@ func TestPurgeDumps(t *testing.T) {
t.Errorf("could not create temp file %s: %s", tf, err)
}

f.Close()
os.Chtimes(tf, when, when)
if err := f.Close(); err != nil {
t.Fatal("could not close:", f, err)
}
if err := os.Chtimes(tf, when, when); err != nil {
t.Fatal("could change access time:", tf, err)
}

err = PurgeDumps(logger, wd, "", 0, time.Now())
if err != nil {
Expand All @@ -84,25 +96,40 @@ func TestPurgeDumps(t *testing.T) {
// file without write perms
if runtime.GOOS != "windows" {
tf = helpers.FormatDumpPath(wd, time.RFC3339, "dump", "db", time.Now().Add(-time.Hour), 0)
os.WriteFile(tf, []byte("truc\n"), 0644)
os.Chmod(filepath.Dir(tf), 0555)
if err := os.WriteFile(tf, []byte("truc\n"), 0644); err != nil {
t.Errorf("cannot write file %s: %v", tf, err)
}
tfDir := filepath.Dir(tf)

if err := os.Chmod(tfDir, 0555); err != nil {
t.Errorf("cannot chmod on %s", tfDir)
}

err = PurgeDumps(logger, wd, "db", 0, time.Now())
if err == nil {
t.Errorf("bad perms on file did not gave an error")
}
os.Chmod(filepath.Dir(tf), 0755)
if err := os.Chmod(tfDir, 0755); err != nil {
t.Errorf("cannot chmod on %s: %v", tfDir, err)
}

// dir without write perms
tf = helpers.FormatDumpPath(wd, time.RFC3339, "d", "db", time.Now().Add(-time.Hour), 0)
os.MkdirAll(tf, 0755)
os.Chmod(filepath.Dir(tf), 0555)
if err := os.MkdirAll(tf, 0755); err != nil {
t.Errorf("can't create dir %s: %v", tf, err)
}
if err := os.Chmod(tfDir, 0555); err != nil {
t.Errorf("cannot chmod on %s: %v", tfDir, err)
}

err = PurgeDumps(logger, wd, "db", 0, time.Now())
if err == nil {
t.Errorf("bad perms on dir did not gave an error")
}
os.Chmod(filepath.Dir(tf), 0755)

if err := os.Chmod(tfDir, 0755); err != nil {
t.Logf("cannot chmod %s: %v", tfDir, err)
}
}

// time and keep limits
Expand Down Expand Up @@ -142,8 +169,12 @@ func TestPurgeDumps(t *testing.T) {
for i := 1; i <= 3; i++ {
when := time.Now().Add(-time.Hour * time.Duration(i))
tf = helpers.FormatDumpPath(wd, st.format, "dump", "db", when, 0)
os.WriteFile(tf, []byte("truc\n"), 0644)
os.Chtimes(tf, when, when)
if err := os.WriteFile(tf, []byte("truc\n"), 0644); err != nil {
t.Logf("cannot write on %s: %v", tf, err)
}
if err := os.Chtimes(tf, when, when); err != nil {
t.Logf("cannot change times on %s: %v", tf, err)
}
}

if err := PurgeDumps(logger, wd, "db", st.keep, st.limit); err != nil {
Expand All @@ -154,7 +185,7 @@ func TestPurgeDumps(t *testing.T) {
if err != nil {
t.Fatal("could not open workdir:", err)
}
defer dir.Close()
helpers.WrappedClose(dir, &err)

fi, err := dir.Readdir(-1)
if err != nil {
Expand All @@ -175,7 +206,9 @@ func TestPurgeDumps(t *testing.T) {
)
}

os.RemoveAll(wd)
if err := os.RemoveAll(wd); err != nil {
t.Fatal("coult not remove directory", wd, err)
}
})
}
}
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -1561,7 +1561,9 @@ func postProcessFiles(inFiles chan sumFileJob, wg *sync.WaitGroup, opts config.O
l.Infoln("uploaded", j.Path)
if opts.DeleteUploaded {
l.Infoln("removing", j.Path)
os.Remove(j.Path)
if err := os.Remove(j.Path); err != nil {
l.Errorln(err)
}
}
}
}
Expand Down
Loading