Linus Walleij | 543badf | 2007-02-05 19:07:38 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file delfile.c |
| 3 | * Example program to delete a file off the device. |
| 4 | * |
Linus Walleij | c12e175 | 2008-02-18 22:45:45 +0000 | [diff] [blame] | 5 | * Copyright (C) 2005-2008 Linus Walleij <triad@df.lth.se> |
Linus Walleij | 543badf | 2007-02-05 19:07:38 +0000 | [diff] [blame] | 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 | 43ff8fc | 2006-10-10 11:16:53 +0000 | [diff] [blame] | 25 | |
Linus Walleij | 843210f | 2010-10-17 22:40:45 +0000 | [diff] [blame] | 26 | #include "common.h" |
| 27 | #include "string.h" |
| 28 | #include "pathutils.h" |
| 29 | #include "connect.h" |
Linus Walleij | 95698cd | 2006-02-24 10:40:40 +0000 | [diff] [blame] | 30 | |
cjdebenh | 71f45f0 | 2006-11-08 02:19:57 +0000 | [diff] [blame] | 31 | extern LIBMTP_mtpdevice_t *device; |
| 32 | extern LIBMTP_folder_t *folders; |
| 33 | extern LIBMTP_file_t *files; |
| 34 | |
| 35 | void delfile_usage(void) |
Linus Walleij | 95698cd | 2006-02-24 10:40:40 +0000 | [diff] [blame] | 36 | { |
Linus Walleij | 3725329 | 2006-10-11 08:38:14 +0000 | [diff] [blame] | 37 | printf("Usage: delfile [-n] <fileid/trackid> | -f <filename>\n"); |
Linus Walleij | 95698cd | 2006-02-24 10:40:40 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Linus Walleij | 843210f | 2010-10-17 22:40:45 +0000 | [diff] [blame] | 40 | int |
cjdebenh | c80f9b4 | 2006-11-09 22:29:26 +0000 | [diff] [blame] | 41 | delfile_function(char * path) |
Linus Walleij | 43ff8fc | 2006-10-10 11:16:53 +0000 | [diff] [blame] | 42 | { |
Linus Walleij | c12e175 | 2008-02-18 22:45:45 +0000 | [diff] [blame] | 43 | uint32_t id = parse_path (path,files,folders); |
| 44 | |
cjdebenh | 71f45f0 | 2006-11-08 02:19:57 +0000 | [diff] [blame] | 45 | if (id > 0) { |
| 46 | printf("Deleting %s which has item_id:%d\n",path,id); |
| 47 | int ret = 1; |
| 48 | ret = LIBMTP_Delete_Object(device, id); |
| 49 | if (ret != 0) { |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 50 | LIBMTP_Dump_Errorstack(device); |
| 51 | LIBMTP_Clear_Errorstack(device); |
cjdebenh | 71f45f0 | 2006-11-08 02:19:57 +0000 | [diff] [blame] | 52 | printf("Failed to remove file\n"); |
Linus Walleij | 843210f | 2010-10-17 22:40:45 +0000 | [diff] [blame] | 53 | return 1; |
cjdebenh | 71f45f0 | 2006-11-08 02:19:57 +0000 | [diff] [blame] | 54 | } |
Linus Walleij | 43ff8fc | 2006-10-10 11:16:53 +0000 | [diff] [blame] | 55 | } |
Linus Walleij | 843210f | 2010-10-17 22:40:45 +0000 | [diff] [blame] | 56 | return 0; |
Linus Walleij | 43ff8fc | 2006-10-10 11:16:53 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Linus Walleij | 843210f | 2010-10-17 22:40:45 +0000 | [diff] [blame] | 59 | int delfile_command(int argc, char **argv) |
Linus Walleij | 43ff8fc | 2006-10-10 11:16:53 +0000 | [diff] [blame] | 60 | { |
Linus Walleij | 3725329 | 2006-10-11 08:38:14 +0000 | [diff] [blame] | 61 | int FILENAME = 1; |
| 62 | int ITEMID = 2; |
| 63 | int field_type = 0; |
Linus Walleij | c12e175 | 2008-02-18 22:45:45 +0000 | [diff] [blame] | 64 | int i; |
Linus Walleij | 843210f | 2010-10-17 22:40:45 +0000 | [diff] [blame] | 65 | int ret = 0; |
Linus Walleij | c12e175 | 2008-02-18 22:45:45 +0000 | [diff] [blame] | 66 | |
Linus Walleij | 3725329 | 2006-10-11 08:38:14 +0000 | [diff] [blame] | 67 | if ( argc > 2 ) { |
| 68 | if (strncmp(argv[1],"-f",2) == 0) { |
| 69 | field_type = FILENAME; |
| 70 | strcpy(argv[1],""); |
| 71 | } else if (strncmp(argv[1],"-n",2) == 0) { |
| 72 | field_type = ITEMID; |
| 73 | strcpy(argv[1],"0"); |
| 74 | } else { |
cjdebenh | 71f45f0 | 2006-11-08 02:19:57 +0000 | [diff] [blame] | 75 | delfile_usage(); |
Linus Walleij | 843210f | 2010-10-17 22:40:45 +0000 | [diff] [blame] | 76 | return 0; |
Linus Walleij | 43ff8fc | 2006-10-10 11:16:53 +0000 | [diff] [blame] | 77 | } |
Linus Walleij | 3725329 | 2006-10-11 08:38:14 +0000 | [diff] [blame] | 78 | } else { |
cjdebenh | 71f45f0 | 2006-11-08 02:19:57 +0000 | [diff] [blame] | 79 | delfile_usage(); |
Linus Walleij | 843210f | 2010-10-17 22:40:45 +0000 | [diff] [blame] | 80 | return 0; |
Linus Walleij | 95698cd | 2006-02-24 10:40:40 +0000 | [diff] [blame] | 81 | } |
Linus Walleij | c12e175 | 2008-02-18 22:45:45 +0000 | [diff] [blame] | 82 | |
Linus Walleij | 3725329 | 2006-10-11 08:38:14 +0000 | [diff] [blame] | 83 | for (i=1;i<argc;i++) { |
Linus Walleij | c12e175 | 2008-02-18 22:45:45 +0000 | [diff] [blame] | 84 | uint32_t id; |
cjdebenh | 71f45f0 | 2006-11-08 02:19:57 +0000 | [diff] [blame] | 85 | char *endptr; |
Linus Walleij | c12e175 | 2008-02-18 22:45:45 +0000 | [diff] [blame] | 86 | |
Linus Walleij | 3725329 | 2006-10-11 08:38:14 +0000 | [diff] [blame] | 87 | if (field_type == ITEMID) { |
| 88 | // Sanity check song ID |
| 89 | id = strtoul(argv[i], &endptr, 10); |
| 90 | if ( *endptr != 0 ) { |
| 91 | fprintf(stderr, "illegal value %s .. skipping\n", argv[i]); |
| 92 | id = 0; |
| 93 | } |
| 94 | } else { |
| 95 | if (strlen(argv[i]) > 0) { |
cjdebenh | 71f45f0 | 2006-11-08 02:19:57 +0000 | [diff] [blame] | 96 | id = parse_path (argv[i],files,folders); |
Linus Walleij | 3725329 | 2006-10-11 08:38:14 +0000 | [diff] [blame] | 97 | } else { |
| 98 | id = 0; |
| 99 | } |
| 100 | } |
Linus Walleij | 3725329 | 2006-10-11 08:38:14 +0000 | [diff] [blame] | 101 | if (id > 0 ) { |
cjdebenh | 71f45f0 | 2006-11-08 02:19:57 +0000 | [diff] [blame] | 102 | printf("Deleting %s\n",argv[i]); |
| 103 | ret = LIBMTP_Delete_Object(device, id); |
Linus Walleij | 3725329 | 2006-10-11 08:38:14 +0000 | [diff] [blame] | 104 | } |
cjdebenh | 71f45f0 | 2006-11-08 02:19:57 +0000 | [diff] [blame] | 105 | if ( ret != 0 ) { |
Linus Walleij | 3725329 | 2006-10-11 08:38:14 +0000 | [diff] [blame] | 106 | printf("Failed to delete file:%s\n",argv[i]); |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 107 | LIBMTP_Dump_Errorstack(device); |
| 108 | LIBMTP_Clear_Errorstack(device); |
cjdebenh | 71f45f0 | 2006-11-08 02:19:57 +0000 | [diff] [blame] | 109 | ret = 1; |
| 110 | } |
Linus Walleij | 95698cd | 2006-02-24 10:40:40 +0000 | [diff] [blame] | 111 | } |
Linus Walleij | 843210f | 2010-10-17 22:40:45 +0000 | [diff] [blame] | 112 | return ret; |
Linus Walleij | 95698cd | 2006-02-24 10:40:40 +0000 | [diff] [blame] | 113 | } |
| 114 | |