Compute directory size

This commit is contained in:
Quentin 2021-04-27 16:24:01 +02:00
parent ca13aaf50b
commit 0b4b735e46
Signed by: quentin
GPG key ID: A98E9B769E4FF428
7 changed files with 35 additions and 6 deletions

View file

@ -2,6 +2,7 @@ package main
import( import(
"log" "log"
"code.cloudfoundry.org/bytefmt"
) )
func cmdHead(config *configCollect) { func cmdHead(config *configCollect) {
@ -29,7 +30,9 @@ func cmdHead(config *configCollect) {
func cmdLs(config *configCollect) { func cmdLs(config *configCollect) {
en := NewEntryNode(config) en := NewEntryNode(config)
en.Walk(new(LsWalker)) lw := new(LsWalker)
en.Walk(lw)
log.Println("Total size:", bytefmt.ByteSize(lw.TotalSize))
} }
func cmdInfo(config *configCollect) { func cmdInfo(config *configCollect) {
@ -59,3 +62,8 @@ func cmdS3File(config *configCollect) {
sw := NewS3Walker(config) sw := NewS3Walker(config)
sw.onFile(en) sw.onFile(en)
} }
func cmdS3Del(config *configCollect) {
sw := NewS3Walker(config)
sw.onDelete()
}

View file

@ -6,6 +6,7 @@ type configCollect struct {
Info bool `docopt:"info"` Info bool `docopt:"info"`
Cp bool `docopt:"cp"` Cp bool `docopt:"cp"`
S3 bool `docopt:"s3"` S3 bool `docopt:"s3"`
S3Del bool `docopt:"s3del"`
Storage string `docopt:"--storage"` Storage string `docopt:"--storage"`
DirId string `docopt:"--dir"` DirId string `docopt:"--dir"`
FileId string `docopt:"--file"` FileId string `docopt:"--file"`

1
go.mod
View file

@ -3,6 +3,7 @@ module git.deuxfleurs.fr/quentin/seafile_recovery
go 1.15 go 1.15
require ( require (
code.cloudfoundry.org/bytefmt v0.0.0-20200131002437-cf55d5288a48
github.com/aws/aws-sdk-go v1.38.25 github.com/aws/aws-sdk-go v1.38.25
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815 github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815
github.com/minio/minio-go/v7 v7.0.10 github.com/minio/minio-go/v7 v7.0.10

2
go.sum
View file

@ -1,3 +1,5 @@
code.cloudfoundry.org/bytefmt v0.0.0-20200131002437-cf55d5288a48 h1:/EMHruHCFXR9xClkGV/t0rmHrdhX4+trQUcBqjwc9xE=
code.cloudfoundry.org/bytefmt v0.0.0-20200131002437-cf55d5288a48/go.mod h1:wN/zk7mhREp/oviagqUXY3EwuHhWyOvAdsn5Y4CzOrc=
github.com/aws/aws-sdk-go v1.38.25 h1:aNjeh7+MON05cZPtZ6do+KxVT67jPOSQXANA46gOQao= github.com/aws/aws-sdk-go v1.38.25 h1:aNjeh7+MON05cZPtZ6do+KxVT67jPOSQXANA46gOQao=
github.com/aws/aws-sdk-go v1.38.25/go.mod h1:hcU610XS61/+aQV88ixoOzUoG7v3b31pl2zKMmprdro= github.com/aws/aws-sdk-go v1.38.25/go.mod h1:hcU610XS61/+aQV88ixoOzUoG7v3b31pl2zKMmprdro=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

View file

@ -2,10 +2,13 @@ package main
import ("log") import ("log")
type LsWalker struct { } type LsWalker struct {
TotalSize uint64
}
func (lw* LsWalker) onDir(dn *DirNode) { func (lw* LsWalker) onDir(dn *DirNode) {
log.Println(dn.String()) log.Println(dn.String())
} }
func (lw* LsWalker) onFile(fn *FileNode) { func (lw* LsWalker) onFile(fn *FileNode) {
lw.TotalSize += uint64(fn.Ent.Size)
log.Println(fn.String()) log.Println(fn.String())
} }

View file

@ -65,9 +65,9 @@ func (sw* S3Walker) onDir(dn *DirNode) {
} }
func (sw* S3Walker) onFile(fn *FileNode) { func (sw* S3Walker) onFile(fn *FileNode) {
fn.Parse() fn.Parse()
path := filepath.Join(sw.pathPrefix, fn.AbsolutePath) p := filepath.Join(sw.pathPrefix, fn.AbsolutePath)[1:]
contentType := mime.TypeByExtension(filepath.Ext(path)) contentType := mime.TypeByExtension(filepath.Ext(p))
info, err := sw.mc.PutObject(sw.ctx, sw.bucket, path, fn, int64(fn.Elem.FileSize), minio.PutObjectOptions{ ContentType: contentType }) info, err := sw.mc.PutObject(sw.ctx, sw.bucket, p, fn, int64(fn.Elem.FileSize), minio.PutObjectOptions{ ContentType: contentType })
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} else if info.Size != int64(fn.Elem.FileSize) { } else if info.Size != int64(fn.Elem.FileSize) {
@ -75,3 +75,12 @@ func (sw* S3Walker) onFile(fn *FileNode) {
} }
log.Println(fn.String()) log.Println(fn.String())
} }
func (sw* S3Walker) onDelete() {
opts := minio.RemoveObjectOptions {}
err := sw.mc.RemoveObject(context.Background(), sw.bucket, sw.pathPrefix, opts)
if err != nil {
log.Fatal(err)
}
log.Println(sw.pathPrefix)
}

View file

@ -13,6 +13,7 @@ Usage:
seafile_recovery [--storage=<sto>] ls <repoid> (--dir=<dirid> | --file=<fileid>) seafile_recovery [--storage=<sto>] ls <repoid> (--dir=<dirid> | --file=<fileid>)
seafile_recovery [--storage=<sto>] cp <repoid> (--dir=<dirid> | --file=<fileid>) <dest> seafile_recovery [--storage=<sto>] cp <repoid> (--dir=<dirid> | --file=<fileid>) <dest>
seafile_recovery [--storage=<sto>] s3 <repoid> (--dir=<dirid> | --file=<pathid>) <dest> seafile_recovery [--storage=<sto>] s3 <repoid> (--dir=<dirid> | --file=<pathid>) <dest>
seafile_recovery s3del <dest>
seafile_recovery (-h | --help) seafile_recovery (-h | --help)
Options: Options:
@ -27,7 +28,9 @@ Options:
if err != nil { log.Fatal(err) } if err != nil { log.Fatal(err) }
opts.Bind(config) opts.Bind(config)
checkRootFolder(config.Storage) if !config.S3Del {
checkRootFolder(config.Storage)
}
rexists := repoExistsIn(config.Storage, config.RepoId) rexists := repoExistsIn(config.Storage, config.RepoId)
if config.Head { if config.Head {
@ -59,6 +62,8 @@ Options:
} else { } else {
cmdS3File(config) cmdS3File(config)
} }
} else if config.S3Del{
cmdS3Del(config)
} else { } else {
log.Fatal("This command is not implemented") log.Fatal("This command is not implemented")
} }