blob: 4562f87163502ef1f309c6c22864114ab52727d7 [file] [log] [blame]
Linus Walleij543badf2007-02-05 19:07:38 +00001/**
2 * \file getfile.c
3 * Example program to retrieve a file off the device.
4 *
5 * Copyright (C) 2005-2007 Linus Walleij <triad@df.lth.se>
6 * Copyright (C) 2006 Chris A. Debenham <chris@adebenham.com>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
22 */
Linus Walleij2eaaff02009-01-15 21:30:36 +000023#include <stdlib.h>
24#include <limits.h>
Linus Walleijdcde6082006-02-17 16:16:34 +000025
Linus Walleij843210f2010-10-17 22:40:45 +000026#include "common.h"
27#include "pathutils.h"
28#include "connect.h"
Linus Walleijdcde6082006-02-17 16:16:34 +000029
cjdebenh4a99f502006-11-09 22:03:05 +000030extern LIBMTP_folder_t *folders;
31extern LIBMTP_file_t *files;
32extern LIBMTP_mtpdevice_t *device;
33
cjdebenhc80f9b42006-11-09 22:29:26 +000034void getfile_usage (void)
Linus Walleijdcde6082006-02-17 16:16:34 +000035{
Linus Walleijf6bc1782006-03-24 15:12:47 +000036 fprintf(stderr, "getfile <fileid/trackid> <filename>\n");
Linus Walleijdcde6082006-02-17 16:16:34 +000037}
38
Linus Walleij843210f2010-10-17 22:40:45 +000039int
cjdebenhc80f9b42006-11-09 22:29:26 +000040getfile_function(char * from_path,char * to_path)
Linus Walleijdcde6082006-02-17 16:16:34 +000041{
cjdebenh4a99f502006-11-09 22:03:05 +000042 int id = parse_path (from_path,files,folders);
43 if (id > 0) {
44 printf("Getting %s to %s\n",from_path,to_path);
45 if (LIBMTP_Get_File_To_File(device, id, to_path, progress, NULL) != 0 ) {
46 printf("\nError getting file from MTP device.\n");
Linus Walleij070e9b42007-01-22 08:49:28 +000047 LIBMTP_Dump_Errorstack(device);
48 LIBMTP_Clear_Errorstack(device);
Linus Walleij843210f2010-10-17 22:40:45 +000049 return 1;
cjdebenh4a99f502006-11-09 22:03:05 +000050 }
51 }
Linus Walleij843210f2010-10-17 22:40:45 +000052 return 0;
cjdebenh4a99f502006-11-09 22:03:05 +000053}
54
55
Linus Walleij843210f2010-10-17 22:40:45 +000056int getfile_command(int argc, char **argv)
cjdebenh4a99f502006-11-09 22:03:05 +000057{
Linus Walleij6f050022009-05-06 21:14:41 +000058 uint32_t id;
Linus Walleijdcde6082006-02-17 16:16:34 +000059 char *endptr;
60 char *file;
Linus Walleij843210f2010-10-17 22:40:45 +000061 int ret = 0;
Linus Walleijdcde6082006-02-17 16:16:34 +000062
Linus Walleijf6bc1782006-03-24 15:12:47 +000063 // We need file ID and filename
Linus Walleij0cd85432006-02-20 14:37:50 +000064 if ( argc != 3 ) {
cjdebenh4a99f502006-11-09 22:03:05 +000065 getfile_usage();
Linus Walleij843210f2010-10-17 22:40:45 +000066 return 0;
Linus Walleijdcde6082006-02-17 16:16:34 +000067 }
68
69 // Sanity check song ID
Linus Walleij0cd85432006-02-20 14:37:50 +000070 id = strtoul(argv[1], &endptr, 10);
Linus Walleijdcde6082006-02-17 16:16:34 +000071 if ( *endptr != 0 ) {
Linus Walleij0cd85432006-02-20 14:37:50 +000072 fprintf(stderr, "illegal value %s\n", argv[1]);
Linus Walleij843210f2010-10-17 22:40:45 +000073 return 1;
Linus Walleijdcde6082006-02-17 16:16:34 +000074 } else if ( ! id ) {
Linus Walleijf6bc1782006-03-24 15:12:47 +000075 fprintf(stderr, "bad file/track id %u\n", id);
Linus Walleij843210f2010-10-17 22:40:45 +000076 return 1;
Linus Walleij0cd85432006-02-20 14:37:50 +000077 }
Linus Walleijdcde6082006-02-17 16:16:34 +000078
79 // Filename, e.g. "foo.mp3"
Linus Walleij0cd85432006-02-20 14:37:50 +000080 file = argv[2];
Linus Walleijf6bc1782006-03-24 15:12:47 +000081 printf("Getting file/track %d to local file %s\n", id, file);
Linus Walleijdcde6082006-02-17 16:16:34 +000082
Linus Walleijf6bc1782006-03-24 15:12:47 +000083 // This function will also work just as well for tracks.
84 if (LIBMTP_Get_File_To_File(device, id, file, progress, NULL) != 0 ) {
Linus Walleijee73ef22006-08-27 19:56:00 +000085 printf("\nError getting file from MTP device.\n");
Linus Walleij843210f2010-10-17 22:40:45 +000086 ret = 1;
Linus Walleijdcde6082006-02-17 16:16:34 +000087 }
Linus Walleijee73ef22006-08-27 19:56:00 +000088 // Terminate progress bar.
89 printf("\n");
Linus Walleijdcde6082006-02-17 16:16:34 +000090
Linus Walleij843210f2010-10-17 22:40:45 +000091 return ret;
92}