Linus Walleij | 71c7929 | 2011-03-05 22:08:07 +0100 | [diff] [blame] | 1 | /** |
| 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 */ |
| 31 | void recursive_file_tree(LIBMTP_mtpdevice_t *, |
| 32 | LIBMTP_devicestorage_t *, |
| 33 | uint32_t, |
| 34 | int); |
| 35 | |
| 36 | void 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 Walleij | 8a19928 | 2011-03-05 22:28:41 +0100 | [diff] [blame] | 45 | storage->id, |
Linus Walleij | 71c7929 | 2011-03-05 22:08:07 +0100 | [diff] [blame] | 46 | leaf); |
| 47 | if (files == NULL) { |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | /* Iterate over the filelisting */ |
Lei Zhang | 9f1d7ad | 2013-09-04 17:45:30 -0700 | [diff] [blame] | 52 | file = files; |
| 53 | while (file != NULL) { |
Linus Walleij | 71c7929 | 2011-03-05 22:08:07 +0100 | [diff] [blame] | 54 | int i; |
Lei Zhang | 9f1d7ad | 2013-09-04 17:45:30 -0700 | [diff] [blame] | 55 | LIBMTP_file_t *oldfile; |
Linus Walleij | 71c7929 | 2011-03-05 22:08:07 +0100 | [diff] [blame] | 56 | |
| 57 | /* Indent */ |
| 58 | for (i = 0; i < depth; i++) { |
| 59 | printf(" "); |
| 60 | } |
| 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); |
| 64 | } |
Lei Zhang | 9f1d7ad | 2013-09-04 17:45:30 -0700 | [diff] [blame] | 65 | |
| 66 | oldfile = file; |
| 67 | file = file->next; |
| 68 | LIBMTP_destroy_file_t(oldfile); |
Linus Walleij | 71c7929 | 2011-03-05 22:08:07 +0100 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
| 72 | int main (int argc, char **argv) |
| 73 | { |
| 74 | LIBMTP_raw_device_t * rawdevices; |
| 75 | int numrawdevices; |
| 76 | LIBMTP_error_number_t err; |
| 77 | int i; |
| 78 | |
| 79 | int opt; |
| 80 | extern int optind; |
| 81 | extern char *optarg; |
| 82 | |
| 83 | while ((opt = getopt(argc, argv, "d")) != -1 ) { |
| 84 | switch (opt) { |
| 85 | case 'd': |
Catalin Patulea | 406d5f9 | 2012-07-20 19:00:24 -0400 | [diff] [blame] | 86 | LIBMTP_Set_Debug(LIBMTP_DEBUG_PTP | LIBMTP_DEBUG_DATA); |
Linus Walleij | 71c7929 | 2011-03-05 22:08:07 +0100 | [diff] [blame] | 87 | break; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | argc -= optind; |
| 92 | argv += optind; |
| 93 | |
| 94 | LIBMTP_Init(); |
| 95 | |
| 96 | err = LIBMTP_Detect_Raw_Devices(&rawdevices, &numrawdevices); |
| 97 | switch(err) { |
| 98 | case LIBMTP_ERROR_NO_DEVICE_ATTACHED: |
| 99 | fprintf(stdout, " No raw devices found.\n"); |
| 100 | return 0; |
| 101 | case LIBMTP_ERROR_CONNECTING: |
| 102 | fprintf(stderr, "Detect: There has been an error connecting. Exiting\n"); |
| 103 | return 1; |
| 104 | case LIBMTP_ERROR_MEMORY_ALLOCATION: |
| 105 | fprintf(stderr, "Detect: Encountered a Memory Allocation Error. Exiting\n"); |
| 106 | return 1; |
| 107 | case LIBMTP_ERROR_NONE: |
| 108 | break; |
| 109 | case LIBMTP_ERROR_GENERAL: |
| 110 | default: |
| 111 | fprintf(stderr, "Unknown connection error.\n"); |
| 112 | return 1; |
| 113 | } |
| 114 | |
| 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; |
| 120 | char *friendlyname; |
| 121 | int ret; |
| 122 | |
| 123 | device = LIBMTP_Open_Raw_Device_Uncached(&rawdevices[i]); |
| 124 | if (device == NULL) { |
| 125 | fprintf(stderr, "Unable to open raw device %d\n", i); |
| 126 | continue; |
| 127 | } |
| 128 | |
| 129 | LIBMTP_Dump_Errorstack(device); |
| 130 | LIBMTP_Clear_Errorstack(device); |
| 131 | |
| 132 | friendlyname = LIBMTP_Get_Friendlyname(device); |
| 133 | if (friendlyname == NULL) { |
| 134 | printf("Device: (NULL)\n"); |
| 135 | } else { |
| 136 | printf("Device: %s\n", friendlyname); |
| 137 | free(friendlyname); |
| 138 | } |
| 139 | |
| 140 | /* Get all storages for this device */ |
| 141 | ret = LIBMTP_Get_Storage(device, LIBMTP_STORAGE_SORTBY_NOTSORTED); |
| 142 | if (ret != 0) { |
Catalin Patulea | da25de0 | 2012-07-19 00:57:42 -0400 | [diff] [blame] | 143 | perror("LIBMTP_Get_Storage()"); |
Linus Walleij | 71c7929 | 2011-03-05 22:08:07 +0100 | [diff] [blame] | 144 | goto bailout; |
| 145 | } |
| 146 | |
| 147 | /* Loop over storages */ |
| 148 | for (storage = device->storage; storage != 0; storage = storage->next) { |
Linus Walleij | 1e4dfea | 2011-03-05 22:50:39 +0100 | [diff] [blame] | 149 | fprintf(stdout, "Storage: %s\n", storage->StorageDescription); |
Stanisław Pitucha | 4c162fa | 2017-02-04 09:35:54 +1100 | [diff] [blame] | 150 | recursive_file_tree(device, storage, LIBMTP_FILES_AND_FOLDERS_ROOT, 0); |
Linus Walleij | 71c7929 | 2011-03-05 22:08:07 +0100 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | bailout: |
| 154 | LIBMTP_Release_Device(device); |
| 155 | } /* End For Loop */ |
| 156 | |
| 157 | free(rawdevices); |
| 158 | |
| 159 | printf("OK.\n"); |
| 160 | |
| 161 | return 0; |
| 162 | } |