Blame examples/delfile.c

Packit Bot a284d4
/** 
Packit Bot a284d4
 * \file delfile.c
Packit Bot a284d4
 * Example program to delete a file off the device.
Packit Bot a284d4
 *
Packit Bot a284d4
 * Copyright (C) 2005-2008 Linus Walleij <triad@df.lth.se>
Packit Bot a284d4
 * Copyright (C) 2006 Chris A. Debenham <chris@adebenham.com>
Packit Bot a284d4
 *
Packit Bot a284d4
 * This library is free software; you can redistribute it and/or
Packit Bot a284d4
 * modify it under the terms of the GNU Lesser General Public
Packit Bot a284d4
 * License as published by the Free Software Foundation; either
Packit Bot a284d4
 * version 2 of the License, or (at your option) any later version.
Packit Bot a284d4
 *
Packit Bot a284d4
 * This library is distributed in the hope that it will be useful,
Packit Bot a284d4
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Bot a284d4
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Bot a284d4
 * Lesser General Public License for more details.
Packit Bot a284d4
 *
Packit Bot a284d4
 * You should have received a copy of the GNU Lesser General Public
Packit Bot a284d4
 * License along with this library; if not, write to the
Packit Bot a284d4
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Packit Bot a284d4
 * Boston, MA 02111-1307, USA.
Packit Bot a284d4
 */
Packit Bot a284d4
#include <stdlib.h>
Packit Bot a284d4
#include <limits.h>
Packit Bot a284d4
Packit Bot a284d4
#include "common.h"
Packit Bot a284d4
#include "string.h"
Packit Bot a284d4
#include "pathutils.h"
Packit Bot a284d4
#include "connect.h"
Packit Bot a284d4
Packit Bot a284d4
extern LIBMTP_mtpdevice_t *device;
Packit Bot a284d4
extern LIBMTP_folder_t *folders;
Packit Bot a284d4
extern LIBMTP_file_t *files;
Packit Bot a284d4
Packit Bot a284d4
void delfile_usage(void)
Packit Bot a284d4
{
Packit Bot a284d4
  printf("Usage: delfile [-n] <fileid/trackid> | -f <filename>\n");
Packit Bot a284d4
}
Packit Bot a284d4
Packit Bot a284d4
int
Packit Bot a284d4
delfile_function(char * path)
Packit Bot a284d4
{
Packit Bot a284d4
  uint32_t id = parse_path (path,files,folders);
Packit Bot a284d4
Packit Bot a284d4
  if (id > 0) {
Packit Bot a284d4
    printf("Deleting %s which has item_id:%d\n",path,id);
Packit Bot a284d4
    int ret = 1;
Packit Bot a284d4
    ret = LIBMTP_Delete_Object(device, id);
Packit Bot a284d4
    if (ret != 0) {
Packit Bot a284d4
      LIBMTP_Dump_Errorstack(device);
Packit Bot a284d4
      LIBMTP_Clear_Errorstack(device);
Packit Bot a284d4
      printf("Failed to remove file\n");
Packit Bot a284d4
      return 1;
Packit Bot a284d4
    }
Packit Bot a284d4
  }
Packit Bot a284d4
  return 0;
Packit Bot a284d4
}
Packit Bot a284d4
Packit Bot a284d4
int delfile_command(int argc, char **argv)
Packit Bot a284d4
{
Packit Bot a284d4
  int FILENAME = 1;
Packit Bot a284d4
  int ITEMID = 2;
Packit Bot a284d4
  int field_type = 0;
Packit Bot a284d4
  int i;
Packit Bot a284d4
  int ret = 0;
Packit Bot a284d4
Packit Bot a284d4
  if ( argc > 2 ) {
Packit Bot a284d4
    if (strncmp(argv[1],"-f",2) == 0) {
Packit Bot a284d4
      field_type = FILENAME;
Packit Bot a284d4
      strcpy(argv[1],"");
Packit Bot a284d4
    } else if (strncmp(argv[1],"-n",2) == 0) {
Packit Bot a284d4
      field_type = ITEMID;
Packit Bot a284d4
      strcpy(argv[1],"0");
Packit Bot a284d4
    } else {
Packit Bot a284d4
      delfile_usage();
Packit Bot a284d4
      return 0;
Packit Bot a284d4
    }
Packit Bot a284d4
  } else {
Packit Bot a284d4
    delfile_usage();
Packit Bot a284d4
    return 0;
Packit Bot a284d4
  }
Packit Bot a284d4
Packit Bot a284d4
  for (i=1;i
Packit Bot a284d4
    uint32_t id;
Packit Bot a284d4
    char *endptr;
Packit Bot a284d4
Packit Bot a284d4
    if (field_type == ITEMID) {
Packit Bot a284d4
      // Sanity check song ID
Packit Bot a284d4
      id = strtoul(argv[i], &endptr, 10);
Packit Bot a284d4
      if ( *endptr != 0 ) {
Packit Bot a284d4
        fprintf(stderr, "illegal value %s .. skipping\n", argv[i]);
Packit Bot a284d4
        id = 0;
Packit Bot a284d4
      }
Packit Bot a284d4
    } else {
Packit Bot a284d4
      if (strlen(argv[i]) > 0) {
Packit Bot a284d4
        id = parse_path (argv[i],files,folders);
Packit Bot a284d4
      } else {
Packit Bot a284d4
        id = 0;
Packit Bot a284d4
      }
Packit Bot a284d4
    }
Packit Bot a284d4
    if (id > 0 ) {
Packit Bot a284d4
      printf("Deleting %s\n",argv[i]);
Packit Bot a284d4
      ret = LIBMTP_Delete_Object(device, id);
Packit Bot a284d4
    }
Packit Bot a284d4
    if ( ret != 0 ) {
Packit Bot a284d4
      printf("Failed to delete file:%s\n",argv[i]);
Packit Bot a284d4
      LIBMTP_Dump_Errorstack(device);
Packit Bot a284d4
      LIBMTP_Clear_Errorstack(device);
Packit Bot a284d4
      ret = 1;
Packit Bot a284d4
    }
Packit Bot a284d4
  }
Packit Bot a284d4
  return ret;
Packit Bot a284d4
}
Packit Bot a284d4