basic functions working

This commit is contained in:
ston1th 2019-05-02 14:17:24 +02:00
commit ffeeae5702
22 changed files with 420 additions and 129 deletions

View file

@ -3,13 +3,22 @@ package fs
import (
"errors"
"git.giftfish.de/ston1th/docstore/pkg/core"
"io"
"os"
"path/filepath"
"strings"
)
type Mode int
const (
Undefined Mode = iota
IsDir
IsFile
)
func (fs *Filesystem) GetFile(path string) (f *os.File, fi os.FileInfo, err error) {
path = strings.TrimPrefix(filepath.Clean(path), core.RawPrefix)
path = strings.TrimPrefix(Clean(path), core.RawPrefix)
p := filepath.Join(fs.Base, path)
f, err = os.Open(p)
if err != nil {
@ -25,8 +34,8 @@ func (fs *Filesystem) GetFile(path string) (f *os.File, fi os.FileInfo, err erro
return
}
func (fs *Filesystem) Mode(path string) (mode FileMode, err error) {
path = filepath.Clean(path)
func (fs *Filesystem) Mode(path string) (mode Mode, err error) {
path = Clean(path)
p := filepath.Join(fs.Base, path)
fi, err := os.Stat(p)
if err != nil {
@ -41,17 +50,46 @@ func (fs *Filesystem) Mode(path string) (mode FileMode, err error) {
return
}
func (fs *Filesystem) NewFile(r io.Reader, path, orig, name string) (newfile string, err error) {
path = Clean(path)
orig = Clean(orig)
_, orig = filepath.Split(orig)
name = Clean(name)
_, name = filepath.Split(name)
if name == "" {
name = orig
}
newfile = filepath.Join(path, name)
f, err := os.OpenFile(filepath.Join(fs.Base, newfile), os.O_RDWR|os.O_CREATE, FileMode)
if err != nil {
return
}
defer f.Close()
_, err = io.Copy(f, r)
return
}
func (fs *Filesystem) Mkdir(path, name string) error {
path = Clean(path)
name = Clean(name)
return os.Mkdir(filepath.Join(fs.Base, path, name), DirMode)
}
func (fs *Filesystem) Move(path, dest, name string) (string, error) {
path = filepath.Clean(path)
path = 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))
fullpath := filepath.Join(fs.Base, path)
if fullpath == fs.Base {
return "", errors.New("the root path can not be moved")
}
newfile := Clean(filepath.Join(dest, name))
return newfile, os.Rename(fullpath, filepath.Join(fs.Base, newfile))
}
func (fs *Filesystem) Delete(path string) (removed []string, err error) {
path = filepath.Clean(path)
path = Clean(path)
fullpath := filepath.Join(fs.Base, path)
if fullpath == fs.Base {
return nil, errors.New("the root path can not be deleted")

View file

@ -9,12 +9,9 @@ import (
"strings"
)
type FileMode int
const (
Undefined FileMode = iota
IsDir
IsFile
DirMode = 0750
FileMode = 0640
)
// Directory represents a directory and its contents
@ -40,7 +37,7 @@ func NewFilesystem(base string) (fs *Filesystem, err error) {
}
func (fs *Filesystem) ReadDir(path string) (d *Directory) {
path = filepath.Clean(path)
path = Clean(path)
combined := filepath.Join(fs.Base, path)
f, err := os.Open(combined)
if err != nil {
@ -70,7 +67,12 @@ func (fs *Filesystem) ReadDir(path string) (d *Directory) {
return
}
func (fs *Filesystem) Recursive() (p core.Paths) {
func (fs *Filesystem) Recursive(p string) (paths core.Paths) {
p = Clean(p)
p, _ = filepath.Split(p)
if len(p) >= 2 {
p = p[:len(p)-1]
}
filepath.Walk(fs.Base, func(path string, info os.FileInfo, err error) error {
if err != nil {
return nil
@ -80,16 +82,16 @@ func (fs *Filesystem) Recursive() (p core.Paths) {
if a == "" {
a = "/"
}
p = append(p, core.Path{Abs: a})
paths = append(paths, core.Path{Abs: a, Flag: a == p})
}
return nil
})
sort.Sort(p)
sort.Sort(paths)
return
}
func (fs *Filesystem) RecursiveFiles(p string) (files []string) {
p = filepath.Clean(p)
p = Clean(p)
fullpath := filepath.Join(fs.Base, p)
filepath.Walk(fullpath, func(path string, info os.FileInfo, err error) error {
if err != nil {
@ -104,7 +106,7 @@ func (fs *Filesystem) RecursiveFiles(p string) (files []string) {
}
func Paths(path string) (p core.Paths) {
path = filepath.Clean(path)
path = Clean(path)
s := strings.Split(path, string(filepath.Separator))
for i, v := range s[1:] {
if v == "" {
@ -117,7 +119,7 @@ func Paths(path string) (p core.Paths) {
p = append(p, core.Path{Name: v, Abs: filepath.Join(p[i-1].Abs, v)})
}
if len(p) != 0 {
p[len(p)-1].Indexed = true
p[len(p)-1].Flag = true
}
return
}

9
pkg/fs/helper.go Normal file
View file

@ -0,0 +1,9 @@
package fs
import "path/filepath"
const sep = "/"
func Clean(path string) string {
return filepath.FromSlash(filepath.Clean(sep + path))
}