some bug fixes
This commit is contained in:
parent
0e49d60994
commit
0e914c629e
16 changed files with 179 additions and 52 deletions
|
|
@ -7,6 +7,7 @@ import (
|
|||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Mode int
|
||||
|
|
@ -78,20 +79,40 @@ func (fs *Filesystem) Mkdir(path, name string) error {
|
|||
return os.Mkdir(filepath.Join(fs.Base, dirpath), DirMode)
|
||||
}
|
||||
|
||||
func (fs *Filesystem) Move(path, dest, name string) (string, error) {
|
||||
type Moved struct {
|
||||
Old string
|
||||
New string
|
||||
}
|
||||
|
||||
func (fs *Filesystem) Move(path, dest, name string) (moved []Moved, newfile string, err error) {
|
||||
if name == "" {
|
||||
name = File(path)
|
||||
}
|
||||
fullpath := filepath.Join(fs.Base, Clean(path))
|
||||
path = Clean(path)
|
||||
fullpath := filepath.Join(fs.Base, path)
|
||||
if fullpath == fs.Base {
|
||||
return "", errors.New("the root path can not be moved")
|
||||
err = errors.New("the root path can not be moved")
|
||||
return
|
||||
}
|
||||
newfile := filepath.Join(Clean(dest), Clean(name))
|
||||
err := checkPath(newfile)
|
||||
newfile = filepath.Join(Clean(dest), Clean(name))
|
||||
err = checkPath(newfile)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return
|
||||
}
|
||||
return newfile, os.Rename(fullpath, filepath.Join(fs.Base, newfile))
|
||||
fi, err := os.Stat(fullpath)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
m := fi.Mode()
|
||||
if m.IsDir() {
|
||||
for _, f := range fs.RecursiveFiles(path) {
|
||||
moved = append(moved, Moved{f, strings.Replace(f, path, newfile, 1)})
|
||||
}
|
||||
} else if m.IsRegular() {
|
||||
moved = append(moved, Moved{path, newfile})
|
||||
}
|
||||
err = os.Rename(fullpath, filepath.Join(fs.Base, newfile))
|
||||
return
|
||||
}
|
||||
|
||||
func (fs *Filesystem) Delete(path string) (removed []string, err error) {
|
||||
|
|
|
|||
10
pkg/fs/fs.go
10
pkg/fs/fs.go
|
|
@ -10,6 +10,7 @@ import (
|
|||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
const (
|
||||
|
|
@ -25,6 +26,10 @@ type Directory struct {
|
|||
|
||||
type Filesystem struct {
|
||||
Base string
|
||||
|
||||
// protects scan
|
||||
m sync.RWMutex
|
||||
scan map[string]struct{}
|
||||
}
|
||||
|
||||
func NewFilesystem(base string) (fs *Filesystem, err error) {
|
||||
|
|
@ -48,7 +53,7 @@ func NewFilesystem(base string) (fs *Filesystem, err error) {
|
|||
err = errors.New("base dir '" + base + "' is not a directory")
|
||||
return
|
||||
}
|
||||
fs = &Filesystem{base}
|
||||
fs = &Filesystem{Base: base, scan: make(map[string]struct{})}
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -75,7 +80,8 @@ func (fs *Filesystem) ReadDir(path string) (d *Directory) {
|
|||
continue
|
||||
}
|
||||
if m.IsRegular() {
|
||||
d.Files = append(d.Files, core.Path{Name: n, Abs: filepath.Join(path, n)})
|
||||
abs := filepath.Join(path, n)
|
||||
d.Files = append(d.Files, core.Path{Name: n, Abs: abs, Scan: fs.CheckScan(abs)})
|
||||
}
|
||||
}
|
||||
sort.Sort(d.Dirs)
|
||||
|
|
|
|||
26
pkg/fs/scan.go
Normal file
26
pkg/fs/scan.go
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
package fs
|
||||
|
||||
import "errors"
|
||||
|
||||
func (fs *Filesystem) AddScan(path string) error {
|
||||
if fs.CheckScan(path) {
|
||||
return errors.New("file '" + path + "' is already scanning")
|
||||
}
|
||||
fs.m.Lock()
|
||||
defer fs.m.Unlock()
|
||||
fs.scan[path] = struct{}{}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (fs *Filesystem) CheckScan(path string) (ok bool) {
|
||||
fs.m.RLock()
|
||||
defer fs.m.RUnlock()
|
||||
_, ok = fs.scan[path]
|
||||
return
|
||||
}
|
||||
|
||||
func (fs *Filesystem) RemoveScan(path string) {
|
||||
fs.m.Lock()
|
||||
defer fs.m.Unlock()
|
||||
delete(fs.scan, path)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue