Skip to content
This repository
Browse code

Handle zero bytes files (we can't send them)

Handle files that are deleted while they are opened (we don't send
them).
  • Loading branch information...
commit 3e0dc97ada0da483c3650eb4487db46a42fd1f92 1 parent c2fab36
Han-Wen Nienhuys authored

Showing 3 changed files with 29 additions and 6 deletions. Show diff stats Hide diff stats

  1. +18 5 fs.go
  2. +6 0 mtp.go
  3. +5 1 test.sh
23 fs.go
@@ -9,6 +9,7 @@ import (
9 9 "io/ioutil"
10 10 "log"
11 11 "os"
  12 + "syscall"
12 13 "time"
13 14 )
14 15
@@ -113,18 +114,24 @@ func (n *fileNode) send() error {
113 114 if n.backing == "" {
114 115 log.Panicf("sending file without backing store: %q", n.file.Name())
115 116 }
116   -
117 117 fi, err := os.Stat(n.backing)
118 118 if err != nil {
119 119 log.Printf("could not do stat for send: %v", err)
120 120 return err
121 121 }
  122 + if fi.Size() == 0 {
  123 + log.Printf("cannot send 0 byte file %q", n.file.Name())
  124 + return syscall.EINVAL
  125 + }
122 126 f, err := os.Open(n.backing)
123 127 if err != nil {
124 128 return err
125 129 }
126 130 defer f.Close()
127 131
  132 + if n.file.Name() == "" {
  133 + return nil
  134 + }
128 135 log.Printf("Sending file %q to device: %d bytes.", n.file.Name(), fi.Size())
129 136 if n.file.Id() != 0 {
130 137 // Apparently, you can't overwrite things in MTP.
@@ -403,10 +410,15 @@ func (n *folderNode) Unlink(name string, c *fuse.Context) fuse.Status {
403 410 if f == nil {
404 411 return fuse.ENOENT
405 412 }
406   - err := n.fs.dev.DeleteObject(f.Id())
407   - if err != nil {
408   - log.Printf("DeleteObject failed: %v", err)
409   - return fuse.EIO
  413 +
  414 + if f.Id() != 0 {
  415 + err := n.fs.dev.DeleteObject(f.Id())
  416 + if err != nil {
  417 + log.Printf("DeleteObject failed: %v", err)
  418 + return fuse.EIO
  419 + }
  420 + } else {
  421 + f.SetName("")
410 422 }
411 423 n.Inode().RmChild(name)
412 424
@@ -433,6 +445,7 @@ func (n *folderNode) Create(name string, flags uint32, mode uint32, context *fus
433 445 file: NewFile(0, n.id, n.storageId, name,
434 446 0, now, FILETYPE_UNKNOWN),
435 447 fs: n.fs,
  448 + dirty: true,
436 449 }
437 450 fn.backing = f.Name()
438 451 n.children[name] = fn.file
6 mtp.go
@@ -317,3 +317,9 @@ func (d *File) Filetype() int {
317 317 func (d *File) Name() string {
318 318 return C.GoString(d.filename)
319 319 }
  320 +
  321 +func (f *File) SetName(n string) {
  322 + old := f.filename
  323 + C.free(unsafe.Pointer(old))
  324 + f.filename = C.CString(n)
  325 +}
6 test.sh
@@ -20,6 +20,10 @@ test $(cat "$root/mtpfs-test/test.txt") == "hello"
20 20 touch "$root/mtpfs-test/test.txt"
21 21 echo something else > "$root/mtpfs-test/test.txt"
22 22
  23 +# can't send zero bytes file.
  24 +! touch "$root/mtpfs-test/zerobytes.txt"
  25 +rm "$root/mtpfs-test/zerobytes.txt"
  26 +
23 27 mv "$root/mtpfs-test/test.txt" "$root/mtpfs-test/test2.txt"
24 28 ! test -f "$root/mtpfs-test/test.txt"
25 29 test -f "$root/mtpfs-test/test2.txt"
@@ -41,5 +45,5 @@ test -d "$root/mtpfs-test/create"
41 45 test -f "$root/mtpfs-test/test2.txt"
42 46 test -f "$root/mtpfs-test/dest.txt"
43 47 ! test -f "$root/mtpfs-test/src.txt"
44   -
  48 +! test -f "$root/mtpfs-test/zerobytes.txt"
45 49 fusermount -u $mount

0 comments on commit 3e0dc97

Please sign in to comment.
Something went wrong with that request. Please try again.