Home | History | Annotate | Download | only in examples
      1 /**
      2  * \file delfile.c
      3  * Example program to delete a file off the device.
      4  *
      5  * Copyright (C) 2005-2008 Linus Walleij <triad (at) df.lth.se>
      6  * Copyright (C) 2006 Chris A. Debenham <chris (at) adebenham.com>
      7  *
      8  * This library is free software; you can redistribute it and/or
      9  * modify it under the terms of the GNU Lesser General Public
     10  * License as published by the Free Software Foundation; either
     11  * version 2 of the License, or (at your option) any later version.
     12  *
     13  * This library is distributed in the hope that it will be useful,
     14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     16  * Lesser General Public License for more details.
     17  *
     18  * You should have received a copy of the GNU Lesser General Public
     19  * License along with this library; if not, write to the
     20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     21  * Boston, MA 02111-1307, USA.
     22  */
     23 #include <stdlib.h>
     24 #include <limits.h>
     25 
     26 #include "common.h"
     27 #include "string.h"
     28 #include "pathutils.h"
     29 #include "connect.h"
     30 
     31 extern LIBMTP_mtpdevice_t *device;
     32 extern LIBMTP_folder_t *folders;
     33 extern LIBMTP_file_t *files;
     34 
     35 void delfile_usage(void)
     36 {
     37   printf("Usage: delfile [-n] <fileid/trackid> | -f <filename>\n");
     38 }
     39 
     40 int
     41 delfile_function(char * path)
     42 {
     43   uint32_t id = parse_path (path,files,folders);
     44 
     45   if (id > 0) {
     46     printf("Deleting %s which has item_id:%d\n",path,id);
     47     int ret = 1;
     48     ret = LIBMTP_Delete_Object(device, id);
     49     if (ret != 0) {
     50       LIBMTP_Dump_Errorstack(device);
     51       LIBMTP_Clear_Errorstack(device);
     52       printf("Failed to remove file\n");
     53       return 1;
     54     }
     55   }
     56   return 0;
     57 }
     58 
     59 int delfile_command(int argc, char **argv)
     60 {
     61   int FILENAME = 1;
     62   int ITEMID = 2;
     63   int field_type = 0;
     64   int i;
     65   int ret = 0;
     66 
     67   if ( argc > 2 ) {
     68     if (strncmp(argv[1],"-f",2) == 0) {
     69       field_type = FILENAME;
     70       strcpy(argv[1],"");
     71     } else if (strncmp(argv[1],"-n",2) == 0) {
     72       field_type = ITEMID;
     73       strcpy(argv[1],"0");
     74     } else {
     75       delfile_usage();
     76       return 0;
     77     }
     78   } else {
     79     delfile_usage();
     80     return 0;
     81   }
     82 
     83   for (i=1;i<argc;i++) {
     84     uint32_t id;
     85     char *endptr;
     86 
     87     if (field_type == ITEMID) {
     88       // Sanity check song ID
     89       id = strtoul(argv[i], &endptr, 10);
     90       if ( *endptr != 0 ) {
     91         fprintf(stderr, "illegal value %s .. skipping\n", argv[i]);
     92         id = 0;
     93       }
     94     } else {
     95       if (strlen(argv[i]) > 0) {
     96         id = parse_path (argv[i],files,folders);
     97       } else {
     98         id = 0;
     99       }
    100     }
    101     if (id > 0 ) {
    102       printf("Deleting %s\n",argv[i]);
    103       ret = LIBMTP_Delete_Object(device, id);
    104     }
    105     if ( ret != 0 ) {
    106       printf("Failed to delete file:%s\n",argv[i]);
    107       LIBMTP_Dump_Errorstack(device);
    108       LIBMTP_Clear_Errorstack(device);
    109       ret = 1;
    110     }
    111   }
    112   return ret;
    113 }
    114 
    115