vfs/dir.go

172 lines
4.7 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"
"golang.org/x/net/context"
"os"
"path"
)
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-05-14 05:24:52 +00:00
return &versionedDir{
2015-05-18 01:23:53 +00:00
dirs: make(map[string]*versionedDir),
files: make(map[string]*versionedFile),
node: node,
2015-05-24 11:56:29 +00:00
inode: node.ver.inodeAloc.AllocInode(),
2015-05-18 01:23:53 +00:00
parent: parent}
2015-05-14 05:24:52 +00:00
}
func (this *versionedDir) createDir(name string) (*versionedDir, error) {
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-05-24 11:56:29 +00:00
node, err := newVersionedNode(childPath, this.node.ver)
if err != nil {
return nil, err
}
2015-05-24 11:56:29 +00:00
dir := newVersionedDir(node, this)
2015-05-25 05:03:39 +00:00
this.dirs[name] = dir
return dir, nil
}
func (this *versionedDir) createFile(name string, flags int) (*versionedFile, error) {
childPath := path.Join(this.node.path, name)
handle, err := os.OpenFile(this.node.ver.rebasePath(childPath), flags, 0644)
if err != nil {
return nil, err
}
node, err := newVersionedNode(childPath, this.node.ver)
if err != nil {
return nil, err
}
file := newVersionedFile(node, this)
file.handle = handle
2015-05-25 05:03:39 +00:00
this.files[name] = file
return file, nil
}
func (this *versionedDir) Attr(attr *fuse.Attr) {
2015-05-16 14:04:34 +00:00
this.node.attr(attr)
attr.Inode = this.inode
}
2015-05-26 03:14:29 +00:00
func (this *versionedDir) Getattr(ctx context.Context, req *fuse.GetattrRequest, resp *fuse.GetattrResponse) error {
2015-05-26 07:28:12 +00:00
if err := this.node.updateInfo(); err != nil {
2015-05-26 03:14:29 +00:00
return err
}
this.Attr(&resp.Attr)
return nil
}
func (this *versionedDir) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *fuse.SetattrResponse) error {
2015-05-26 07:28:12 +00:00
return this.node.setAttr(req, resp)
2015-05-26 03:14:29 +00:00
}
func (this *versionedDir) Create(ctx context.Context, req *fuse.CreateRequest, resp *fuse.CreateResponse) (fs.Node, fs.Handle, error) {
if req.Mode.IsDir() {
dir, err := this.createDir(req.Name)
if err != nil {
return nil, nil, err
}
return dir, dir, nil
} else {
file, err := this.createFile(req.Name, int(req.Flags))
if err != nil {
return nil, nil, err
}
return file, file, nil
}
}
func (this *versionedDir) Remove(ctx context.Context, req *fuse.RemoveRequest) error {
var fullPath string
if req.Dir {
fullPath = this.dirs[req.Name].node.rebasedPath()
delete(this.dirs, req.Name)
} else {
fullPath = this.files[req.Name].node.rebasedPath()
delete(this.files, req.Name)
}
return os.Remove(fullPath)
}
2015-05-24 09:05:45 +00:00
func (this *versionedDir) Mkdir(ctx context.Context, req *fuse.MkdirRequest) (fs.Node, error) {
return this.createDir(req.Name)
}
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
}