blob: 2ae2509da5c03a579c331902e8b1d068ec2fd1e4 [file] [log] [blame]
Linus Walleij543badf2007-02-05 19:07:38 +00001/**
2 * \file delfile.c
3 * Example program to delete a file off the device.
4 *
Linus Walleijc12e1752008-02-18 22:45:45 +00005 * Copyright (C) 2005-2008 Linus Walleij <triad@df.lth.se>
Linus Walleij543badf2007-02-05 19:07:38 +00006 * Copyright (C) 2006 Chris A. Debenham <chris@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 */
Linus Walleij2eaaff02009-01-15 21:30:36 +000023#include <stdlib.h>
24#include <limits.h>
Linus Walleij43ff8fc2006-10-10 11:16:53 +000025
Linus Walleij843210f2010-10-17 22:40:45 +000026#include "common.h"
27#include "string.h"
28#include "pathutils.h"
29#include "connect.h"
Linus Walleij95698cd2006-02-24 10:40:40 +000030
cjdebenh71f45f02006-11-08 02:19:57 +000031extern LIBMTP_mtpdevice_t *device;
32extern LIBMTP_folder_t *folders;
33extern LIBMTP_file_t *files;
34
35void delfile_usage(void)
Linus Walleij95698cd2006-02-24 10:40:40 +000036{
Linus Walleij37253292006-10-11 08:38:14 +000037 printf("Usage: delfile [-n] <fileid/trackid> | -f <filename>\n");
Linus Walleij95698cd2006-02-24 10:40:40 +000038}
39
Linus Walleij843210f2010-10-17 22:40:45 +000040int
cjdebenhc80f9b42006-11-09 22:29:26 +000041delfile_function(char * path)
Linus Walleij43ff8fc2006-10-10 11:16:53 +000042{
Linus Walleijc12e1752008-02-18 22:45:45 +000043 uint32_t id = parse_path (path,files,folders);
44
cjdebenh71f45f02006-11-08 02:19:57 +000045 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) {
Linus Walleij070e9b42007-01-22 08:49:28 +000050 LIBMTP_Dump_Errorstack(device);
51 LIBMTP_Clear_Errorstack(device);
cjdebenh71f45f02006-11-08 02:19:57 +000052 printf("Failed to remove file\n");
Linus Walleij843210f2010-10-17 22:40:45 +000053 return 1;
cjdebenh71f45f02006-11-08 02:19:57 +000054 }
Linus Walleij43ff8fc2006-10-10 11:16:53 +000055 }
Linus Walleij843210f2010-10-17 22:40:45 +000056 return 0;
Linus Walleij43ff8fc2006-10-10 11:16:53 +000057}
58
Linus Walleij843210f2010-10-17 22:40:45 +000059int delfile_command(int argc, char **argv)
Linus Walleij43ff8fc2006-10-10 11:16:53 +000060{
Linus Walleij37253292006-10-11 08:38:14 +000061 int FILENAME = 1;
62 int ITEMID = 2;
63 int field_type = 0;
Linus Walleijc12e1752008-02-18 22:45:45 +000064 int i;
Linus Walleij843210f2010-10-17 22:40:45 +000065 int ret = 0;
Linus Walleijc12e1752008-02-18 22:45:45 +000066
Linus Walleij37253292006-10-11 08:38:14 +000067 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 {
cjdebenh71f45f02006-11-08 02:19:57 +000075 delfile_usage();
Linus Walleij843210f2010-10-17 22:40:45 +000076 return 0;
Linus Walleij43ff8fc2006-10-10 11:16:53 +000077 }
Linus Walleij37253292006-10-11 08:38:14 +000078 } else {
cjdebenh71f45f02006-11-08 02:19:57 +000079 delfile_usage();
Linus Walleij843210f2010-10-17 22:40:45 +000080 return 0;
Linus Walleij95698cd2006-02-24 10:40:40 +000081 }
Linus Walleijc12e1752008-02-18 22:45:45 +000082
Linus Walleij37253292006-10-11 08:38:14 +000083 for (i=1;i<argc;i++) {
Linus Walleijc12e1752008-02-18 22:45:45 +000084 uint32_t id;
cjdebenh71f45f02006-11-08 02:19:57 +000085 char *endptr;
Linus Walleijc12e1752008-02-18 22:45:45 +000086
Linus Walleij37253292006-10-11 08:38:14 +000087 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) {
cjdebenh71f45f02006-11-08 02:19:57 +000096 id = parse_path (argv[i],files,folders);
Linus Walleij37253292006-10-11 08:38:14 +000097 } else {
98 id = 0;
99 }
100 }
Linus Walleij37253292006-10-11 08:38:14 +0000101 if (id > 0 ) {
cjdebenh71f45f02006-11-08 02:19:57 +0000102 printf("Deleting %s\n",argv[i]);
103 ret = LIBMTP_Delete_Object(device, id);
Linus Walleij37253292006-10-11 08:38:14 +0000104 }
cjdebenh71f45f02006-11-08 02:19:57 +0000105 if ( ret != 0 ) {
Linus Walleij37253292006-10-11 08:38:14 +0000106 printf("Failed to delete file:%s\n",argv[i]);
Linus Walleij070e9b42007-01-22 08:49:28 +0000107 LIBMTP_Dump_Errorstack(device);
108 LIBMTP_Clear_Errorstack(device);
cjdebenh71f45f02006-11-08 02:19:57 +0000109 ret = 1;
110 }
Linus Walleij95698cd2006-02-24 10:40:40 +0000111 }
Linus Walleij843210f2010-10-17 22:40:45 +0000112 return ret;
Linus Walleij95698cd2006-02-24 10:40:40 +0000113}
114