WIP list files

This commit is contained in:
Quentin 2021-04-23 12:18:38 +02:00
parent 1fa3c3afbb
commit c09c5406e9
Signed by: quentin
GPG Key ID: A98E9B769E4FF428
3 changed files with 98 additions and 21 deletions

View File

@ -1,7 +1,6 @@
package main package main
import( import(
"fmt"
"log" "log"
) )
@ -27,7 +26,7 @@ func cmdHead(config configCollect) {
log.Println("Proposing following HEAD:\n"+rc.Head.Content.String()) log.Println("Proposing following HEAD:\n"+rc.Head.Content.String())
} }
func cmdLs(config configCollect) { func cmdLs(config *configCollect) {
rf := NewRepoFs(config) rf := NewRepoFs(config)
log.Println(fmt.Sprintf("%#v", rf.Path.Elem)) log.Println(rf.EntryNode.String())
} }

112
fs.go
View File

@ -17,7 +17,7 @@ import(
type DirElem struct { type DirElem struct {
Version int `json:"version"` Version int `json:"version"`
DirType int `json:"type,omitempty"` DirType int `json:"type,omitempty"`
DirID string `json:"dir_id,omitempty"` DirId string `json:"dir_id,omitempty"`
Entries []*DirEnt `json:"dirents"` Entries []*DirEnt `json:"dirents"`
} }
@ -34,39 +34,72 @@ type DirEnt struct {
type FileElem struct { type FileElem struct {
Version int `json:"version"` Version int `json:"version"`
FileType int `json:"type,omitempty"` FileType int `json:"type,omitempty"`
FileID string `json:"file_id,omitempty"` FileId string `json:"file_id,omitempty"`
FileSize uint64 `json:"size"` FileSize uint64 `json:"size"`
BlkIDs []string `json:"block_ids"` BlkIds []string `json:"block_ids"`
} }
type DirNode struct { type DirNode struct {
Id string Config *configCollect
Folders *DirNode Ent *DirEnt
Files []FileElem
Elem DirElem Elem DirElem
Folders []*DirNode
Files []*FileNode
Parent *DirNode
AbsolutePath string
}
type FileNode struct {
Config *configCollect
Ent *DirEnt
Elem FileElem
} }
type RepoFs struct { type RepoFs struct {
Config configCollect Config *configCollect
Path *DirNode EntryNode *DirNode
} }
func NewRepoFs(config configCollect) *RepoFs { func NewRepoFs(config *configCollect) *RepoFs {
rf := new(RepoFs) rf := new(RepoFs)
rf.Config = config 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 return rf
} }
func (rf* RepoFs) String() string { func NewDirNode(parent *DirNode, ent *DirEnt) *DirNode {
return fmt.Sprintf("%#v", rf) 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 { func (dn* DirNode) String() string {
dn := new(DirNode) return fmt.Sprintf("%v %v", dn.Ent.Id[:6], dn.AbsolutePath)
dn.Id = id }
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) file, err := os.Open(path)
if err != nil { log.Fatal(err) } if err != nil { log.Fatal(err) }
@ -79,7 +112,52 @@ func NewDirNode(base string, id string) *DirNode {
err = jdec.Decode(&dn.Elem) err = jdec.Decode(&dn.Elem)
if err != nil { log.Fatal(err) } 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() { /*func (rf* RepoFs) Walk() {

View File

@ -32,7 +32,7 @@ Options:
cmdHead(config) cmdHead(config)
} else if config.Ls { } else if config.Ls {
if !rexists["fs"] { log.Fatal("No fs folder found for repo ",config.RepoId) } if !rexists["fs"] { log.Fatal("No fs folder found for repo ",config.RepoId) }
cmdLs(config) cmdLs(&config)
} else if config.Dump { } else if config.Dump {
if !rexists["fs"] { log.Fatal("No fs folder found for repo ",config.RepoId) } 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 !rexists["blocks"] { log.Fatal("No blocks folder found for repo ",config.RepoId) }