/usr/share/gocode/src/github.com/jacobsa/fuse/samples/flushfs/flush_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 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 | // 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 flushfs
import (
"fmt"
"os"
"sync"
"golang.org/x/net/context"
"github.com/jacobsa/fuse"
"github.com/jacobsa/fuse/fuseops"
"github.com/jacobsa/fuse/fuseutil"
)
// Create a file system whose sole contents are a file named "foo" and a
// directory named "bar".
//
// The file may be opened for reading and/or writing. Its initial contents are
// empty. Whenever a flush or fsync is received, the supplied function will be
// called with the current contents of the file and its status returned.
//
// The directory cannot be modified.
func NewFileSystem(
reportFlush func(string) error,
reportFsync func(string) error) (server fuse.Server, err error) {
fs := &flushFS{
reportFlush: reportFlush,
reportFsync: reportFsync,
}
server = fuseutil.NewFileSystemServer(fs)
return
}
const (
fooID = fuseops.RootInodeID + 1 + iota
barID
)
type flushFS struct {
fuseutil.NotImplementedFileSystem
reportFlush func(string) error
reportFsync func(string) error
mu sync.Mutex
fooContents []byte // GUARDED_BY(mu)
}
////////////////////////////////////////////////////////////////////////
// Helpers
////////////////////////////////////////////////////////////////////////
// LOCKS_REQUIRED(fs.mu)
func (fs *flushFS) rootAttributes() fuseops.InodeAttributes {
return fuseops.InodeAttributes{
Nlink: 1,
Mode: 0777 | os.ModeDir,
}
}
// LOCKS_REQUIRED(fs.mu)
func (fs *flushFS) fooAttributes() fuseops.InodeAttributes {
return fuseops.InodeAttributes{
Nlink: 1,
Mode: 0777,
Size: uint64(len(fs.fooContents)),
}
}
// LOCKS_REQUIRED(fs.mu)
func (fs *flushFS) barAttributes() fuseops.InodeAttributes {
return fuseops.InodeAttributes{
Nlink: 1,
Mode: 0777 | os.ModeDir,
}
}
////////////////////////////////////////////////////////////////////////
// FileSystem methods
////////////////////////////////////////////////////////////////////////
func (fs *flushFS) LookUpInode(
ctx context.Context,
op *fuseops.LookUpInodeOp) (err error) {
fs.mu.Lock()
defer fs.mu.Unlock()
// Sanity check.
if op.Parent != fuseops.RootInodeID {
err = fuse.ENOENT
return
}
// Set up the entry.
switch op.Name {
case "foo":
op.Entry = fuseops.ChildInodeEntry{
Child: fooID,
Attributes: fs.fooAttributes(),
}
case "bar":
op.Entry = fuseops.ChildInodeEntry{
Child: barID,
Attributes: fs.barAttributes(),
}
default:
err = fuse.ENOENT
return
}
return
}
func (fs *flushFS) GetInodeAttributes(
ctx context.Context,
op *fuseops.GetInodeAttributesOp) (err error) {
fs.mu.Lock()
defer fs.mu.Unlock()
switch op.Inode {
case fuseops.RootInodeID:
op.Attributes = fs.rootAttributes()
return
case fooID:
op.Attributes = fs.fooAttributes()
return
case barID:
op.Attributes = fs.barAttributes()
return
default:
err = fuse.ENOENT
return
}
}
func (fs *flushFS) OpenFile(
ctx context.Context,
op *fuseops.OpenFileOp) (err error) {
fs.mu.Lock()
defer fs.mu.Unlock()
// Sanity check.
if op.Inode != fooID {
err = fuse.ENOSYS
return
}
return
}
func (fs *flushFS) ReadFile(
ctx context.Context,
op *fuseops.ReadFileOp) (err error) {
fs.mu.Lock()
defer fs.mu.Unlock()
// Ensure the offset is in range.
if op.Offset > int64(len(fs.fooContents)) {
return
}
// Read what we can.
op.BytesRead = copy(op.Dst, fs.fooContents[op.Offset:])
return
}
func (fs *flushFS) WriteFile(
ctx context.Context,
op *fuseops.WriteFileOp) (err error) {
fs.mu.Lock()
defer fs.mu.Unlock()
// Ensure that the contents slice is long enough.
newLen := int(op.Offset) + len(op.Data)
if len(fs.fooContents) < newLen {
padding := make([]byte, newLen-len(fs.fooContents))
fs.fooContents = append(fs.fooContents, padding...)
}
// Copy in the data.
n := copy(fs.fooContents[op.Offset:], op.Data)
// Sanity check.
if n != len(op.Data) {
panic(fmt.Sprintf("Unexpected short copy: %v", n))
}
return
}
func (fs *flushFS) SyncFile(
ctx context.Context,
op *fuseops.SyncFileOp) (err error) {
fs.mu.Lock()
defer fs.mu.Unlock()
err = fs.reportFsync(string(fs.fooContents))
return
}
func (fs *flushFS) FlushFile(
ctx context.Context,
op *fuseops.FlushFileOp) (err error) {
fs.mu.Lock()
defer fs.mu.Unlock()
err = fs.reportFlush(string(fs.fooContents))
return
}
func (fs *flushFS) OpenDir(
ctx context.Context,
op *fuseops.OpenDirOp) (err error) {
fs.mu.Lock()
defer fs.mu.Unlock()
// Sanity check.
switch op.Inode {
case fuseops.RootInodeID:
case barID:
default:
err = fuse.ENOENT
return
}
return
}
func (fs *flushFS) ReadDir(
ctx context.Context,
op *fuseops.ReadDirOp) (err error) {
fs.mu.Lock()
defer fs.mu.Unlock()
// Create the appropriate listing.
var dirents []fuseutil.Dirent
switch op.Inode {
case fuseops.RootInodeID:
dirents = []fuseutil.Dirent{
fuseutil.Dirent{
Offset: 1,
Inode: fooID,
Name: "foo",
Type: fuseutil.DT_File,
},
fuseutil.Dirent{
Offset: 2,
Inode: barID,
Name: "bar",
Type: fuseutil.DT_Directory,
},
}
case barID:
default:
err = fmt.Errorf("Unexpected inode: %v", op.Inode)
return
}
// If the offset is for the end of the listing, we're done. Otherwise we
// expect it to be for the start.
switch op.Offset {
case fuseops.DirOffset(len(dirents)):
return
case 0:
default:
err = fmt.Errorf("Unexpected offset: %v", op.Offset)
return
}
// Fill in the listing.
for _, de := range dirents {
n := fuseutil.WriteDirent(op.Dst[op.BytesRead:], de)
// We don't support doing this in anything more than one shot.
if n == 0 {
err = fmt.Errorf("Couldn't fit listing in %v bytes", len(op.Dst))
return
}
op.BytesRead += n
}
return
}
|