vfs/file.go

138 lines
3.8 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-05-25 04:27:54 +00:00
"errors"
"golang.org/x/net/context"
2015-05-18 02:58:49 +00:00
"os"
)
2015-05-28 09:20:38 +00:00
//
// versionedFile
//
type versionedFile struct {
2015-05-18 01:23:53 +00:00
node *versionedNode
inode uint64
parent *versionedDir
2015-05-25 04:27:54 +00:00
handle *os.File
}
2015-05-24 11:56:29 +00:00
func newVersionedFile(node *versionedNode, parent *versionedDir) *versionedFile {
2015-06-16 09:08:30 +00:00
return &versionedFile{node, allocInode(), parent, nil}
2015-05-14 05:24:52 +00:00
}
func (this *versionedFile) Open(ctx context.Context, req *fuse.OpenRequest, resp *fuse.OpenResponse) (fs.Handle, error) {
if this.handle != nil {
return nil, errors.New("attempted to open already opened file")
2015-05-25 04:27:54 +00:00
}
handle, err := os.OpenFile(this.node.rebasedPath(), int(req.Flags), 0644)
if err != nil {
return nil, err
2015-05-25 04:27:54 +00:00
}
this.handle = handle
return this, nil
2015-05-25 04:27:54 +00:00
}
2015-06-16 05:32:03 +00:00
func (this *versionedFile) 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 *versionedFile) 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-18 02:58:49 +00:00
return err
}
2015-06-16 05:32:03 +00:00
this.Attr(ctx, &resp.Attr)
2015-05-18 02:58:49 +00:00
return nil
}
2015-05-26 03:14:29 +00:00
func (this *versionedFile) 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-18 02:58:49 +00:00
}
2015-05-24 09:45:06 +00:00
func (this *versionedFile) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) error {
2015-05-25 04:27:54 +00:00
if this.handle == nil {
2015-05-26 03:14:29 +00:00
return errors.New("attempted to read from unopened file")
2015-05-24 06:16:12 +00:00
}
2015-05-24 09:45:06 +00:00
resp.Data = make([]byte, req.Size)
2015-05-25 04:27:54 +00:00
if _, err := this.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
2015-05-24 09:45:06 +00:00
func (this *versionedFile) ReadAll(ctx context.Context) ([]byte, error) {
2015-05-25 04:27:54 +00:00
if this.handle == nil {
2015-05-26 03:14:29 +00:00
return nil, errors.New("attempted to read from unopened file")
2015-05-25 04:27:54 +00:00
}
data := make([]byte, this.node.info.Size())
if _, err := this.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
func (this *versionedFile) Write(ctx context.Context, req *fuse.WriteRequest, resp *fuse.WriteResponse) error {
if this.handle == nil {
return errors.New("attempted to write to unopened file")
}
size, err := this.handle.WriteAt(req.Data, req.Offset)
if err != nil {
return err
}
resp.Size = size
return nil
}
func (this *versionedFile) Release(ctx context.Context, req *fuse.ReleaseRequest) error {
if this.handle == nil {
return errors.New("attempted to release unopened file")
}
this.handle.Close()
this.handle = nil
return nil
}
func (this *versionedFile) Fsync(ctx context.Context, req *fuse.FsyncRequest) error {
if this.handle == nil {
return errors.New("attempted to sync unopened file")
}
return this.handle.Sync()
}