delfile.c

00001 #include "common.h"
00002 #include "string.h"
00003 
00004 LIBMTP_folder_t *folders;
00005 LIBMTP_file_t *files;
00006 
00007 static void usage(void)
00008 {
00009   printf("Usage: delfile [-n] <fileid/trackid> | -f <filename>\n");
00010 }
00011 
00012 /* Find the folder_id of a given path
00013  * Runs by walking through folders structure */
00014 static int
00015 lookup_folder_id (LIBMTP_folder_t * folder, char * path, char * parent)
00016 {
00017   int ret = -1;
00018   if (folder == NULL) {
00019     return ret;
00020   }
00021   char * current = malloc (strlen(parent) + strlen(folder->name) + 2);
00022   sprintf(current,"%s/%s",parent,folder->name);
00023   if (strcasecmp (path, current) == 0) {
00024     free (current);
00025     return folder->folder_id;
00026   }
00027   if (strncasecmp (path, current, strlen (current)) == 0) {
00028     ret = lookup_folder_id (folder->child, path, current);
00029   }
00030   free (current);
00031   if (ret >= 0) {
00032     return ret;
00033   }
00034   ret = lookup_folder_id (folder->sibling, path, parent);
00035   return ret;
00036 }
00037 
00038 /* Parses a string to find item_id */
00039 static int
00040 parse_path (char * path)
00041 {
00042   // Check if path is a folder
00043   int item_id = lookup_folder_id(folders,path,"");
00044   if (item_id == -1) {
00045   int len = strlen(strrchr(path,'/'));
00046   char * filename = malloc(len);
00047   int index = strlen (path) - len;
00048   filename = strncpy (filename, &path[index+1],len);
00049   char * parent = malloc(index);
00050   parent = strncpy(parent, path, index);
00051   parent[index] = '\0';
00052   int parent_id = lookup_folder_id(folders,parent,"");
00053     LIBMTP_file_t * file;
00054     file = files;
00055     while (file != NULL) {
00056       if (file->parent_id == parent_id) {
00057       if (strcasecmp (files->filename, filename) == 0) {
00058         int item_id = files->item_id;
00059         return item_id;
00060       }
00061     }
00062       file = file->next;
00063     }
00064   } else {
00065     return item_id;
00066   }
00067 
00068   return 0;
00069 }
00070     
00071   
00072 int main (int argc, char **argv)
00073 {
00074   LIBMTP_mtpdevice_t *device;
00075   u_int32_t id = 0;
00076   int i;
00077   char *endptr;
00078   int ret = 1;
00079   int FILENAME = 1;
00080   int ITEMID = 2;
00081   int field_type = 0;
00082 
00083   if ( argc > 2 ) {
00084     if (strncmp(argv[1],"-f",2) == 0) {
00085       field_type = FILENAME;
00086       strcpy(argv[1],"");
00087     } else if (strncmp(argv[1],"-n",2) == 0) {
00088       field_type = ITEMID;
00089       strcpy(argv[1],"0");
00090     } else {
00091       usage();
00092       return 1;
00093     }
00094   } else {
00095     usage();
00096     return 1;
00097   }
00098   
00099   LIBMTP_Init();
00100   device = LIBMTP_Get_First_Device();
00101   if (device == NULL) {
00102     printf("No devices.\n");
00103     return 0;
00104   }
00105   if (field_type == FILENAME) {
00106     files = LIBMTP_Get_Filelisting (device);
00107     folders = LIBMTP_Get_Folder_List (device);
00108   }
00109 
00110   for (i=1;i<argc;i++) {
00111     if (field_type == ITEMID) {
00112       // Sanity check song ID
00113       id = strtoul(argv[i], &endptr, 10);
00114       if ( *endptr != 0 ) {
00115         fprintf(stderr, "illegal value %s .. skipping\n", argv[i]);
00116         id = 0;
00117       }
00118     } else {
00119       if (strlen(argv[i]) > 0) {
00120         id = parse_path (argv[i]);
00121       } else {
00122         id = 0;
00123       }
00124     }
00125     ret = 0;
00126     if (id > 0 ) {
00127         printf("Deleting %s\n",argv[i]);
00128         ret = LIBMTP_Delete_Object(device, id);
00129     }
00130   if ( ret != 0 ) {
00131       printf("Failed to delete file:%s\n",argv[i]);
00132     ret = 1;
00133   }
00134   }
00135   
00136   
00137   LIBMTP_Release_Device(device);
00138   printf("OK.\n");
00139   return ret;
00140 }
00141 

Generated on Mon Nov 6 02:45:44 2006 for libmtp by  doxygen 1.4.7