Skip to content
This repository has been archived by the owner on Oct 22, 2021. It is now read-only.

Commit

Permalink
Fix bugs in list and stat (#16)
Browse files Browse the repository at this point in the history
* Implementation of basic functions

* Fix bugs in list and stat

* Setup integration tests

* Fix nil reader in write

* Fix nil reader and non zero size in write

* Fix wrong return value when stating a nil object

* Fix write not using the given sizse

* Revert "Setup integration tests"

This reverts commit d04127f.
  • Loading branch information
KaiyuanLiu0 authored Sep 26, 2021
1 parent 2739a98 commit 84bde91
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package storj
import (
"context"
"io"
"strings"

"github.com/beyondstorage/go-storage/v4/services"
. "github.com/beyondstorage/go-storage/v4/types"
Expand All @@ -27,17 +28,21 @@ func (s *Storage) delete(ctx context.Context, path string, opt pairStorageDelete
rp := s.getAbsPath(path)

_, err = s.project.DeleteObject(ctx, s.name, rp)
return
return err
}

func (s *Storage) list(ctx context.Context, path string, opt pairStorageList) (oi *ObjectIterator, err error) {
rp := s.getAbsPath(path)
if !opt.HasListMode || opt.ListMode.IsDir() {
nextFn := func(ctx context.Context, page *ObjectPage) error {
options := uplink.ListObjectsOptions{System: true}
options := uplink.ListObjectsOptions{Prefix: rp, System: true}
dirObject := s.project.ListObjects(ctx, s.name, &options)
for dirObject.Next() {
if dirObject.Item().Key == rp {
continue
}
o := NewObject(s, true)
o.Path = dirObject.Item().Key
o.Path = dirObject.Item().Key[len(rp):]
if dirObject.Item().IsPrefix {
o.Mode |= ModeDir
} else {
Expand All @@ -49,7 +54,7 @@ func (s *Storage) list(ctx context.Context, path string, opt pairStorageList) (o
return IterateDone
}
oi = NewObjectIterator(ctx, nextFn, nil)
return
return oi, err
} else {
return nil, services.ListModeInvalidError{Actual: opt.ListMode}
}
Expand All @@ -66,20 +71,20 @@ func (s *Storage) read(ctx context.Context, path string, w io.Writer, opt pairSt
rp := s.getAbsPath(path)
download, err := s.project.DownloadObject(ctx, s.name, rp, nil)
if err != nil {
return 0, err
return 0, services.ErrObjectNotExist
}
n, err = io.Copy(w, download)
return
return n, err
}

func (s *Storage) stat(ctx context.Context, path string, opt pairStorageStat) (o *Object, err error) {
rp := s.getAbsPath(path)
object, err := s.project.StatObject(ctx, s.name, rp)
if err != nil {
return nil, err
return nil, services.ErrObjectNotExist
}
o = NewObject(s, true)
o.Path = object.Key
o.Path = path
if object.IsPrefix {
o.Mode |= ModeDir
} else {
Expand All @@ -98,13 +103,21 @@ func (s *Storage) write(ctx context.Context, path string, r io.Reader, size int6
if err != nil {
return 0, err
}
n, err = io.Copy(upload, r)
if r == nil {
if size == 0 {
r = strings.NewReader("")
} else {
return 0, services.ServiceError{}
}
}

n, err = io.CopyN(upload, r, size)
if err != nil {
return 0, err
}
err = upload.Commit()
if err != nil {
return 0, err
}
return
return n, err
}

0 comments on commit 84bde91

Please sign in to comment.