Skip to content

Commit

Permalink
Collect all backing stores in one directory; delete in one go.
Browse files Browse the repository at this point in the history
  • Loading branch information
hanwen committed Apr 24, 2012
1 parent e77affc commit ee64635
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
20 changes: 7 additions & 13 deletions fs.go
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
9 changes: 8 additions & 1 deletion main.go
Expand Up @@ -3,7 +3,9 @@ package main
import (
"flag"
"github.com/hanwen/go-fuse/fuse"
"io/ioutil"
"log"
"os"
)

func main() {
Expand Down Expand Up @@ -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)

Expand All @@ -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.