vfs/file.go

190 lines
4.5 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-05-18 02:58:49 +00:00
"os"
"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
// verFile
2015-05-28 09:20:38 +00:00
//
2015-06-21 02:19:37 +00:00
type handleMap map[fuse.HandleID]*verFileHandle
2015-06-19 08:51:10 +00:00
type verFile struct {
node *verNode
inode uint64
parent *verDir
2015-06-21 02:19:37 +00:00
handles handleMap
mutex sync.Mutex
}
2015-06-19 08:51:10 +00:00
func newVerFile(node *verNode, parent *verDir) *verFile {
return &verFile{node, allocInode(), parent, make(handleMap), sync.Mutex{}}
2015-05-14 05:24:52 +00:00
}
2015-06-19 08:51:10 +00:00
func (vf *verFile) version() error {
vf.mutex.Lock()
defer vf.mutex.Unlock()
2015-06-21 02:19:37 +00:00
if vf.node.flags&NodeFlagNew == NodeFlagNew {
return nil
}
2015-06-24 03:42:36 +00:00
node := newVerNode(vf.node.path, vf.node.ver.db.lastVer(), vf.node, NodeFlagNew)
2015-06-19 08:51:10 +00:00
if _, err := copyFile(vf.node.rebasedPath(), node.rebasedPath()); err != nil {
return err
}
2015-06-17 11:18:55 +00:00
vf.node = node
2015-06-22 10:22:08 +00:00
node.ver.meta.modifyNode(node.path)
2015-06-17 11:11:16 +00:00
return nil
}
2015-06-21 02:19:37 +00:00
func (vf *verFile) open(flags fuse.OpenFlags, mode os.FileMode) (*verFileHandle, fuse.HandleID, error) {
2015-06-20 13:21:02 +00:00
if !flags.IsReadOnly() {
if err := vf.version(); err != nil {
2015-06-21 02:19:37 +00:00
return nil, 0, err
}
}
path := vf.node.rebasedPath()
2015-06-20 11:50:11 +00:00
handle, err := os.OpenFile(path, int(flags), mode)
if err != nil {
2015-06-21 02:19:37 +00:00
return nil, 0, err
}
2015-06-20 13:21:02 +00:00
id := allocHandleId()
2015-06-21 02:19:37 +00:00
verHandle := &verFileHandle{vf, path, handle}
2015-06-22 10:22:08 +00:00
vf.mutex.Lock()
vf.handles[id] = verHandle
vf.mutex.Unlock()
2015-06-21 02:19:37 +00:00
return verHandle, id, nil
}
2015-06-21 02:19:37 +00:00
func (vf *verFile) release(handle fuse.HandleID) {
vf.mutex.Lock()
delete(vf.handles, handle)
vf.mutex.Unlock()
}
// Node
2015-06-19 08:51:10 +00:00
func (vf *verFile) Attr(ctx context.Context, attr *fuse.Attr) error {
vf.node.attr(attr)
attr.Inode = vf.inode
return nil
}
// NodeGetattrer
2015-06-19 08:51:10 +00:00
func (vf *verFile) Getattr(ctx context.Context, req *fuse.GetattrRequest, resp *fuse.GetattrResponse) error {
return vf.Attr(ctx, &resp.Attr)
}
// NodeSetattrer
2015-06-19 08:51:10 +00:00
func (vf *verFile) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *fuse.SetattrResponse) error {
return vf.node.setAttr(req, resp)
}
// NodeOpener
2015-06-19 08:51:10 +00:00
func (vf *verFile) Open(ctx context.Context, req *fuse.OpenRequest, resp *fuse.OpenResponse) (fs.Handle, error) {
2015-06-21 02:19:37 +00:00
handle, id, err := vf.open(req.Flags, 0644)
if err != nil {
return nil, err
2015-05-25 04:27:54 +00:00
}
2015-06-21 02:19:37 +00:00
resp.Handle = id
return handle, nil
2015-05-25 04:27:54 +00:00
}
// NodeFsyncer
func (vf *verFile) Fsync(ctx context.Context, req *fuse.FsyncRequest) error {
vf.mutex.Lock()
defer vf.mutex.Unlock()
2015-06-21 02:19:37 +00:00
return vf.handles[req.Handle].handle.Sync()
}
//
// verFileHandle
//
2015-05-18 02:58:49 +00:00
type verFileHandle struct {
node *verFile
path string
handle *os.File
2015-05-18 02:58:49 +00:00
}
// HandleReader
func (vfh *verFileHandle) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) error {
2015-05-24 09:45:06 +00:00
resp.Data = make([]byte, req.Size)
if _, err := vfh.handle.ReadAt(resp.Data, req.Offset); err != nil {
2015-05-24 06:16:12 +00:00
return err
}
return nil
}
2015-05-18 02:58:49 +00:00
// HandleReadAller
func (vfh *verFileHandle) ReadAll(ctx context.Context) ([]byte, error) {
info, err := os.Stat(vfh.path)
2015-06-17 07:41:43 +00:00
if err != nil {
return nil, err
}
data := make([]byte, info.Size())
if _, err := vfh.handle.Read(data); err != nil {
return nil, err
}
2015-05-25 04:27:54 +00:00
return data, nil
}
2015-05-26 03:14:29 +00:00
// HandleWriter
func (vfh *verFileHandle) Write(ctx context.Context, req *fuse.WriteRequest, resp *fuse.WriteResponse) error {
size, err := vfh.handle.WriteAt(req.Data, req.Offset)
2015-05-26 03:14:29 +00:00
if err != nil {
return err
}
resp.Size = size
return nil
}
// HandleReleaser
func (vfh *verFileHandle) Release(ctx context.Context, req *fuse.ReleaseRequest) error {
vfh.handle.Close()
vfh.handle = nil
2015-06-21 02:19:37 +00:00
vfh.node.release(req.Handle)
return nil
}