first working version
This commit is contained in:
parent
18995db757
commit
de359ab415
47 changed files with 1016 additions and 2012 deletions
72
pkg/fs/file.go
Normal file
72
pkg/fs/file.go
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
package fs
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"git.giftfish.de/ston1th/docstore/pkg/core"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func (fs *Filesystem) GetFile(path string) (f *os.File, fi os.FileInfo, err error) {
|
||||
path = strings.TrimPrefix(filepath.Clean(path), core.RawPrefix)
|
||||
p := filepath.Join(fs.Base, path)
|
||||
f, err = os.Open(p)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
fi, err = f.Stat()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if !fi.Mode().IsRegular() {
|
||||
err = os.ErrNotExist
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (fs *Filesystem) Mode(path string) (mode FileMode, err error) {
|
||||
path = filepath.Clean(path)
|
||||
p := filepath.Join(fs.Base, path)
|
||||
fi, err := os.Stat(p)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
m := fi.Mode()
|
||||
if m.IsDir() {
|
||||
mode = IsDir
|
||||
} else if m.IsRegular() {
|
||||
mode = IsFile
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (fs *Filesystem) Move(path, dest, name string) (string, error) {
|
||||
path = filepath.Clean(path)
|
||||
if name == "" {
|
||||
_, name = filepath.Split(path)
|
||||
}
|
||||
newfile := filepath.Clean(filepath.Join(dest, name))
|
||||
return newfile, os.Rename(filepath.Join(fs.Base, path), filepath.Join(fs.Base, newfile))
|
||||
}
|
||||
|
||||
func (fs *Filesystem) Delete(path string) (removed []string, err error) {
|
||||
path = filepath.Clean(path)
|
||||
fullpath := filepath.Join(fs.Base, path)
|
||||
if fullpath == fs.Base {
|
||||
return nil, errors.New("the root path can not be deleted")
|
||||
}
|
||||
fi, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
m := fi.Mode()
|
||||
if m.IsDir() {
|
||||
err = os.RemoveAll(fullpath)
|
||||
removed = fs.RecursiveFiles(path)
|
||||
} else if m.IsRegular() {
|
||||
err = os.Remove(fullpath)
|
||||
removed = []string{path}
|
||||
}
|
||||
return
|
||||
}
|
||||
120
pkg/fs/fs.go
Normal file
120
pkg/fs/fs.go
Normal file
|
|
@ -0,0 +1,120 @@
|
|||
package fs
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"git.giftfish.de/ston1th/docstore/pkg/core"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type FileMode int
|
||||
|
||||
const (
|
||||
Undefined FileMode = iota
|
||||
IsDir
|
||||
IsFile
|
||||
)
|
||||
|
||||
// Directory represents a directory and its contents
|
||||
type Directory struct {
|
||||
Dirs core.Paths
|
||||
Files core.Paths
|
||||
}
|
||||
|
||||
type Filesystem struct {
|
||||
Base string
|
||||
}
|
||||
|
||||
func NewFilesystem(base string) (fs *Filesystem, err error) {
|
||||
fi, err := os.Stat(base)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if !fi.IsDir() {
|
||||
err = errors.New("base dir '" + base + "' is not a directory")
|
||||
}
|
||||
fs = &Filesystem{base}
|
||||
return
|
||||
}
|
||||
|
||||
func (fs *Filesystem) ReadDir(path string) (d *Directory) {
|
||||
path = filepath.Clean(path)
|
||||
combined := filepath.Join(fs.Base, path)
|
||||
f, err := os.Open(combined)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
names, err := f.Readdirnames(0)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
d = new(Directory)
|
||||
for _, n := range names {
|
||||
fi, err := os.Stat(filepath.Join(combined, n))
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
m := fi.Mode()
|
||||
if m.IsDir() {
|
||||
d.Dirs = append(d.Dirs, core.Path{Name: n, Abs: filepath.Join(path, n)})
|
||||
continue
|
||||
}
|
||||
if m.IsRegular() {
|
||||
d.Files = append(d.Files, core.Path{Name: n, Abs: filepath.Join(path, n)})
|
||||
}
|
||||
}
|
||||
sort.Sort(d.Dirs)
|
||||
sort.Sort(d.Files)
|
||||
return
|
||||
}
|
||||
|
||||
func (fs *Filesystem) Recursive() (p core.Paths) {
|
||||
filepath.Walk(fs.Base, func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
if info.IsDir() {
|
||||
a := strings.TrimPrefix(path, fs.Base)
|
||||
if a == "" {
|
||||
a = "/"
|
||||
}
|
||||
p = append(p, core.Path{Abs: a})
|
||||
}
|
||||
return nil
|
||||
})
|
||||
sort.Sort(p)
|
||||
return
|
||||
}
|
||||
|
||||
func (fs *Filesystem) RecursiveFiles(p string) (files []string) {
|
||||
p = filepath.Clean(p)
|
||||
fullpath := filepath.Join(fs.Base, p)
|
||||
filepath.Walk(fullpath, func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
if info.Mode().IsRegular() {
|
||||
files = append(files, strings.TrimPrefix(path, fullpath))
|
||||
}
|
||||
return nil
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func Paths(path string) (p core.Paths) {
|
||||
path = filepath.Clean(path)
|
||||
s := strings.Split(path, string(filepath.Separator))
|
||||
for i, v := range s[1:] {
|
||||
if v == "" {
|
||||
continue
|
||||
}
|
||||
if i == 0 {
|
||||
p = append(p, core.Path{Name: v, Abs: "/" + v})
|
||||
continue
|
||||
}
|
||||
p = append(p, core.Path{Name: v, Abs: filepath.Join(p[i-1].Abs, v)})
|
||||
}
|
||||
return
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue