Linus Walleij | 543badf | 2007-02-05 19:07:38 +0000 | [diff] [blame] | 1 | /** |
| 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 Walleij | 2eaaff0 | 2009-01-15 21:30:36 +0000 | [diff] [blame] | 23 | #include <stdlib.h> |
| 24 | #include <limits.h> |
Linus Walleij | dcde608 | 2006-02-17 16:16:34 +0000 | [diff] [blame] | 25 | |
Linus Walleij | 843210f | 2010-10-17 22:40:45 +0000 | [diff] [blame] | 26 | #include "common.h" |
| 27 | #include "pathutils.h" |
| 28 | #include "connect.h" |
Linus Walleij | dcde608 | 2006-02-17 16:16:34 +0000 | [diff] [blame] | 29 | |
cjdebenh | 4a99f50 | 2006-11-09 22:03:05 +0000 | [diff] [blame] | 30 | extern LIBMTP_folder_t *folders; |
| 31 | extern LIBMTP_file_t *files; |
| 32 | extern LIBMTP_mtpdevice_t *device; |
| 33 | |
cjdebenh | c80f9b4 | 2006-11-09 22:29:26 +0000 | [diff] [blame] | 34 | void getfile_usage (void) |
Linus Walleij | dcde608 | 2006-02-17 16:16:34 +0000 | [diff] [blame] | 35 | { |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 36 | fprintf(stderr, "getfile <fileid/trackid> <filename>\n"); |
Linus Walleij | dcde608 | 2006-02-17 16:16:34 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Linus Walleij | 843210f | 2010-10-17 22:40:45 +0000 | [diff] [blame] | 39 | int |
cjdebenh | c80f9b4 | 2006-11-09 22:29:26 +0000 | [diff] [blame] | 40 | getfile_function(char * from_path,char * to_path) |
Linus Walleij | dcde608 | 2006-02-17 16:16:34 +0000 | [diff] [blame] | 41 | { |
cjdebenh | 4a99f50 | 2006-11-09 22:03:05 +0000 | [diff] [blame] | 42 | 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 Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 47 | LIBMTP_Dump_Errorstack(device); |
| 48 | LIBMTP_Clear_Errorstack(device); |
Linus Walleij | 843210f | 2010-10-17 22:40:45 +0000 | [diff] [blame] | 49 | return 1; |
cjdebenh | 4a99f50 | 2006-11-09 22:03:05 +0000 | [diff] [blame] | 50 | } |
| 51 | } |
Linus Walleij | 843210f | 2010-10-17 22:40:45 +0000 | [diff] [blame] | 52 | return 0; |
cjdebenh | 4a99f50 | 2006-11-09 22:03:05 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | |
Linus Walleij | 843210f | 2010-10-17 22:40:45 +0000 | [diff] [blame] | 56 | int getfile_command(int argc, char **argv) |
cjdebenh | 4a99f50 | 2006-11-09 22:03:05 +0000 | [diff] [blame] | 57 | { |
Linus Walleij | 6f05002 | 2009-05-06 21:14:41 +0000 | [diff] [blame] | 58 | uint32_t id; |
Linus Walleij | dcde608 | 2006-02-17 16:16:34 +0000 | [diff] [blame] | 59 | char *endptr; |
| 60 | char *file; |
Linus Walleij | 843210f | 2010-10-17 22:40:45 +0000 | [diff] [blame] | 61 | int ret = 0; |
Linus Walleij | dcde608 | 2006-02-17 16:16:34 +0000 | [diff] [blame] | 62 | |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 63 | // We need file ID and filename |
Linus Walleij | 0cd8543 | 2006-02-20 14:37:50 +0000 | [diff] [blame] | 64 | if ( argc != 3 ) { |
cjdebenh | 4a99f50 | 2006-11-09 22:03:05 +0000 | [diff] [blame] | 65 | getfile_usage(); |
Linus Walleij | 843210f | 2010-10-17 22:40:45 +0000 | [diff] [blame] | 66 | return 0; |
Linus Walleij | dcde608 | 2006-02-17 16:16:34 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | // Sanity check song ID |
Linus Walleij | 0cd8543 | 2006-02-20 14:37:50 +0000 | [diff] [blame] | 70 | id = strtoul(argv[1], &endptr, 10); |
Linus Walleij | dcde608 | 2006-02-17 16:16:34 +0000 | [diff] [blame] | 71 | if ( *endptr != 0 ) { |
Linus Walleij | 0cd8543 | 2006-02-20 14:37:50 +0000 | [diff] [blame] | 72 | fprintf(stderr, "illegal value %s\n", argv[1]); |
Linus Walleij | 843210f | 2010-10-17 22:40:45 +0000 | [diff] [blame] | 73 | return 1; |
Linus Walleij | dcde608 | 2006-02-17 16:16:34 +0000 | [diff] [blame] | 74 | } else if ( ! id ) { |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 75 | fprintf(stderr, "bad file/track id %u\n", id); |
Linus Walleij | 843210f | 2010-10-17 22:40:45 +0000 | [diff] [blame] | 76 | return 1; |
Linus Walleij | 0cd8543 | 2006-02-20 14:37:50 +0000 | [diff] [blame] | 77 | } |
Linus Walleij | dcde608 | 2006-02-17 16:16:34 +0000 | [diff] [blame] | 78 | |
| 79 | // Filename, e.g. "foo.mp3" |
Linus Walleij | 0cd8543 | 2006-02-20 14:37:50 +0000 | [diff] [blame] | 80 | file = argv[2]; |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 81 | printf("Getting file/track %d to local file %s\n", id, file); |
Linus Walleij | dcde608 | 2006-02-17 16:16:34 +0000 | [diff] [blame] | 82 | |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 83 | // This function will also work just as well for tracks. |
| 84 | if (LIBMTP_Get_File_To_File(device, id, file, progress, NULL) != 0 ) { |
Linus Walleij | ee73ef2 | 2006-08-27 19:56:00 +0000 | [diff] [blame] | 85 | printf("\nError getting file from MTP device.\n"); |
Linus Walleij | 843210f | 2010-10-17 22:40:45 +0000 | [diff] [blame] | 86 | ret = 1; |
Linus Walleij | dcde608 | 2006-02-17 16:16:34 +0000 | [diff] [blame] | 87 | } |
Linus Walleij | ee73ef2 | 2006-08-27 19:56:00 +0000 | [diff] [blame] | 88 | // Terminate progress bar. |
| 89 | printf("\n"); |
Linus Walleij | dcde608 | 2006-02-17 16:16:34 +0000 | [diff] [blame] | 90 | |
Linus Walleij | 843210f | 2010-10-17 22:40:45 +0000 | [diff] [blame] | 91 | return ret; |
| 92 | } |