Linus Walleij | 95698cd | 2006-02-24 10:40:40 +0000 | [diff] [blame] | 1 | #include "common.h" |
Linus Walleij | 43ff8fc | 2006-10-10 11:16:53 +0000 | [diff] [blame] | 2 | #include "string.h" |
cjdebenh | 71f45f0 | 2006-11-08 02:19:57 +0000 | [diff] [blame] | 3 | #include "pathutils.h" |
Linus Walleij | 43ff8fc | 2006-10-10 11:16:53 +0000 | [diff] [blame] | 4 | |
cjdebenh | 71f45f0 | 2006-11-08 02:19:57 +0000 | [diff] [blame] | 5 | void delfile_usage(void); |
cjdebenh | c80f9b4 | 2006-11-09 22:29:26 +0000 | [diff] [blame^] | 6 | void delfile_function(char *); |
| 7 | void delfile_command(int, char **); |
Linus Walleij | 95698cd | 2006-02-24 10:40:40 +0000 | [diff] [blame] | 8 | |
cjdebenh | 71f45f0 | 2006-11-08 02:19:57 +0000 | [diff] [blame] | 9 | extern LIBMTP_mtpdevice_t *device; |
| 10 | extern LIBMTP_folder_t *folders; |
| 11 | extern LIBMTP_file_t *files; |
| 12 | |
| 13 | void delfile_usage(void) |
Linus Walleij | 95698cd | 2006-02-24 10:40:40 +0000 | [diff] [blame] | 14 | { |
Linus Walleij | 3725329 | 2006-10-11 08:38:14 +0000 | [diff] [blame] | 15 | printf("Usage: delfile [-n] <fileid/trackid> | -f <filename>\n"); |
Linus Walleij | 95698cd | 2006-02-24 10:40:40 +0000 | [diff] [blame] | 16 | } |
| 17 | |
cjdebenh | 71f45f0 | 2006-11-08 02:19:57 +0000 | [diff] [blame] | 18 | void |
cjdebenh | c80f9b4 | 2006-11-09 22:29:26 +0000 | [diff] [blame^] | 19 | delfile_function(char * path) |
Linus Walleij | 43ff8fc | 2006-10-10 11:16:53 +0000 | [diff] [blame] | 20 | { |
cjdebenh | 71f45f0 | 2006-11-08 02:19:57 +0000 | [diff] [blame] | 21 | int id = parse_path (path,files,folders); |
| 22 | if (id > 0) { |
| 23 | printf("Deleting %s which has item_id:%d\n",path,id); |
| 24 | int ret = 1; |
| 25 | ret = LIBMTP_Delete_Object(device, id); |
| 26 | if (ret != 0) { |
| 27 | printf("Failed to remove file\n"); |
| 28 | } |
Linus Walleij | 43ff8fc | 2006-10-10 11:16:53 +0000 | [diff] [blame] | 29 | } |
Linus Walleij | 43ff8fc | 2006-10-10 11:16:53 +0000 | [diff] [blame] | 30 | } |
| 31 | |
cjdebenh | c80f9b4 | 2006-11-09 22:29:26 +0000 | [diff] [blame^] | 32 | void delfile_command(int argc, char **argv) |
Linus Walleij | 43ff8fc | 2006-10-10 11:16:53 +0000 | [diff] [blame] | 33 | { |
Linus Walleij | 3725329 | 2006-10-11 08:38:14 +0000 | [diff] [blame] | 34 | int FILENAME = 1; |
| 35 | int ITEMID = 2; |
| 36 | int field_type = 0; |
Linus Walleij | 3725329 | 2006-10-11 08:38:14 +0000 | [diff] [blame] | 37 | if ( argc > 2 ) { |
| 38 | if (strncmp(argv[1],"-f",2) == 0) { |
| 39 | field_type = FILENAME; |
| 40 | strcpy(argv[1],""); |
| 41 | } else if (strncmp(argv[1],"-n",2) == 0) { |
| 42 | field_type = ITEMID; |
| 43 | strcpy(argv[1],"0"); |
| 44 | } else { |
cjdebenh | 71f45f0 | 2006-11-08 02:19:57 +0000 | [diff] [blame] | 45 | delfile_usage(); |
| 46 | return; |
Linus Walleij | 43ff8fc | 2006-10-10 11:16:53 +0000 | [diff] [blame] | 47 | } |
Linus Walleij | 3725329 | 2006-10-11 08:38:14 +0000 | [diff] [blame] | 48 | } else { |
cjdebenh | 71f45f0 | 2006-11-08 02:19:57 +0000 | [diff] [blame] | 49 | delfile_usage(); |
| 50 | return; |
Linus Walleij | 95698cd | 2006-02-24 10:40:40 +0000 | [diff] [blame] | 51 | } |
cjdebenh | 71f45f0 | 2006-11-08 02:19:57 +0000 | [diff] [blame] | 52 | int i; |
Linus Walleij | 3725329 | 2006-10-11 08:38:14 +0000 | [diff] [blame] | 53 | for (i=1;i<argc;i++) { |
cjdebenh | 71f45f0 | 2006-11-08 02:19:57 +0000 | [diff] [blame] | 54 | int id; |
| 55 | char *endptr; |
Linus Walleij | 3725329 | 2006-10-11 08:38:14 +0000 | [diff] [blame] | 56 | if (field_type == ITEMID) { |
| 57 | // Sanity check song ID |
| 58 | id = strtoul(argv[i], &endptr, 10); |
| 59 | if ( *endptr != 0 ) { |
| 60 | fprintf(stderr, "illegal value %s .. skipping\n", argv[i]); |
| 61 | id = 0; |
| 62 | } |
| 63 | } else { |
| 64 | if (strlen(argv[i]) > 0) { |
cjdebenh | 71f45f0 | 2006-11-08 02:19:57 +0000 | [diff] [blame] | 65 | id = parse_path (argv[i],files,folders); |
Linus Walleij | 3725329 | 2006-10-11 08:38:14 +0000 | [diff] [blame] | 66 | } else { |
| 67 | id = 0; |
| 68 | } |
| 69 | } |
cjdebenh | 71f45f0 | 2006-11-08 02:19:57 +0000 | [diff] [blame] | 70 | int ret = 0; |
Linus Walleij | 3725329 | 2006-10-11 08:38:14 +0000 | [diff] [blame] | 71 | if (id > 0 ) { |
cjdebenh | 71f45f0 | 2006-11-08 02:19:57 +0000 | [diff] [blame] | 72 | printf("Deleting %s\n",argv[i]); |
| 73 | ret = LIBMTP_Delete_Object(device, id); |
Linus Walleij | 3725329 | 2006-10-11 08:38:14 +0000 | [diff] [blame] | 74 | } |
cjdebenh | 71f45f0 | 2006-11-08 02:19:57 +0000 | [diff] [blame] | 75 | if ( ret != 0 ) { |
Linus Walleij | 3725329 | 2006-10-11 08:38:14 +0000 | [diff] [blame] | 76 | printf("Failed to delete file:%s\n",argv[i]); |
cjdebenh | 71f45f0 | 2006-11-08 02:19:57 +0000 | [diff] [blame] | 77 | ret = 1; |
| 78 | } |
Linus Walleij | 95698cd | 2006-02-24 10:40:40 +0000 | [diff] [blame] | 79 | } |
Linus Walleij | 95698cd | 2006-02-24 10:40:40 +0000 | [diff] [blame] | 80 | } |
| 81 | |