forked from hanwen/go-mtpfs
/
mtp.go
351 lines (289 loc) · 8.29 KB
/
mtp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// Copyright 2012 Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"log"
"reflect"
"strings"
"time"
"unsafe"
)
// #cgo LDFLAGS: -lmtp -L/usr/local/lib
// #include <libmtp.h>
// #include <stdlib.h>
import "C"
var _ = log.Println
/*
This file has a partial cgo wrapping for libmtp, so users should
never have to call import "C"
*/
// TODO - we leak C strings all over the place.
type Device C.LIBMTP_mtpdevice_t
type Error C.LIBMTP_error_t
type MtpError C.LIBMTP_error_number_t
type RawDevice C.LIBMTP_raw_device_t
type DeviceStorage C.LIBMTP_devicestorage_t
type Folder C.LIBMTP_folder_t
type File C.LIBMTP_file_t
const NOPARENT_ID = 0xFFFFFFFF
const DEBUG_PTP = int(C.LIBMTP_DEBUG_PTP)
const DEBUG_PLST = int(C.LIBMTP_DEBUG_PLST)
const DEBUG_USB = int(C.LIBMTP_DEBUG_USB)
const DEBUG_DATA = int(C.LIBMTP_DEBUG_DATA)
const DEBUG_ALL = int(C.LIBMTP_DEBUG_ALL)
const FILETYPE_FOLDER = int(C.LIBMTP_FILETYPE_FOLDER)
const FILETYPE_UNKNOWN = C.LIBMTP_FILETYPE_UNKNOWN
func Init() {
C.LIBMTP_Init()
log.Printf("compiled against libmtp %s", MtpVersion())
}
func MtpVersion() string {
return C.LIBMTP_VERSION_STRING
}
func SetDebug(mask int) {
C.LIBMTP_Set_Debug(C.int(mask))
}
func Detect() (devs []*RawDevice, err error) {
var rawdevices *C.LIBMTP_raw_device_t
var numrawdevices C.int
errno := MtpError(C.LIBMTP_Detect_Raw_Devices(&rawdevices, &numrawdevices))
if errno == C.LIBMTP_ERROR_NO_DEVICE_ATTACHED {
return nil, nil
}
if errno != C.LIBMTP_ERROR_NONE {
return nil, errno
}
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
}
func (e MtpError) Error() string {
switch e {
case C.LIBMTP_ERROR_CONNECTING:
return "error connecting"
case C.LIBMTP_ERROR_MEMORY_ALLOCATION:
return "memory allocation error"
case C.LIBMTP_ERROR_GENERAL:
return "general error."
}
return "unknown error"
}
////////////////
// Raw devices.
func (r *RawDevice) me() *C.LIBMTP_raw_device_t {
return (*C.LIBMTP_raw_device_t)(r)
}
func (r *RawDevice) Open() (*Device, error) {
dev := C.LIBMTP_Open_Raw_Device_Uncached(r.me())
if dev == nil {
return nil, fmt.Errorf("open: open returned nil")
}
return (*Device)(dev), nil
}
func (d *RawDevice) String() string {
vendor := "unknown"
if d.device_entry.vendor != nil {
vendor = C.GoString(d.device_entry.vendor)
}
product := "unknown"
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",
vendor, product,
d.device_entry.vendor_id,
d.device_entry.product_id,
d.bus_location,
d.devnum)
}
////////////////
// Device
func (d *Device) me() *C.LIBMTP_mtpdevice_t {
return (*C.LIBMTP_mtpdevice_t)(d)
}
func (d *Device) Release() {
C.LIBMTP_Release_Device(d.me())
}
func (d *Device) ErrorStack() error {
var messages []string
for p := C.LIBMTP_Get_Errorstack(d.me()); p != nil; p = p.next {
messages = append(messages, C.GoString(p.error_text))
}
if len(messages) == 0 {
return nil
}
C.LIBMTP_Clear_Errorstack(d.me())
return fmt.Errorf("%s", strings.Join(messages, "\n"))
}
func (d *Device) GetStorage(sortOrder int) error {
err := C.LIBMTP_Get_Storage(d.me(), C.int(sortOrder))
if err != 0 {
return d.ErrorStack()
}
return nil
}
func (d *Device) FilesAndFolders(storageId uint32, parentId uint32) (files []*File, err error) {
file := C.LIBMTP_Get_Files_And_Folders(d.me(), C.uint32_t(storageId), C.uint32_t(parentId))
for f := (*File)(file); f != nil; f = (*File)(f.next) {
files = append(files, f)
}
return files, d.ErrorStack()
}
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) {
folders = append(folders, f)
}
return folders
}
func (d *Device) ListStorage() (storages []*DeviceStorage) {
for p := d.storage; p != nil; p = p.next {
storages = append(storages, (*DeviceStorage)(p))
}
return
}
func (d *Device) Filemetadata(id uint32) (*File, error) {
f := (C.LIBMTP_Get_Filemetadata(d.me(), C.uint32_t(id)))
return (*File)(f), d.ErrorStack()
}
func (d *Device) GetFileToFileDescriptor(id uint32, fd uintptr) error {
code := C.LIBMTP_Get_File_To_File_Descriptor(d.me(), C.uint32_t(id), C.int(fd), nil, nil)
if code != 0 {
return d.ErrorStack()
}
return nil
}
func (d *Device) SendFromFileDescriptor(file *File, fd uintptr) error {
code := C.LIBMTP_Send_File_From_File_Descriptor(d.me(), C.int(fd), (*C.LIBMTP_file_t)(file), nil, nil)
if code != 0 {
return d.ErrorStack()
}
return nil
}
func (d *Device) ModelName() (name string) {
cname := C.LIBMTP_Get_Modelname(d.me())
name = C.GoString(cname)
C.free(unsafe.Pointer(cname))
return name
}
func (d *Device) CreateFolder(parent uint32, name string, storage uint32) (uint32, error) {
cname := C.CString(name)
id := C.LIBMTP_Create_Folder(d.me(), cname, C.uint32_t(parent), C.uint32_t(storage))
if newName := C.GoString(cname); newName != name {
log.Printf("Folder name changed to %q", newName)
}
C.free(unsafe.Pointer(cname))
return uint32(id), d.ErrorStack()
}
func (d *Device) SetFileName(f *File, name string) error {
C.LIBMTP_Set_File_Name(d.me(), f.me(), C.CString(name))
return d.ErrorStack()
}
func (d *Device) DeleteObject(id uint32) error {
c := C.LIBMTP_Delete_Object(d.me(), C.uint32_t(id))
if c != 0 {
return d.ErrorStack()
}
return nil
}
func (d *Device) GetStringFromObject(id uint32, prop int) (string, error) {
mId := C.uint32_t(id)
mProp := C.LIBMTP_property_t(prop)
cstr := C.LIBMTP_Get_String_From_Object(d.me(), mId, mProp)
result := C.GoString(cstr)
C.free(unsafe.Pointer(cstr))
return result, d.ErrorStack()
}
func (d *Device) GetIntFromObject(id uint32, prop int, bits int) (uint64, error) {
var result uint64
mId := C.uint32_t(id)
mProp := C.LIBMTP_property_t(prop)
switch bits {
case 64:
result = uint64(C.LIBMTP_Get_u64_From_Object(d.me(), mId, mProp, 0))
case 32:
result = uint64(C.LIBMTP_Get_u32_From_Object(d.me(), mId, mProp, 0))
case 16:
result = uint64(C.LIBMTP_Get_u16_From_Object(d.me(), mId, mProp, 0))
case 8:
result = uint64(C.LIBMTP_Get_u8_From_Object(d.me(), mId, mProp, 0))
default:
return 0, fmt.Errorf("unsupported bit size %d", bits)
}
return result, d.ErrorStack()
}
const DateModified = C.LIBMTP_PROPERTY_DateModified
////////////////
// DeviceStorage
func (d *DeviceStorage) IsHierarchical() bool {
return d.FilesystemType == 0x02
}
func (d *DeviceStorage) IsRemovable() bool {
return (d.StorageType == 0x02 || // PTP_ST_RemovableROM
d.StorageType == 0x04) // C.PTP_ST_RemovableRAM
}
func (d *DeviceStorage) me() *C.LIBMTP_devicestorage_t {
return (*C.LIBMTP_devicestorage_t)(d)
}
func (d *DeviceStorage) Id() uint32 {
return uint32(d.me().id)
}
func (d *DeviceStorage) Description() string {
return C.GoString(d.StorageDescription)
}
////////////////
// Files.
func NewFile(id uint32, parent uint32, storage_id uint32, filename string, size uint64,
mtime time.Time, fileType int) *File {
f := C.LIBMTP_new_file_t()
f.item_id = C.uint32_t(id)
f.parent_id = C.uint32_t(parent)
f.storage_id = C.uint32_t(storage_id)
f.filename = C.CString(filename)
f.filesize = C.uint64_t(size)
f.modificationdate = C.time_t(mtime.Unix())
f.filetype = C.LIBMTP_filetype_t(fileType)
return (*File)(f)
}
func (f *File) Destroy() {
C.LIBMTP_destroy_file_t(f.me())
}
func (f *File) me() *C.LIBMTP_file_t {
return (*C.LIBMTP_file_t)(f)
}
func (d *File) StorageId() uint32 {
return uint32(d.storage_id)
}
func (f *File) Mtime() time.Time {
return time.Unix(int64(f.modificationdate), 0)
}
func (f *File) SetMtime(t time.Time) {
f.modificationdate = C.time_t(t.Unix())
}
func (f *File) SetFilesize(sz uint64) {
f.filesize = C.uint64_t(sz)
}
func (d *File) Id() uint32 {
return uint32(d.item_id)
}
func (d *File) SetId(id uint32) {
d.item_id = C.uint32_t(id)
}
func (d *File) Filetype() int {
return int(d.filetype)
}
func (d *File) Name() string {
return C.GoString(d.filename)
}
func (f *File) SetName(n string) {
old := f.filename
C.free(unsafe.Pointer(old))
f.filename = C.CString(n)
}