libmtp: getfile.c

- Main Page
- Modules
- Data Structures
- Files
- Directories
- Examples

# getfile.c

    00001 #include "common.h"
    00002
    00003 static int progress (u_int64_t const sent, u_int64_t const total, void const * const data)
    00004 {
    00005   int percent = (sent*100)/total;
    00006 #ifdef __WIN32__
    00007   printf("Progress: %I64u of %I64u (%d%%)\r", sent, total, percent);
    00008 #else
    00009   printf("Progress: %llu of %llu (%d%%)\r", sent, total, percent);
    00010 #endif
    00011   fflush(stdout);
    00012   return 0;
    00013 }
    00014
    00015 static void usage (void)
    00016 {
    00017   fprintf(stderr, "getfile <fileid/trackid> <filename>\n");
    00018 }
    00019
    00020 int main (int argc, char **argv)
    00021 {
    00022   LIBMTP_mtpdevice_t *device;
    00023   u_int32_t id;
    00024   char *endptr;
    00025   char *file;
    00026
    00027   // We need file ID and filename
    00028   if ( argc != 3 ) {
    00029     usage();
    00030     return 1;
    00031   }
    00032
    00033   // Sanity check song ID
    00034   id = strtoul(argv[1], &endptr, 10);
    00035   if ( *endptr != 0 ) {
    00036     fprintf(stderr, "illegal value %s\n", argv[1]);
    00037     return 1;
    00038   } else if ( ! id ) {
    00039     fprintf(stderr, "bad file/track id %u\n", id);
    00040     return 1;
    00041  }
    00042
    00043   // Filename, e.g. "foo.mp3"
    00044   file = argv[2];
    00045   printf("Getting file/track %d to local file %s\n", id, file);
    00046
    00047   LIBMTP_Init();
    00048   device = LIBMTP_Get_First_Device();
    00049   if (device == NULL) {
    00050     printf("No devices. Connect/replug device and try again.\n");
    00051     exit (0);
    00052   }
    00053
    00054   // This function will also work just as well for tracks.
    00055   if (LIBMTP_Get_File_To_File(device, id, file, progress, NULL) != 0 ) {
    00056     printf("\nError getting file from MTP device.\n");
    00057   }
    00058   // Terminate progress bar.
    00059   printf("\n");
    00060
    00061   LIBMTP_Release_Device(device);
    00062   printf("OK.\n");
    00063   exit (0);
    00064 }
    00065