From 0b4b735e464211ca8c90a1574814a4e3cc8742ce Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Tue, 27 Apr 2021 16:24:01 +0200 Subject: [PATCH] Compute directory size --- command.go | 10 +++++++++- config.go | 1 + go.mod | 1 + go.sum | 2 ++ ls_walker.go | 5 ++++- s3_walker.go | 15 ++++++++++++--- seafile_recovery.go | 7 ++++++- 7 files changed, 35 insertions(+), 6 deletions(-) diff --git a/command.go b/command.go index 0017b17..aa2d51e 100644 --- a/command.go +++ b/command.go @@ -2,6 +2,7 @@ package main import( "log" + "code.cloudfoundry.org/bytefmt" ) func cmdHead(config *configCollect) { @@ -29,7 +30,9 @@ func cmdHead(config *configCollect) { func cmdLs(config *configCollect) { 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) { @@ -59,3 +62,8 @@ func cmdS3File(config *configCollect) { sw := NewS3Walker(config) sw.onFile(en) } + +func cmdS3Del(config *configCollect) { + sw := NewS3Walker(config) + sw.onDelete() +} diff --git a/config.go b/config.go index 27c3820..0b83df5 100644 --- a/config.go +++ b/config.go @@ -6,6 +6,7 @@ type configCollect struct { Info bool `docopt:"info"` Cp bool `docopt:"cp"` S3 bool `docopt:"s3"` + S3Del bool `docopt:"s3del"` Storage string `docopt:"--storage"` DirId string `docopt:"--dir"` FileId string `docopt:"--file"` diff --git a/go.mod b/go.mod index 38da86f..4eddb3a 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module git.deuxfleurs.fr/quentin/seafile_recovery go 1.15 require ( + code.cloudfoundry.org/bytefmt v0.0.0-20200131002437-cf55d5288a48 github.com/aws/aws-sdk-go v1.38.25 github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815 github.com/minio/minio-go/v7 v7.0.10 diff --git a/go.sum b/go.sum index 37d92a5..ad23fab 100644 --- a/go.sum +++ b/go.sum @@ -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/go.mod h1:hcU610XS61/+aQV88ixoOzUoG7v3b31pl2zKMmprdro= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= diff --git a/ls_walker.go b/ls_walker.go index 199a370..343cc52 100644 --- a/ls_walker.go +++ b/ls_walker.go @@ -2,10 +2,13 @@ package main import ("log") -type LsWalker struct { } +type LsWalker struct { + TotalSize uint64 + } func (lw* LsWalker) onDir(dn *DirNode) { log.Println(dn.String()) } func (lw* LsWalker) onFile(fn *FileNode) { + lw.TotalSize += uint64(fn.Ent.Size) log.Println(fn.String()) } diff --git a/s3_walker.go b/s3_walker.go index f00ebd4..02e3ef2 100644 --- a/s3_walker.go +++ b/s3_walker.go @@ -65,9 +65,9 @@ func (sw* S3Walker) onDir(dn *DirNode) { } func (sw* S3Walker) onFile(fn *FileNode) { fn.Parse() - path := filepath.Join(sw.pathPrefix, fn.AbsolutePath) - contentType := mime.TypeByExtension(filepath.Ext(path)) - info, err := sw.mc.PutObject(sw.ctx, sw.bucket, path, fn, int64(fn.Elem.FileSize), minio.PutObjectOptions{ ContentType: contentType }) + p := filepath.Join(sw.pathPrefix, fn.AbsolutePath)[1:] + contentType := mime.TypeByExtension(filepath.Ext(p)) + info, err := sw.mc.PutObject(sw.ctx, sw.bucket, p, fn, int64(fn.Elem.FileSize), minio.PutObjectOptions{ ContentType: contentType }) if err != nil { log.Fatalln(err) } else if info.Size != int64(fn.Elem.FileSize) { @@ -75,3 +75,12 @@ func (sw* S3Walker) onFile(fn *FileNode) { } 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) +} diff --git a/seafile_recovery.go b/seafile_recovery.go index 6849f16..250b150 100644 --- a/seafile_recovery.go +++ b/seafile_recovery.go @@ -13,6 +13,7 @@ Usage: seafile_recovery [--storage=] ls (--dir= | --file=) seafile_recovery [--storage=] cp (--dir= | --file=) seafile_recovery [--storage=] s3 (--dir= | --file=) + seafile_recovery s3del seafile_recovery (-h | --help) Options: @@ -27,7 +28,9 @@ Options: if err != nil { log.Fatal(err) } opts.Bind(config) - checkRootFolder(config.Storage) + if !config.S3Del { + checkRootFolder(config.Storage) + } rexists := repoExistsIn(config.Storage, config.RepoId) if config.Head { @@ -59,6 +62,8 @@ Options: } else { cmdS3File(config) } + } else if config.S3Del{ + cmdS3Del(config) } else { log.Fatal("This command is not implemented") }