Fix fix and address review

This commit is contained in:
Daniel 2019-08-22 22:34:27 +02:00
parent dca046b1c1
commit d79e46d325

View file

@ -10,6 +10,7 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"runtime"
"strings" "strings"
"time" "time"
@ -17,11 +18,14 @@ import (
"github.com/safing/portbase/database/query" "github.com/safing/portbase/database/query"
"github.com/safing/portbase/database/record" "github.com/safing/portbase/database/record"
"github.com/safing/portbase/database/storage" "github.com/safing/portbase/database/storage"
"github.com/google/renameio"
) )
const ( const (
defaultFileMode = os.FileMode(int(0644)) defaultFileMode = os.FileMode(int(0644))
defaultDirMode = os.FileMode(int(0755)) defaultDirMode = os.FileMode(int(0755))
onWindows = runtime.GOOS == "windows"
) )
// FSTree database storage. // FSTree database storage.
@ -189,12 +193,12 @@ func (fst *FSTree) queryExecutor(walkRoot string, queryIter *iterator.Iterator,
} }
// continue // continue
return nil return nil
} else { }
// still in scope? // still in scope?
if !strings.HasPrefix(path, fst.basePath) { if !strings.HasPrefix(path, fst.basePath) {
return nil return nil
} }
}
// read file // read file
data, err := ioutil.ReadFile(path) data, err := ioutil.ReadFile(path)
@ -271,12 +275,19 @@ func (fst *FSTree) Shutdown() error {
// TODO: Replace with github.com/google/renamio.WriteFile as soon as it is fixed on Windows. // TODO: Replace with github.com/google/renamio.WriteFile as soon as it is fixed on Windows.
// This function is forked from https://github.com/google/renameio/blob/a368f9987532a68a3d676566141654a81aa8100b/writefile.go. // This function is forked from https://github.com/google/renameio/blob/a368f9987532a68a3d676566141654a81aa8100b/writefile.go.
func writeFile(filename string, data []byte, perm os.FileMode) error { func writeFile(filename string, data []byte, perm os.FileMode) error {
t, err := TempFile("", filename) t, err := renameio.TempFile("", filename)
if err != nil { if err != nil {
return err return err
} }
defer t.Cleanup() defer t.Cleanup()
// Set permissions before writing data, in case the data is sensitive.
if !onWindows {
if err := t.Chmod(perm); err != nil {
return err
}
}
if _, err := t.Write(data); err != nil { if _, err := t.Write(data); err != nil {
return err return err
} }