blob: 6427f442757c649ef8ca6dba7f12cfcbd766cd8c [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"
3
4LIBMTP_folder_t *folders;
5LIBMTP_file_t *files;
Linus Walleij95698cd2006-02-24 10:40:40 +00006
7static void usage(void)
8{
Linus Walleij43ff8fc2006-10-10 11:16:53 +00009 printf("Usage: delfile <fileid/trackid> <filename>\n");
10 printf(" if filename is set then fileid/trackid is ignored\n");
Linus Walleij95698cd2006-02-24 10:40:40 +000011}
12
Linus Walleij43ff8fc2006-10-10 11:16:53 +000013/* Find the folder_id of a given path
14 * Runs by walking through folders structure */
15static int
16lookup_folder_id (LIBMTP_folder_t * folder, char * path, char * parent)
17{
18 int ret = -1;
19 if (folder == NULL) {
20 return ret;
21 }
22 char * current = malloc (strlen(parent) + strlen(folder->name) + 2);
23 sprintf(current,"%s/%s",parent,folder->name);
24 if (strcasecmp (path, current) == 0) {
25 free (current);
26 return folder->folder_id;
27 }
28 if (strncasecmp (path, current, strlen (current)) == 0) {
29 ret = lookup_folder_id (folder->child, path, current);
30 }
31 free (current);
32 if (ret >= 0) {
33 return ret;
34 }
35 ret = lookup_folder_id (folder->sibling, path, parent);
36 return ret;
37}
38
39/* Parses a string to find item_id */
40static int
41parse_path (const char * path)
42{
43 int len = strlen(strrchr(path,'/'));
44 char * filename = malloc(len);
45 int index = strlen (path) - len;
46 filename = strncpy (filename, &path[index+1],len);
47 char * parent = malloc(index);
48 parent = strncpy(parent, path, index);
49 parent[index] = '\0';
50 int parent_id = lookup_folder_id(folders,parent,"");
51
52 while (files != NULL) {
53 if (files->parent_id == parent_id) {
54 if (strcasecmp (files->filename, filename) == 0) {
55 int item_id = files->item_id;
56 return item_id;
57 }
58 }
59 files = files->next;
60 }
61
62 return 0;
63}
64
65
Linus Walleij95698cd2006-02-24 10:40:40 +000066int main (int argc, char **argv)
67{
68 LIBMTP_mtpdevice_t *device;
Linus Walleij43ff8fc2006-10-10 11:16:53 +000069 u_int32_t id = 0;
Linus Walleij95698cd2006-02-24 10:40:40 +000070 char *endptr;
Linus Walleij43ff8fc2006-10-10 11:16:53 +000071 int ret = 1;
Linus Walleij95698cd2006-02-24 10:40:40 +000072
Linus Walleij43ff8fc2006-10-10 11:16:53 +000073 if ( argc == 2 ) {
74 // Sanity check song ID
75 id = strtoul(argv[1], &endptr, 10);
76 if ( *endptr != 0 ) {
77 fprintf(stderr, "illegal value %s\n", argv[1]);
78 usage();
79 return 1;
80 } else if ( ! id ) {
81 fprintf(stderr, "bad file ID %u\n", id);
82 usage();
83 return 1;
84 }
85 } else if (argc != 3) {
Linus Walleij95698cd2006-02-24 10:40:40 +000086 usage();
87 return 1;
88 }
89
90 LIBMTP_Init();
91 device = LIBMTP_Get_First_Device();
92 if (device == NULL) {
93 printf("No devices.\n");
94 return 0;
95 }
96
Linus Walleij43ff8fc2006-10-10 11:16:53 +000097 if (argc == 3) {
98 files = LIBMTP_Get_Filelisting (device);
99 folders = LIBMTP_Get_Folder_List (device);
100 id = parse_path (argv[2]);
101 printf ("%d\n",id);
102 }
103
104 if (id > 0 ) ret = LIBMTP_Delete_Object(device, id);
Linus Walleij95698cd2006-02-24 10:40:40 +0000105
106 if ( ret != 0 ) {
Linus Walleijf6bc1782006-03-24 15:12:47 +0000107 printf("Failed to delete file.\n");
Linus Walleij43ff8fc2006-10-10 11:16:53 +0000108 ret = 1;
Linus Walleij95698cd2006-02-24 10:40:40 +0000109 }
110
111 LIBMTP_Release_Device(device);
112 printf("OK.\n");
Linus Walleij43ff8fc2006-10-10 11:16:53 +0000113 return ret;
Linus Walleij95698cd2006-02-24 10:40:40 +0000114}
115