3 * List all files and folders of all storages recursively
5 * Copyright (C) 2011 Linus Walleij <triad@df.lth.se>
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.
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.
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.
30 /* Clever prototype to be able to recurse */
31 void recursive_file_tree(LIBMTP_mtpdevice_t *,
32 LIBMTP_devicestorage_t *,
36 void recursive_file_tree(LIBMTP_mtpdevice_t *device,
37 LIBMTP_devicestorage_t *storage,
44 files = LIBMTP_Get_Files_And_Folders(device,
51 /* Iterate over the filelisting */
53 while (file != NULL) {
55 LIBMTP_file_t *oldfile;
58 for (i = 0; i < depth; i++) {
61 printf("%u %s\n", file->item_id, file->filename);
62 if (file->filetype == LIBMTP_FILETYPE_FOLDER) {
63 recursive_file_tree(device, storage, file->item_id, depth+2);
68 LIBMTP_destroy_file_t(oldfile);
72 int main (int argc, char **argv)
74 LIBMTP_raw_device_t * rawdevices;
76 LIBMTP_error_number_t err;
83 while ((opt = getopt(argc, argv, "d")) != -1 ) {
86 LIBMTP_Set_Debug(LIBMTP_DEBUG_PTP | LIBMTP_DEBUG_DATA);
96 err = LIBMTP_Detect_Raw_Devices(&rawdevices, &numrawdevices);
98 case LIBMTP_ERROR_NO_DEVICE_ATTACHED:
99 fprintf(stdout, " No raw devices found.\n");
101 case LIBMTP_ERROR_CONNECTING:
102 fprintf(stderr, "Detect: There has been an error connecting. Exiting\n");
104 case LIBMTP_ERROR_MEMORY_ALLOCATION:
105 fprintf(stderr, "Detect: Encountered a Memory Allocation Error. Exiting\n");
107 case LIBMTP_ERROR_NONE:
109 case LIBMTP_ERROR_GENERAL:
111 fprintf(stderr, "Unknown connection error.\n");
115 /* Iterate over connected MTP devices */
116 fprintf(stdout, "Attempting to connect device(s)\n");
117 for (i = 0; i < numrawdevices; i++) {
118 LIBMTP_mtpdevice_t *device;
119 LIBMTP_devicestorage_t *storage;
123 device = LIBMTP_Open_Raw_Device_Uncached(&rawdevices[i]);
124 if (device == NULL) {
125 fprintf(stderr, "Unable to open raw device %d\n", i);
129 LIBMTP_Dump_Errorstack(device);
130 LIBMTP_Clear_Errorstack(device);
132 friendlyname = LIBMTP_Get_Friendlyname(device);
133 if (friendlyname == NULL) {
134 printf("Device: (NULL)\n");
136 printf("Device: %s\n", friendlyname);
140 /* Get all storages for this device */
141 ret = LIBMTP_Get_Storage(device, LIBMTP_STORAGE_SORTBY_NOTSORTED);
143 perror("LIBMTP_Get_Storage()");
147 /* Loop over storages */
148 for (storage = device->storage; storage != 0; storage = storage->next) {
149 fprintf(stdout, "Storage: %s\n", storage->StorageDescription);
150 recursive_file_tree(device, storage, 0, 0);
154 LIBMTP_Release_Device(device);