Expand ID logic

This commit is contained in:
Quentin 2021-04-24 11:09:15 +02:00
parent c0c8368012
commit 67b86ae256
Signed by: quentin
GPG Key ID: A98E9B769E4FF428
3 changed files with 48 additions and 3 deletions

View File

@ -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())
}

43
fs.go
View File

@ -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))

View File

@ -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) }