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 int id = parse_path (path,files,folders);
00044 if (id > 0) {
00045 printf("Deleting %s which has item_id:%d\n",path,id);
00046 int ret = 1;
00047 ret = LIBMTP_Delete_Object(device, id);
00048 if (ret != 0) {
00049 LIBMTP_Dump_Errorstack(device);
00050 LIBMTP_Clear_Errorstack(device);
00051 printf("Failed to remove file\n");
00052 }
00053 }
00054 }
00055
00056 void delfile_command(int argc, char **argv)
00057 {
00058 int FILENAME = 1;
00059 int ITEMID = 2;
00060 int field_type = 0;
00061 if ( argc > 2 ) {
00062 if (strncmp(argv[1],"-f",2) == 0) {
00063 field_type = FILENAME;
00064 strcpy(argv[1],"");
00065 } else if (strncmp(argv[1],"-n",2) == 0) {
00066 field_type = ITEMID;
00067 strcpy(argv[1],"0");
00068 } else {
00069 delfile_usage();
00070 return;
00071 }
00072 } else {
00073 delfile_usage();
00074 return;
00075 }
00076 int i;
00077 for (i=1;i<argc;i++) {
00078 int id;
00079 char *endptr;
00080 if (field_type == ITEMID) {
00081
00082 id = strtoul(argv[i], &endptr, 10);
00083 if ( *endptr != 0 ) {
00084 fprintf(stderr, "illegal value %s .. skipping\n", argv[i]);
00085 id = 0;
00086 }
00087 } else {
00088 if (strlen(argv[i]) > 0) {
00089 id = parse_path (argv[i],files,folders);
00090 } else {
00091 id = 0;
00092 }
00093 }
00094 int ret = 0;
00095 if (id > 0 ) {
00096 printf("Deleting %s\n",argv[i]);
00097 ret = LIBMTP_Delete_Object(device, id);
00098 }
00099 if ( ret != 0 ) {
00100 printf("Failed to delete file:%s\n",argv[i]);
00101 LIBMTP_Dump_Errorstack(device);
00102 LIBMTP_Clear_Errorstack(device);
00103 ret = 1;
00104 }
00105 }
00106 }
00107