Skip to content

Commit

Permalink
impl WalkDir for webdav
Browse files Browse the repository at this point in the history
  • Loading branch information
zyxkad committed Feb 8, 2024
1 parent b3cb272 commit e93b6eb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
11 changes: 9 additions & 2 deletions storage_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,18 @@ func (s *LocalStorage) ServeMeasure(rw http.ResponseWriter, req *http.Request, s
return nil
}

func walkCacheDir(cacheDir string, walker func(hash string) (err error)) (err error) {
var hex256 = func() (hex256 []string) {
hex256 = make([]string, 0x100)
var b [1]byte
for i := 0; i < 0x100; i++ {
b[0] = (byte)(i)
dir := hex.EncodeToString(b[:])
hex256[i] = hex.EncodeToString(b[:])
}
return
}()

func walkCacheDir(cacheDir string, walker func(hash string) (err error)) (err error) {
for _, dir := range hex256 {
files, err := os.ReadDir(filepath.Join(cacheDir, dir))
if err != nil {
if errors.Is(err, os.ErrNotExist) {
Expand Down
20 changes: 18 additions & 2 deletions storage_webdav.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,11 @@ func (s *WebDavStorage) hashToPath(hash string) string {
}

func (s *WebDavStorage) Size(hash string) (int64, error) {
stat, err := os.Stat(s.hashToPath(hash))
stat, err := s.cli.Stat(context.Background(), s.hashToPath(hash))
if err != nil {
return 0, err
}
return stat.Size(), nil
return stat.Size, nil
}

func (s *WebDavStorage) Open(hash string) (io.ReadCloser, error) {
Expand All @@ -183,6 +183,22 @@ func (s *WebDavStorage) Remove(hash string) error {
}

func (s *WebDavStorage) WalkDir(walker func(hash string) error) error {
ctx := context.Background()
for _, dir := range hex256 {
files, err := s.cli.ReadDir(ctx, dir, false)
if err != nil {
continue
}
for _, f := range files {
if !f.IsDir {
if hash := path.Base(f.Path); len(hash) >= 2 && hash[:2] == dir {
if err := walker(hash); err != nil {
return err
}
}
}
}
}
return nil
}

Expand Down

0 comments on commit e93b6eb

Please sign in to comment.