Line 0
Link Here
|
|
|
1 |
/* GIO - GLib Input, Output and Streaming Library |
2 |
* MTP Backend |
3 |
* |
4 |
* Copyright (C) 2012 Philip Langdale <philipl@overt.org> |
5 |
* |
6 |
* This library is free software; you can redistribute it and/or |
7 |
* modify it under the terms of the GNU Lesser General Public |
8 |
* License as published by the Free Software Foundation; either |
9 |
* version 2 of the License, or (at your option) any later version. |
10 |
* |
11 |
* This library is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 |
* Lesser General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU Lesser General |
17 |
* Public License along with this library; if not, write to the |
18 |
* Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
19 |
* Boston, MA 02111-1307, USA. |
20 |
*/ |
21 |
|
22 |
|
23 |
#include <config.h> |
24 |
|
25 |
#include <sys/types.h> |
26 |
#include <sys/stat.h> |
27 |
#include <errno.h> |
28 |
#include <unistd.h> |
29 |
#include <fcntl.h> |
30 |
#include <string.h> |
31 |
#include <stdlib.h> |
32 |
|
33 |
#include <glib/gstdio.h> |
34 |
#include <glib/gi18n.h> |
35 |
#include <gio/gio.h> |
36 |
|
37 |
#include <libmtp.h> |
38 |
|
39 |
#include "gvfsbackendmtp.h" |
40 |
#include "gvfsicon.h" |
41 |
#include "gvfsjobopenforread.h" |
42 |
#include "gvfsjobread.h" |
43 |
#include "gvfsjobseekread.h" |
44 |
#include "gvfsjobopenforwrite.h" |
45 |
#include "gvfsjobwrite.h" |
46 |
#include "gvfsjobclosewrite.h" |
47 |
#include "gvfsjobseekwrite.h" |
48 |
#include "gvfsjobsetdisplayname.h" |
49 |
#include "gvfsjobqueryinfo.h" |
50 |
#include "gvfsjobdelete.h" |
51 |
#include "gvfsjobqueryfsinfo.h" |
52 |
#include "gvfsjobqueryattributes.h" |
53 |
#include "gvfsjobenumerate.h" |
54 |
#include "gvfsdaemonprotocol.h" |
55 |
#include "gvfsjobcreatemonitor.h" |
56 |
#include "gvfsjobmakedirectory.h" |
57 |
#include "gvfsmonitor.h" |
58 |
|
59 |
|
60 |
/* ------------------------------------------------------------------------------------------------- */ |
61 |
|
62 |
/* showing debug traces */ |
63 |
#define DEBUG_SHOW_TRACES 1 |
64 |
#define DEBUG_SHOW_ENUMERATE_TRACES 0 |
65 |
|
66 |
static void |
67 |
DEBUG (const gchar *message, ...) |
68 |
{ |
69 |
#if DEBUG_SHOW_TRACES |
70 |
va_list args; |
71 |
va_start (args, message); |
72 |
g_vfprintf (stderr, message, args); |
73 |
va_end (args); |
74 |
g_fprintf (stderr, "\n"); |
75 |
fflush (stderr); |
76 |
#endif |
77 |
} |
78 |
|
79 |
static void |
80 |
DEBUG_ENUMERATE (const gchar *message, ...) |
81 |
{ |
82 |
#if DEBUG_SHOW_ENUMERATE_TRACES |
83 |
va_list args; |
84 |
va_start (args, message); |
85 |
g_vfprintf (stderr, message, args); |
86 |
va_end (args); |
87 |
g_fprintf (stderr, "\n"); |
88 |
fflush (stderr); |
89 |
#endif |
90 |
} |
91 |
|
92 |
|
93 |
/************************************************ |
94 |
* Storage constants copied from ptp.h |
95 |
* |
96 |
* ptp.h is treated as a private header by libmtp |
97 |
************************************************/ |
98 |
|
99 |
/* PTP Storage Types */ |
100 |
|
101 |
#define PTP_ST_Undefined 0x0000 |
102 |
#define PTP_ST_FixedROM 0x0001 |
103 |
#define PTP_ST_RemovableROM 0x0002 |
104 |
#define PTP_ST_FixedRAM 0x0003 |
105 |
#define PTP_ST_RemovableRAM 0x0004 |
106 |
|
107 |
|
108 |
/************************************************ |
109 |
* Initialization |
110 |
************************************************/ |
111 |
|
112 |
G_DEFINE_TYPE (GVfsBackendMtp, g_vfs_backend_mtp, G_VFS_TYPE_BACKEND) |
113 |
|
114 |
static void |
115 |
g_vfs_backend_mtp_init (GVfsBackendMtp *backend) |
116 |
{ |
117 |
DEBUG ("(I) g_vfs_backend_mtp_init"); |
118 |
GMountSpec *mount_spec; |
119 |
|
120 |
g_mutex_init (&backend->mutex); |
121 |
g_vfs_backend_set_display_name (G_VFS_BACKEND (backend), "mtp"); |
122 |
g_vfs_backend_set_icon_name (G_VFS_BACKEND (backend), "multimedia-player"); |
123 |
|
124 |
mount_spec = g_mount_spec_new ("mtp"); |
125 |
g_vfs_backend_set_mount_spec (G_VFS_BACKEND (backend), mount_spec); |
126 |
g_mount_spec_unref (mount_spec); |
127 |
|
128 |
backend->monitors = g_hash_table_new (g_direct_hash, g_direct_equal); |
129 |
|
130 |
DEBUG ("(I) g_vfs_backend_mtp_init done."); |
131 |
} |
132 |
|
133 |
static void |
134 |
g_vfs_backend_mtp_finalize (GObject *object) |
135 |
{ |
136 |
GVfsBackendMtp *backend; |
137 |
|
138 |
DEBUG ("(I) g_vfs_backend_mtp_finalize"); |
139 |
|
140 |
backend = G_VFS_BACKEND_MTP (object); |
141 |
|
142 |
g_hash_table_unref (backend->monitors); |
143 |
g_mutex_clear (&backend->mutex); |
144 |
|
145 |
if (G_OBJECT_CLASS (g_vfs_backend_mtp_parent_class)->finalize) |
146 |
(*G_OBJECT_CLASS (g_vfs_backend_mtp_parent_class)->finalize) (object); |
147 |
|
148 |
DEBUG ("(I) g_vfs_backend_mtp_finalize done."); |
149 |
} |
150 |
|
151 |
|
152 |
/************************************************ |
153 |
* Monitors |
154 |
************************************************/ |
155 |
|
156 |
static void |
157 |
do_create_dir_monitor (GVfsBackend *backend, |
158 |
GVfsJobCreateMonitor *job, |
159 |
const char *filename, |
160 |
GFileMonitorFlags flags) |
161 |
{ |
162 |
GVfsBackendMtp *mtp_backend = G_VFS_BACKEND_MTP (backend); |
163 |
|
164 |
DEBUG ("(I) create_dir_monitor (%s)", filename); |
165 |
|
166 |
GVfsMonitor *vfs_monitor = g_vfs_monitor_new (backend); |
167 |
|
168 |
g_object_set_data_full (G_OBJECT (vfs_monitor), "gvfsbackendmtp:path", |
169 |
g_strdup (filename), g_free); |
170 |
|
171 |
g_vfs_job_create_monitor_set_monitor (job, vfs_monitor); |
172 |
g_hash_table_insert (mtp_backend->monitors, vfs_monitor, NULL); |
173 |
g_object_weak_ref (G_OBJECT (vfs_monitor), (GWeakNotify)g_hash_table_remove, mtp_backend->monitors); |
174 |
g_object_unref (vfs_monitor); |
175 |
g_vfs_job_succeeded (G_VFS_JOB (job)); |
176 |
|
177 |
DEBUG ("(I) create_dir_monitor done."); |
178 |
} |
179 |
|
180 |
|
181 |
static void |
182 |
do_create_file_monitor (GVfsBackend *backend, |
183 |
GVfsJobCreateMonitor *job, |
184 |
const char *filename, |
185 |
GFileMonitorFlags flags) |
186 |
{ |
187 |
GVfsBackendMtp *mtp_backend = G_VFS_BACKEND_MTP (backend); |
188 |
|
189 |
DEBUG ("(I) create_file_monitor (%s)", filename); |
190 |
|
191 |
GVfsMonitor *vfs_monitor = g_vfs_monitor_new (backend); |
192 |
|
193 |
g_object_set_data_full (G_OBJECT (vfs_monitor), "gvfsbackendmtp:path", |
194 |
g_strdup (filename), g_free); |
195 |
|
196 |
g_vfs_job_create_monitor_set_monitor (job, vfs_monitor); |
197 |
g_hash_table_insert (mtp_backend->monitors, vfs_monitor, NULL); |
198 |
g_object_weak_ref (G_OBJECT (vfs_monitor), (GWeakNotify)g_hash_table_remove, mtp_backend->monitors); |
199 |
g_object_unref (vfs_monitor); |
200 |
g_vfs_job_succeeded (G_VFS_JOB (job)); |
201 |
|
202 |
DEBUG ("(I) create_file_monitor done."); |
203 |
} |
204 |
|
205 |
static void |
206 |
emit_event_internal (GVfsMonitor *monitor, |
207 |
const char *path, |
208 |
GFileMonitorEvent event) |
209 |
{ |
210 |
DEBUG ("(III) emit_event_internal (%s, %d)", path, event); |
211 |
|
212 |
char *dir = g_dirname (path); |
213 |
const char *monitored_path = g_object_get_data (G_OBJECT (monitor), "gvfsbackendmtp:path"); |
214 |
if (g_strcmp0 (dir, monitored_path) == 0) { |
215 |
DEBUG ("(III) emit_event_internal: Event %d on directory %s for %s", event, dir, path); |
216 |
g_vfs_monitor_emit_event (monitor, event, path, NULL); |
217 |
} else if (g_strcmp0 (path, monitored_path) == 0) { |
218 |
DEBUG ("(III) emit_event_internal: Event %d on file %s", event, path); |
219 |
g_vfs_monitor_emit_event (monitor, event, path, NULL); |
220 |
} |
221 |
g_free (dir); |
222 |
|
223 |
DEBUG ("(III) emit_event_internal done."); |
224 |
} |
225 |
|
226 |
static void |
227 |
emit_create_event (gpointer key, |
228 |
gpointer value, |
229 |
gpointer user_data) |
230 |
{ |
231 |
DEBUG ("(II) emit_create_event."); |
232 |
emit_event_internal (key, user_data, G_FILE_MONITOR_EVENT_CREATED); |
233 |
} |
234 |
|
235 |
static void |
236 |
emit_delete_event (gpointer key, |
237 |
gpointer value, |
238 |
gpointer user_data) |
239 |
{ |
240 |
DEBUG ("(II) emit_delete_event."); |
241 |
emit_event_internal (key, user_data, G_FILE_MONITOR_EVENT_DELETED); |
242 |
} |
243 |
|
244 |
static void |
245 |
emit_change_event (gpointer key, |
246 |
gpointer value, |
247 |
gpointer user_data) |
248 |
{ |
249 |
DEBUG ("(II) emit_change_event."); |
250 |
emit_event_internal (key, user_data, G_FILE_MONITOR_EVENT_CHANGED); |
251 |
} |
252 |
|
253 |
|
254 |
/************************************************ |
255 |
* Errors |
256 |
************************************************/ |
257 |
|
258 |
static void |
259 |
fail_job (GVfsJob *job, LIBMTP_mtpdevice_t *device) |
260 |
{ |
261 |
LIBMTP_error_t *error = LIBMTP_Get_Errorstack (device); |
262 |
|
263 |
g_vfs_job_failed (job, G_IO_ERROR, |
264 |
g_vfs_job_is_cancelled (job) ? |
265 |
G_IO_ERROR_CANCELLED : |
266 |
G_IO_ERROR_FAILED, |
267 |
_("libmtp error: %s"), |
268 |
g_strrstr (error->error_text, ":") + 1); |
269 |
|
270 |
LIBMTP_Clear_Errorstack (device); |
271 |
} |
272 |
|
273 |
|
274 |
/************************************************ |
275 |
* Mounts |
276 |
************************************************/ |
277 |
|
278 |
static LIBMTP_mtpdevice_t * |
279 |
get_device (GVfsBackend *backend, const char *id, GVfsJob *job); |
280 |
|
281 |
|
282 |
static void |
283 |
on_uevent (GUdevClient *client, gchar *action, GUdevDevice *device, gpointer user_data) |
284 |
{ |
285 |
const char *dev_path = g_udev_device_get_device_file (device); |
286 |
DEBUG ("(I) on_uevent (action %s, device %s)", action, dev_path); |
287 |
|
288 |
if (dev_path == NULL) { |
289 |
return; |
290 |
} |
291 |
|
292 |
GVfsBackendMtp *op_backend = G_VFS_BACKEND_MTP (user_data); |
293 |
|
294 |
if (g_strcmp0 (op_backend->dev_path, dev_path) == 0 && |
295 |
g_str_equal (action, "remove")) { |
296 |
DEBUG ("(I) on_uevent: Quiting after remove event on device %s", dev_path); |
297 |
/* TODO: need a cleaner way to force unmount ourselves */ |
298 |
exit (1); |
299 |
} |
300 |
|
301 |
DEBUG ("(I) on_uevent done."); |
302 |
} |
303 |
|
304 |
#if HAVE_LIBMTP_READ_EVENT |
305 |
static gpointer |
306 |
check_event (gpointer user_data) |
307 |
{ |
308 |
GVfsBackendMtp *backend = user_data; |
309 |
|
310 |
LIBMTP_event_t event; |
311 |
int ret = 0; |
312 |
while (ret == 0) { |
313 |
uint32_t param1; |
314 |
char *path; |
315 |
ret = LIBMTP_Read_Event (backend->device, &event, ¶m1); |
316 |
switch (event) { |
317 |
case LIBMTP_EVENT_STORE_ADDED: |
318 |
path = g_strdup_printf ("/%u", param1); |
319 |
g_mutex_lock (&backend->mutex); |
320 |
g_hash_table_foreach (backend->monitors, emit_create_event, path); |
321 |
g_mutex_unlock (&backend->mutex); |
322 |
g_free (path); |
323 |
break; |
324 |
default: |
325 |
break; |
326 |
} |
327 |
} |
328 |
return NULL; |
329 |
} |
330 |
#endif |
331 |
|
332 |
static gboolean |
333 |
mtp_heartbeat (GVfsBackendMtp *backend) |
334 |
{ |
335 |
if (g_mutex_trylock (&backend->mutex)) { |
336 |
LIBMTP_Dump_Device_Info(backend->device); |
337 |
g_mutex_unlock (&backend->mutex); |
338 |
} |
339 |
return TRUE; |
340 |
} |
341 |
|
342 |
static void |
343 |
do_mount (GVfsBackend *backend, |
344 |
GVfsJobMount *job, |
345 |
GMountSpec *mount_spec, |
346 |
GMountSource *mount_source, |
347 |
gboolean is_automount) |
348 |
{ |
349 |
GVfsBackendMtp *op_backend = G_VFS_BACKEND_MTP (backend); |
350 |
|
351 |
DEBUG ("(I) do_mount"); |
352 |
|
353 |
const char *host = g_mount_spec_get (mount_spec, "host"); |
354 |
DEBUG ("(I) do_mount: host=%s", host); |
355 |
if (host == NULL) { |
356 |
GError *error; |
357 |
g_set_error_literal (&error, G_IO_ERROR, G_IO_ERROR_FAILED, _("No device specified")); |
358 |
g_vfs_job_failed_from_error (G_VFS_JOB (job), error); |
359 |
g_error_free (error); |
360 |
return; |
361 |
} |
362 |
|
363 |
const char *subsystems[] = {"usb", NULL}; |
364 |
op_backend->gudev_client = g_udev_client_new (subsystems); |
365 |
if (op_backend->gudev_client == NULL) { |
366 |
GError *error; |
367 |
g_set_error_literal (&error, G_IO_ERROR, G_IO_ERROR_FAILED, _("Cannot create gudev client")); |
368 |
g_vfs_job_failed_from_error (G_VFS_JOB (job), error); |
369 |
g_error_free (error); |
370 |
return; |
371 |
} |
372 |
g_signal_connect (op_backend->gudev_client, "uevent", G_CALLBACK (on_uevent), op_backend); |
373 |
|
374 |
/* turn usb:001,041 string into an udev device name */ |
375 |
if (!g_str_has_prefix (host, "[usb:")) { |
376 |
g_vfs_job_failed (G_VFS_JOB (job), |
377 |
G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, |
378 |
_("Unexpected host uri format.")); |
379 |
return; |
380 |
} |
381 |
|
382 |
char *comma; |
383 |
char *dev_path = g_strconcat ("/dev/bus/usb/", host + 5, NULL); |
384 |
if ((comma = strchr (dev_path, ',')) == NULL) { |
385 |
g_free (dev_path); |
386 |
g_vfs_job_failed (G_VFS_JOB (job), |
387 |
G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, |
388 |
_("Malformed host uri.")); |
389 |
return; |
390 |
} |
391 |
*comma = '/'; |
392 |
dev_path[strlen (dev_path) -1] = '\0'; |
393 |
DEBUG ("(I) do_mount: Parsed '%s' into device name %s", host, dev_path); |
394 |
|
395 |
/* find corresponding GUdevDevice */ |
396 |
if (!g_udev_client_query_by_device_file (op_backend->gudev_client, dev_path)) { |
397 |
g_free (dev_path); |
398 |
g_vfs_job_failed (G_VFS_JOB (job), |
399 |
G_IO_ERROR, G_IO_ERROR_NOT_FOUND, |
400 |
_("Couldn't find matching udev device.")); |
401 |
return; |
402 |
} |
403 |
|
404 |
op_backend->dev_path = dev_path; |
405 |
|
406 |
LIBMTP_Init (); |
407 |
|
408 |
get_device (backend, host, G_VFS_JOB (job)); |
409 |
if (!G_VFS_JOB (job)->failed) { |
410 |
GMountSpec *mtp_mount_spec = g_mount_spec_new ("mtp"); |
411 |
g_mount_spec_set (mtp_mount_spec, "host", host); |
412 |
g_vfs_backend_set_mount_spec (backend, mtp_mount_spec); |
413 |
g_mount_spec_unref (mtp_mount_spec); |
414 |
|
415 |
g_vfs_job_succeeded (G_VFS_JOB (job)); |
416 |
|
417 |
op_backend->hb_id = |
418 |
g_timeout_add_seconds (900, (GSourceFunc)mtp_heartbeat, op_backend); |
419 |
|
420 |
#if HAVE_LIBMTP_READ_EVENT |
421 |
g_thread_new ("events", check_event, backend); |
422 |
#endif |
423 |
} |
424 |
DEBUG ("(I) do_mount done."); |
425 |
} |
426 |
|
427 |
|
428 |
static void |
429 |
do_unmount (GVfsBackend *backend, GVfsJobUnmount *job, |
430 |
GMountUnmountFlags flags, |
431 |
GMountSource *mount_source) |
432 |
{ |
433 |
GVfsBackendMtp *op_backend; |
434 |
|
435 |
DEBUG ("(I) do_umount"); |
436 |
|
437 |
op_backend = G_VFS_BACKEND_MTP (backend); |
438 |
|
439 |
g_source_remove (op_backend->hb_id); |
440 |
g_object_unref (op_backend->gudev_client); |
441 |
g_free (op_backend->dev_path); |
442 |
|
443 |
g_mutex_lock (&G_VFS_BACKEND_MTP (backend)->mutex); |
444 |
LIBMTP_Release_Device (op_backend->device); |
445 |
g_mutex_unlock (&G_VFS_BACKEND_MTP (backend)->mutex); |
446 |
|
447 |
g_vfs_job_succeeded (G_VFS_JOB (job)); |
448 |
|
449 |
DEBUG ("(I) do_umount done."); |
450 |
} |
451 |
|
452 |
|
453 |
|
454 |
|
455 |
|
456 |
|
457 |
|
458 |
/************************************************ |
459 |
* Queries |
460 |
* |
461 |
*/ |
462 |
|
463 |
LIBMTP_mtpdevice_t * |
464 |
get_device (GVfsBackend *backend, const char *id, GVfsJob *job) { |
465 |
DEBUG ("(II) get_device: %s", id); |
466 |
|
467 |
LIBMTP_mtpdevice_t *device = NULL; |
468 |
|
469 |
if (G_VFS_BACKEND_MTP (backend)->device != NULL) { |
470 |
DEBUG ("(II) get_device: Returning cached device %p", device); |
471 |
return G_VFS_BACKEND_MTP (backend)->device; |
472 |
} |
473 |
|
474 |
LIBMTP_raw_device_t * rawdevices; |
475 |
int numrawdevices; |
476 |
LIBMTP_error_number_t err; |
477 |
|
478 |
err = LIBMTP_Detect_Raw_Devices (&rawdevices, &numrawdevices); |
479 |
switch (err) { |
480 |
case LIBMTP_ERROR_NONE: |
481 |
break; |
482 |
case LIBMTP_ERROR_NO_DEVICE_ATTACHED: |
483 |
g_vfs_job_failed (G_VFS_JOB (job), |
484 |
G_IO_ERROR, G_IO_ERROR_NOT_FOUND, |
485 |
_("No MTP devices found")); |
486 |
goto exit; |
487 |
case LIBMTP_ERROR_CONNECTING: |
488 |
g_vfs_job_failed (G_VFS_JOB (job), |
489 |
G_IO_ERROR, G_IO_ERROR_CONNECTION_REFUSED, |
490 |
_("Unable to connect to MTP device")); |
491 |
goto exit; |
492 |
case LIBMTP_ERROR_MEMORY_ALLOCATION: |
493 |
g_vfs_job_failed (G_VFS_JOB (job), |
494 |
G_FILE_ERROR, G_FILE_ERROR_NOMEM, |
495 |
_("Unable to allocate memory while detecting MTP devices")); |
496 |
goto exit; |
497 |
case LIBMTP_ERROR_GENERAL: |
498 |
default: |
499 |
g_vfs_job_failed (G_VFS_JOB (job), |
500 |
G_IO_ERROR, G_IO_ERROR_FAILED, |
501 |
_("Generic libmtp error")); |
502 |
goto exit; |
503 |
} |
504 |
|
505 |
/* Iterate over connected MTP devices */ |
506 |
int i; |
507 |
for (i = 0; i < numrawdevices; i++) { |
508 |
char *name; |
509 |
name = g_strdup_printf ("[usb:%03u,%03u]", |
510 |
rawdevices[i].bus_location, |
511 |
rawdevices[i].devnum); |
512 |
|
513 |
if (strcmp (id, name) == 0) { |
514 |
device = LIBMTP_Open_Raw_Device_Uncached (&rawdevices[i]); |
515 |
if (device == NULL) { |
516 |
g_vfs_job_failed (G_VFS_JOB (job), |
517 |
G_IO_ERROR, G_IO_ERROR_FAILED, |
518 |
_("Unable to open MTP device '%s'"), name); |
519 |
g_free (name); |
520 |
goto exit; |
521 |
} |
522 |
|
523 |
DEBUG ("(II) get_device: Storing device %s", name); |
524 |
G_VFS_BACKEND_MTP (backend)->device = device; |
525 |
|
526 |
LIBMTP_Dump_Errorstack (device); |
527 |
LIBMTP_Clear_Errorstack (device); |
528 |
break; |
529 |
} else { |
530 |
g_free (name); |
531 |
} |
532 |
} |
533 |
|
534 |
exit: |
535 |
DEBUG ("(II) get_device done."); |
536 |
return device; |
537 |
} |
538 |
|
539 |
static void |
540 |
get_device_info (GVfsBackendMtp *backend, GFileInfo *info) |
541 |
{ |
542 |
LIBMTP_mtpdevice_t *device = backend->device; |
543 |
const char *name; |
544 |
|
545 |
name = g_mount_spec_get (g_vfs_backend_get_mount_spec (G_VFS_BACKEND (backend)), "host"); |
546 |
|
547 |
DEBUG_ENUMERATE ("(II) get_device_info: %s", name); |
548 |
|
549 |
g_file_info_set_file_type (info, G_FILE_TYPE_DIRECTORY); |
550 |
g_file_info_set_name (info, name); |
551 |
|
552 |
char *friendlyname = LIBMTP_Get_Friendlyname (device); |
553 |
g_file_info_set_display_name (info, friendlyname == NULL ? |
554 |
_("Unnamed Device") : friendlyname); |
555 |
free (friendlyname); |
556 |
|
557 |
g_file_info_set_file_type (info, G_FILE_TYPE_DIRECTORY); |
558 |
g_file_info_set_content_type (info, "inode/directory"); |
559 |
g_file_info_set_size (info, 0); |
560 |
|
561 |
GIcon *icon = g_themed_icon_new ("multimedia-player"); |
562 |
g_file_info_set_icon (info, icon); |
563 |
g_object_unref (icon); |
564 |
|
565 |
g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_READ, TRUE); |
566 |
g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE, TRUE); |
567 |
g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_DELETE, FALSE); |
568 |
g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE, TRUE); |
569 |
g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_TRASH, FALSE); |
570 |
g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_RENAME, TRUE); |
571 |
|
572 |
g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_FILESYSTEM_TYPE, "mtpfs"); |
573 |
|
574 |
int ret = LIBMTP_Get_Storage (device, LIBMTP_STORAGE_SORTBY_NOTSORTED); |
575 |
if (ret != 0) { |
576 |
LIBMTP_Dump_Errorstack (device); |
577 |
LIBMTP_Clear_Errorstack (device); |
578 |
DEBUG_ENUMERATE ("(II) get_device_info done with no stores."); |
579 |
return; |
580 |
} |
581 |
guint64 freeSpace = 0; |
582 |
guint64 maxSpace = 0; |
583 |
LIBMTP_devicestorage_t *storage; |
584 |
for (storage = device->storage; storage != 0; storage = storage->next) { |
585 |
freeSpace += storage->FreeSpaceInBytes; |
586 |
maxSpace += storage->MaxCapacity; |
587 |
} |
588 |
|
589 |
g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_FREE, freeSpace); |
590 |
g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_SIZE, maxSpace); |
591 |
|
592 |
DEBUG_ENUMERATE ("(II) get_device_info done."); |
593 |
} |
594 |
|
595 |
static void |
596 |
get_storage_info (LIBMTP_devicestorage_t *storage, GFileInfo *info) { |
597 |
|
598 |
char *id = g_strdup_printf ("%u", storage->id); |
599 |
g_file_info_set_name (info, id); |
600 |
g_free (id); |
601 |
|
602 |
DEBUG_ENUMERATE ("(II) get_storage_info: %s", storage->id); |
603 |
|
604 |
g_file_info_set_display_name (info, storage->StorageDescription); |
605 |
g_file_info_set_file_type (info, G_FILE_TYPE_DIRECTORY); |
606 |
g_file_info_set_content_type (info, "inode/directory"); |
607 |
g_file_info_set_size (info, 0); |
608 |
|
609 |
GIcon *icon; |
610 |
switch (storage->StorageType) { |
611 |
case PTP_ST_FixedROM: |
612 |
g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY, TRUE); |
613 |
icon = g_themed_icon_new_with_default_fallbacks ("drive-harddisk"); |
614 |
break; |
615 |
case PTP_ST_RemovableROM: |
616 |
g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY, TRUE); |
617 |
icon = g_themed_icon_new_with_default_fallbacks ("media-memory-sd"); |
618 |
break; |
619 |
case PTP_ST_RemovableRAM: |
620 |
g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY, FALSE); |
621 |
icon = g_themed_icon_new_with_default_fallbacks ("media-memory-sd"); |
622 |
break; |
623 |
case PTP_ST_FixedRAM: |
624 |
default: |
625 |
g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY, FALSE); |
626 |
icon = g_themed_icon_new_with_default_fallbacks ("drive-harddisk"); |
627 |
break; |
628 |
} |
629 |
g_file_info_set_icon (info, icon); |
630 |
g_object_unref (icon); |
631 |
|
632 |
g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_READ, TRUE); |
633 |
g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE, TRUE); |
634 |
g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_DELETE, FALSE); |
635 |
g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE, TRUE); |
636 |
g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_TRASH, FALSE); |
637 |
g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_RENAME, FALSE); |
638 |
|
639 |
g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_FREE, storage->FreeSpaceInBytes); |
640 |
g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_SIZE, storage->MaxCapacity); |
641 |
g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_FILESYSTEM_TYPE, "mtpfs"); |
642 |
|
643 |
DEBUG_ENUMERATE ("(II) get_storage_info done."); |
644 |
} |
645 |
|
646 |
static void |
647 |
get_file_info (GVfsBackend *backend, |
648 |
LIBMTP_mtpdevice_t *device, |
649 |
GFileInfo *info, |
650 |
LIBMTP_file_t *file) { |
651 |
GIcon *icon = NULL; |
652 |
char *content_type = NULL; |
653 |
|
654 |
char *id = g_strdup_printf ("%u", file->item_id); |
655 |
g_file_info_set_name (info, id); |
656 |
g_free (id); |
657 |
|
658 |
DEBUG_ENUMERATE ("(II) get_file_info: %u", file->item_id); |
659 |
|
660 |
g_file_info_set_display_name (info, file->filename); |
661 |
|
662 |
switch (file->filetype) { |
663 |
case LIBMTP_FILETYPE_FOLDER: |
664 |
g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE, TRUE); |
665 |
g_file_info_set_file_type (info, G_FILE_TYPE_DIRECTORY); |
666 |
g_file_info_set_content_type (info, "inode/directory"); |
667 |
icon = g_themed_icon_new ("folder"); |
668 |
break; |
669 |
default: |
670 |
g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE, FALSE); |
671 |
g_file_info_set_file_type (info, G_FILE_TYPE_REGULAR); |
672 |
content_type = g_content_type_guess (file->filename, NULL, 0, NULL); |
673 |
g_file_info_set_content_type (info, content_type); |
674 |
icon = g_content_type_get_icon (content_type); |
675 |
break; |
676 |
} |
677 |
|
678 |
|
679 |
#if HAVE_LIBMTP_GET_THUMBNAIL |
680 |
if (LIBMTP_FILETYPE_IS_IMAGE (file->filetype) || |
681 |
LIBMTP_FILETYPE_IS_VIDEO (file->filetype) || |
682 |
LIBMTP_FILETYPE_IS_AUDIOVIDEO (file->filetype)) { |
683 |
|
684 |
char *icon_id; |
685 |
GIcon *icon; |
686 |
GMountSpec *mount_spec; |
687 |
|
688 |
mount_spec = g_vfs_backend_get_mount_spec (backend); |
689 |
icon_id = g_strdup_printf ("%u", file->item_id); |
690 |
icon = g_vfs_icon_new (mount_spec, |
691 |
icon_id); |
692 |
g_file_info_set_attribute_object (info, |
693 |
G_FILE_ATTRIBUTE_PREVIEW_ICON, |
694 |
G_OBJECT (icon)); |
695 |
g_object_unref (icon); |
696 |
g_free (icon_id); |
697 |
} |
698 |
#endif |
699 |
|
700 |
g_file_info_set_size (info, file->filesize); |
701 |
|
702 |
GTimeVal modtime = { file->modificationdate, 0 }; |
703 |
g_file_info_set_modification_time (info, &modtime); |
704 |
|
705 |
g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_READ, TRUE); |
706 |
g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE, TRUE); |
707 |
g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_DELETE, TRUE); |
708 |
g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_TRASH, FALSE); |
709 |
g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_RENAME, TRUE); |
710 |
g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_COPY_NAME, file->filename); |
711 |
|
712 |
|
713 |
if (icon != NULL) { |
714 |
g_file_info_set_icon (info, icon); |
715 |
g_object_unref (icon); |
716 |
} |
717 |
g_free (content_type); |
718 |
|
719 |
DEBUG_ENUMERATE ("(II) get_file_info done."); |
720 |
} |
721 |
|
722 |
|
723 |
static void |
724 |
do_enumerate (GVfsBackend *backend, |
725 |
GVfsJobEnumerate *job, |
726 |
const char *filename, |
727 |
GFileAttributeMatcher *attribute_matcher, |
728 |
GFileQueryInfoFlags flags) |
729 |
{ |
730 |
GVfsBackendMtp *op_backend = G_VFS_BACKEND_MTP (backend); |
731 |
GFileInfo *info; |
732 |
|
733 |
gchar **elements = g_strsplit_set (filename, "/", -1); |
734 |
unsigned int ne = 0; |
735 |
for (ne = 0; elements[ne] != NULL; ne++); |
736 |
|
737 |
DEBUG ("(I) do_enumerate (filename = %s, n_elements = %d) ", filename, ne); |
738 |
|
739 |
g_mutex_lock (&G_VFS_BACKEND_MTP (backend)->mutex); |
740 |
|
741 |
LIBMTP_mtpdevice_t *device; |
742 |
device = op_backend->device; |
743 |
|
744 |
if (ne == 2 && elements[1][0] == '\0') { |
745 |
LIBMTP_devicestorage_t *storage; |
746 |
|
747 |
int ret = LIBMTP_Get_Storage (device, LIBMTP_STORAGE_SORTBY_NOTSORTED); |
748 |
if (ret != 0) { |
749 |
LIBMTP_Dump_Errorstack (device); |
750 |
LIBMTP_Clear_Errorstack (device); |
751 |
goto success; |
752 |
} |
753 |
for (storage = device->storage; storage != 0; storage = storage->next) { |
754 |
info = g_file_info_new (); |
755 |
get_storage_info (storage, info); |
756 |
g_vfs_job_enumerate_add_info (job, info); |
757 |
g_object_unref (info); |
758 |
} |
759 |
} else { |
760 |
LIBMTP_file_t *files; |
761 |
|
762 |
int pid = (ne == 2 ? -1 : strtol (elements[ne-1], NULL, 10)); |
763 |
|
764 |
LIBMTP_Clear_Errorstack (device); |
765 |
files = LIBMTP_Get_Files_And_Folders (device, strtol (elements[1], NULL, 10), pid); |
766 |
if (files == NULL && LIBMTP_Get_Errorstack (device) != NULL) { |
767 |
fail_job (G_VFS_JOB (job), device); |
768 |
goto exit; |
769 |
} |
770 |
while (files != NULL) { |
771 |
LIBMTP_file_t *file = files; |
772 |
files = files->next; |
773 |
|
774 |
info = g_file_info_new (); |
775 |
get_file_info (backend, device, info, file); |
776 |
g_vfs_job_enumerate_add_info (job, info); |
777 |
g_object_unref (info); |
778 |
|
779 |
LIBMTP_destroy_file_t (file); |
780 |
} |
781 |
} |
782 |
|
783 |
success: |
784 |
g_vfs_job_enumerate_done (job); |
785 |
g_vfs_job_succeeded (G_VFS_JOB (job)); |
786 |
|
787 |
exit: |
788 |
g_strfreev (elements); |
789 |
g_mutex_unlock (&G_VFS_BACKEND_MTP (backend)->mutex); |
790 |
DEBUG ("(I) do_enumerate done."); |
791 |
} |
792 |
|
793 |
static void |
794 |
do_query_info (GVfsBackend *backend, |
795 |
GVfsJobQueryInfo *job, |
796 |
const char *filename, |
797 |
GFileQueryInfoFlags flags, |
798 |
GFileInfo *info, |
799 |
GFileAttributeMatcher *matcher) |
800 |
{ |
801 |
DEBUG ("(I) do_query_info (filename = %s) ", filename); |
802 |
g_mutex_lock (&G_VFS_BACKEND_MTP (backend)->mutex); |
803 |
|
804 |
gchar **elements = g_strsplit_set (filename, "/", -1); |
805 |
unsigned int ne = 0; |
806 |
for (ne = 0; elements[ne] != NULL; ne++); |
807 |
|
808 |
LIBMTP_mtpdevice_t *device; |
809 |
device = G_VFS_BACKEND_MTP (backend)->device; |
810 |
|
811 |
if (ne == 2 && elements[1][0] == '\0') { |
812 |
get_device_info (G_VFS_BACKEND_MTP (backend), info); |
813 |
} else if (ne < 3) { |
814 |
LIBMTP_devicestorage_t *storage; |
815 |
int ret = LIBMTP_Get_Storage (device, LIBMTP_STORAGE_SORTBY_NOTSORTED); |
816 |
if (ret != 0) { |
817 |
LIBMTP_Dump_Errorstack (device); |
818 |
LIBMTP_Clear_Errorstack (device); |
819 |
g_vfs_job_failed (G_VFS_JOB (job), |
820 |
G_IO_ERROR, G_IO_ERROR_NOT_FOUND, |
821 |
_("No storage volumes found")); |
822 |
goto exit; |
823 |
} |
824 |
for (storage = device->storage; storage != 0; storage = storage->next) { |
825 |
if (storage->id == strtol (elements[ne-1], NULL, 10)) { |
826 |
DEBUG ("(III) found storage %u", storage->id); |
827 |
get_storage_info (storage, info); |
828 |
} |
829 |
} |
830 |
} else { |
831 |
LIBMTP_file_t *file = NULL; |
832 |
char *endptr; |
833 |
if (strtol (elements[ne-1], &endptr, 10) == 0 || |
834 |
*endptr != '\0') { |
835 |
DEBUG ("(II) try get files and folders"); |
836 |
int parent_id = -1; |
837 |
if (ne > 3) { |
838 |
parent_id = strtol (elements[ne-2], NULL, 10); |
839 |
} |
840 |
LIBMTP_file_t *i = LIBMTP_Get_Files_And_Folders (device, strtol (elements[1], NULL, 10), |
841 |
parent_id); |
842 |
while (i != NULL) { |
843 |
DEBUG ("(II) backup query (entity = %s, name = %s) ", i->filename, elements[ne-1]); |
844 |
if (strcmp (i->filename, elements[ne-1]) == 0) { |
845 |
file = i; |
846 |
i = i->next; |
847 |
break; |
848 |
} else { |
849 |
LIBMTP_file_t *tmp = i; |
850 |
i = i->next; |
851 |
LIBMTP_destroy_file_t (tmp); |
852 |
} |
853 |
} |
854 |
while (i != NULL) { |
855 |
LIBMTP_file_t *tmp = i; |
856 |
i = i->next; |
857 |
LIBMTP_destroy_file_t (tmp); |
858 |
} |
859 |
} else { |
860 |
file = LIBMTP_Get_Filemetadata (device, strtol (elements[ne-1], NULL, 10)); |
861 |
} |
862 |
|
863 |
if (file != NULL) { |
864 |
get_file_info (backend, device, info, file); |
865 |
LIBMTP_destroy_file_t (file); |
866 |
} else { |
867 |
fail_job (G_VFS_JOB (job), device); |
868 |
goto exit; |
869 |
} |
870 |
} |
871 |
|
872 |
g_vfs_job_succeeded (G_VFS_JOB (job)); |
873 |
|
874 |
exit: |
875 |
g_strfreev (elements); |
876 |
g_mutex_unlock (&G_VFS_BACKEND_MTP (backend)->mutex); |
877 |
DEBUG ("(I) do_query_info done."); |
878 |
} |
879 |
|
880 |
|
881 |
static void |
882 |
do_query_fs_info (GVfsBackend *backend, |
883 |
GVfsJobQueryFsInfo *job, |
884 |
const char *filename, |
885 |
GFileInfo *info, |
886 |
GFileAttributeMatcher *attribute_matcher) |
887 |
{ |
888 |
DEBUG ("(I) do_query_fs_info (filename = %s) ", filename); |
889 |
g_mutex_lock (&G_VFS_BACKEND_MTP (backend)->mutex); |
890 |
|
891 |
gchar **elements = g_strsplit_set (filename, "/", -1); |
892 |
unsigned int ne = 0; |
893 |
for (ne = 0; elements[ne] != NULL; ne++); |
894 |
|
895 |
LIBMTP_mtpdevice_t *device; |
896 |
device = G_VFS_BACKEND_MTP (backend)->device; |
897 |
|
898 |
if (ne == 2 && elements[1][0] == '\0') { |
899 |
get_device_info (G_VFS_BACKEND_MTP (backend), info); |
900 |
} else { |
901 |
LIBMTP_devicestorage_t *storage; |
902 |
int ret = LIBMTP_Get_Storage (device, LIBMTP_STORAGE_SORTBY_NOTSORTED); |
903 |
if (ret != 0) { |
904 |
LIBMTP_Dump_Errorstack (device); |
905 |
LIBMTP_Clear_Errorstack (device); |
906 |
g_vfs_job_failed (G_VFS_JOB (job), |
907 |
G_IO_ERROR, G_IO_ERROR_NOT_FOUND, |
908 |
_("No storage volumes found")); |
909 |
goto exit; |
910 |
} |
911 |
for (storage = device->storage; storage != 0; storage = storage->next) { |
912 |
if (storage->id == strtol (elements[1], NULL, 10)) { |
913 |
get_storage_info (storage, info); |
914 |
} |
915 |
} |
916 |
} |
917 |
|
918 |
g_vfs_job_succeeded (G_VFS_JOB (job)); |
919 |
|
920 |
exit: |
921 |
g_strfreev (elements); |
922 |
g_mutex_unlock (&G_VFS_BACKEND_MTP (backend)->mutex); |
923 |
|
924 |
DEBUG ("(I) do_query_fs_info done."); |
925 |
} |
926 |
|
927 |
|
928 |
/************************************************ |
929 |
* Operations |
930 |
* |
931 |
*/ |
932 |
|
933 |
typedef struct { |
934 |
GFileProgressCallback progress_callback; |
935 |
gpointer progress_callback_data; |
936 |
GVfsJob *job; |
937 |
} MtpProgressData; |
938 |
|
939 |
|
940 |
static int mtp_progress (uint64_t const sent, uint64_t const total, |
941 |
MtpProgressData const * const data) |
942 |
{ |
943 |
if (data->progress_callback) { |
944 |
data->progress_callback (sent, total, data->progress_callback_data); |
945 |
} |
946 |
return g_vfs_job_is_cancelled (data->job); |
947 |
} |
948 |
|
949 |
static void |
950 |
do_make_directory (GVfsBackend *backend, |
951 |
GVfsJobMakeDirectory *job, |
952 |
const char *filename) |
953 |
{ |
954 |
DEBUG ("(I) do_make_directory (filename = %s) ", filename); |
955 |
g_mutex_lock (&G_VFS_BACKEND_MTP (backend)->mutex); |
956 |
|
957 |
gchar **elements = g_strsplit_set (filename, "/", -1); |
958 |
unsigned int ne = 0; |
959 |
for (ne = 0; elements[ne] != NULL; ne++); |
960 |
|
961 |
if (ne < 3) { |
962 |
g_vfs_job_failed (G_VFS_JOB (job), |
963 |
G_IO_ERROR, G_IO_ERROR_FAILED, |
964 |
_("Cannot make directory in this location")); |
965 |
goto exit; |
966 |
} |
967 |
|
968 |
LIBMTP_mtpdevice_t *device; |
969 |
device = G_VFS_BACKEND_MTP (backend)->device; |
970 |
|
971 |
int parent_id = 0; |
972 |
if (ne > 3) { |
973 |
parent_id = strtol (elements[ne-2], NULL, 10); |
974 |
} |
975 |
|
976 |
int ret = LIBMTP_Create_Folder (device, elements[ne-1], parent_id, strtol (elements[1], NULL, 10)); |
977 |
if (ret == 0) { |
978 |
fail_job (G_VFS_JOB (job), device); |
979 |
goto exit; |
980 |
} |
981 |
|
982 |
g_vfs_job_succeeded (G_VFS_JOB (job)); |
983 |
|
984 |
g_hash_table_foreach (G_VFS_BACKEND_MTP (backend)->monitors, emit_create_event, (char *)filename); |
985 |
|
986 |
exit: |
987 |
g_strfreev (elements); |
988 |
g_mutex_unlock (&G_VFS_BACKEND_MTP (backend)->mutex); |
989 |
|
990 |
DEBUG ("(I) do_make_directory done."); |
991 |
} |
992 |
|
993 |
|
994 |
static void |
995 |
do_pull (GVfsBackend *backend, |
996 |
GVfsJobPull *job, |
997 |
const char *source, |
998 |
const char *local_path, |
999 |
GFileCopyFlags flags, |
1000 |
gboolean remove_source, |
1001 |
GFileProgressCallback progress_callback, |
1002 |
gpointer progress_callback_data) |
1003 |
{ |
1004 |
DEBUG ("(I) do_pull (filename = %s, local_path = %s) ", source, local_path); |
1005 |
g_mutex_lock (&G_VFS_BACKEND_MTP (backend)->mutex); |
1006 |
|
1007 |
GFileInfo *info = NULL; |
1008 |
gchar **elements = g_strsplit_set (source, "/", -1); |
1009 |
unsigned int ne = 0; |
1010 |
for (ne = 0; elements[ne] != NULL; ne++); |
1011 |
|
1012 |
if (ne < 3) { |
1013 |
g_vfs_job_failed (G_VFS_JOB (job), |
1014 |
G_IO_ERROR, G_IO_ERROR_NOT_REGULAR_FILE, |
1015 |
_("Not a regular file")); |
1016 |
goto exit; |
1017 |
} |
1018 |
|
1019 |
LIBMTP_mtpdevice_t *device; |
1020 |
device = G_VFS_BACKEND_MTP (backend)->device; |
1021 |
|
1022 |
LIBMTP_file_t *file = LIBMTP_Get_Filemetadata (device, strtol (elements[ne-1], NULL, 10)); |
1023 |
if (file == NULL) { |
1024 |
g_vfs_job_failed (G_VFS_JOB (job), |
1025 |
G_IO_ERROR, G_IO_ERROR_NOT_FOUND, |
1026 |
_("File does not exist")); |
1027 |
goto exit; |
1028 |
} |
1029 |
|
1030 |
info = g_file_info_new (); |
1031 |
get_file_info (backend, device, info, file); |
1032 |
LIBMTP_destroy_file_t (file); |
1033 |
file = NULL; |
1034 |
if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY) { |
1035 |
GError *error; |
1036 |
GFile *file = g_file_new_for_path (local_path); |
1037 |
g_assert (file != NULL); |
1038 |
if (file) { |
1039 |
error = NULL; |
1040 |
if (g_file_make_directory (file, G_VFS_JOB (job)->cancellable, &error)) { |
1041 |
g_vfs_job_succeeded (G_VFS_JOB (job)); |
1042 |
} else if (error->code == G_IO_ERROR_EXISTS) { |
1043 |
g_vfs_job_succeeded (G_VFS_JOB (job)); |
1044 |
g_error_free (error); |
1045 |
} else { |
1046 |
g_vfs_job_failed_from_error (G_VFS_JOB (job), error); |
1047 |
DEBUG ("(II) pull dir failed: %s", error->message); |
1048 |
g_error_free (error); |
1049 |
} |
1050 |
g_object_unref (file); |
1051 |
} |
1052 |
} else { |
1053 |
MtpProgressData *mtp_progress_data = g_new0 (MtpProgressData, 1); |
1054 |
mtp_progress_data->progress_callback = progress_callback; |
1055 |
mtp_progress_data->progress_callback_data = progress_callback_data; |
1056 |
mtp_progress_data->job = G_VFS_JOB (job); |
1057 |
int ret = LIBMTP_Get_File_To_File (device, |
1058 |
strtol (elements[ne-1], NULL, 10), |
1059 |
local_path, |
1060 |
(LIBMTP_progressfunc_t)mtp_progress, |
1061 |
mtp_progress_data); |
1062 |
g_free (mtp_progress_data); |
1063 |
if (ret != 0) { |
1064 |
fail_job (G_VFS_JOB (job), device); |
1065 |
goto exit; |
1066 |
} |
1067 |
g_vfs_job_succeeded (G_VFS_JOB (job)); |
1068 |
} |
1069 |
|
1070 |
exit: |
1071 |
if (info != NULL) { |
1072 |
g_object_unref (info); |
1073 |
} |
1074 |
g_strfreev (elements); |
1075 |
g_mutex_unlock (&G_VFS_BACKEND_MTP (backend)->mutex); |
1076 |
|
1077 |
DEBUG ("(I) do_pull done."); |
1078 |
} |
1079 |
|
1080 |
|
1081 |
static void |
1082 |
do_push (GVfsBackend *backend, |
1083 |
GVfsJobPush *job, |
1084 |
const char *destination, |
1085 |
const char *local_path, |
1086 |
GFileCopyFlags flags, |
1087 |
gboolean remove_source, |
1088 |
GFileProgressCallback progress_callback, |
1089 |
gpointer progress_callback_data) |
1090 |
{ |
1091 |
DEBUG ("(I) do_push (filename = %s, local_path = %s) ", destination, local_path); |
1092 |
g_mutex_lock (&G_VFS_BACKEND_MTP (backend)->mutex); |
1093 |
|
1094 |
GFile *file = NULL; |
1095 |
GFileInfo *info = NULL; |
1096 |
gchar **elements = g_strsplit_set (destination, "/", -1); |
1097 |
unsigned int ne = 0; |
1098 |
for (ne = 0; elements[ne] != NULL; ne++); |
1099 |
|
1100 |
if (ne < 3) { |
1101 |
g_vfs_job_failed (G_VFS_JOB (job), |
1102 |
G_IO_ERROR, G_IO_ERROR_NOT_REGULAR_FILE, |
1103 |
_("Cannot write to this location")); |
1104 |
goto exit; |
1105 |
} |
1106 |
|
1107 |
LIBMTP_mtpdevice_t *device; |
1108 |
device = G_VFS_BACKEND_MTP (backend)->device; |
1109 |
|
1110 |
int parent_id = 0; |
1111 |
|
1112 |
if (ne > 3) { |
1113 |
parent_id = strtol (elements[ne-2], NULL, 10); |
1114 |
} |
1115 |
|
1116 |
file = g_file_new_for_path (local_path); |
1117 |
if (!file) { |
1118 |
g_vfs_job_failed (G_VFS_JOB (job), |
1119 |
G_IO_ERROR, G_IO_ERROR_NOT_FOUND, |
1120 |
_("File not found")); |
1121 |
goto exit; |
1122 |
} |
1123 |
|
1124 |
if (g_file_query_file_type (file, G_FILE_QUERY_INFO_NONE, |
1125 |
G_VFS_JOB (job)->cancellable) == |
1126 |
G_FILE_TYPE_DIRECTORY) { |
1127 |
/* |
1128 |
* It happens to be the case that we can reuse do_make_directory |
1129 |
* here. |
1130 |
*/ |
1131 |
return do_make_directory (backend, G_VFS_JOB_MAKE_DIRECTORY (job), |
1132 |
elements[ne-1]); |
1133 |
} |
1134 |
|
1135 |
GError *error = NULL; |
1136 |
info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_SIZE, |
1137 |
G_FILE_QUERY_INFO_NONE, |
1138 |
G_VFS_JOB (job)->cancellable, |
1139 |
&error); |
1140 |
if (!info) { |
1141 |
g_vfs_job_failed_from_error (G_VFS_JOB (job), error); |
1142 |
g_error_free (error); |
1143 |
goto exit; |
1144 |
} |
1145 |
|
1146 |
LIBMTP_file_t *mtpfile = LIBMTP_new_file_t (); |
1147 |
mtpfile->filename = strdup (elements[ne-1]); |
1148 |
mtpfile->parent_id = parent_id; |
1149 |
mtpfile->storage_id = strtol (elements[1], NULL, 10); |
1150 |
mtpfile->filetype = LIBMTP_FILETYPE_UNKNOWN; |
1151 |
mtpfile->filesize = g_file_info_get_size (info); |
1152 |
|
1153 |
MtpProgressData *mtp_progress_data = g_new0 (MtpProgressData, 1); |
1154 |
mtp_progress_data->progress_callback = progress_callback; |
1155 |
mtp_progress_data->progress_callback_data = progress_callback_data; |
1156 |
mtp_progress_data->job = G_VFS_JOB (job); |
1157 |
int ret = LIBMTP_Send_File_From_File (device, local_path, mtpfile, |
1158 |
(LIBMTP_progressfunc_t)mtp_progress, |
1159 |
mtp_progress_data); |
1160 |
g_free (mtp_progress_data); |
1161 |
LIBMTP_destroy_file_t (mtpfile); |
1162 |
if (ret != 0) { |
1163 |
fail_job (G_VFS_JOB (job), device); |
1164 |
goto exit; |
1165 |
} |
1166 |
|
1167 |
g_vfs_job_succeeded (G_VFS_JOB (job)); |
1168 |
|
1169 |
g_hash_table_foreach (G_VFS_BACKEND_MTP (backend)->monitors, |
1170 |
emit_create_event, |
1171 |
(char *)destination); |
1172 |
|
1173 |
exit: |
1174 |
if (file) { |
1175 |
g_object_unref (file); |
1176 |
} |
1177 |
if (info) { |
1178 |
g_object_unref (info); |
1179 |
} |
1180 |
g_strfreev (elements); |
1181 |
g_mutex_unlock (&G_VFS_BACKEND_MTP (backend)->mutex); |
1182 |
|
1183 |
DEBUG ("(I) do_push done."); |
1184 |
} |
1185 |
|
1186 |
|
1187 |
static void |
1188 |
do_delete (GVfsBackend *backend, |
1189 |
GVfsJobDelete *job, |
1190 |
const char *filename) |
1191 |
{ |
1192 |
DEBUG ("(I) do_delete (filename = %s) ", filename); |
1193 |
g_mutex_lock (&G_VFS_BACKEND_MTP (backend)->mutex); |
1194 |
|
1195 |
gchar **elements = g_strsplit_set (filename, "/", -1); |
1196 |
unsigned int ne = 0; |
1197 |
for (ne = 0; elements[ne] != NULL; ne++); |
1198 |
|
1199 |
if (ne < 3) { |
1200 |
g_vfs_job_failed (G_VFS_JOB (job), |
1201 |
G_IO_ERROR, G_IO_ERROR_FAILED, |
1202 |
_("Cannot delete this entity")); |
1203 |
goto exit; |
1204 |
} |
1205 |
|
1206 |
LIBMTP_mtpdevice_t *device; |
1207 |
device = G_VFS_BACKEND_MTP (backend)->device; |
1208 |
|
1209 |
int ret = LIBMTP_Delete_Object (device, strtol (elements[ne-1], NULL, 10)); |
1210 |
if (ret != 0) { |
1211 |
fail_job (G_VFS_JOB (job), device); |
1212 |
goto exit; |
1213 |
} |
1214 |
g_vfs_job_succeeded (G_VFS_JOB (job)); |
1215 |
|
1216 |
g_hash_table_foreach (G_VFS_BACKEND_MTP (backend)->monitors, |
1217 |
emit_delete_event, |
1218 |
(char *)filename); |
1219 |
|
1220 |
exit: |
1221 |
g_strfreev (elements); |
1222 |
g_mutex_unlock (&G_VFS_BACKEND_MTP (backend)->mutex); |
1223 |
|
1224 |
DEBUG ("(I) do_delete done."); |
1225 |
} |
1226 |
|
1227 |
|
1228 |
static void |
1229 |
do_set_display_name (GVfsBackend *backend, |
1230 |
GVfsJobSetDisplayName *job, |
1231 |
const char *filename, |
1232 |
const char *display_name) |
1233 |
{ |
1234 |
DEBUG ("(I) do_set_display_name '%s' --> '%s' ", filename, display_name); |
1235 |
g_mutex_lock (&G_VFS_BACKEND_MTP (backend)->mutex); |
1236 |
|
1237 |
gchar **elements = g_strsplit_set (filename, "/", -1); |
1238 |
unsigned int ne = 0; |
1239 |
for (ne = 0; elements[ne] != NULL; ne++); |
1240 |
|
1241 |
if (ne < 3) { |
1242 |
g_vfs_job_failed (G_VFS_JOB (job), |
1243 |
G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, |
1244 |
_("Can't rename volume")); |
1245 |
goto exit; |
1246 |
} |
1247 |
|
1248 |
LIBMTP_mtpdevice_t *device; |
1249 |
device = G_VFS_BACKEND_MTP (backend)->device; |
1250 |
|
1251 |
LIBMTP_file_t *file = LIBMTP_Get_Filemetadata (device, strtol (elements[ne-1], NULL, 10)); |
1252 |
int ret = LIBMTP_Set_File_Name (device, file, display_name); |
1253 |
if (ret != 0) { |
1254 |
fail_job (G_VFS_JOB (job), device); |
1255 |
goto exit; |
1256 |
} |
1257 |
LIBMTP_destroy_file_t (file); |
1258 |
file = NULL; |
1259 |
g_vfs_job_set_display_name_set_new_path (job, filename); |
1260 |
g_vfs_job_succeeded (G_VFS_JOB (job)); |
1261 |
|
1262 |
g_hash_table_foreach (G_VFS_BACKEND_MTP (backend)->monitors, |
1263 |
emit_change_event, |
1264 |
(char *)filename); |
1265 |
|
1266 |
exit: |
1267 |
g_strfreev (elements); |
1268 |
g_mutex_unlock (&G_VFS_BACKEND_MTP (backend)->mutex); |
1269 |
|
1270 |
DEBUG ("(I) do_set_display_name done."); |
1271 |
} |
1272 |
|
1273 |
|
1274 |
#if HAVE_LIBMTP_GET_THUMBNAIL |
1275 |
static void |
1276 |
do_open_icon_for_read (GVfsBackend *backend, |
1277 |
GVfsJobOpenIconForRead *job, |
1278 |
const char *icon_id) |
1279 |
{ |
1280 |
DEBUG ("(I) do_open_icon_for_read (%s)", icon_id); |
1281 |
g_mutex_lock (&G_VFS_BACKEND_MTP (backend)->mutex); |
1282 |
|
1283 |
guint id = strtol (icon_id, NULL, 10); |
1284 |
|
1285 |
if (id > 0) { |
1286 |
unsigned char *data; |
1287 |
unsigned int size; |
1288 |
int ret = LIBMTP_Get_Thumbnail (G_VFS_BACKEND_MTP (backend)->device, id, |
1289 |
&data, &size); |
1290 |
if (ret == 0) { |
1291 |
DEBUG ("File %u has thumbnail: %u", id, size); |
1292 |
GByteArray *bytes = g_byte_array_sized_new (size); |
1293 |
g_byte_array_append (bytes, data, size); |
1294 |
free (data); |
1295 |
g_vfs_job_open_for_read_set_can_seek (G_VFS_JOB_OPEN_FOR_READ (job), FALSE); |
1296 |
g_vfs_job_open_for_read_set_handle (G_VFS_JOB_OPEN_FOR_READ (job), bytes); |
1297 |
g_vfs_job_succeeded (G_VFS_JOB (job)); |
1298 |
} else { |
1299 |
LIBMTP_filesampledata_t *sample_data = LIBMTP_new_filesampledata_t (); |
1300 |
ret = LIBMTP_Get_Representative_Sample (G_VFS_BACKEND_MTP (backend)->device, |
1301 |
id, sample_data); |
1302 |
if (ret == 0) { |
1303 |
DEBUG ("File %u has sampledata: %u", id, size); |
1304 |
GByteArray *bytes = g_byte_array_sized_new (sample_data->size); |
1305 |
g_byte_array_append (bytes, (const guint8 *)sample_data->data, sample_data->size); |
1306 |
LIBMTP_destroy_filesampledata_t (sample_data); |
1307 |
g_vfs_job_open_for_read_set_can_seek (G_VFS_JOB_OPEN_FOR_READ (job), FALSE); |
1308 |
g_vfs_job_open_for_read_set_handle (G_VFS_JOB_OPEN_FOR_READ (job), bytes); |
1309 |
g_vfs_job_succeeded (G_VFS_JOB (job)); |
1310 |
} else { |
1311 |
DEBUG ("File %u has no thumbnail:", id); |
1312 |
g_vfs_job_failed (G_VFS_JOB (job), |
1313 |
G_IO_ERROR, |
1314 |
G_IO_ERROR_NOT_FOUND, |
1315 |
_("No thumbnail for entity '%s'"), |
1316 |
icon_id); |
1317 |
} |
1318 |
} |
1319 |
} else { |
1320 |
g_vfs_job_failed (G_VFS_JOB (job), |
1321 |
G_IO_ERROR, |
1322 |
G_IO_ERROR_INVALID_ARGUMENT, |
1323 |
_("Malformed icon identifier '%s'"), |
1324 |
icon_id); |
1325 |
} |
1326 |
g_mutex_unlock (&G_VFS_BACKEND_MTP (backend)->mutex); |
1327 |
|
1328 |
DEBUG ("(I) do_open_icon_for_read done."); |
1329 |
} |
1330 |
|
1331 |
static gboolean |
1332 |
try_read (GVfsBackend *backend, |
1333 |
GVfsJobRead *job, |
1334 |
GVfsBackendHandle handle, |
1335 |
char *buffer, |
1336 |
gsize bytes_requested) |
1337 |
{ |
1338 |
GByteArray *bytes = handle; |
1339 |
|
1340 |
DEBUG ("(I) try_read (%u %lu)", bytes->len, bytes_requested); |
1341 |
|
1342 |
gsize bytes_to_copy = MIN (bytes->len, bytes_requested); |
1343 |
if (bytes_to_copy == 0) { |
1344 |
goto out; |
1345 |
} |
1346 |
memcpy (buffer, bytes->data, bytes_to_copy); |
1347 |
g_byte_array_remove_range (bytes, 0, bytes_to_copy); |
1348 |
|
1349 |
out: |
1350 |
g_vfs_job_read_set_size (job, bytes_to_copy); |
1351 |
g_vfs_job_succeeded (G_VFS_JOB (job)); |
1352 |
|
1353 |
DEBUG ("(I) try_read done."); |
1354 |
return TRUE; |
1355 |
} |
1356 |
|
1357 |
static void |
1358 |
do_close_read (GVfsBackend *backend, |
1359 |
GVfsJobCloseRead *job, |
1360 |
GVfsBackendHandle handle) |
1361 |
{ |
1362 |
DEBUG ("(I) do_close_read"); |
1363 |
g_byte_array_unref (handle); |
1364 |
g_vfs_job_succeeded (G_VFS_JOB (job)); |
1365 |
DEBUG ("(I) do_close_read done."); |
1366 |
} |
1367 |
#endif /* HAVE_LIBMTP_GET_THUMBNAIL */ |
1368 |
|
1369 |
|
1370 |
/************************************************ |
1371 |
* Class init |
1372 |
* |
1373 |
*/ |
1374 |
|
1375 |
|
1376 |
static void |
1377 |
g_vfs_backend_mtp_class_init (GVfsBackendMtpClass *klass) |
1378 |
{ |
1379 |
GObjectClass *gobject_class = G_OBJECT_CLASS (klass); |
1380 |
GVfsBackendClass *backend_class = G_VFS_BACKEND_CLASS (klass); |
1381 |
|
1382 |
gobject_class->finalize = g_vfs_backend_mtp_finalize; |
1383 |
|
1384 |
backend_class->mount = do_mount; |
1385 |
backend_class->unmount = do_unmount; |
1386 |
backend_class->query_info = do_query_info; |
1387 |
backend_class->enumerate = do_enumerate; |
1388 |
backend_class->query_fs_info = do_query_fs_info; |
1389 |
backend_class->pull = do_pull; |
1390 |
backend_class->push = do_push; |
1391 |
backend_class->make_directory = do_make_directory; |
1392 |
backend_class->delete = do_delete; |
1393 |
backend_class->set_display_name = do_set_display_name; |
1394 |
backend_class->create_dir_monitor = do_create_dir_monitor; |
1395 |
backend_class->create_file_monitor = do_create_file_monitor; |
1396 |
#if HAVE_LIBMTP_GET_THUMBNAIL |
1397 |
backend_class->open_icon_for_read = do_open_icon_for_read; |
1398 |
backend_class->try_read = try_read; |
1399 |
backend_class->close_read = do_close_read; |
1400 |
#endif |
1401 |
} |