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

@ -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
}