mount.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright 2015 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package fuse
  15. import (
  16. "fmt"
  17. "os"
  18. "golang.org/x/net/context"
  19. )
  20. // Server is an interface for any type that knows how to serve ops read from a
  21. // connection.
  22. type Server interface {
  23. // Read and serve ops from the supplied connection until EOF. Do not return
  24. // until all operations have been responded to. Must not be called more than
  25. // once.
  26. ServeOps(*Connection)
  27. }
  28. // Mount attempts to mount a file system on the given directory, using the
  29. // supplied Server to serve connection requests. It blocks until the file
  30. // system is successfully mounted.
  31. func Mount(
  32. dir string,
  33. server Server,
  34. config *MountConfig) (mfs *MountedFileSystem, err error) {
  35. // Sanity check: make sure the mount point exists and is a directory. This
  36. // saves us from some confusing errors later on OS X.
  37. fi, err := os.Stat(dir)
  38. switch {
  39. case os.IsNotExist(err):
  40. return
  41. case err != nil:
  42. err = fmt.Errorf("Statting mount point: %v", err)
  43. return
  44. case !fi.IsDir():
  45. err = fmt.Errorf("Mount point %s is not a directory", dir)
  46. return
  47. }
  48. // Initialize the struct.
  49. mfs = &MountedFileSystem{
  50. dir: dir,
  51. joinStatusAvailable: make(chan struct{}),
  52. }
  53. // Begin the mounting process, which will continue in the background.
  54. ready := make(chan error, 1)
  55. dev, err := mount(dir, config, ready)
  56. if err != nil {
  57. err = fmt.Errorf("mount: %v", err)
  58. return
  59. }
  60. // Choose a parent context for ops.
  61. cfgCopy := *config
  62. if cfgCopy.OpContext == nil {
  63. cfgCopy.OpContext = context.Background()
  64. }
  65. // Create a Connection object wrapping the device.
  66. connection, err := newConnection(
  67. cfgCopy,
  68. config.DebugLogger,
  69. config.ErrorLogger,
  70. dev)
  71. if err != nil {
  72. err = fmt.Errorf("newConnection: %v", err)
  73. return
  74. }
  75. // Serve the connection in the background. When done, set the join status.
  76. go func() {
  77. server.ServeOps(connection)
  78. mfs.joinStatus = connection.close()
  79. close(mfs.joinStatusAvailable)
  80. }()
  81. // Wait for the mount process to complete.
  82. if err = <-ready; err != nil {
  83. err = fmt.Errorf("mount (background): %v", err)
  84. return
  85. }
  86. return
  87. }