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 (
|
2015-03-24 07:34:50 +03:00
|
|
|
"fmt"
|
2015-05-25 07:09:23 +03:00
|
|
|
"log"
|
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"
|
|
|
|
)
|
|
|
|
|
2015-03-24 08:08:42 +03:00
|
|
|
// A type that knows how to serve ops read from a connection.
|
|
|
|
type Server interface {
|
|
|
|
// Read and serve ops from the supplied connection until EOF.
|
|
|
|
ServeOps(*Connection)
|
|
|
|
}
|
2015-03-24 07:16:55 +03:00
|
|
|
|
2015-03-24 07:12:21 +03:00
|
|
|
// A struct representing the status of a mount operation, with a method that
|
|
|
|
// waits for unmounting.
|
2015-02-27 00:54:16 +03:00
|
|
|
type MountedFileSystem struct {
|
|
|
|
dir string
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2015-04-02 03:10:55 +03:00
|
|
|
// Block until a mounted file system has been unmounted. Do not return
|
|
|
|
// successfully until all ops read from the connection have been responded to
|
|
|
|
// (i.e. the file system server has finished processing all in-flight ops).
|
|
|
|
//
|
|
|
|
// The return value will be non-nil if anything unexpected happened while
|
|
|
|
// serving. May be called multiple times.
|
2015-02-27 00:54:16 +03:00
|
|
|
func (mfs *MountedFileSystem) Join(ctx context.Context) error {
|
|
|
|
select {
|
|
|
|
case <-mfs.joinStatusAvailable:
|
|
|
|
return mfs.joinStatus
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-17 07:25:06 +03:00
|
|
|
// Optional configuration accepted by Mount.
|
|
|
|
type MountConfig struct {
|
2015-05-01 04:17:15 +03:00
|
|
|
// The context from which every op read from the connetion by the sever
|
|
|
|
// should inherit. If nil, context.Background() will be used.
|
|
|
|
OpContext context.Context
|
|
|
|
|
2015-05-18 07:06:07 +03:00
|
|
|
// If non-empty, the name of the file system as displayed by e.g. `mount`.
|
|
|
|
// This is important because the `umount` command requires root privileges if
|
|
|
|
// it doesn't agree with /etc/fstab.
|
|
|
|
FSName string
|
|
|
|
|
2015-05-18 03:21:33 +03:00
|
|
|
// Mount the file system in read-only mode. File modes will appear as normal,
|
|
|
|
// but opening a file for writing and metadata operations like chmod,
|
|
|
|
// chtimes, etc. will fail.
|
2015-05-18 02:49:37 +03:00
|
|
|
ReadOnly bool
|
|
|
|
|
2015-05-25 07:09:23 +03:00
|
|
|
// A logger to use for logging errors. All errors are logged, with the
|
2015-05-25 07:15:49 +03:00
|
|
|
// exception of a few blacklisted errors that are expected. If nil, no error
|
|
|
|
// logging is performed.
|
2015-05-25 07:09:23 +03:00
|
|
|
ErrorLogger *log.Logger
|
|
|
|
|
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-05-19 04:39:18 +03:00
|
|
|
|
|
|
|
// Additional key=value options to pass unadulterated to the underlying mount
|
|
|
|
// command. See `man 8 mount`, the fuse documentation, etc. for
|
|
|
|
// system-specific information.
|
|
|
|
//
|
|
|
|
// For expert use only! May invalidate other guarantees made in the
|
|
|
|
// documentation for this package.
|
|
|
|
Options map[string]string
|
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-04-01 05:25:16 +03:00
|
|
|
isDarwin := runtime.GOOS == "darwin"
|
|
|
|
|
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-05-18 07:06:07 +03:00
|
|
|
// Special file system name?
|
|
|
|
if c.FSName != "" {
|
|
|
|
opts = append(opts, bazilfuse.FSName(c.FSName))
|
|
|
|
}
|
|
|
|
|
2015-05-18 03:09:06 +03:00
|
|
|
// Read only?
|
|
|
|
if c.ReadOnly {
|
|
|
|
opts = append(opts, bazilfuse.ReadOnly())
|
|
|
|
}
|
|
|
|
|
2015-04-01 05:25:16 +03:00
|
|
|
// OS X: set novncache when appropriate.
|
|
|
|
if isDarwin && !c.EnableVnodeCaching {
|
2015-03-17 08:22:19 +03:00
|
|
|
opts = append(opts, bazilfuse.SetOption("novncache", ""))
|
2015-03-17 08:06:27 +03:00
|
|
|
}
|
|
|
|
|
2015-04-01 05:25:16 +03:00
|
|
|
// OS X: disable the use of "Apple Double" (._foo and .DS_Store) files, which
|
|
|
|
// just add noise to debug output and can have significant cost on
|
|
|
|
// network-based file systems.
|
|
|
|
//
|
|
|
|
// Cf. https://github.com/osxfuse/osxfuse/wiki/Mount-options
|
|
|
|
if isDarwin {
|
|
|
|
opts = append(opts, bazilfuse.SetOption("noappledouble", ""))
|
|
|
|
}
|
|
|
|
|
2015-05-19 04:39:18 +03:00
|
|
|
// Last but not least: other user-supplied options.
|
|
|
|
for k, v := range c.Options {
|
|
|
|
opts = append(opts, bazilfuse.SetOption(k, v))
|
|
|
|
}
|
|
|
|
|
2015-03-17 07:25:48 +03:00
|
|
|
return
|
|
|
|
}
|
2015-03-17 07:25:06 +03:00
|
|
|
|
2015-03-24 07:16:55 +03:00
|
|
|
// Attempt to mount a file system on the given directory, using the supplied
|
|
|
|
// Server to serve connection requests. This function blocks until the file
|
|
|
|
// system is successfully mounted. On some systems, this requires the supplied
|
|
|
|
// Server to make forward progress (in particular, to respond to
|
|
|
|
// fuseops.InitOp).
|
2015-02-27 00:54:16 +03:00
|
|
|
func Mount(
|
|
|
|
dir string,
|
2015-03-24 07:16:55 +03:00
|
|
|
server Server,
|
2015-03-17 07:25:06 +03:00
|
|
|
config *MountConfig) (mfs *MountedFileSystem, err error) {
|
2015-03-24 07:34:50 +03:00
|
|
|
logger := getLogger()
|
|
|
|
|
2015-02-27 00:54:16 +03:00
|
|
|
// Initialize the struct.
|
|
|
|
mfs = &MountedFileSystem{
|
2015-03-24 07:34:50 +03:00
|
|
|
dir: dir,
|
|
|
|
joinStatusAvailable: make(chan struct{}),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open a bazilfuse connection.
|
|
|
|
logger.Println("Opening a bazilfuse connection.")
|
|
|
|
bfConn, err := bazilfuse.Mount(mfs.dir, config.bazilfuseOptions()...)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("bazilfuse.Mount: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-05-01 04:17:15 +03:00
|
|
|
// Choose a parent context for ops.
|
|
|
|
opContext := config.OpContext
|
|
|
|
if opContext == nil {
|
|
|
|
opContext = context.Background()
|
|
|
|
}
|
|
|
|
|
2015-03-24 07:34:50 +03:00
|
|
|
// Create our own Connection object wrapping it.
|
2015-05-01 04:17:15 +03:00
|
|
|
connection, err := newConnection(opContext, logger, bfConn)
|
2015-03-24 07:34:50 +03:00
|
|
|
if err != nil {
|
|
|
|
bfConn.Close()
|
|
|
|
err = fmt.Errorf("newConnection: %v", err)
|
|
|
|
return
|
2015-02-27 00:54:16 +03:00
|
|
|
}
|
|
|
|
|
2015-03-24 07:34:50 +03:00
|
|
|
// Serve the connection in the background. When done, set the join status.
|
|
|
|
go func() {
|
2015-03-24 08:08:42 +03:00
|
|
|
server.ServeOps(connection)
|
2015-03-24 07:34:50 +03:00
|
|
|
mfs.joinStatus = connection.close()
|
|
|
|
close(mfs.joinStatusAvailable)
|
|
|
|
}()
|
2015-02-27 00:54:16 +03:00
|
|
|
|
2015-03-24 07:34:50 +03:00
|
|
|
// Wait for the connection to say it is ready.
|
|
|
|
if err = connection.waitForReady(); err != nil {
|
|
|
|
err = fmt.Errorf("WaitForReady: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2015-03-24 07:16:55 +03:00
|
|
|
|
2015-02-27 00:54:16 +03:00
|
|
|
return
|
|
|
|
}
|