vfs/dir.go

248 lines
5.9 KiB
Go
Raw Normal View History

/*
* Copyright (c) 2015 Alex Yatskov <alex@foosoft.net>
* Author: Alex Yatskov <alex@foosoft.net>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package main
import (
2015-06-16 08:18:00 +00:00
"errors"
"os"
"path"
"sync"
2015-06-17 11:11:16 +00:00
"bazil.org/fuse"
"bazil.org/fuse/fs"
"golang.org/x/net/context"
)
2015-05-28 09:20:38 +00:00
//
2015-06-19 08:51:10 +00:00
// verDir
2015-05-28 09:20:38 +00:00
//
2015-06-19 08:51:10 +00:00
type verDir struct {
dirs map[string]*verDir
files map[string]*verFile
node *verNode
2015-05-18 01:23:53 +00:00
inode uint64
2015-06-19 08:51:10 +00:00
parent *verDir
mutex sync.Mutex
}
2015-06-19 08:51:10 +00:00
func newVerDir(node *verNode, parent *verDir) *verDir {
dirs := make(map[string]*verDir)
files := make(map[string]*verFile)
2015-06-16 09:08:30 +00:00
return &verDir{dirs, files, node, allocInode(), parent, sync.Mutex{}}
2015-06-16 07:57:39 +00:00
}
2015-06-19 08:51:10 +00:00
func (vd *verDir) version() error {
vd.mutex.Lock()
defer vd.mutex.Unlock()
2015-06-21 02:19:37 +00:00
if vd.node.flags&NodeFlagNew == NodeFlagNew {
2015-06-16 07:57:39 +00:00
return nil
}
2015-06-24 03:42:36 +00:00
node := newVerNode(vd.node.path, vd.node.ver.db.lastVer(), vd.node, NodeFlagDir|NodeFlagNew)
2015-06-17 07:41:43 +00:00
if err := os.MkdirAll(node.rebasedPath(), 0755); err != nil {
2015-06-16 07:57:39 +00:00
return err
}
2015-06-17 11:18:55 +00:00
vd.node = node
2015-06-22 10:22:08 +00:00
node.ver.meta.modifyNode(node.path)
2015-06-17 11:11:16 +00:00
2015-06-16 07:57:39 +00:00
return nil
2015-05-14 05:24:52 +00:00
}
2015-06-19 08:51:10 +00:00
func (vd *verDir) createDir(name string) (*verDir, error) {
2015-06-17 11:18:55 +00:00
if err := vd.version(); err != nil {
2015-06-16 08:18:00 +00:00
return nil, err
}
2015-06-17 11:18:55 +00:00
childPath := path.Join(vd.node.path, name)
if err := os.Mkdir(vd.node.ver.rebasePath(childPath), 0755); err != nil {
return nil, err
}
2015-06-21 02:19:37 +00:00
node := newVerNode(childPath, vd.node.ver, nil, NodeFlagDir|NodeFlagNew)
2015-06-19 08:51:10 +00:00
dir := newVerDir(node, vd)
2015-06-22 10:22:08 +00:00
vd.mutex.Lock()
2015-06-17 11:18:55 +00:00
vd.dirs[name] = dir
vd.mutex.Unlock()
2015-06-22 10:22:08 +00:00
node.ver.meta.createNode(node.path)
return dir, nil
}
2015-06-21 02:19:37 +00:00
func (vd *verDir) createFile(name string, flags fuse.OpenFlags, mode os.FileMode) (*verFile, *verFileHandle, fuse.HandleID, error) {
2015-06-17 11:18:55 +00:00
if err := vd.version(); err != nil {
2015-06-21 02:19:37 +00:00
return nil, nil, 0, err
2015-06-16 08:18:00 +00:00
}
2015-06-17 11:18:55 +00:00
childPath := path.Join(vd.node.path, name)
2015-06-21 02:19:37 +00:00
node := newVerNode(childPath, vd.node.ver, nil, NodeFlagNew)
file := newVerFile(node, vd)
2015-06-21 02:19:37 +00:00
handle, id, err := file.open(flags, mode)
if err != nil {
2015-06-21 02:19:37 +00:00
return nil, nil, 0, err
}
vd.mutex.Lock()
2015-06-17 11:18:55 +00:00
vd.files[name] = file
vd.mutex.Unlock()
2015-06-22 10:22:08 +00:00
node.ver.meta.createNode(node.path)
2015-06-21 02:19:37 +00:00
return file, handle, id, nil
}
2015-06-21 02:37:02 +00:00
func (vd *verDir) removeDir(name string) error {
2015-06-21 13:02:46 +00:00
if err := vd.version(); err != nil {
return err
}
2015-06-21 02:37:02 +00:00
node := vd.dirs[name].node
if node.flags&NodeFlagNew == NodeFlagNew {
if err := os.Remove(node.rebasedPath()); err != nil {
return err
}
2015-06-22 04:10:07 +00:00
} else {
2015-06-22 04:09:22 +00:00
vd.node.ver.meta.removeNode(node.path)
2015-06-22 04:00:32 +00:00
}
2015-06-21 02:37:02 +00:00
2015-07-06 08:56:12 +00:00
vd.mutex.Lock()
2015-06-22 04:00:32 +00:00
delete(vd.dirs, name)
2015-07-06 08:56:12 +00:00
vd.mutex.Unlock()
2015-06-21 02:37:02 +00:00
return nil
}
func (vd *verDir) removeFile(name string) error {
2015-06-21 13:02:46 +00:00
if err := vd.version(); err != nil {
return err
}
2015-06-21 02:37:02 +00:00
node := vd.files[name].node
if node.flags&NodeFlagNew == NodeFlagNew {
if err := os.Remove(node.rebasedPath()); err != nil {
return err
}
2015-06-22 04:10:07 +00:00
} else {
2015-06-22 04:09:22 +00:00
vd.node.ver.meta.removeNode(node.path)
2015-06-22 04:00:32 +00:00
}
2015-06-21 02:37:02 +00:00
2015-07-06 08:56:12 +00:00
vd.mutex.Lock()
2015-06-22 04:00:32 +00:00
delete(vd.files, name)
2015-07-06 08:56:12 +00:00
vd.mutex.Unlock()
2015-06-21 02:37:02 +00:00
return nil
}
2015-06-18 06:17:07 +00:00
// Node
2015-06-19 08:51:10 +00:00
func (vd *verDir) Attr(ctx context.Context, attr *fuse.Attr) error {
2015-06-17 11:18:55 +00:00
if err := vd.node.attr(attr); err != nil {
2015-06-17 07:41:43 +00:00
return err
}
2015-06-17 11:18:55 +00:00
attr.Inode = vd.inode
2015-06-16 05:32:03 +00:00
return nil
}
2015-06-18 06:17:07 +00:00
// NodeGetattrer
2015-06-19 08:51:10 +00:00
func (vd *verDir) Getattr(ctx context.Context, req *fuse.GetattrRequest, resp *fuse.GetattrResponse) error {
2015-06-17 11:18:55 +00:00
return vd.Attr(ctx, &resp.Attr)
2015-05-26 03:14:29 +00:00
}
2015-06-18 06:17:07 +00:00
// NodeSetattrer
2015-06-19 08:51:10 +00:00
func (vd *verDir) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *fuse.SetattrResponse) error {
2015-06-17 11:18:55 +00:00
vd.version()
return vd.node.setAttr(req, resp)
2015-05-26 03:14:29 +00:00
}
2015-06-18 06:17:07 +00:00
// NodeCreater
2015-06-19 08:51:10 +00:00
func (vd *verDir) Create(ctx context.Context, req *fuse.CreateRequest, resp *fuse.CreateResponse) (node fs.Node, handle fs.Handle, err error) {
if req.Mode.IsDir() {
2015-06-20 11:50:11 +00:00
node, err = vd.createDir(req.Name)
handle = node
2015-06-16 08:18:00 +00:00
} else if req.Mode.IsRegular() {
2015-06-21 02:19:37 +00:00
node, handle, resp.Handle, err = vd.createFile(req.Name, req.Flags, req.Mode)
2015-06-16 08:18:00 +00:00
} else {
err = errors.New("unsupported filetype")
}
2015-06-16 08:18:00 +00:00
return
}
2015-06-18 06:17:07 +00:00
// NodeMkdirer
2015-06-19 08:51:10 +00:00
func (vd *verDir) Mkdir(ctx context.Context, req *fuse.MkdirRequest) (fs.Node, error) {
2015-06-17 11:18:55 +00:00
return vd.createDir(req.Name)
2015-06-16 08:05:29 +00:00
}
2015-06-18 06:17:07 +00:00
// NodeRemover
2015-06-19 08:51:10 +00:00
func (vd *verDir) Remove(ctx context.Context, req *fuse.RemoveRequest) error {
2015-06-21 02:37:02 +00:00
if req.Dir {
return vd.removeDir(req.Name)
} else {
2015-06-21 02:37:02 +00:00
return vd.removeFile(req.Name)
}
}
2015-06-18 06:17:07 +00:00
// NodeRequestLookuper
2015-06-19 08:51:10 +00:00
func (vd *verDir) Lookup(ctx context.Context, name string) (fs.Node, error) {
vd.mutex.Lock()
defer vd.mutex.Unlock()
2015-06-18 06:17:07 +00:00
if dir, ok := vd.dirs[name]; ok {
return dir, nil
}
if file, ok := vd.files[name]; ok {
return file, nil
}
return nil, fuse.ENOENT
}
// HandleReadDirAller
2015-06-19 08:51:10 +00:00
func (vd *verDir) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
vd.mutex.Lock()
defer vd.mutex.Unlock()
2015-06-17 11:18:55 +00:00
entries := []fuse.Dirent{{Inode: vd.inode, Name: ".", Type: fuse.DT_Dir}}
if vd.parent != nil {
entry := fuse.Dirent{Inode: vd.parent.inode, Name: "..", Type: fuse.DT_Dir}
2015-05-18 01:23:53 +00:00
entries = append(entries, entry)
}
2015-06-17 11:18:55 +00:00
for name, dir := range vd.dirs {
entry := fuse.Dirent{Inode: dir.inode, Name: name, Type: fuse.DT_Dir}
entries = append(entries, entry)
}
2015-05-18 01:23:53 +00:00
2015-06-17 11:18:55 +00:00
for name, file := range vd.files {
entry := fuse.Dirent{Inode: file.inode, Name: name, Type: fuse.DT_File}
entries = append(entries, entry)
}
2015-05-16 11:33:35 +00:00
return entries, nil
}