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