Skip to content

Commit

Permalink
Merge pull request #77 from xushiwei/q
Browse files Browse the repository at this point in the history
remote.SyncOpen bugfix; cached.WriteConf
  • Loading branch information
xushiwei authored Sep 12, 2023
2 parents aab80cc + f9a8739 commit a99ed55
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
13 changes: 4 additions & 9 deletions http/fs/cached/remote/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ func checkDirCached(dir string) fs.FileInfo {
return fi
}

func touchDirCached(dir string) error {
func TouchDirCached(dir string) error {
cacheFile := filepath.Join(dir, dirListCacheFile)
return os.WriteFile(cacheFile, nil, 0666)
}

func writeStubFile(localFile string, fi fs.FileInfo) error {
func WriteStubFile(localFile string, fi fs.FileInfo) error {
if fi.IsDir() {
return os.Mkdir(localFile, 0755)
}
Expand Down Expand Up @@ -188,11 +188,6 @@ func (p *remote) SyncLstat(local string, name string) (fi fs.FileInfo, err error
}

func (p *remote) SyncOpen(local string, name string) (f http.File, err error) {
if name == "/" {
name = "."
} else {
name = strings.TrimPrefix(name, "/")
}
f, err = p.bucket.Open(name)
if err != nil {
log.Printf(`[ERROR] bucket.Open("%s"): %v\n`, name, err)
Expand All @@ -215,12 +210,12 @@ func (p *remote) SyncOpen(local string, name string) (f http.File, err error) {
base := filepath.Join(local, name)
for _, fi := range fis {
itemFile := base + "/" + fi.Name()
if writeStubFile(itemFile, fi) != nil {
if WriteStubFile(itemFile, fi) != nil {
nError++
}
}
if nError == 0 {
touchDirCached(base)
TouchDirCached(base)
} else {
log.Printf("[WARN] writeStubFile fail (%d errors)", nError)
}
Expand Down
13 changes: 11 additions & 2 deletions http/fsx/cached/cached.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const (
confFileName = remote.SysFilePrefix + "conf"
)

type config struct {
type Config struct {
Base string `json:"base"` // url of base file system
CacheFile bool `json:"cacheFile"`
}
Expand All @@ -57,7 +57,7 @@ func New(ctx context.Context, localDir string, offline ...bool) (fs http.FileSys
if err != nil {
return
}
var conf config
var conf Config
err = json.Unmarshal(b, &conf)
if err != nil {
return
Expand All @@ -74,4 +74,13 @@ func New(ctx context.Context, localDir string, offline ...bool) (fs http.FileSys
return
}

func WriteConf(localDir string, conf *Config) (err error) {
confFile := filepath.Join(localDir, confFileName)
b, err := json.MarshalIndent(conf, "", " ")
if err != nil {
return
}
return os.WriteFile(confFile, b, 0644)
}

// -----------------------------------------------------------------------------------------

0 comments on commit a99ed55

Please sign in to comment.