00001 #include "common.h"
00002 #include "string.h"
00003 #include "pathutils.h"
00004
00005 void delfile_usage(void);
00006 void delfile_function(char *);
00007 void delfile_command(int, char **);
00008
00009 extern LIBMTP_mtpdevice_t *device;
00010 extern LIBMTP_folder_t *folders;
00011 extern LIBMTP_file_t *files;
00012
00013 void delfile_usage(void)
00014 {
00015 printf("Usage: delfile [-n] <fileid/trackid> | -f <filename>\n");
00016 }
00017
00018 void
00019 delfile_function(char * path)
00020 {
00021 int id = parse_path (path,files,folders);
00022 if (id > 0) {
00023 printf("Deleting %s which has item_id:%d\n",path,id);
00024 int ret = 1;
00025 ret = LIBMTP_Delete_Object(device, id);
00026 if (ret != 0) {
00027 printf("Failed to remove file\n");
00028 }
00029 }
00030 }
00031
00032 void delfile_command(int argc, char **argv)
00033 {
00034 int FILENAME = 1;
00035 int ITEMID = 2;
00036 int field_type = 0;
00037 if ( argc > 2 ) {
00038 if (strncmp(argv[1],"-f",2) == 0) {
00039 field_type = FILENAME;
00040 strcpy(argv[1],"");
00041 } else if (strncmp(argv[1],"-n",2) == 0) {
00042 field_type = ITEMID;
00043 strcpy(argv[1],"0");
00044 } else {
00045 delfile_usage();
00046 return;
00047 }
00048 } else {
00049 delfile_usage();
00050 return;
00051 }
00052 int i;
00053 for (i=1;i<argc;i++) {
00054 int id;
00055 char *endptr;
00056 if (field_type == ITEMID) {
00057
00058 id = strtoul(argv[i], &endptr, 10);
00059 if ( *endptr != 0 ) {
00060 fprintf(stderr, "illegal value %s .. skipping\n", argv[i]);
00061 id = 0;
00062 }
00063 } else {
00064 if (strlen(argv[i]) > 0) {
00065 id = parse_path (argv[i],files,folders);
00066 } else {
00067 id = 0;
00068 }
00069 }
00070 int ret = 0;
00071 if (id > 0 ) {
00072 printf("Deleting %s\n",argv[i]);
00073 ret = LIBMTP_Delete_Object(device, id);
00074 }
00075 if ( ret != 0 ) {
00076 printf("Failed to delete file:%s\n",argv[i]);
00077 ret = 1;
00078 }
00079 }
00080 }
00081