From 67b86ae2560e74b15e6d3c771cb35bea2fa20058 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Sat, 24 Apr 2021 11:09:15 +0200 Subject: [PATCH] Expand ID logic --- command.go | 6 ++++++ fs.go | 43 +++++++++++++++++++++++++++++++++++++++++-- seafile_recovery.go | 2 +- 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/command.go b/command.go index 7d8ccf3..248860b 100644 --- a/command.go +++ b/command.go @@ -38,3 +38,9 @@ func cmdLs(config *configCollect) { en := NewEntryNode(config) en.Walk(new(LsWalker)) } + +func cmdInfo(config *configCollect) { + en := NewEntryFileNode(config) + en.Parse() + log.Println(en.Elem.String()) +} diff --git a/fs.go b/fs.go index 3233e20..ab9b3e7 100644 --- a/fs.go +++ b/fs.go @@ -5,6 +5,7 @@ import( "fmt" "log" "os" + "io/ioutil" "encoding/json" "syscall" "path/filepath" @@ -13,6 +14,8 @@ import( // Imported from https://github.com/haiwen/seafile-server/blob/master/fileserver/fsmgr/fsmgr.go // License AGPLv3 +const emptyId = "0000000000000000000000000000000000000000" + //SeafDir is a dir object type DirElem struct { Version int `json:"version"` @@ -53,15 +56,41 @@ type FileNode struct { AbsolutePath string } +func (fe* FileElem) String() string { + return fmt.Sprintf("Size: %v, Blocks: %v", fe.FileSize, len(fe.BlkIds)) +} + type TreeObserver interface { onDir(dir *DirNode) onFile(file *FileNode) } +func expandId(config *configCollect, userId string) string { + if len(userId) == len(emptyId) { + return userId + } + + if len(userId) < 2 { + log.Fatal("User ID",userId,"is too short and thus cannot be expanded") + } + + path := filepath.Join(config.Storage, "fs", config.RepoId, userId[:2]) + files, err := ioutil.ReadDir(path) + if err != nil { log.Fatal("Unable to read dir", path, "in order to expand ID", userId) } + for _, f := range files { + if f.Name()[:len(userId)-2] == userId[2:] { + return userId[:2] + f.Name() + } + } + + log.Fatal("Unable to find", userId[2:],"in",path) + return emptyId +} + func NewEntryNode(config *configCollect) *DirNode { entryNode := new(DirNode) entryNode.Ent = new(DirEnt) - entryNode.Ent.Id = config.DirId + entryNode.Ent.Id = expandId(config, config.DirId) entryNode.Ent.Name = "" entryNode.AbsolutePath = "/" entryNode.Config = config @@ -95,6 +124,16 @@ func NewFileNode(parent *DirNode, ent *DirEnt) *FileNode { return fn } +func NewEntryFileNode(config *configCollect) *FileNode { + fn := new (FileNode) + fn.Ent = new(DirEnt) + fn.Ent.Id = expandId(config, config.FileId) + fn.Config = 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:]) @@ -131,7 +170,7 @@ func (dn* DirNode) Children() ([]*DirNode, []*FileNode) { files := make([]*FileNode, 0) for _, el := range dn.Elem.Entries { - if el.Id == "0000000000000000000000000000000000000000" { + if el.Id == emptyId { log.Println("[Lost] "+dn.AbsolutePath+el.Name) } else if IsDir(el.Mode) { folders = append(folders, NewDirNode(dn, el)) diff --git a/seafile_recovery.go b/seafile_recovery.go index 3425696..02a7a90 100644 --- a/seafile_recovery.go +++ b/seafile_recovery.go @@ -39,7 +39,7 @@ Options: if len(config.DirId) > 0 { cmdLs(config) } else { - //cmdInfo(config) + cmdInfo(config) } } else if config.Cp { if !rexists["fs"] { log.Fatal("No fs folder found for repo ",config.RepoId) }