blob: 3b65a99d28566b348c71ee9d8f5b5006d1ff23e9 [file] [log] [blame]
Linus Walleij95698cd2006-02-24 10:40:40 +00001#include "common.h"
Linus Walleij43ff8fc2006-10-10 11:16:53 +00002#include "string.h"
cjdebenh71f45f02006-11-08 02:19:57 +00003#include "pathutils.h"
Linus Walleij43ff8fc2006-10-10 11:16:53 +00004
cjdebenh71f45f02006-11-08 02:19:57 +00005void delfile_usage(void);
cjdebenhc80f9b42006-11-09 22:29:26 +00006void delfile_function(char *);
7void delfile_command(int, char **);
Linus Walleij95698cd2006-02-24 10:40:40 +00008
cjdebenh71f45f02006-11-08 02:19:57 +00009extern LIBMTP_mtpdevice_t *device;
10extern LIBMTP_folder_t *folders;
11extern LIBMTP_file_t *files;
12
13void delfile_usage(void)
Linus Walleij95698cd2006-02-24 10:40:40 +000014{
Linus Walleij37253292006-10-11 08:38:14 +000015 printf("Usage: delfile [-n] <fileid/trackid> | -f <filename>\n");
Linus Walleij95698cd2006-02-24 10:40:40 +000016}
17
cjdebenh71f45f02006-11-08 02:19:57 +000018void
cjdebenhc80f9b42006-11-09 22:29:26 +000019delfile_function(char * path)
Linus Walleij43ff8fc2006-10-10 11:16:53 +000020{
cjdebenh71f45f02006-11-08 02:19:57 +000021 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 Walleij43ff8fc2006-10-10 11:16:53 +000029 }
Linus Walleij43ff8fc2006-10-10 11:16:53 +000030}
31
cjdebenhc80f9b42006-11-09 22:29:26 +000032void delfile_command(int argc, char **argv)
Linus Walleij43ff8fc2006-10-10 11:16:53 +000033{
Linus Walleij37253292006-10-11 08:38:14 +000034 int FILENAME = 1;
35 int ITEMID = 2;
36 int field_type = 0;
Linus Walleij37253292006-10-11 08:38:14 +000037 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 {
cjdebenh71f45f02006-11-08 02:19:57 +000045 delfile_usage();
46 return;
Linus Walleij43ff8fc2006-10-10 11:16:53 +000047 }
Linus Walleij37253292006-10-11 08:38:14 +000048 } else {
cjdebenh71f45f02006-11-08 02:19:57 +000049 delfile_usage();
50 return;
Linus Walleij95698cd2006-02-24 10:40:40 +000051 }
cjdebenh71f45f02006-11-08 02:19:57 +000052 int i;
Linus Walleij37253292006-10-11 08:38:14 +000053 for (i=1;i<argc;i++) {
cjdebenh71f45f02006-11-08 02:19:57 +000054 int id;
55 char *endptr;
Linus Walleij37253292006-10-11 08:38:14 +000056 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) {
cjdebenh71f45f02006-11-08 02:19:57 +000065 id = parse_path (argv[i],files,folders);
Linus Walleij37253292006-10-11 08:38:14 +000066 } else {
67 id = 0;
68 }
69 }
cjdebenh71f45f02006-11-08 02:19:57 +000070 int ret = 0;
Linus Walleij37253292006-10-11 08:38:14 +000071 if (id > 0 ) {
cjdebenh71f45f02006-11-08 02:19:57 +000072 printf("Deleting %s\n",argv[i]);
73 ret = LIBMTP_Delete_Object(device, id);
Linus Walleij37253292006-10-11 08:38:14 +000074 }
cjdebenh71f45f02006-11-08 02:19:57 +000075 if ( ret != 0 ) {
Linus Walleij37253292006-10-11 08:38:14 +000076 printf("Failed to delete file:%s\n",argv[i]);
cjdebenh71f45f02006-11-08 02:19:57 +000077 ret = 1;
78 }
Linus Walleij95698cd2006-02-24 10:40:40 +000079 }
Linus Walleij95698cd2006-02-24 10:40:40 +000080}
81