seafile_recovery/fs.go

249 lines
5.6 KiB
Go
Raw Permalink Normal View History

2021-04-22 20:43:44 +00:00
package main
import(
"compress/zlib"
"fmt"
"log"
"os"
2021-04-26 09:05:31 +00:00
"io"
2021-04-24 09:09:15 +00:00
"io/ioutil"
2021-04-22 20:43:44 +00:00
"encoding/json"
"syscall"
"path/filepath"
)
// Imported from https://github.com/haiwen/seafile-server/blob/master/fileserver/fsmgr/fsmgr.go
// License AGPLv3
2021-04-24 09:09:15 +00:00
const emptyId = "0000000000000000000000000000000000000000"
2021-04-22 20:43:44 +00:00
//SeafDir is a dir object
type DirElem struct {
Version int `json:"version"`
DirType int `json:"type,omitempty"`
2021-04-23 10:18:38 +00:00
DirId string `json:"dir_id,omitempty"`
2021-04-22 20:43:44 +00:00
Entries []*DirEnt `json:"dirents"`
}
// SeafDirent is a dir entry object
type DirEnt struct {
Mode uint32 `json:"mode"`
Id string `json:"id"`
Name string `json:"name"`
Mtime int64 `json:"mtime"`
Modifier string `json:"modifier"`
Size int64 `json:"size"`
}
type FileElem struct {
Version int `json:"version"`
FileType int `json:"type,omitempty"`
2021-04-23 10:18:38 +00:00
FileId string `json:"file_id,omitempty"`
2021-04-22 20:43:44 +00:00
FileSize uint64 `json:"size"`
2021-04-23 10:18:38 +00:00
BlkIds []string `json:"block_ids"`
2021-04-22 20:43:44 +00:00
}
type DirNode struct {
2021-04-23 10:18:38 +00:00
Config *configCollect
Ent *DirEnt
2021-04-22 20:43:44 +00:00
Elem DirElem
2021-04-23 10:18:38 +00:00
AbsolutePath string
}
type FileNode struct {
Config *configCollect
Ent *DirEnt
Elem FileElem
2021-04-23 14:44:42 +00:00
AbsolutePath string
2021-04-26 09:05:31 +00:00
ReadBytes uint64
CurrentBlock *os.File
RemainingBlocks []string
2021-04-22 20:43:44 +00:00
}
2021-04-24 09:09:15 +00:00
func (fe* FileElem) String() string {
return fmt.Sprintf("Size: %v, Blocks: %v", fe.FileSize, len(fe.BlkIds))
}
2021-04-23 20:22:26 +00:00
type TreeObserver interface {
onDir(dir *DirNode)
onFile(file *FileNode)
2021-04-22 20:43:44 +00:00
}
2021-04-24 09:09:15 +00:00
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
}
2021-04-23 20:22:26 +00:00
func NewEntryNode(config *configCollect) *DirNode {
entryNode := new(DirNode)
entryNode.Ent = new(DirEnt)
2021-04-24 09:09:15 +00:00
entryNode.Ent.Id = expandId(config, config.DirId)
2021-04-23 20:22:26 +00:00
entryNode.Ent.Name = ""
entryNode.AbsolutePath = "/"
entryNode.Config = config
2021-04-23 10:18:38 +00:00
2021-04-23 20:22:26 +00:00
return entryNode
2021-04-22 20:43:44 +00:00
}
2021-04-23 10:18:38 +00:00
func NewDirNode(parent *DirNode, ent *DirEnt) *DirNode {
dn := new(DirNode)
dn.Ent = ent
dn.Config = parent.Config
2021-04-23 20:22:26 +00:00
dn.AbsolutePath = parent.AbsolutePath + ent.Name + "/"
2021-04-23 10:18:38 +00:00
return dn
2021-04-22 20:43:44 +00:00
}
2021-04-23 10:18:38 +00:00
func (dn* DirNode) String() string {
return fmt.Sprintf("%v %v", dn.Ent.Id[:6], dn.AbsolutePath)
}
2021-04-23 14:44:42 +00:00
func (fn* FileNode) String() string {
return fmt.Sprintf("%v %v", fn.Ent.Id[:6], fn.AbsolutePath)
}
2021-04-23 10:18:38 +00:00
func NewFileNode(parent *DirNode, ent *DirEnt) *FileNode {
fn := new (FileNode)
fn.Ent = ent
fn.Config = parent.Config
2021-04-23 14:44:42 +00:00
fn.AbsolutePath = parent.AbsolutePath + ent.Name
2021-04-22 20:43:44 +00:00
2021-04-23 10:18:38 +00:00
return fn
}
2021-04-24 09:09:15 +00:00
func NewEntryFileNode(config *configCollect) *FileNode {
fn := new (FileNode)
fn.Ent = new(DirEnt)
fn.Ent.Id = expandId(config, config.FileId)
fn.Config = config
return fn
}
2021-04-23 10:18:38 +00:00
func (dn* DirNode) Parse() {
path := filepath.Join(dn.Config.Storage, "fs", dn.Config.RepoId, dn.Ent.Id[:2], dn.Ent.Id[2:])
2021-04-22 20:43:44 +00:00
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(&dn.Elem)
if err != nil { log.Fatal(err) }
2021-04-23 20:39:43 +00:00
2021-04-23 10:18:38 +00:00
}
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) }
2021-04-26 09:05:31 +00:00
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
2021-04-23 10:18:38 +00:00
}
2021-04-23 20:22:26 +00:00
func (dn* DirNode) Children() ([]*DirNode, []*FileNode) {
folders := make([]*DirNode,0)
files := make([]*FileNode, 0)
for _, el := range dn.Elem.Entries {
2021-04-24 09:09:15 +00:00
if el.Id == emptyId {
2021-04-23 20:39:43 +00:00
log.Println("[Lost] "+dn.AbsolutePath+el.Name)
} else if IsDir(el.Mode) {
2021-04-23 20:22:26 +00:00
folders = append(folders, NewDirNode(dn, el))
} else if IsRegular(el.Mode) {
files = append(files, NewFileNode(dn, el))
} else {
log.Fatal("Unknown mode", el.Mode, "for", el.Name, "in object id", dn.Ent.Id)
}
}
return folders, files
}
2021-04-23 10:18:38 +00:00
2021-04-23 20:22:26 +00:00
func (dn* DirNode) Walk(tobs TreeObserver) {
2021-04-23 14:44:42 +00:00
qNode := []*DirNode{dn}
2021-04-23 20:22:26 +00:00
for len(qNode) > 0 {
2021-04-23 10:18:38 +00:00
2021-04-23 14:44:42 +00:00
cursor := qNode[0]
qNode = qNode[1:]
cursor.Parse()
2021-04-23 20:22:26 +00:00
dir, file := cursor.Children()
qNode = append(dir, qNode...)
2021-04-23 10:18:38 +00:00
2021-04-23 20:22:26 +00:00
tobs.onDir(cursor)
for _, f := range file {
tobs.onFile(f)
2021-04-23 14:44:42 +00:00
}
}
2021-04-22 20:43:44 +00:00
}
// IsDir check if the mode is dir.
func IsDir(m uint32) bool {
return (m & syscall.S_IFMT) == syscall.S_IFDIR
}
// IsRegular Check if the mode is regular.
func IsRegular(m uint32) bool {
return (m & syscall.S_IFMT) == syscall.S_IFREG
}