2015-02-27 00:54:16 +03:00
|
|
|
// Copyright 2015 Google Inc. All Rights Reserved.
|
2015-03-04 00:27:42 +03:00
|
|
|
//
|
|
|
|
// 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.
|
2015-02-27 00:54:16 +03:00
|
|
|
|
2015-02-27 00:57:18 +03:00
|
|
|
package fuse
|
2015-02-27 00:54:16 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2015-03-17 08:22:19 +03:00
|
|
|
"runtime"
|
2015-02-27 00:54:16 +03:00
|
|
|
|
2015-03-17 07:03:11 +03:00
|
|
|
"github.com/jacobsa/bazilfuse"
|
2015-02-27 00:54:16 +03:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
// A struct representing the status of a mount operation, with methods for
|
|
|
|
// waiting on the mount to complete, waiting for unmounting, and causing
|
|
|
|
// unmounting.
|
|
|
|
type MountedFileSystem struct {
|
|
|
|
dir string
|
|
|
|
|
|
|
|
// The result to return from WaitForReady. Not valid until the channel is
|
|
|
|
// closed.
|
|
|
|
readyStatus error
|
|
|
|
readyStatusAvailable chan struct{}
|
|
|
|
|
|
|
|
// The result to return from Join. Not valid until the channel is closed.
|
|
|
|
joinStatus error
|
|
|
|
joinStatusAvailable chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the directory on which the file system is mounted (or where we
|
|
|
|
// attempted to mount it.)
|
|
|
|
func (mfs *MountedFileSystem) Dir() string {
|
|
|
|
return mfs.dir
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait until the mount point is ready to be used. After a successful return
|
|
|
|
// from this function, the contents of the mounted file system should be
|
|
|
|
// visible in the directory supplied to NewMountPoint. May be called multiple
|
|
|
|
// times.
|
|
|
|
func (mfs *MountedFileSystem) WaitForReady(ctx context.Context) error {
|
|
|
|
select {
|
|
|
|
case <-mfs.readyStatusAvailable:
|
|
|
|
return mfs.readyStatus
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Block until a mounted file system has been unmounted. The return value will
|
|
|
|
// be non-nil if anything unexpected happened while serving. May be called
|
|
|
|
// multiple times. Must not be called unless WaitForReady has returned nil.
|
|
|
|
func (mfs *MountedFileSystem) Join(ctx context.Context) error {
|
|
|
|
select {
|
|
|
|
case <-mfs.joinStatusAvailable:
|
|
|
|
return mfs.joinStatus
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to unmount the file system. Use Join to wait for it to actually be
|
|
|
|
// unmounted. You must first call WaitForReady to ensure there is no race with
|
|
|
|
// mounting.
|
|
|
|
func (mfs *MountedFileSystem) Unmount() error {
|
2015-02-27 01:32:18 +03:00
|
|
|
return bazilfuse.Unmount(mfs.dir)
|
2015-02-27 00:54:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Runs in the background.
|
|
|
|
func (mfs *MountedFileSystem) mountAndServe(
|
|
|
|
server *server,
|
2015-02-27 01:32:18 +03:00
|
|
|
options []bazilfuse.MountOption) {
|
2015-02-27 00:54:16 +03:00
|
|
|
logger := getLogger()
|
|
|
|
|
|
|
|
// Open a FUSE connection.
|
|
|
|
logger.Println("Opening a FUSE connection.")
|
2015-02-27 01:32:18 +03:00
|
|
|
c, err := bazilfuse.Mount(mfs.dir, options...)
|
2015-02-27 00:54:16 +03:00
|
|
|
if err != nil {
|
2015-02-27 01:32:18 +03:00
|
|
|
mfs.readyStatus = errors.New("bazilfuse.Mount: " + err.Error())
|
2015-02-27 00:54:16 +03:00
|
|
|
close(mfs.readyStatusAvailable)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer c.Close()
|
|
|
|
|
|
|
|
// Start a goroutine that will notify the MountedFileSystem object when the
|
|
|
|
// connection says it is ready (or it fails to become ready).
|
|
|
|
go func() {
|
|
|
|
logger.Println("Waiting for the FUSE connection to be ready.")
|
|
|
|
<-c.Ready
|
|
|
|
logger.Println("The FUSE connection is ready.")
|
|
|
|
|
|
|
|
mfs.readyStatus = c.MountError
|
|
|
|
close(mfs.readyStatusAvailable)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Serve the connection using the file system object.
|
|
|
|
logger.Println("Serving the FUSE connection.")
|
|
|
|
if err := server.Serve(c); err != nil {
|
|
|
|
mfs.joinStatus = errors.New("Serve: " + err.Error())
|
|
|
|
close(mfs.joinStatusAvailable)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Signal that everything is okay.
|
|
|
|
close(mfs.joinStatusAvailable)
|
|
|
|
}
|
|
|
|
|
2015-03-17 07:25:06 +03:00
|
|
|
// Optional configuration accepted by Mount.
|
|
|
|
type MountConfig struct {
|
2015-03-17 08:19:13 +03:00
|
|
|
// OS X only.
|
|
|
|
//
|
|
|
|
// Normally on OS X we mount with the novncache option
|
|
|
|
// (cf. http://goo.gl/1pTjuk), which disables entry caching in the kernel.
|
2015-03-17 08:39:48 +03:00
|
|
|
// This is because osxfuse does not honor the entry expiration values we
|
|
|
|
// return to it, instead caching potentially forever (cf.
|
|
|
|
// http://goo.gl/8yR0Ie), and it is probably better to fail to cache than to
|
|
|
|
// cache for too long, since the latter is more likely to hide consistency
|
|
|
|
// bugs that are difficult to detect and diagnose.
|
2015-03-17 08:19:13 +03:00
|
|
|
//
|
|
|
|
// This field disables the use of novncache, restoring entry caching. Beware:
|
|
|
|
// the value of ChildInodeEntry.EntryExpiration is ignored by the kernel, and
|
|
|
|
// entries will be cached for an arbitrarily long time.
|
|
|
|
EnableVnodeCaching bool
|
2015-03-17 07:25:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convert to mount options to be passed to package bazilfuse.
|
2015-03-17 07:25:48 +03:00
|
|
|
func (c *MountConfig) bazilfuseOptions() (opts []bazilfuse.MountOption) {
|
2015-03-17 08:22:19 +03:00
|
|
|
// Enable permissions checking in the kernel. See the comments on
|
|
|
|
// InodeAttributes.Mode.
|
|
|
|
opts = append(opts, bazilfuse.SetOption("default_permissions", ""))
|
2015-03-17 08:19:13 +03:00
|
|
|
|
2015-03-17 08:22:19 +03:00
|
|
|
// OS X only: set novncache when appropriate.
|
|
|
|
if runtime.GOOS == "darwin" && !c.EnableVnodeCaching {
|
|
|
|
opts = append(opts, bazilfuse.SetOption("novncache", ""))
|
2015-03-17 08:06:27 +03:00
|
|
|
}
|
|
|
|
|
2015-03-17 07:25:48 +03:00
|
|
|
return
|
|
|
|
}
|
2015-03-17 07:25:06 +03:00
|
|
|
|
2015-02-27 00:54:16 +03:00
|
|
|
// Attempt to mount the supplied file system on the given directory.
|
|
|
|
// mfs.WaitForReady() must be called to find out whether the mount was
|
|
|
|
// successful.
|
|
|
|
func Mount(
|
|
|
|
dir string,
|
|
|
|
fs FileSystem,
|
2015-03-17 07:25:06 +03:00
|
|
|
config *MountConfig) (mfs *MountedFileSystem, err error) {
|
2015-02-27 00:54:16 +03:00
|
|
|
// Create a server object.
|
|
|
|
server, err := newServer(fs)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize the struct.
|
|
|
|
mfs = &MountedFileSystem{
|
|
|
|
dir: dir,
|
|
|
|
readyStatusAvailable: make(chan struct{}),
|
|
|
|
joinStatusAvailable: make(chan struct{}),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mount in the background.
|
2015-03-17 07:25:06 +03:00
|
|
|
go mfs.mountAndServe(server, config.bazilfuseOptions())
|
2015-02-27 00:54:16 +03:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|