From aca98e2d2a09d8fbcc3a36c3bc278041598bc91a Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Mon, 26 Apr 2021 11:05:31 +0200 Subject: [PATCH] Successfully extracted an image --- command.go | 19 +++++++++++++++++++ config.go | 2 +- fs.go | 34 ++++++++++++++++++++++++++++++++++ seafile_recovery.go | 9 +++++++-- 4 files changed, 61 insertions(+), 3 deletions(-) diff --git a/command.go b/command.go index 248860b..c7e606e 100644 --- a/command.go +++ b/command.go @@ -2,6 +2,8 @@ package main import( "log" + "os" + "io" ) func cmdHead(config *configCollect) { @@ -44,3 +46,20 @@ func cmdInfo(config *configCollect) { en.Parse() log.Println(en.Elem.String()) } + +func cmdCpFile(config *configCollect) { + en := NewEntryFileNode(config) + en.Parse() + + file, err := os.Create(config.Dest) + if err != nil { log.Fatal(err) } + written, err := io.Copy(file, en) + if err != nil { log.Fatal(err) } + if uint64(written) != en.Elem.FileSize { + log.Fatal(written, "bytes written,", en.Elem.FileSize, "bytes expected") + } +} + +func cmdCpDir(config *configCollect) { + +} diff --git a/config.go b/config.go index a59ee56..27c3820 100644 --- a/config.go +++ b/config.go @@ -10,5 +10,5 @@ type configCollect struct { DirId string `docopt:"--dir"` FileId string `docopt:"--file"` RepoId string `docopt:""` - S3Url string `docopt:""` + Dest string `docopt:""` } diff --git a/fs.go b/fs.go index ab9b3e7..cb61345 100644 --- a/fs.go +++ b/fs.go @@ -5,6 +5,7 @@ import( "fmt" "log" "os" + "io" "io/ioutil" "encoding/json" "syscall" @@ -54,6 +55,10 @@ type FileNode struct { Ent *DirEnt Elem FileElem AbsolutePath string + + ReadBytes uint64 + CurrentBlock *os.File + RemainingBlocks []string } func (fe* FileElem) String() string { @@ -163,6 +168,35 @@ func (fn* FileNode) Parse() { jdec := json.NewDecoder(zfile) err = jdec.Decode(&fn.Elem) if err != nil { log.Fatal(err) } + + fn.RemainingBlocks = fn.Elem.BlkIds +} + +func (fn* FileNode) Read(p []byte) (n int, err error) { + if fn.CurrentBlock == nil && len(fn.RemainingBlocks) == 0 { + return 0, io.EOF + } + + if fn.CurrentBlock == nil { + blockId := fn.RemainingBlocks[0] + fn.RemainingBlocks = fn.RemainingBlocks[1:] + path := filepath.Join(fn.Config.Storage, "blocks", fn.Config.RepoId, blockId[:2], blockId[2:]) + fn.CurrentBlock, err = os.Open(path) + if err != nil { + return 0, err + } + } + + n, err = fn.CurrentBlock.Read(p) + fn.ReadBytes += uint64(n) + + if err == io.EOF { + err = nil + fn.CurrentBlock.Close() + fn.CurrentBlock = nil + } + + return n, err } func (dn* DirNode) Children() ([]*DirNode, []*FileNode) { diff --git a/seafile_recovery.go b/seafile_recovery.go index 02a7a90..a0e4a90 100644 --- a/seafile_recovery.go +++ b/seafile_recovery.go @@ -11,8 +11,8 @@ func main() { Usage: seafile_recovery [--storage=] head seafile_recovery [--storage=] ls (--dir= | --file=) - seafile_recovery [--storage=] cp (--dir= | --file=) - seafile_recovery [--storage=] s3 (--dir= | --file=) + seafile_recovery [--storage=] cp (--dir= | --file=) + seafile_recovery [--storage=] s3 (--dir= | --file=) seafile_recovery (-h | --help) Options: @@ -45,6 +45,11 @@ Options: if !rexists["fs"] { log.Fatal("No fs folder found for repo ",config.RepoId) } if !rexists["blocks"] { log.Fatal("No blocks folder found for repo ",config.RepoId) } + if len(config.DirId) > 0 { + cmdCpDir(config) + } else { + cmdCpFile(config) + } } else if config.S3 { if !rexists["fs"] { log.Fatal("No fs folder found for repo ",config.RepoId) } if !rexists["blocks"] { log.Fatal("No blocks folder found for repo ",config.RepoId) }