libmtp: getfile.c

libmtp 1.1.20

getfile.c
#include <stdlib.h>
#include <limits.h>
#include "common.h"
#include "pathutils.h"
#include "connect.h"
extern LIBMTP_folder_t *folders;
extern LIBMTP_file_t *files;
extern LIBMTP_mtpdevice_t *device;
void getfile_usage (void)
{
fprintf(stderr, "getfile [<deviceid>] <fileid/trackid> <filename>\n");
}
int
getfile_function(char * from_path,char * to_path)
{
int id = parse_path (from_path,files,folders);
if (id > 0) {
printf("Getting %s to %s\n",from_path,to_path);
if (LIBMTP_Get_File_To_File(device, id, to_path, progress, NULL) != 0 ) {
printf("\nError getting file from MTP device.\n");
LIBMTP_Dump_Errorstack(device);
LIBMTP_Clear_Errorstack(device);
return 1;
}
}
return 0;
}
LIBMTP_mtpdevice_t *getfile_device(int argc, char **argv)
{
if (argc == 3)
return LIBMTP_Get_First_Device();
if (argc == 4) {
uint32_t id;
char *endptr;
// Sanity check device ID
id = strtoul(argv[1], &endptr, 10);
if ( *endptr != 0 ) {
fprintf(stderr, "illegal value %s\n", argv[1]);
return NULL;
}
return LIBMTP_Get_Device(id);
}
getfile_usage();
return NULL;
}
int getfile_command(int argc, char **argv)
{
uint32_t id;
char *endptr;
char *file;
int off = (argc == 4 ? 1 : 0);
int ret = 0;
// We need file ID and filename (device ID is optional)
if ( argc != 3 && argc != 4 ) {
getfile_usage();
return 0;
}
// Sanity check song ID
id = strtoul(argv[1 + off], &endptr, 10);
if ( *endptr != 0 ) {
fprintf(stderr, "illegal value %s\n", argv[1 + off]);
return 1;
} else if ( ! id ) {
fprintf(stderr, "bad file/track id %u\n", id);
return 1;
}
// Filename, e.g. "foo.mp3"
file = argv[2 + off];
printf("Getting file/track %d to local file %s\n", id, file);
// This function will also work just as well for tracks.
if (LIBMTP_Get_File_To_File(device, id, file, progress, NULL) != 0 ) {
printf("\nError getting file from MTP device.\n");
ret = 1;
}
// Terminate progress bar.
printf("\n");
return ret;
}
LIBMTP_Get_First_Device
LIBMTP_mtpdevice_t * LIBMTP_Get_First_Device(void)
Definition: libmtp.c:1719
LIBMTP_Dump_Errorstack
void LIBMTP_Dump_Errorstack(LIBMTP_mtpdevice_t *device)
Definition: libmtp.c:2586
LIBMTP_Clear_Errorstack
void LIBMTP_Clear_Errorstack(LIBMTP_mtpdevice_t *device)
Definition: libmtp.c:2559
LIBMTP_Get_Device
LIBMTP_mtpdevice_t * LIBMTP_Get_Device(int device_nr)
Definition: libmtp.c:1687
LIBMTP_Get_File_To_File
int LIBMTP_Get_File_To_File(LIBMTP_mtpdevice_t *device, uint32_t const id, char const *const path, LIBMTP_progressfunc_t const callback, void const *const data)
Definition: libmtp.c:5212
LIBMTP_file_struct
Definition: libmtp.h:694
LIBMTP_folder_struct
Definition: libmtp.h:766
LIBMTP_mtpdevice_struct
Definition: libmtp.h:635