From c09c5406e9beae4f3b87ddd371e66a22c6e8526a Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Fri, 23 Apr 2021 12:18:38 +0200 Subject: [PATCH] WIP list files --- command.go | 5 +- fs.go | 112 +++++++++++++++++++++++++++++++++++++------- seafile_recovery.go | 2 +- 3 files changed, 98 insertions(+), 21 deletions(-) diff --git a/command.go b/command.go index 00a3ae4..0a8254a 100644 --- a/command.go +++ b/command.go @@ -1,7 +1,6 @@ package main import( - "fmt" "log" ) @@ -27,7 +26,7 @@ func cmdHead(config configCollect) { log.Println("Proposing following HEAD:\n"+rc.Head.Content.String()) } -func cmdLs(config configCollect) { +func cmdLs(config *configCollect) { rf := NewRepoFs(config) - log.Println(fmt.Sprintf("%#v", rf.Path.Elem)) + log.Println(rf.EntryNode.String()) } diff --git a/fs.go b/fs.go index a6c2f8e..b17d58d 100644 --- a/fs.go +++ b/fs.go @@ -17,7 +17,7 @@ import( type DirElem struct { Version int `json:"version"` DirType int `json:"type,omitempty"` - DirID string `json:"dir_id,omitempty"` + DirId string `json:"dir_id,omitempty"` Entries []*DirEnt `json:"dirents"` } @@ -34,39 +34,72 @@ type DirEnt struct { type FileElem struct { Version int `json:"version"` FileType int `json:"type,omitempty"` - FileID string `json:"file_id,omitempty"` + FileId string `json:"file_id,omitempty"` FileSize uint64 `json:"size"` - BlkIDs []string `json:"block_ids"` + BlkIds []string `json:"block_ids"` } type DirNode struct { - Id string - Folders *DirNode - Files []FileElem + Config *configCollect + Ent *DirEnt Elem DirElem + Folders []*DirNode + Files []*FileNode + Parent *DirNode + AbsolutePath string +} + +type FileNode struct { + Config *configCollect + Ent *DirEnt + Elem FileElem } type RepoFs struct { - Config configCollect - Path *DirNode + Config *configCollect + EntryNode *DirNode } -func NewRepoFs(config configCollect) *RepoFs { +func NewRepoFs(config *configCollect) *RepoFs { rf := new(RepoFs) rf.Config = config - rf.Path = NewDirNode(filepath.Join(config.Storage, "fs", config.RepoId), config.PathId) + rf.EntryNode = new(DirNode) + rf.EntryNode.Ent = new(DirEnt) + rf.EntryNode.Ent.Id = config.PathId + rf.EntryNode.Ent.Name = "" + rf.EntryNode.AbsolutePath = "/" + rf.EntryNode.Files = make([]*FileNode,0) + rf.EntryNode.Folders = make([]*DirNode,0) + return rf } -func (rf* RepoFs) String() string { - return fmt.Sprintf("%#v", rf) +func NewDirNode(parent *DirNode, ent *DirEnt) *DirNode { + dn := new(DirNode) + dn.Ent = ent + dn.Config = parent.Config + dn.Files = make([]*FileNode,0) + dn.Folders = make([]*DirNode,0) + dn.Parent = parent + dn.AbsolutePath = dn.Parent.AbsolutePath + ent.Name + "/" + + return dn } -func NewDirNode(base string, id string) *DirNode { - dn := new(DirNode) - dn.Id = id +func (dn* DirNode) String() string { + return fmt.Sprintf("%v %v", dn.Ent.Id[:6], dn.AbsolutePath) +} - path := filepath.Join(base, id[:2], id[2:]) +func NewFileNode(parent *DirNode, ent *DirEnt) *FileNode { + fn := new (FileNode) + fn.Ent = ent + fn.Config = parent.Config + + return fn +} + +func (dn* DirNode) Parse() { + path := filepath.Join(dn.Config.Storage, "fs", dn.Config.RepoId, dn.Ent.Id[:2], dn.Ent.Id[2:]) file, err := os.Open(path) if err != nil { log.Fatal(err) } @@ -79,7 +112,52 @@ func NewDirNode(base string, id string) *DirNode { err = jdec.Decode(&dn.Elem) if err != nil { log.Fatal(err) } - return dn + for _, el := range dn.Elem.Entries { + if IsDir(el.Mode) { + dn.Folders = append(dn.Folders, NewDirNode(dn, el)) + } else if IsRegular(el.Mode) { + dn.Files = append(dn.Files, NewFileNode(dn, el)) + } else { + log.Fatal("Unknown mode", el.Mode, "for", el.Name, "in object id", dn.Ent.Id) + } + } +} + +func (fn* FileNode) Parse() { + path := filepath.Join(fn.Config.Storage, "fs", fn.Config.RepoId, fn.Ent.Id[:2], fn.Ent.Id[2:]) + + file, err := os.Open(path) + if err != nil { log.Fatal(err) } + defer file.Close() + + zfile, err := zlib.NewReader(file) + if err != nil { log.Fatal(err) } + + jdec := json.NewDecoder(zfile) + err = jdec.Decode(&fn.Elem) + if err != nil { log.Fatal(err) } +} + +type ExpandStrat int +const ( + ExpandOnlyDirectory ExpandStrat = iota + ExpandAll +) + +func (dn* DirNode) Expand(strat ExpandStrat) { + for _, cdn := range dn.Folders { + cdn.Parse() + } + + if strat == ExpandAll { + for _, cfn := range dn.Files { + cfn.Parse() + } + } +} + +func (dn* DirNode) Walk(strat ExpandStrat) { + } /*func (rf* RepoFs) Walk() { diff --git a/seafile_recovery.go b/seafile_recovery.go index 4b2a363..a04a83d 100644 --- a/seafile_recovery.go +++ b/seafile_recovery.go @@ -32,7 +32,7 @@ Options: cmdHead(config) } else if config.Ls { if !rexists["fs"] { log.Fatal("No fs folder found for repo ",config.RepoId) } - cmdLs(config) + cmdLs(&config) } else if config.Dump { 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) }