vfs/dir.go

209 lines
5.3 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 (
"bazil.org/fuse"
"bazil.org/fuse/fs"
2015-06-16 08:18:00 +00:00
"errors"
"golang.org/x/net/context"
"os"
"path"
)
2015-05-28 09:20:38 +00:00
//
// versionedDir
//
type versionedDir struct {
2015-05-18 01:23:53 +00:00
dirs map[string]*versionedDir
files map[string]*versionedFile
node *versionedNode
inode uint64
parent *versionedDir
}
2015-05-24 11:56:29 +00:00
func newVersionedDir(node *versionedNode, parent *versionedDir) *versionedDir {
2015-06-16 09:08:30 +00:00
dirs := make(map[string]*versionedDir)
files := make(map[string]*versionedFile)
return &versionedDir{dirs, files, node, allocInode(), parent}
2015-06-16 07:57:39 +00:00
}
func (this *versionedDir) version() error {
2015-06-16 09:08:30 +00:00
if this.node.versioned {
2015-06-16 07:57:39 +00:00
return nil
}
version := this.node.ver.db.lastVersion()
rebasedPath := version.rebasePath(this.node.path)
if err := os.MkdirAll(rebasedPath, 0755); err != nil {
return err
}
node, err := newVersionedNode(this.node.path, version, this.node)
if err != nil {
return err
}
2015-06-16 09:08:30 +00:00
node.versioned = true
2015-06-16 07:57:39 +00:00
this.node = node
return nil
2015-05-14 05:24:52 +00:00
}
func (this *versionedDir) createDir(name string) (*versionedDir, error) {
2015-06-16 08:18:00 +00:00
if err := this.version(); err != nil {
return nil, err
}
2015-06-16 08:18:00 +00:00
childPath := path.Join(this.node.path, name)
2015-05-24 11:56:29 +00:00
if err := os.Mkdir(this.node.ver.rebasePath(childPath), 0755); err != nil {
return nil, err
}
2015-06-16 05:32:03 +00:00
node, err := newVersionedNode(childPath, this.node.ver, nil)
if err != nil {
return nil, err
}
2015-05-24 11:56:29 +00:00
dir := newVersionedDir(node, this)
this.dirs[name] = dir
2015-06-16 09:08:30 +00:00
return dir, nil
}
func (this *versionedDir) createFile(name string, flags int) (*versionedFile, error) {
2015-06-16 08:18:00 +00:00
if err := this.version(); err != nil {
return nil, err
}
2015-06-16 08:18:00 +00:00
childPath := path.Join(this.node.path, name)
handle, err := os.OpenFile(this.node.ver.rebasePath(childPath), flags, 0644)
if err != nil {
return nil, err
}
2015-06-16 05:32:03 +00:00
node, err := newVersionedNode(childPath, this.node.ver, nil)
if err != nil {
return nil, err
}
file := newVersionedFile(node, this)
file.handle = handle
this.files[name] = file
2015-06-16 09:08:30 +00:00
return file, nil
}
2015-06-16 05:32:03 +00:00
func (this *versionedDir) Attr(ctx context.Context, attr *fuse.Attr) error {
2015-05-16 14:04:34 +00:00
this.node.attr(attr)
attr.Inode = this.inode
2015-06-16 05:32:03 +00:00
return nil
}
2015-05-26 03:14:29 +00:00
func (this *versionedDir) Getattr(ctx context.Context, req *fuse.GetattrRequest, resp *fuse.GetattrResponse) error {
2015-06-16 08:05:29 +00:00
if err := this.node.sync(); err != nil {
2015-05-26 03:14:29 +00:00
return err
}
2015-06-16 05:32:03 +00:00
this.Attr(ctx, &resp.Attr)
2015-05-26 03:14:29 +00:00
return nil
}
func (this *versionedDir) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *fuse.SetattrResponse) error {
2015-06-16 08:18:00 +00:00
this.version()
2015-05-26 07:28:12 +00:00
return this.node.setAttr(req, resp)
2015-05-26 03:14:29 +00:00
}
2015-06-16 08:18:00 +00:00
func (this *versionedDir) Create(ctx context.Context, req *fuse.CreateRequest, resp *fuse.CreateResponse) (node fs.Node, handle fs.Handle, err error) {
if req.Mode.IsDir() {
2015-06-16 08:18:00 +00:00
var dir *versionedDir
if dir, err = this.createDir(req.Name); err == nil {
node = dir
handle = dir
}
2015-06-16 08:18:00 +00:00
} else if req.Mode.IsRegular() {
var file *versionedFile
if file, err = this.createFile(req.Name, int(req.Flags)); err == nil {
node = file
handle = file
}
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-16 08:05:29 +00:00
func (this *versionedDir) Mkdir(ctx context.Context, req *fuse.MkdirRequest) (fs.Node, error) {
return this.createDir(req.Name)
}
2015-06-16 09:08:30 +00:00
func (this *versionedDir) Remove(ctx context.Context, req *fuse.RemoveRequest) (err error) {
if req.Dir {
2015-06-16 09:08:30 +00:00
if err = this.dirs[req.Name].node.remove(); err == nil {
delete(this.dirs, req.Name)
}
} else {
2015-06-16 09:08:30 +00:00
if err = this.files[req.Name].node.remove(); err == nil {
delete(this.files, req.Name)
}
}
2015-06-16 09:08:30 +00:00
return
}
func (this *versionedDir) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
2015-05-18 01:23:53 +00:00
entries := []fuse.Dirent{{Inode: this.inode, Name: ".", Type: fuse.DT_Dir}}
if this.parent != nil {
entry := fuse.Dirent{Inode: this.parent.inode, Name: "..", Type: fuse.DT_Dir}
entries = append(entries, entry)
}
for name, dir := range this.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
for name, file := range this.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
}
func (this *versionedDir) Lookup(ctx context.Context, name string) (fs.Node, error) {
if dir, ok := this.dirs[name]; ok {
return dir, nil
}
if file, ok := this.files[name]; ok {
return file, nil
}
return nil, fuse.ENOENT
}