Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

More comments, gofmt.

  • Loading branch information...
commit df2b769738b2373e941cd84b0fa411f900678682 1 parent 56c4ddf
@hanwen authored
Showing with 62 additions and 65 deletions.
  1. +39 −40 fs.go
  2. +9 −9 main.go
  3. +14 −16 mtp.go
View
79 fs.go
@@ -16,11 +16,12 @@ type DeviceFs struct {
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.
+/* 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.
*/
+
func NewDeviceFs(d *Device) *DeviceFs {
root := rootNode{}
fs := &DeviceFs{root: &root, dev: d}
@@ -46,8 +47,8 @@ func (fs *DeviceFs) newFolder(id uint32, storage uint32) *folderNode {
return &folderNode{
fileNode: fileNode{
storageId: storage,
- id: id,
- fs: fs,
+ id: id,
+ fs: fs,
},
}
}
@@ -55,17 +56,17 @@ func (fs *DeviceFs) newFolder(id uint32, storage uint32) *folderNode {
func (fs *DeviceFs) newFile(file *File) *fileNode {
n := &fileNode{
storageId: file.StorageId(),
- id: file.Id(),
- file: file,
- fs: fs,
+ id: file.Id(),
+ file: file,
+ fs: fs,
}
-
+
return n
}
type rootNode struct {
fuse.DefaultFsNode
- fs *DeviceFs
+ fs *DeviceFs
}
func (fs *DeviceFs) OnMount(conn *fuse.FileSystemConnector) {
@@ -82,11 +83,11 @@ func (fs *DeviceFs) OnMount(conn *fuse.FileSystemConnector) {
type fileNode struct {
fuse.DefaultFsNode
storageId uint32
- id uint32
- file *File
- fs *DeviceFs
- dirty bool
-
+ id uint32
+ file *File
+ fs *DeviceFs
+ dirty bool
+
backing string
}
@@ -101,22 +102,22 @@ func (n *fileNode) send() error {
if !n.dirty {
return nil
}
-
+
if n.backing == "" {
log.Panicf("sending file without backing store: %q", n.file.Name())
}
-
+
fi, err := os.Stat(n.backing)
if err != nil {
log.Println("could not do GetAttr on close.", err)
return err
}
-
+
log.Printf("Sending file %q to device: %d bytes.", n.file.Name(), fi.Size())
- if n.file.Id() != 0 {
+ if n.file.Id() != 0 {
n.fs.dev.DeleteObject(n.file.Id())
}
-
+
n.file.SetFilesize(uint64(fi.Size()))
f, err := os.Open(n.backing)
@@ -129,7 +130,7 @@ func (n *fileNode) send() error {
err = n.fs.dev.SendFromFileDescriptor(n.file, f.Fd())
dt := time.Now().Sub(start)
log.Printf("Sent %d bytes in %d ms. %.1f MB/s", fi.Size(),
- dt.Nanoseconds()/1e6, 1e3 * float64(fi.Size())/float64(dt.Nanoseconds()))
+ dt.Nanoseconds()/1e6, 1e3*float64(fi.Size())/float64(dt.Nanoseconds()))
n.dirty = false
return err
}
@@ -140,7 +141,7 @@ func (n *fileNode) fetch() error {
if n.backing != "" {
return nil
}
-
+
f, err := ioutil.TempFile("", "go-mtpfs")
if err != nil {
return err
@@ -148,7 +149,7 @@ func (n *fileNode) fetch() error {
defer f.Close()
log.Println("fetching to", f.Name())
-
+
err = n.fs.dev.GetFileToFileDescriptor(n.id, f.Fd())
if err == nil {
n.backing = f.Name()
@@ -163,10 +164,10 @@ func (n *fileNode) Open(flags uint32, context *fuse.Context) (file fuse.File, co
return nil, fuse.ToStatus(err)
}
- if flags & fuse.O_ANYWRITE != 0 {
+ if flags&fuse.O_ANYWRITE != 0 {
p := &pendingFile{
LoopbackFile: fuse.LoopbackFile{File: f},
- node: n,
+ node: n,
}
return p, fuse.OK
}
@@ -178,7 +179,7 @@ func (n *fileNode) Truncate(file fuse.File, size uint64, context *fuse.Context)
n.file.filesize = 0
if file != nil {
return file.Truncate(size)
- } else if n.backing != "" {
+ } else if n.backing != "" {
return fuse.ToStatus(os.Truncate(n.backing, int64(size)))
}
return fuse.OK
@@ -210,7 +211,7 @@ func (n *fileNode) Utimens(file fuse.File, AtimeNs int64, MtimeNs int64, context
// TODO - fix mtimes for directories too.
return
}
-
+
n.file.SetMtime(time.Unix(0, MtimeNs))
// TODO - if we have no dirty backing store, should set object property.
return fuse.OK
@@ -238,7 +239,7 @@ func (n *folderNode) fetch() {
}
n.files = map[string]*File{}
n.folders = map[string]uint32{}
-
+
l := n.fs.dev.FilesAndFolders(n.storageId, n.id)
for _, f := range l {
if f.Filetype() == FILETYPE_FOLDER {
@@ -249,11 +250,10 @@ func (n *folderNode) fetch() {
}
}
-
func (n *folderNode) OpenDir(context *fuse.Context) (stream chan fuse.DirEntry, status fuse.Status) {
n.fetch()
-
- stream = make(chan fuse.DirEntry, len(n.folders) + len(n.files))
+
+ stream = make(chan fuse.DirEntry, len(n.folders)+len(n.files))
for n := range n.folders {
stream <- fuse.DirEntry{Name: n, Mode: fuse.S_IFDIR | 0755}
}
@@ -300,7 +300,7 @@ func (n *folderNode) Mkdir(name string, mode uint32, context *fuse.Context) (*fu
return a, f, fuse.OK
}
-func (n *folderNode) Unlink(name string, c *fuse.Context) (fuse.Status) {
+func (n *folderNode) Unlink(name string, c *fuse.Context) fuse.Status {
n.fetch()
f := n.files[name]
if f == nil {
@@ -315,9 +315,9 @@ func (n *folderNode) Unlink(name string, c *fuse.Context) (fuse.Status) {
return fuse.OK
}
-func (n *folderNode) Rmdir(name string, c *fuse.Context) (fuse.Status) {
+func (n *folderNode) Rmdir(name string, c *fuse.Context) fuse.Status {
n.fetch()
-
+
id := n.folders[name]
if id == 0 {
return fuse.ENOENT
@@ -348,21 +348,20 @@ func (n *folderNode) Create(name string, flags uint32, mode uint32, context *fus
fn.backing = f.Name()
n.files[name] = fn.file
n.Inode().New(false, fn)
-
+
p := &pendingFile{
LoopbackFile: fuse.LoopbackFile{File: f},
- node: fn,
+ node: fn,
}
a := &fuse.Attr{
Mode: fuse.S_IFREG | 0644,
}
a.SetTimes(&now, &now, &now)
-
+
return p, a, fn, fuse.OK
}
-
////////////////
// writing files.
@@ -386,7 +385,7 @@ func (p *pendingFile) Flush() fuse.Status {
if !code.Ok() {
return code
}
-
+
s := fuse.ToStatus(p.node.send())
if s == fuse.ENOSYS {
return fuse.EIO
View
18 main.go
@@ -1,8 +1,9 @@
package main
+
import (
"flag"
- "log"
"github.com/hanwen/go-fuse/fuse"
+ "log"
)
func main() {
@@ -24,17 +25,17 @@ func main() {
for _, d := range devs {
log.Printf("device %v: ", d)
}
- if len(devs) == 0 {
+ if len(devs) == 0 {
log.Fatal("no device found. Try replugging it.")
}
- if len(devs) > 1 {
+ if len(devs) > 1 {
log.Fatal("must have exactly one device")
}
-
+
rdev := devs[0]
dev, err := rdev.Open()
- if err != nil {
+ if err != nil {
log.Fatalf("rdev.open: %v", err)
}
defer dev.Release()
@@ -48,19 +49,18 @@ func main() {
if len(dev.ListStorage()) == 0 {
log.Fatalf("No storages found. Try replugging the device or resetting its transport mode.")
}
-
+
fs := NewDeviceFs(dev)
conn := fuse.NewFileSystemConnector(fs, fuse.NewFileSystemOptions())
rawFs := fuse.NewLockingRawFileSystem(conn)
-
+
mount := fuse.NewMountState(rawFs)
if err := mount.Mount(mountpoint, nil); err != nil {
log.Fatalf("mount failed: %v", err)
}
-
+
conn.Debug = *fsdebug
mount.Debug = *fsdebug
log.Println("starting FUSE.")
mount.Loop()
}
-
View
30 mtp.go
@@ -1,8 +1,8 @@
package main
import (
- "log"
"fmt"
+ "log"
"reflect"
"time"
"unsafe"
@@ -12,8 +12,7 @@ import (
// #include <libmtp.h>
import "C"
-var _ = log.Println
-
+var _ = log.Println
/*
This file has a partial cgo wrapping for libmtp, so users should
@@ -37,7 +36,7 @@ const FILETYPE_FOLDER = int(C.LIBMTP_FILETYPE_FOLDER)
const FILETYPE_UNKNOWN = C.LIBMTP_FILETYPE_UNKNOWN
func (e MtpError) Error() string {
- switch (e) {
+ switch e {
case C.LIBMTP_ERROR_CONNECTING:
return "error connecting"
case C.LIBMTP_ERROR_MEMORY_ALLOCATION:
@@ -66,19 +65,19 @@ func (d *RawDevice) String() string {
vendor = C.GoString(d.device_entry.vendor)
}
product := "unknown"
- if d.device_entry.product != nil {
+ if d.device_entry.product != nil {
product = C.GoString(d.device_entry.product)
}
-
- return fmt.Sprintf("%s: %s (%04x:%04x) @ bus %d, dev %d\n",
+
+ return fmt.Sprintf("%s: %s (%04x:%04x) @ bus %d, dev %d\n",
vendor, product,
- d.device_entry.vendor_id,
- d.device_entry.product_id,
- d.bus_location,
- d.devnum)
+ d.device_entry.vendor_id,
+ d.device_entry.product_id,
+ d.bus_location,
+ d.devnum)
}
-func (d *Device) me() (*C.LIBMTP_mtpdevice_t) {
+func (d *Device) me() *C.LIBMTP_mtpdevice_t {
return (*C.LIBMTP_mtpdevice_t)(d)
}
@@ -143,7 +142,7 @@ func (d *Device) FilesAndFolders(storageId uint32, parentId uint32) (files []*Fi
func (d *Device) FolderList(s *DeviceStorage) (folders []*Folder) {
folder := C.LIBMTP_Get_Folder_List_For_Storage(d.me(), s.id)
- for f := (*Folder)(folder); f != nil; f = (*Folder)(f.sibling) {
+ for f := (*Folder)(folder); f != nil; f = (*Folder)(f.sibling) {
folders = append(folders, f)
}
return folders
@@ -244,12 +243,12 @@ func Detect() (devs []*RawDevice, err error) {
}
slice := &reflect.SliceHeader{uintptr(unsafe.Pointer(rawdevices)), int(numrawdevices), int(numrawdevices)}
rdevs := *(*[]RawDevice)(unsafe.Pointer(slice))
-
+
for _, d := range rdevs {
newD := d
devs = append(devs, &newD)
}
-
+
// todo dealloc rawdevices
return devs, nil
}
@@ -270,4 +269,3 @@ func NewFile(id uint32, parent uint32, storage_id uint32, filename string, size
func (f *File) Destroy() {
C.LIBMTP_destroy_file_t(f.me())
}
-
Please sign in to comment.
Something went wrong with that request. Please try again.