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" |
| 3 | |
| 4 | LIBMTP_folder_t *folders; |
| 5 | LIBMTP_file_t *files; |
Linus Walleij | 95698cd | 2006-02-24 10:40:40 +0000 | [diff] [blame] | 6 | |
| 7 | static void usage(void) |
| 8 | { |
Linus Walleij | 43ff8fc | 2006-10-10 11:16:53 +0000 | [diff] [blame^] | 9 | printf("Usage: delfile <fileid/trackid> <filename>\n"); |
| 10 | printf(" if filename is set then fileid/trackid is ignored\n"); |
Linus Walleij | 95698cd | 2006-02-24 10:40:40 +0000 | [diff] [blame] | 11 | } |
| 12 | |
Linus Walleij | 43ff8fc | 2006-10-10 11:16:53 +0000 | [diff] [blame^] | 13 | /* Find the folder_id of a given path |
| 14 | * Runs by walking through folders structure */ |
| 15 | static int |
| 16 | lookup_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 */ |
| 40 | static int |
| 41 | parse_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 Walleij | 95698cd | 2006-02-24 10:40:40 +0000 | [diff] [blame] | 66 | int main (int argc, char **argv) |
| 67 | { |
| 68 | LIBMTP_mtpdevice_t *device; |
Linus Walleij | 43ff8fc | 2006-10-10 11:16:53 +0000 | [diff] [blame^] | 69 | u_int32_t id = 0; |
Linus Walleij | 95698cd | 2006-02-24 10:40:40 +0000 | [diff] [blame] | 70 | char *endptr; |
Linus Walleij | 43ff8fc | 2006-10-10 11:16:53 +0000 | [diff] [blame^] | 71 | int ret = 1; |
Linus Walleij | 95698cd | 2006-02-24 10:40:40 +0000 | [diff] [blame] | 72 | |
Linus Walleij | 43ff8fc | 2006-10-10 11:16:53 +0000 | [diff] [blame^] | 73 | 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 Walleij | 95698cd | 2006-02-24 10:40:40 +0000 | [diff] [blame] | 86 | 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 Walleij | 43ff8fc | 2006-10-10 11:16:53 +0000 | [diff] [blame^] | 97 | 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 Walleij | 95698cd | 2006-02-24 10:40:40 +0000 | [diff] [blame] | 105 | |
| 106 | if ( ret != 0 ) { |
Linus Walleij | f6bc178 | 2006-03-24 15:12:47 +0000 | [diff] [blame] | 107 | printf("Failed to delete file.\n"); |
Linus Walleij | 43ff8fc | 2006-10-10 11:16:53 +0000 | [diff] [blame^] | 108 | ret = 1; |
Linus Walleij | 95698cd | 2006-02-24 10:40:40 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | LIBMTP_Release_Device(device); |
| 112 | printf("OK.\n"); |
Linus Walleij | 43ff8fc | 2006-10-10 11:16:53 +0000 | [diff] [blame^] | 113 | return ret; |
Linus Walleij | 95698cd | 2006-02-24 10:40:40 +0000 | [diff] [blame] | 114 | } |
| 115 | |