static gboolean rb_mtp_source_transfer_track_to_disk (LIBMTP_mtpdevice_t *device, LIBMTP_track_t *track, const char *uri) { int ret = -1; char *path; if (device == NULL || track == NULL || strlen (uri) == 0) { rb_debug ("device (%p), track (%p), or URI (%s) not supplied", device, track, uri); return FALSE; } if (rb_check_dir_has_space_uri (uri, track->filesize) == FALSE) { rb_debug ("not enough space to transfer track %d to %s", track->item_id, uri); return FALSE; } path = g_filename_from_uri (uri, NULL, NULL); if (path != NULL) { ret = LIBMTP_Get_Track_To_File (device, track->item_id, path, NULL, NULL); rb_debug ("LIBMTP_Get_Track_To_File(%d, %s) returned %d", track->item_id, path, ret); if (ret != 0) { report_libmtp_errors (device, TRUE); } g_free (path); } else { g_warning ("couldn't get path from URI %s", uri); } return (ret == 0); }
static void download_track (RBMtpThread *thread, RBMtpThreadTask *task) { LIBMTP_file_t *fileinfo; LIBMTP_error_t *stack; GError *error = NULL; GFile *dir; RBMtpDownloadCallback cb = (RBMtpDownloadCallback) task->callback; /* first, check there's enough space to copy it */ fileinfo = LIBMTP_Get_Filemetadata (thread->device, task->track_id); if (fileinfo == NULL) { stack = LIBMTP_Get_Errorstack (thread->device); rb_debug ("unable to get track metadata for %u: %s", task->track_id, stack->error_text); error = g_error_new (RB_MTP_THREAD_ERROR, RB_MTP_THREAD_ERROR_GET_TRACK, _("Unable to copy file from MTP device: %s"), stack->error_text); LIBMTP_Clear_Errorstack (thread->device); cb (task->track_id, NULL, error, task->user_data); g_error_free (error); return; } if (task->filename[0] == '\0') { dir = g_file_new_for_path (g_get_tmp_dir ()); } else { GFile *file = g_file_new_for_path (task->filename); dir = g_file_get_parent (file); g_object_unref (file); } rb_debug ("checking for %" G_GINT64_FORMAT " bytes available", fileinfo->filesize); if (rb_check_dir_has_space (dir, fileinfo->filesize) == FALSE) { char *dpath = g_file_get_path (dir); rb_debug ("not enough space in %s", dpath); error = g_error_new (RB_MTP_THREAD_ERROR, RB_MTP_THREAD_ERROR_NO_SPACE, _("Not enough space in %s"), dpath); g_free (dpath); } LIBMTP_destroy_file_t (fileinfo); g_object_unref (dir); if (error != NULL) { rb_debug ("bailing out due to error: %s", error->message); cb (task->track_id, NULL, error, task->user_data); g_error_free (error); return; } if (task->filename[0] == '\0') { /* download to a temporary file */ int fd; GError *tmperror = NULL; g_free (task->filename); fd = g_file_open_tmp ("rb-mtp-temp-XXXXXX", &task->filename, &tmperror); if (fd == -1) { rb_debug ("unable to open temporary file: %s", tmperror->message); error = g_error_new (RB_MTP_THREAD_ERROR, RB_MTP_THREAD_ERROR_TEMPFILE, _("Unable to open temporary file: %s"), tmperror->message); g_error_free (tmperror); cb (task->track_id, NULL, error, task->user_data); g_error_free (error); return; } else { rb_debug ("downloading track %u to file descriptor %d", task->track_id, fd); if (LIBMTP_Get_Track_To_File_Descriptor (thread->device, task->track_id, fd, NULL, NULL)) { stack = LIBMTP_Get_Errorstack (thread->device); rb_debug ("unable to retrieve track %u: %s", task->track_id, stack->error_text); error = g_error_new (RB_MTP_THREAD_ERROR, RB_MTP_THREAD_ERROR_GET_TRACK, _("Unable to copy file from MTP device: %s"), stack->error_text); LIBMTP_Clear_Errorstack (thread->device); cb (task->track_id, NULL, error, task->user_data); g_error_free (error); close (fd); remove (task->filename); return; } rb_debug ("done downloading track"); close (fd); } } else { if (LIBMTP_Get_Track_To_File (thread->device, task->track_id, task->filename, NULL, NULL)) { stack = LIBMTP_Get_Errorstack (thread->device); error = g_error_new (RB_MTP_THREAD_ERROR, RB_MTP_THREAD_ERROR_GET_TRACK, _("Unable to copy file from MTP device: %s"), stack->error_text); LIBMTP_Clear_Errorstack (thread->device); cb (task->track_id, NULL, error, task->user_data); g_error_free (error); return; } } cb (task->track_id, task->filename, NULL, task->user_data); }