diff --git a/lib/fs/fs.go b/lib/fs/fs.go index 43fbe04fa..f8e90c104 100644 --- a/lib/fs/fs.go +++ b/lib/fs/fs.go @@ -154,7 +154,7 @@ func RemoveDirContents(dir string) { // Skip special dirs. continue } - fullPath := dir + "/" + name + fullPath := filepath.Join(dir, name) MustRemoveAll(fullPath) } MustSyncPath(dir) @@ -258,7 +258,7 @@ func MustRemoveTemporaryDirs(dir string) { } dirName := de.Name() if IsScheduledForRemoval(dirName) { - fullPath := dir + "/" + dirName + fullPath := filepath.Join(dir, dirName) MustRemoveAll(fullPath) } } @@ -281,8 +281,8 @@ func HardLinkFiles(srcDir, dstDir string) error { continue } fn := de.Name() - srcPath := srcDir + "/" + fn - dstPath := dstDir + "/" + fn + srcPath := filepath.Join(srcDir, fn) + dstPath := filepath.Join(dstDir, fn) if err := os.Link(srcPath, dstPath); err != nil { 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. func CreateFlockFile(dir string) (*os.File, error) { - flockFile := dir + "/flock.lock" + flockFile := filepath.Join(dir, FlockFilename) 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. func MustGetFreeSpace(path string) uint64 { // Try obtaining cached value at first. diff --git a/lib/persistentqueue/filenames.go b/lib/persistentqueue/filenames.go new file mode 100644 index 000000000..5a40b762a --- /dev/null +++ b/lib/persistentqueue/filenames.go @@ -0,0 +1,5 @@ +package persistentqueue + +const ( + metainfoFilename = "metainfo.json" +) diff --git a/lib/persistentqueue/persistentqueue.go b/lib/persistentqueue/persistentqueue.go index bcca20d28..fef7bbdbe 100644 --- a/lib/persistentqueue/persistentqueue.go +++ b/lib/persistentqueue/persistentqueue.go @@ -5,6 +5,7 @@ import ( "fmt" "io" "os" + "path/filepath" "regexp" "strconv" "time" @@ -226,16 +227,16 @@ func tryOpeningQueue(path, name string, chunkFileSize, maxBlockSize, maxPendingB } for _, de := range des { fname := de.Name() - filepath := path + "/" + fname + filepath := filepath.Join(path, fname) if de.IsDir() { logger.Errorf("skipping unknown directory %q", filepath) continue } - if fname == "metainfo.json" { + if fname == metainfoFilename { // skip metainfo file continue } - if fname == "flock.lock" { + if fname == fs.FlockFilename { // skip flock file continue } @@ -356,11 +357,11 @@ func (q *queue) MustClose() { } 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 { - return q.dir + "/metainfo.json" + return filepath.Join(q.dir, metainfoFilename) } // MustWriteBlock writes block to q. diff --git a/lib/persistentqueue/persistentqueue_test.go b/lib/persistentqueue/persistentqueue_test.go index 4aa3daf1c..19a3c9c1b 100644 --- a/lib/persistentqueue/persistentqueue_test.go +++ b/lib/persistentqueue/persistentqueue_test.go @@ -3,6 +3,7 @@ package persistentqueue import ( "fmt" "os" + "path/filepath" "strconv" "testing" ) @@ -24,7 +25,7 @@ func TestQueueOpen(t *testing.T) { t.Run("invalid-metainfo", func(t *testing.T) { path := "queue-open-invalid-metainfo" mustCreateDir(path) - mustCreateFile(path+"/metainfo.json", "foobarbaz") + mustCreateFile(filepath.Join(path, metainfoFilename), "foobarbaz") q := mustOpen(path, "foobar", 0) q.MustClose() mustDeleteDir(path) @@ -33,8 +34,8 @@ func TestQueueOpen(t *testing.T) { path := "queue-open-junk-files-and-dir" mustCreateDir(path) mustCreateEmptyMetainfo(path, "foobar") - mustCreateFile(path+"/junk-file", "foobar") - mustCreateDir(path + "/junk-dir") + mustCreateFile(filepath.Join(path, "junk-file"), "foobar") + mustCreateDir(filepath.Join(path, "junk-dir")) q := mustOpen(path, "foobar", 0) q.MustClose() mustDeleteDir(path) @@ -43,7 +44,7 @@ func TestQueueOpen(t *testing.T) { path := "queue-open-invalid-chunk-offset" mustCreateDir(path) 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.MustClose() mustDeleteDir(path) @@ -52,7 +53,7 @@ func TestQueueOpen(t *testing.T) { path := "queue-open-too-new-chunk" mustCreateDir(path) 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.MustClose() mustDeleteDir(path) @@ -65,10 +66,10 @@ func TestQueueOpen(t *testing.T) { ReaderOffset: 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) } - mustCreateFile(fmt.Sprintf("%s/%016X", path, 0), "adfsfd") + mustCreateFile(filepath.Join(path, fmt.Sprintf("%016X", 0)), "adfsfd") q := mustOpen(path, mi.Name, 0) q.MustClose() mustDeleteDir(path) @@ -80,7 +81,7 @@ func TestQueueOpen(t *testing.T) { Name: "foobar", 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) } q := mustOpen(path, mi.Name, 0) @@ -90,7 +91,7 @@ func TestQueueOpen(t *testing.T) { t.Run("metainfo-dir", func(t *testing.T) { path := "queue-open-metainfo-dir" mustCreateDir(path) - mustCreateDir(path + "/metainfo.json") + mustCreateDir(filepath.Join(path, metainfoFilename)) q := mustOpen(path, "foobar", 0) q.MustClose() mustDeleteDir(path) @@ -103,10 +104,10 @@ func TestQueueOpen(t *testing.T) { ReaderOffset: 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) } - mustCreateFile(fmt.Sprintf("%s/%016X", path, 0), "sdf") + mustCreateFile(filepath.Join(path, fmt.Sprintf("%016X", 0)), "sdf") q := mustOpen(path, mi.Name, 0) q.MustClose() mustDeleteDir(path) @@ -115,7 +116,7 @@ func TestQueueOpen(t *testing.T) { path := "too-small-reader-file" mustCreateDir(path) 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.MustClose() mustDeleteDir(path) @@ -126,10 +127,10 @@ func TestQueueOpen(t *testing.T) { mi := &metainfo{ 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) } - mustCreateFile(fmt.Sprintf("%s/%016X", path, 0), "sdf") + mustCreateFile(filepath.Join(path, fmt.Sprintf("%016X", 0)), "sdf") q := mustOpen(path, "baz", 0) q.MustClose() mustDeleteDir(path) @@ -391,7 +392,7 @@ func mustDeleteDir(path string) { func mustCreateEmptyMetainfo(path, name string) { var mi metainfo 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)) } }