Permalink
Browse files

Collect all backing stores in one directory; delete in one go.

  • Loading branch information...
hanwen committed Apr 24, 2012
1 parent e77affc commit ee64635593fb2dacc08fd78ed2c0dcab06e1bf04
Showing with 15 additions and 14 deletions.
  1. +7 −13 fs.go
  2. +8 −1 main.go
View
20 fs.go
@@ -10,19 +10,20 @@ import (
type DeviceFs struct {
fuse.DefaultNodeFileSystem
+ backingDir string
root *rootNode
dev *Device
}
/* DeviceFs is a simple filesystem interface to an MTP device. It
should be wrapped in a Locking(Raw)FileSystem to make sure it is
threadsafe. The file system assumes the device does not touch the
-storage.
-*/
+storage. Arguments are the opened mtp device and a directory for the
+backing store. */
-func NewDeviceFs(d *Device) *DeviceFs {
+func NewDeviceFs(d *Device, dir string) *DeviceFs {
root := rootNode{}
- fs := &DeviceFs{root: &root, dev: d}
+ fs := &DeviceFs{root: &root, dev: d, backingDir: dir}
root.fs = fs
return fs
}
@@ -138,7 +139,7 @@ func (n *fileNode) fetch() error {
return nil
}
- f, err := ioutil.TempFile("", "go-mtpfs")
+ f, err := ioutil.TempFile(n.fs.backingDir, "")
if err != nil {
return err
}
@@ -221,13 +222,6 @@ type folderNode struct {
folders map[string]uint32
}
-func (n *folderNode) OnForget() {
- n.fileNode.OnForget()
- if n.backing != "" {
- os.Remove(n.backing)
- }
-}
-
func (n *folderNode) fetch() {
if n.files != nil {
return
@@ -328,7 +322,7 @@ func (n *folderNode) Rmdir(name string, c *fuse.Context) fuse.Status {
func (n *folderNode) Create(name string, flags uint32, mode uint32, context *fuse.Context) (file fuse.File, fi *fuse.Attr, node fuse.FsNode, code fuse.Status) {
n.fetch()
- f, err := ioutil.TempFile("", "go-mtpfs")
+ f, err := ioutil.TempFile(n.fs.backingDir, "")
if err != nil {
return nil, nil, nil, fuse.ToStatus(err)
View
@@ -3,7 +3,9 @@ package main
import (
"flag"
"github.com/hanwen/go-fuse/fuse"
+ "io/ioutil"
"log"
+ "os"
)
func main() {
@@ -50,7 +52,11 @@ func main() {
log.Fatalf("No storages found. Try replugging the device or resetting its transport mode.")
}
- fs := NewDeviceFs(dev)
+ backing, err := ioutil.TempDir("", "go-mtpfs")
+ if err != nil {
+ log.Fatalf("TempDir failed: %v", err)
+ }
+ fs := NewDeviceFs(dev, backing)
conn := fuse.NewFileSystemConnector(fs, fuse.NewFileSystemOptions())
rawFs := fuse.NewLockingRawFileSystem(conn)
@@ -63,4 +69,5 @@ func main() {
mount.Debug = *fsdebug
log.Println("starting FUSE.")
mount.Loop()
+ os.RemoveAll(backing)
}

0 comments on commit ee64635

Please sign in to comment.