blob: dcfea7ef3889869dbe12654dc2d0ea27c4aa71ce [file] [log] [blame]
Linus Walleij71c79292011-03-05 22:08:07 +01001/**
2 * \file filetree.c
3 * List all files and folders of all storages recursively
4 *
5 * Copyright (C) 2011 Linus Walleij <triad@df.lth.se>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
21 */
22#include "common.h"
23#include "util.h"
24#include <unistd.h>
25#include <stdlib.h>
26#include <stdio.h>
27#include <string.h>
28#include <errno.h>
29
30/* Clever prototype to be able to recurse */
31void recursive_file_tree(LIBMTP_mtpdevice_t *,
32 LIBMTP_devicestorage_t *,
33 uint32_t,
34 int);
35
36void recursive_file_tree(LIBMTP_mtpdevice_t *device,
37 LIBMTP_devicestorage_t *storage,
38 uint32_t leaf,
39 int depth)
40{
41 LIBMTP_file_t *files;
42 LIBMTP_file_t *file;
43
44 files = LIBMTP_Get_Files_And_Folders(device,
Linus Walleij8a199282011-03-05 22:28:41 +010045 storage->id,
Linus Walleij71c79292011-03-05 22:08:07 +010046 leaf);
47 if (files == NULL) {
48 return;
49 }
50
51 /* Iterate over the filelisting */
52 for (file = files; file != NULL; file = file->next) {
53 int i;
54
55 /* Indent */
56 for (i = 0; i < depth; i++) {
57 printf(" ");
58 }
59 printf("%u %s\n", file->item_id, file->filename);
60 if (file->filetype == LIBMTP_FILETYPE_FOLDER) {
61 recursive_file_tree(device, storage, file->item_id, depth+2);
62 }
63 }
64}
65
66int main (int argc, char **argv)
67{
68 LIBMTP_raw_device_t * rawdevices;
69 int numrawdevices;
70 LIBMTP_error_number_t err;
71 int i;
72
73 int opt;
74 extern int optind;
75 extern char *optarg;
76
77 while ((opt = getopt(argc, argv, "d")) != -1 ) {
78 switch (opt) {
79 case 'd':
80 LIBMTP_Set_Debug(9);
81 break;
82 }
83 }
84
85 argc -= optind;
86 argv += optind;
87
88 LIBMTP_Init();
89
90 err = LIBMTP_Detect_Raw_Devices(&rawdevices, &numrawdevices);
91 switch(err) {
92 case LIBMTP_ERROR_NO_DEVICE_ATTACHED:
93 fprintf(stdout, " No raw devices found.\n");
94 return 0;
95 case LIBMTP_ERROR_CONNECTING:
96 fprintf(stderr, "Detect: There has been an error connecting. Exiting\n");
97 return 1;
98 case LIBMTP_ERROR_MEMORY_ALLOCATION:
99 fprintf(stderr, "Detect: Encountered a Memory Allocation Error. Exiting\n");
100 return 1;
101 case LIBMTP_ERROR_NONE:
102 break;
103 case LIBMTP_ERROR_GENERAL:
104 default:
105 fprintf(stderr, "Unknown connection error.\n");
106 return 1;
107 }
108
109 /* Iterate over connected MTP devices */
110 fprintf(stdout, "Attempting to connect device(s)\n");
111 for (i = 0; i < numrawdevices; i++) {
112 LIBMTP_mtpdevice_t *device;
113 LIBMTP_devicestorage_t *storage;
114 char *friendlyname;
115 int ret;
116
117 device = LIBMTP_Open_Raw_Device_Uncached(&rawdevices[i]);
118 if (device == NULL) {
119 fprintf(stderr, "Unable to open raw device %d\n", i);
120 continue;
121 }
122
123 LIBMTP_Dump_Errorstack(device);
124 LIBMTP_Clear_Errorstack(device);
125
126 friendlyname = LIBMTP_Get_Friendlyname(device);
127 if (friendlyname == NULL) {
128 printf("Device: (NULL)\n");
129 } else {
130 printf("Device: %s\n", friendlyname);
131 free(friendlyname);
132 }
133
134 /* Get all storages for this device */
135 ret = LIBMTP_Get_Storage(device, LIBMTP_STORAGE_SORTBY_NOTSORTED);
136 if (ret != 0) {
137 perror("LIBMTP_Get_Storage()\n");
138 goto bailout;
139 }
140
141 /* Loop over storages */
142 for (storage = device->storage; storage != 0; storage = storage->next) {
Linus Walleij1e4dfea2011-03-05 22:50:39 +0100143 fprintf(stdout, "Storage: %s\n", storage->StorageDescription);
Linus Walleij71c79292011-03-05 22:08:07 +0100144 recursive_file_tree(device, storage, 0, 0);
145 }
146
147 bailout:
148 LIBMTP_Release_Device(device);
149 } /* End For Loop */
150
151 free(rawdevices);
152
153 printf("OK.\n");
154
155 return 0;
156}