/usr/share/gocode/src/github.com/jacobsa/fuse/samples/hellofs/hello_fs.go is in golang-github-jacobsa-fuse-dev 0.0~git20150806.0.9a7512a-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 | // Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package hellofs
import (
"io"
"os"
"strings"
"golang.org/x/net/context"
"github.com/jacobsa/fuse"
"github.com/jacobsa/fuse/fuseops"
"github.com/jacobsa/fuse/fuseutil"
"github.com/jacobsa/timeutil"
)
// Create a file system with a fixed structure that looks like this:
//
// hello
// dir/
// world
//
// Each file contains the string "Hello, world!".
func NewHelloFS(clock timeutil.Clock) (server fuse.Server, err error) {
fs := &helloFS{
Clock: clock,
}
server = fuseutil.NewFileSystemServer(fs)
return
}
type helloFS struct {
fuseutil.NotImplementedFileSystem
Clock timeutil.Clock
}
const (
rootInode fuseops.InodeID = fuseops.RootInodeID + iota
helloInode
dirInode
worldInode
)
type inodeInfo struct {
attributes fuseops.InodeAttributes
// File or directory?
dir bool
// For directories, children.
children []fuseutil.Dirent
}
// We have a fixed directory structure.
var gInodeInfo = map[fuseops.InodeID]inodeInfo{
// root
rootInode: inodeInfo{
attributes: fuseops.InodeAttributes{
Nlink: 1,
Mode: 0555 | os.ModeDir,
},
dir: true,
children: []fuseutil.Dirent{
fuseutil.Dirent{
Offset: 1,
Inode: helloInode,
Name: "hello",
Type: fuseutil.DT_File,
},
fuseutil.Dirent{
Offset: 2,
Inode: dirInode,
Name: "dir",
Type: fuseutil.DT_Directory,
},
},
},
// hello
helloInode: inodeInfo{
attributes: fuseops.InodeAttributes{
Nlink: 1,
Mode: 0444,
Size: uint64(len("Hello, world!")),
},
},
// dir
dirInode: inodeInfo{
attributes: fuseops.InodeAttributes{
Nlink: 1,
Mode: 0555 | os.ModeDir,
},
dir: true,
children: []fuseutil.Dirent{
fuseutil.Dirent{
Offset: 1,
Inode: worldInode,
Name: "world",
Type: fuseutil.DT_File,
},
},
},
// world
worldInode: inodeInfo{
attributes: fuseops.InodeAttributes{
Nlink: 1,
Mode: 0444,
Size: uint64(len("Hello, world!")),
},
},
}
func findChildInode(
name string,
children []fuseutil.Dirent) (inode fuseops.InodeID, err error) {
for _, e := range children {
if e.Name == name {
inode = e.Inode
return
}
}
err = fuse.ENOENT
return
}
func (fs *helloFS) patchAttributes(
attr *fuseops.InodeAttributes) {
now := fs.Clock.Now()
attr.Atime = now
attr.Mtime = now
attr.Crtime = now
}
func (fs *helloFS) LookUpInode(
ctx context.Context,
op *fuseops.LookUpInodeOp) (err error) {
// Find the info for the parent.
parentInfo, ok := gInodeInfo[op.Parent]
if !ok {
err = fuse.ENOENT
return
}
// Find the child within the parent.
childInode, err := findChildInode(op.Name, parentInfo.children)
if err != nil {
return
}
// Copy over information.
op.Entry.Child = childInode
op.Entry.Attributes = gInodeInfo[childInode].attributes
// Patch attributes.
fs.patchAttributes(&op.Entry.Attributes)
return
}
func (fs *helloFS) GetInodeAttributes(
ctx context.Context,
op *fuseops.GetInodeAttributesOp) (err error) {
// Find the info for this inode.
info, ok := gInodeInfo[op.Inode]
if !ok {
err = fuse.ENOENT
return
}
// Copy over its attributes.
op.Attributes = info.attributes
// Patch attributes.
fs.patchAttributes(&op.Attributes)
return
}
func (fs *helloFS) OpenDir(
ctx context.Context,
op *fuseops.OpenDirOp) (err error) {
// Allow opening any directory.
return
}
func (fs *helloFS) ReadDir(
ctx context.Context,
op *fuseops.ReadDirOp) (err error) {
// Find the info for this inode.
info, ok := gInodeInfo[op.Inode]
if !ok {
err = fuse.ENOENT
return
}
if !info.dir {
err = fuse.EIO
return
}
entries := info.children
// Grab the range of interest.
if op.Offset > fuseops.DirOffset(len(entries)) {
err = fuse.EIO
return
}
entries = entries[op.Offset:]
// Resume at the specified offset into the array.
for _, e := range entries {
n := fuseutil.WriteDirent(op.Dst[op.BytesRead:], e)
if n == 0 {
break
}
op.BytesRead += n
}
return
}
func (fs *helloFS) OpenFile(
ctx context.Context,
op *fuseops.OpenFileOp) (err error) {
// Allow opening any file.
return
}
func (fs *helloFS) ReadFile(
ctx context.Context,
op *fuseops.ReadFileOp) (err error) {
// Let io.ReaderAt deal with the semantics.
reader := strings.NewReader("Hello, world!")
op.BytesRead, err = reader.ReadAt(op.Dst, op.Offset)
// Special case: FUSE doesn't expect us to return io.EOF.
if err == io.EOF {
err = nil
}
return
}
|