delfile.c

00001 
00023 #include "common.h"
00024 #include "string.h"
00025 #include "pathutils.h"
00026 
00027 void delfile_usage(void);
00028 void delfile_function(char *);
00029 void delfile_command(int, char **);
00030 
00031 extern LIBMTP_mtpdevice_t *device;
00032 extern LIBMTP_folder_t *folders;
00033 extern LIBMTP_file_t *files;
00034 
00035 void delfile_usage(void)
00036 {
00037   printf("Usage: delfile [-n] <fileid/trackid> | -f <filename>\n");
00038 }
00039 
00040 void
00041 delfile_function(char * path)
00042 {
00043   uint32_t id = parse_path (path,files,folders);
00044 
00045   if (id > 0) {
00046     printf("Deleting %s which has item_id:%d\n",path,id);
00047     int ret = 1;
00048     ret = LIBMTP_Delete_Object(device, id);
00049     if (ret != 0) {
00050       LIBMTP_Dump_Errorstack(device);
00051       LIBMTP_Clear_Errorstack(device);
00052       printf("Failed to remove file\n");
00053     }
00054   }
00055 }
00056 
00057 void delfile_command(int argc, char **argv)
00058 {
00059   int FILENAME = 1;
00060   int ITEMID = 2;
00061   int field_type = 0;
00062   int i;
00063 
00064   if ( argc > 2 ) {
00065     if (strncmp(argv[1],"-f",2) == 0) {
00066       field_type = FILENAME;
00067       strcpy(argv[1],"");
00068     } else if (strncmp(argv[1],"-n",2) == 0) {
00069       field_type = ITEMID;
00070       strcpy(argv[1],"0");
00071     } else {
00072       delfile_usage();
00073       return;
00074     }
00075   } else {
00076     delfile_usage();
00077     return;
00078   }
00079 
00080   for (i=1;i<argc;i++) {
00081     uint32_t id;
00082     char *endptr;
00083     int ret = 0;
00084 
00085     if (field_type == ITEMID) {
00086       // Sanity check song ID
00087       id = strtoul(argv[i], &endptr, 10);
00088       if ( *endptr != 0 ) {
00089         fprintf(stderr, "illegal value %s .. skipping\n", argv[i]);
00090         id = 0;
00091       }
00092     } else {
00093       if (strlen(argv[i]) > 0) {
00094         id = parse_path (argv[i],files,folders);
00095       } else {
00096         id = 0;
00097       }
00098     }
00099     if (id > 0 ) {
00100       printf("Deleting %s\n",argv[i]);
00101       ret = LIBMTP_Delete_Object(device, id);
00102     }
00103     if ( ret != 0 ) {
00104       printf("Failed to delete file:%s\n",argv[i]);
00105       LIBMTP_Dump_Errorstack(device);
00106       LIBMTP_Clear_Errorstack(device);
00107       ret = 1;
00108     }
00109   }
00110 }
00111 

Generated on Tue Aug 26 14:44:58 2008 for libmtp by  doxygen 1.4.7