Browse Source

Add draft implementation of put_file in MTP module

Add a quick draft of a 'put_file' implementation for the
libmtp-based scheme module. It can only write at the root of the
device's primary storage.
master
Damien Goutte-Gattat 5 years ago
parent
commit
16c0568c11
  1. 71
      src/scheme-libmtp.c
  2. 7
      src/scheme-libmtp.h
  3. 2
      src/scheme-module.c

71
src/scheme-libmtp.c

@ -208,4 +208,75 @@ gfsec_scheme_libmtp_get_file(gfsec_scheme_t scheme,
return rc;
}
/**
* Callback for the LIBMTP_Send_File_To_Handler function.
*
* @param params MTP parameters (unused).
* @param priv Pointer to a getfile_cb_data structure.
* @param wantlen Number of bytes to write.
* @param data The buffer to write the bytes to.
* @param[out] putlen The number of bytes actually written.
*
* @return LIBMTP_HANLDER_RETURN_OK if successful, or
* LIBMTP_HANDLER_RETURN_ERROR if we get more bytes than we expected.
*/
static unsigned short
putfile_cb(void *params,
void *priv,
unsigned wantlen,
unsigned char *data,
unsigned *gotlen)
{
struct getfile_cb_data *cb_data = priv;
(void) params;
if ( cb_data->written + wantlen > cb_data->expected )
return LIBMTP_HANDLER_RETURN_ERROR;
memcpy(data, cb_data->buffer + cb_data->written, wantlen);
cb_data->written += wantlen;
*gotlen = wantlen;
return LIBMTP_HANDLER_RETURN_OK;
}
int
gfsec_scheme_libmtp_put_file(gfsec_scheme_t scheme,
const char *authority,
const char *path,
unsigned char *buffer,
size_t len)
{
LIBMTP_mtpdevice_t *device;
int rc = GFSEC_SCHEME_STATUS_ERROR;
if ( scheme != GFSEC_SCHEME_MTP )
return GFSEC_SCHEME_STATUS_UNSUPPORTED_SCHEME;
if ( (device = find_mtp_device(authority)) ) {
LIBMTP_file_t *file;
struct getfile_cb_data cb_data;
file = LIBMTP_new_file_t();
file->filesize = len;
file->filename = strdup(path);
file->parent_id = 0;
file->storage_id = 0;
cb_data.buffer = buffer;
cb_data.written = 0;
cb_data.expected = len;
if ( LIBMTP_Send_File_From_Handler(device, putfile_cb, &cb_data, file,
NULL, NULL) == 0 )
rc = 0;
LIBMTP_destroy_file_t(file);
LIBMTP_Release_Device(device);
}
return rc;
}
#endif /* HAVE_LIBMTP */

7
src/scheme-libmtp.h

@ -32,6 +32,13 @@ gfsec_scheme_libmtp_get_file(gfsec_scheme_t scheme,
unsigned char **,
size_t *);
int
gfsec_scheme_libmtp_put_file(gfsec_scheme_t,
const char *,
const char *,
unsigned char *,
size_t);
#ifdef __cplusplus
}
#endif

2
src/scheme-module.c

@ -67,7 +67,7 @@ gfsec_scheme_module_init(void)
#ifdef HAVE_LIBMTP
LIBMTP_Init();
mods[n].get_file = gfsec_scheme_libmtp_get_file;
mods[n].put_file = NULL;
mods[n].put_file = gfsec_scheme_libmtp_put_file;
mods[n++].present = 1;
#endif

Loading…
Cancel
Save