Skip to content

Commit 17ed16d

Browse files
committed
fix windows atom mode #31
1 parent 86b25c4 commit 17ed16d

File tree

7 files changed

+58
-30
lines changed

7 files changed

+58
-30
lines changed

cmd/chkbit/main.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"log"
77
"os"
88
"os/signal"
9+
slpath "path"
910
"path/filepath"
1011
"strings"
1112
"sync"
@@ -148,6 +149,27 @@ type CLIDedup struct {
148149
} `cmd:"" help:"run deduplication, makes all duplicate file blocks point to the same space; requires supported OS & filesystem (see tips)"`
149150
}
150151

152+
func toSlash(paths []string) []string {
153+
for i, path := range paths {
154+
paths[i] = filepath.ToSlash(path)
155+
}
156+
return paths
157+
}
158+
159+
func (cli *CLI) toSlash() {
160+
cli.Check.Paths = toSlash(cli.Check.Paths)
161+
cli.Add.Paths = toSlash(cli.Add.Paths)
162+
cli.Update.Paths = toSlash(cli.Update.Paths)
163+
cli.Init.Path = filepath.ToSlash(cli.Init.Path)
164+
cli.Fuse.Path = filepath.ToSlash(cli.Fuse.Path)
165+
cli.Dedup.Detect.Path = filepath.ToSlash(cli.Dedup.Detect.Path)
166+
cli.Dedup.Show.Path = filepath.ToSlash(cli.Dedup.Show.Path)
167+
cli.Dedup.Run.Path = filepath.ToSlash(cli.Dedup.Run.Path)
168+
cli.Util.Fileext.Paths = toSlash(cli.Util.Fileext.Paths)
169+
cli.Util.Filededup.Paths = toSlash(cli.Util.Filededup.Paths)
170+
cli.ShowIgnored.Paths = toSlash(cli.ShowIgnored.Paths)
171+
}
172+
151173
type Main struct {
152174
context *chkbit.Context
153175
dedup *chkbit.Dedup
@@ -441,7 +463,7 @@ func (m *Main) run() int {
441463
var configPath = "chkbit-config.json"
442464
configRoot, err := os.UserConfigDir()
443465
if err == nil {
444-
configPath = filepath.Join(configRoot, "chkbit/config.json")
466+
configPath = slpath.Join(configRoot, "chkbit/config.json")
445467
}
446468

447469
var cli CLI
@@ -460,6 +482,8 @@ func (m *Main) run() int {
460482
ctx = kong.Parse(&cli, kongOptions...)
461483
}
462484

485+
cli.toSlash()
486+
463487
if cli.Quiet {
464488
m.progress = Quiet
465489
} else if fileInfo, err := os.Stdout.Stat(); err == nil && (fileInfo.Mode()&os.ModeCharDevice) == 0 {

context.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66
"os"
7+
slpath "path"
78
"path/filepath"
89
"strings"
910
"sync"
@@ -193,7 +194,7 @@ func (context *Context) scanDir(root string, parentIgnore *Ignore, depth int) {
193194
}
194195

195196
for _, file := range files {
196-
path := filepath.Join(root, file.Name())
197+
path := slpath.Join(root, file.Name())
197198
if isDir(file, path, context.SkipSymlinks) {
198199
if !ignore.shouldIgnore(file.Name()) {
199200
dirList = append(dirList, file.Name())
@@ -209,7 +210,7 @@ func (context *Context) scanDir(root string, parentIgnore *Ignore, depth int) {
209210

210211
if !context.SkipSubdirectories && (context.MaxDepth == 0 || depth < context.MaxDepth) {
211212
for _, name := range dirList {
212-
context.scanDir(filepath.Join(root, name), ignore, depth+1)
213+
context.scanDir(slpath.Join(root, name), ignore, depth+1)
213214
}
214215
}
215216
}
@@ -223,14 +224,15 @@ func (context *Context) UseAtomIndexStore(root string, pathList []string) (relat
223224
}
224225

225226
// below root?
226-
if !strings.HasPrefix(path, root) {
227+
if !strings.HasPrefix(filepath.ToSlash(path), root) {
227228
return nil, fmt.Errorf("path %s is not below the atom index in %s", path, root)
228229
}
229230

230231
relativePath, err := filepath.Rel(root, path)
231232
if err != nil {
232233
return nil, err
233234
}
235+
relativePath = filepath.ToSlash(relativePath)
234236
relativePathList = append(relativePathList, relativePath)
235237
}
236238

dedup.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"errors"
66
"fmt"
77
"os"
8-
"path/filepath"
8+
slpath "path"
99
"slices"
1010
"time"
1111

@@ -116,7 +116,7 @@ func NewDedup(path string, indexName string, createIfNotExists bool) (*Dedup, er
116116
LogQueue: make(chan *LogEvent, 100),
117117
PerfQueue: make(chan *DedupPerfEvent, 100),
118118
}
119-
dedupFile := filepath.Join(path, d.indexName+dedupSuffix)
119+
dedupFile := slpath.Join(path, d.indexName+dedupSuffix)
120120

121121
_, err = os.Stat(dedupFile)
122122
if err != nil {
@@ -254,7 +254,7 @@ func (d *Dedup) DetectDupes(minSize uint64, verbose bool) (err error) {
254254
for hash, bag := range all {
255255
if bag.Size == -1 {
256256
for _, p := range bag.ItemList {
257-
if s, err := os.Stat(filepath.Join(d.rootPath, p.Path)); err == nil {
257+
if s, err := os.Stat(slpath.Join(d.rootPath, p.Path)); err == nil {
258258
bag.Size = s.Size()
259259
break
260260
}
@@ -319,7 +319,7 @@ func (d *Dedup) DetectDupes(minSize uint64, verbose bool) (err error) {
319319
var matches []match
320320
d.perfMonFiles(len(bag.ItemList), float64(i), len(all))
321321
for _, item := range bag.ItemList {
322-
if res, err := GetFileExtents(filepath.Join(d.rootPath, item.Path)); err == nil {
322+
if res, err := GetFileExtents(slpath.Join(d.rootPath, item.Path)); err == nil {
323323
matches = append(matches, match{-1, res, item})
324324
} else if IsNotSupported(err) {
325325
matches = append(matches, match{-1, nil, item})
@@ -508,8 +508,8 @@ func (d *Dedup) Dedup(hashes []string, verbose bool) error {
508508
}
509509

510510
if !list[i].Merged {
511-
a := filepath.Join(d.rootPath, list[0].Path)
512-
b := filepath.Join(d.rootPath, list[i].Path)
511+
a := slpath.Join(d.rootPath, list[0].Path)
512+
b := slpath.Join(d.rootPath, list[i].Path)
513513
if verbose {
514514
d.logMsg(fmt.Sprintf("dedup %s %s \"%s\" -- \"%s\"", hash, intutil.FormatSize(uint64(bag.Size)), a, b))
515515
} else {

fuse.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"errors"
66
"fmt"
77
"os"
8-
"path/filepath"
8+
slpath "path"
99
)
1010

1111
type fuseStore struct {
@@ -69,7 +69,7 @@ func (f *fuseStore) fuseScanDir(root, prefix string) {
6969
}
7070

7171
for _, file := range files {
72-
path := filepath.Join(root, file.Name())
72+
path := slpath.Join(root, file.Name())
7373
if isDir(file, path, f.skipSymlinks) {
7474
newPrefix := prefix + file.Name() + "/"
7575
if fileName, ok, _ := existsMarkerFile(IndexTypeAtom, path, f.indexName); ok {

ignore.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package chkbit
33
import (
44
"bufio"
55
"os"
6-
"path/filepath"
6+
slpath "path"
77
"strings"
88
)
99

@@ -20,7 +20,7 @@ func GetIgnore(context *Context, path string, parentIgnore *Ignore) (*Ignore, er
2020
parentIgnore: parentIgnore,
2121
context: context,
2222
path: path,
23-
name: filepath.Base(path) + "/",
23+
name: slpath.Base(path) + "/",
2424
}
2525
err := ignore.loadIgnore()
2626
if err != nil {
@@ -30,7 +30,7 @@ func GetIgnore(context *Context, path string, parentIgnore *Ignore) (*Ignore, er
3030
}
3131

3232
func (ignore *Ignore) getIgnoreFilepath() string {
33-
return filepath.Join(ignore.path, ignore.context.IgnoreFilename)
33+
return slpath.Join(ignore.path, ignore.context.IgnoreFilename)
3434
}
3535

3636
func (ignore *Ignore) loadIgnore() error {
@@ -76,11 +76,11 @@ func (ignore *Ignore) shouldIgnore2(name string, fullname string) bool {
7676
item = item[1:]
7777
}
7878
}
79-
if match, _ := filepath.Match(item, name); match {
79+
if match, _ := slpath.Match(item, name); match {
8080
return true
8181
}
8282
if fullname != "" {
83-
if match, _ := filepath.Match(item, fullname); match {
83+
if match, _ := slpath.Match(item, fullname); match {
8484
return true
8585
}
8686
}

index.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"encoding/json"
55
"errors"
66
"os"
7-
"path/filepath"
7+
slpath "path"
88
"slices"
99
)
1010

@@ -72,19 +72,19 @@ func getMtS(path string) (mtime, size int64, err error) {
7272
}
7373

7474
func (i *Index) getIndexFilepath() string {
75-
return filepath.Join(i.path, i.context.IndexFilename)
75+
return slpath.Join(i.path, i.context.IndexFilename)
7676
}
7777

7878
func (i *Index) logFilePanic(name string, message string) {
79-
i.context.log(StatusPanic, filepath.Join(i.path, name)+": "+message)
79+
i.context.log(StatusPanic, slpath.Join(i.path, name)+": "+message)
8080
}
8181

8282
func (i *Index) logFile(stat Status, name string) {
83-
i.context.log(stat, filepath.Join(i.path, name))
83+
i.context.log(stat, slpath.Join(i.path, name))
8484
}
8585

8686
func (i *Index) logDir(stat Status, name string) {
87-
i.context.log(stat, filepath.Join(i.path, name)+"/")
87+
i.context.log(stat, slpath.Join(i.path, name)+"/")
8888
}
8989

9090
func (i *Index) calcHashes(ignore *Ignore) {
@@ -196,12 +196,12 @@ func (i *Index) checkFix(forceUpdateDmg bool) {
196196
}
197197

198198
func (i *Index) mtimeChanged(name string, ii idxInfo) bool {
199-
mtime, _, _ := getMtS(filepath.Join(i.path, name))
199+
mtime, _, _ := getMtS(slpath.Join(i.path, name))
200200
return ii.ModTime != mtime
201201
}
202202

203203
func (i *Index) calcFile(name string, algo string) (*idxInfo, error) {
204-
path := filepath.Join(i.path, name)
204+
path := slpath.Join(i.path, name)
205205
mtime, size, err := getMtS(path)
206206
if err != nil {
207207
return nil, err

indexstore.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"errors"
66
"os"
7+
slpath "path"
78
"path/filepath"
89
"slices"
910
"sync"
@@ -209,7 +210,7 @@ func (s *indexStore) Save(indexPath string, value []byte) error {
209210
s.storeDbQueue <- &storeDbItem{[]byte(indexPath), value}
210211
} else {
211212
// try to preserve the directory mod time but ignore if unsupported
212-
dirPath := filepath.Dir(indexPath)
213+
dirPath := slpath.Dir(indexPath)
213214
dirStat, dirErr := os.Stat(dirPath)
214215
err = os.WriteFile(indexPath, value, 0644)
215216
if dirErr == nil {
@@ -297,7 +298,7 @@ func (s *indexStore) exportCache(dbFile, suffix string) (exportFile string, err
297298
}
298299

299300
// remove index filename
300-
key := filepath.Dir(string(k))
301+
key := slpath.Dir(string(k))
301302
if key == "." {
302303
key = ""
303304
}
@@ -431,12 +432,12 @@ func verifyAtomJsonHead(decoder *json.Decoder) error {
431432
}
432433

433434
func getAtomFile(path, indexName, suffix string) string {
434-
return filepath.Join(path, indexName+atomSuffix+suffix)
435+
return slpath.Join(path, indexName+atomSuffix+suffix)
435436
}
436437

437438
func getMarkerFile(st IndexType, path, indexName string) string {
438439
if st == IndexTypeSplit {
439-
return filepath.Join(path, indexName)
440+
return slpath.Join(path, indexName)
440441
} else {
441442
return getAtomFile(path, indexName, "")
442443
}
@@ -502,6 +503,7 @@ func LocateIndex(startPath string, filter IndexType, indexName string) (st Index
502503
if path, err = filepath.Abs(startPath); err != nil {
503504
return
504505
}
506+
path = filepath.ToSlash(path)
505507
for {
506508
var ok bool
507509
for _, st = range IndexTypeList {
@@ -512,8 +514,8 @@ func LocateIndex(startPath string, filter IndexType, indexName string) (st Index
512514
}
513515
}
514516

515-
path = filepath.Dir(path)
516-
if len(path) < 1 || path[len(path)-1] == filepath.Separator {
517+
path = slpath.Dir(path)
518+
if len(path) < 1 || path[len(path)-1] == '/' {
517519
// reached root
518520
err = errMissingIndex
519521
return

0 commit comments

Comments
 (0)