vfs/node.go

136 lines
3.3 KiB
Go
Raw Normal View History

2015-05-16 07:43:46 +00:00
/*
* 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
2015-05-16 11:33:35 +00:00
import (
"os"
2015-05-16 14:04:34 +00:00
"syscall"
"time"
2015-06-17 11:11:16 +00:00
"bazil.org/fuse"
2015-05-16 11:33:35 +00:00
)
2015-05-16 07:43:46 +00:00
2015-05-28 09:20:38 +00:00
//
2015-06-19 08:51:10 +00:00
// verNode
2015-05-28 09:20:38 +00:00
//
2015-06-17 07:41:43 +00:00
const (
NodeFlagDir = 1 << iota
2015-06-21 02:19:37 +00:00
NodeFlagNew
2015-06-17 07:41:43 +00:00
)
2015-05-24 11:56:29 +00:00
2015-06-19 08:51:10 +00:00
type verNode struct {
2015-06-17 07:41:43 +00:00
path string
ver *version
2015-06-19 08:51:10 +00:00
parent *verNode
2015-06-17 07:41:43 +00:00
flags int
2015-05-24 11:56:29 +00:00
}
2015-06-19 08:51:10 +00:00
func newVerNode(path string, ver *version, parent *verNode, flags int) *verNode {
return &verNode{path, ver, parent, flags}
2015-06-16 09:08:30 +00:00
}
2015-06-19 08:51:10 +00:00
func (n *verNode) setAttr(req *fuse.SetattrRequest, resp *fuse.SetattrResponse) error {
2015-06-17 11:18:55 +00:00
if err := n.attr(&resp.Attr); err != nil {
2015-06-17 07:41:43 +00:00
return err
}
2015-05-26 07:28:12 +00:00
if req.Valid&fuse.SetattrMode != 0 {
2015-06-17 11:18:55 +00:00
if err := os.Chmod(n.rebasedPath(), req.Mode); err != nil {
2015-05-26 07:28:12 +00:00
return err
}
2015-06-17 07:41:43 +00:00
resp.Attr.Mode = req.Mode
2015-05-26 07:28:12 +00:00
}
if setGid, setUid := req.Valid&fuse.SetattrGid != 0, req.Valid&fuse.SetattrUid != 0; setGid || setUid {
if setGid {
2015-06-17 07:41:43 +00:00
resp.Attr.Gid = req.Gid
2015-05-26 07:28:12 +00:00
}
if setUid {
2015-06-17 07:41:43 +00:00
resp.Attr.Uid = req.Uid
2015-05-26 07:28:12 +00:00
}
2015-06-17 11:18:55 +00:00
if err := os.Chown(n.rebasedPath(), int(resp.Attr.Uid), int(resp.Attr.Gid)); err != nil {
2015-05-26 07:28:12 +00:00
return err
}
}
if setAtime, setMtime := req.Valid&fuse.SetattrAtime != 0, req.Valid&fuse.SetattrMtime != 0; setAtime || setMtime {
if setAtime {
2015-06-17 07:41:43 +00:00
resp.Attr.Atime = req.Atime
2015-05-26 07:28:12 +00:00
}
if setMtime {
2015-06-17 07:41:43 +00:00
resp.Attr.Mtime = req.Mtime
2015-05-26 07:28:12 +00:00
}
2015-06-17 11:18:55 +00:00
if err := os.Chtimes(n.rebasedPath(), resp.Attr.Atime, resp.Attr.Mtime); err != nil {
2015-05-26 07:28:12 +00:00
return err
}
}
return nil
2015-06-16 09:22:29 +00:00
}
2015-06-19 08:51:10 +00:00
func (n *verNode) rebasedPath() string {
2015-06-17 11:18:55 +00:00
return n.ver.rebasePath(n.path)
2015-05-16 07:43:46 +00:00
}
2015-05-16 11:33:35 +00:00
2015-06-19 08:51:10 +00:00
func (n *verNode) owner(stat syscall.Stat_t) (gid, uid uint32) {
2015-05-26 07:28:12 +00:00
gid = stat.Gid
uid = stat.Uid
return
}
2015-06-19 08:51:10 +00:00
func (n *verNode) times(stat syscall.Stat_t) (atime, mtime, ctime time.Time) {
2015-05-26 07:28:12 +00:00
atime = time.Unix(int64(stat.Atim.Sec), int64(stat.Atim.Nsec))
2015-06-17 07:41:43 +00:00
mtime = time.Unix(int64(stat.Mtim.Sec), int64(stat.Mtim.Nsec))
2015-05-26 07:28:12 +00:00
ctime = time.Unix(int64(stat.Ctim.Sec), int64(stat.Ctim.Nsec))
return
}
2015-06-19 08:51:10 +00:00
func (n *verNode) attr(attr *fuse.Attr) error {
2015-06-17 11:18:55 +00:00
info, err := os.Stat(n.rebasedPath())
2015-06-17 07:41:43 +00:00
if err != nil {
return err
}
2015-05-16 14:04:34 +00:00
2015-06-17 07:41:43 +00:00
stat := info.Sys().(*syscall.Stat_t)
attr.Size = uint64(stat.Size)
2015-05-16 14:04:34 +00:00
attr.Blocks = uint64(stat.Blocks)
2015-06-17 11:18:55 +00:00
attr.Atime, attr.Mtime, attr.Ctime = n.times(*stat)
2015-06-17 07:41:43 +00:00
attr.Mode = info.Mode()
2015-05-16 14:04:34 +00:00
attr.Nlink = uint32(stat.Nlink)
2015-06-17 11:18:55 +00:00
attr.Gid, attr.Uid = n.owner(*stat)
2015-05-16 14:04:34 +00:00
attr.Rdev = uint32(stat.Rdev)
2015-06-17 07:41:43 +00:00
return nil
2015-05-16 14:04:34 +00:00
}
2015-06-16 09:22:29 +00:00
//
2015-06-19 08:51:10 +00:00
// verNodeMap
2015-06-16 09:22:29 +00:00
//
2015-06-19 08:51:10 +00:00
type verNodeMap map[string]*verNode