lib/{fs,persistentqueue}: use filepath.Join() instead of concatenating path parts with /

Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/4014
This commit is contained in:
Aliaksandr Valialkin 2023-04-13 20:12:24 -07:00
parent 90b876cd1e
commit ca54e58c1f
No known key found for this signature in database
GPG key ID: A72BEC6CD3D0DED1
4 changed files with 36 additions and 26 deletions

View file

@ -154,7 +154,7 @@ func RemoveDirContents(dir string) {
// Skip special dirs. // Skip special dirs.
continue continue
} }
fullPath := dir + "/" + name fullPath := filepath.Join(dir, name)
MustRemoveAll(fullPath) MustRemoveAll(fullPath)
} }
MustSyncPath(dir) MustSyncPath(dir)
@ -258,7 +258,7 @@ func MustRemoveTemporaryDirs(dir string) {
} }
dirName := de.Name() dirName := de.Name()
if IsScheduledForRemoval(dirName) { if IsScheduledForRemoval(dirName) {
fullPath := dir + "/" + dirName fullPath := filepath.Join(dir, dirName)
MustRemoveAll(fullPath) MustRemoveAll(fullPath)
} }
} }
@ -281,8 +281,8 @@ func HardLinkFiles(srcDir, dstDir string) error {
continue continue
} }
fn := de.Name() fn := de.Name()
srcPath := srcDir + "/" + fn srcPath := filepath.Join(srcDir, fn)
dstPath := dstDir + "/" + fn dstPath := filepath.Join(dstDir, fn)
if err := os.Link(srcPath, dstPath); err != nil { if err := os.Link(srcPath, dstPath); err != nil {
return err return err
} }
@ -379,13 +379,16 @@ func MustWriteData(w io.Writer, data []byte) {
} }
} }
// CreateFlockFile creates flock.lock file in the directory dir // CreateFlockFile creates FlockFilename file in the directory dir
// and returns the handler to the file. // and returns the handler to the file.
func CreateFlockFile(dir string) (*os.File, error) { func CreateFlockFile(dir string) (*os.File, error) {
flockFile := dir + "/flock.lock" flockFile := filepath.Join(dir, FlockFilename)
return createFlockFile(flockFile) return createFlockFile(flockFile)
} }
// FlockFilename is the filename for the file created by CreateFlockFile().
const FlockFilename = "flock.lock"
// MustGetFreeSpace returns free space for the given directory path. // MustGetFreeSpace returns free space for the given directory path.
func MustGetFreeSpace(path string) uint64 { func MustGetFreeSpace(path string) uint64 {
// Try obtaining cached value at first. // Try obtaining cached value at first.

View file

@ -0,0 +1,5 @@
package persistentqueue
const (
metainfoFilename = "metainfo.json"
)

View file

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"io" "io"
"os" "os"
"path/filepath"
"regexp" "regexp"
"strconv" "strconv"
"time" "time"
@ -226,16 +227,16 @@ func tryOpeningQueue(path, name string, chunkFileSize, maxBlockSize, maxPendingB
} }
for _, de := range des { for _, de := range des {
fname := de.Name() fname := de.Name()
filepath := path + "/" + fname filepath := filepath.Join(path, fname)
if de.IsDir() { if de.IsDir() {
logger.Errorf("skipping unknown directory %q", filepath) logger.Errorf("skipping unknown directory %q", filepath)
continue continue
} }
if fname == "metainfo.json" { if fname == metainfoFilename {
// skip metainfo file // skip metainfo file
continue continue
} }
if fname == "flock.lock" { if fname == fs.FlockFilename {
// skip flock file // skip flock file
continue continue
} }
@ -356,11 +357,11 @@ func (q *queue) MustClose() {
} }
func (q *queue) chunkFilePath(offset uint64) string { func (q *queue) chunkFilePath(offset uint64) string {
return fmt.Sprintf("%s/%016X", q.dir, offset) return filepath.Join(q.dir, fmt.Sprintf("%016X", offset))
} }
func (q *queue) metainfoPath() string { func (q *queue) metainfoPath() string {
return q.dir + "/metainfo.json" return filepath.Join(q.dir, metainfoFilename)
} }
// MustWriteBlock writes block to q. // MustWriteBlock writes block to q.

View file

@ -3,6 +3,7 @@ package persistentqueue
import ( import (
"fmt" "fmt"
"os" "os"
"path/filepath"
"strconv" "strconv"
"testing" "testing"
) )
@ -24,7 +25,7 @@ func TestQueueOpen(t *testing.T) {
t.Run("invalid-metainfo", func(t *testing.T) { t.Run("invalid-metainfo", func(t *testing.T) {
path := "queue-open-invalid-metainfo" path := "queue-open-invalid-metainfo"
mustCreateDir(path) mustCreateDir(path)
mustCreateFile(path+"/metainfo.json", "foobarbaz") mustCreateFile(filepath.Join(path, metainfoFilename), "foobarbaz")
q := mustOpen(path, "foobar", 0) q := mustOpen(path, "foobar", 0)
q.MustClose() q.MustClose()
mustDeleteDir(path) mustDeleteDir(path)
@ -33,8 +34,8 @@ func TestQueueOpen(t *testing.T) {
path := "queue-open-junk-files-and-dir" path := "queue-open-junk-files-and-dir"
mustCreateDir(path) mustCreateDir(path)
mustCreateEmptyMetainfo(path, "foobar") mustCreateEmptyMetainfo(path, "foobar")
mustCreateFile(path+"/junk-file", "foobar") mustCreateFile(filepath.Join(path, "junk-file"), "foobar")
mustCreateDir(path + "/junk-dir") mustCreateDir(filepath.Join(path, "junk-dir"))
q := mustOpen(path, "foobar", 0) q := mustOpen(path, "foobar", 0)
q.MustClose() q.MustClose()
mustDeleteDir(path) mustDeleteDir(path)
@ -43,7 +44,7 @@ func TestQueueOpen(t *testing.T) {
path := "queue-open-invalid-chunk-offset" path := "queue-open-invalid-chunk-offset"
mustCreateDir(path) mustCreateDir(path)
mustCreateEmptyMetainfo(path, "foobar") mustCreateEmptyMetainfo(path, "foobar")
mustCreateFile(fmt.Sprintf("%s/%016X", path, 1234), "qwere") mustCreateFile(filepath.Join(path, fmt.Sprintf("%016X", 1234)), "qwere")
q := mustOpen(path, "foobar", 0) q := mustOpen(path, "foobar", 0)
q.MustClose() q.MustClose()
mustDeleteDir(path) mustDeleteDir(path)
@ -52,7 +53,7 @@ func TestQueueOpen(t *testing.T) {
path := "queue-open-too-new-chunk" path := "queue-open-too-new-chunk"
mustCreateDir(path) mustCreateDir(path)
mustCreateEmptyMetainfo(path, "foobar") mustCreateEmptyMetainfo(path, "foobar")
mustCreateFile(fmt.Sprintf("%s/%016X", path, 100*uint64(defaultChunkFileSize)), "asdf") mustCreateFile(filepath.Join(path, fmt.Sprintf("%016X", 100*uint64(defaultChunkFileSize))), "asdf")
q := mustOpen(path, "foobar", 0) q := mustOpen(path, "foobar", 0)
q.MustClose() q.MustClose()
mustDeleteDir(path) mustDeleteDir(path)
@ -65,10 +66,10 @@ func TestQueueOpen(t *testing.T) {
ReaderOffset: defaultChunkFileSize, ReaderOffset: defaultChunkFileSize,
WriterOffset: defaultChunkFileSize, WriterOffset: defaultChunkFileSize,
} }
if err := mi.WriteToFile(path + "/metainfo.json"); err != nil { if err := mi.WriteToFile(filepath.Join(path, metainfoFilename)); err != nil {
t.Fatalf("unexpected error: %s", err) t.Fatalf("unexpected error: %s", err)
} }
mustCreateFile(fmt.Sprintf("%s/%016X", path, 0), "adfsfd") mustCreateFile(filepath.Join(path, fmt.Sprintf("%016X", 0)), "adfsfd")
q := mustOpen(path, mi.Name, 0) q := mustOpen(path, mi.Name, 0)
q.MustClose() q.MustClose()
mustDeleteDir(path) mustDeleteDir(path)
@ -80,7 +81,7 @@ func TestQueueOpen(t *testing.T) {
Name: "foobar", Name: "foobar",
ReaderOffset: defaultChunkFileSize + 123, ReaderOffset: defaultChunkFileSize + 123,
} }
if err := mi.WriteToFile(path + "/metainfo.json"); err != nil { if err := mi.WriteToFile(filepath.Join(path, metainfoFilename)); err != nil {
t.Fatalf("unexpected error: %s", err) t.Fatalf("unexpected error: %s", err)
} }
q := mustOpen(path, mi.Name, 0) q := mustOpen(path, mi.Name, 0)
@ -90,7 +91,7 @@ func TestQueueOpen(t *testing.T) {
t.Run("metainfo-dir", func(t *testing.T) { t.Run("metainfo-dir", func(t *testing.T) {
path := "queue-open-metainfo-dir" path := "queue-open-metainfo-dir"
mustCreateDir(path) mustCreateDir(path)
mustCreateDir(path + "/metainfo.json") mustCreateDir(filepath.Join(path, metainfoFilename))
q := mustOpen(path, "foobar", 0) q := mustOpen(path, "foobar", 0)
q.MustClose() q.MustClose()
mustDeleteDir(path) mustDeleteDir(path)
@ -103,10 +104,10 @@ func TestQueueOpen(t *testing.T) {
ReaderOffset: 123, ReaderOffset: 123,
WriterOffset: 123, WriterOffset: 123,
} }
if err := mi.WriteToFile(path + "/metainfo.json"); err != nil { if err := mi.WriteToFile(filepath.Join(path, metainfoFilename)); err != nil {
t.Fatalf("unexpected error: %s", err) t.Fatalf("unexpected error: %s", err)
} }
mustCreateFile(fmt.Sprintf("%s/%016X", path, 0), "sdf") mustCreateFile(filepath.Join(path, fmt.Sprintf("%016X", 0)), "sdf")
q := mustOpen(path, mi.Name, 0) q := mustOpen(path, mi.Name, 0)
q.MustClose() q.MustClose()
mustDeleteDir(path) mustDeleteDir(path)
@ -115,7 +116,7 @@ func TestQueueOpen(t *testing.T) {
path := "too-small-reader-file" path := "too-small-reader-file"
mustCreateDir(path) mustCreateDir(path)
mustCreateEmptyMetainfo(path, "foobar") mustCreateEmptyMetainfo(path, "foobar")
mustCreateFile(fmt.Sprintf("%s/%016X", path, 0), "sdfdsf") mustCreateFile(filepath.Join(path, fmt.Sprintf("%016X", 0)), "sdfdsf")
q := mustOpen(path, "foobar", 0) q := mustOpen(path, "foobar", 0)
q.MustClose() q.MustClose()
mustDeleteDir(path) mustDeleteDir(path)
@ -126,10 +127,10 @@ func TestQueueOpen(t *testing.T) {
mi := &metainfo{ mi := &metainfo{
Name: "foobar", Name: "foobar",
} }
if err := mi.WriteToFile(path + "/metainfo.json"); err != nil { if err := mi.WriteToFile(filepath.Join(path, metainfoFilename)); err != nil {
t.Fatalf("unexpected error: %s", err) t.Fatalf("unexpected error: %s", err)
} }
mustCreateFile(fmt.Sprintf("%s/%016X", path, 0), "sdf") mustCreateFile(filepath.Join(path, fmt.Sprintf("%016X", 0)), "sdf")
q := mustOpen(path, "baz", 0) q := mustOpen(path, "baz", 0)
q.MustClose() q.MustClose()
mustDeleteDir(path) mustDeleteDir(path)
@ -391,7 +392,7 @@ func mustDeleteDir(path string) {
func mustCreateEmptyMetainfo(path, name string) { func mustCreateEmptyMetainfo(path, name string) {
var mi metainfo var mi metainfo
mi.Name = name mi.Name = name
if err := mi.WriteToFile(path + "/metainfo.json"); err != nil { if err := mi.WriteToFile(filepath.Join(path, metainfoFilename)); err != nil {
panic(fmt.Errorf("cannot create metainfo: %w", err)) panic(fmt.Errorf("cannot create metainfo: %w", err))
} }
} }