Linus Walleij | 71c7929 | 2011-03-05 22:08:07 +0100 | [diff] [blame] | 1 | /** |
Linus Walleij | 543badf | 2007-02-05 19:07:38 +0000 | [diff] [blame] | 2 | * \file files.c |
| 3 | * Example program that lists all files on a device. |
| 4 | * |
Linus Walleij | 7fe4a39 | 2012-01-12 22:16:19 +0100 | [diff] [blame] | 5 | * Copyright (C) 2005-2012 Linus Walleij <triad@df.lth.se> |
tedbullock | 5164920 | 2007-02-23 03:04:33 +0000 | [diff] [blame] | 6 | * Copyright (C) 2007 Ted Bullock <tbullock@canada.com> |
Linus Walleij | 543badf | 2007-02-05 19:07:38 +0000 | [diff] [blame] | 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 Walleij | 6fd2f08 | 2006-03-28 07:19:22 +0000 | [diff] [blame] | 23 | #include "common.h" |
Linus Walleij | 2eaaff0 | 2009-01-15 21:30:36 +0000 | [diff] [blame] | 24 | #include <stdlib.h> |
Linus Walleij | 6fd2f08 | 2006-03-28 07:19:22 +0000 | [diff] [blame] | 25 | |
| 26 | static void dump_fileinfo(LIBMTP_file_t *file) |
| 27 | { |
Linus Walleij | 64ad9ae | 2006-11-27 11:23:26 +0000 | [diff] [blame] | 28 | printf("File ID: %u\n", file->item_id); |
Linus Walleij | 6fd2f08 | 2006-03-28 07:19:22 +0000 | [diff] [blame] | 29 | if (file->filename != NULL) |
| 30 | printf(" Filename: %s\n", file->filename); |
Linus Walleij | cd3eb3d | 2006-09-02 21:33:22 +0000 | [diff] [blame] | 31 | |
| 32 | // This is sort of special... |
| 33 | if (file->filesize == (uint32_t) -1) { |
| 34 | printf(" None. (abstract file, size = -1)\n"); |
| 35 | } else { |
Linus Walleij | e73f74a | 2007-04-02 08:51:35 +0000 | [diff] [blame] | 36 | #ifdef __WIN32__ |
Linus Walleij | ca2a170 | 2007-10-02 20:41:45 +0000 | [diff] [blame] | 37 | printf(" File size %llu (0x%016I64X) bytes\n", file->filesize, file->filesize); |
Linus Walleij | e73f74a | 2007-04-02 08:51:35 +0000 | [diff] [blame] | 38 | #else |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 39 | printf(" File size %llu (0x%016llX) bytes\n", |
Linus Walleij | 71c7929 | 2011-03-05 22:08:07 +0100 | [diff] [blame] | 40 | (long long unsigned int) file->filesize, |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 41 | (long long unsigned int) file->filesize); |
Linus Walleij | e73f74a | 2007-04-02 08:51:35 +0000 | [diff] [blame] | 42 | #endif |
Linus Walleij | cd3eb3d | 2006-09-02 21:33:22 +0000 | [diff] [blame] | 43 | } |
Linus Walleij | 64ad9ae | 2006-11-27 11:23:26 +0000 | [diff] [blame] | 44 | printf(" Parent ID: %u\n", file->parent_id); |
Linus Walleij | ea68f1f | 2008-06-22 21:54:44 +0000 | [diff] [blame] | 45 | printf(" Storage ID: 0x%08X\n", file->storage_id); |
Linus Walleij | 16c51f0 | 2006-05-04 13:20:22 +0000 | [diff] [blame] | 46 | printf(" Filetype: %s\n", LIBMTP_Get_Filetype_Description(file->filetype)); |
Linus Walleij | 6fd2f08 | 2006-03-28 07:19:22 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Linus Walleij | 0c754e3 | 2015-08-21 22:19:10 +0200 | [diff] [blame] | 49 | static void |
| 50 | dump_files(LIBMTP_mtpdevice_t *device, uint32_t storageid, int leaf) |
Linus Walleij | 6fd2f08 | 2006-03-28 07:19:22 +0000 | [diff] [blame] | 51 | { |
Linus Walleij | 6fd2f08 | 2006-03-28 07:19:22 +0000 | [diff] [blame] | 52 | LIBMTP_file_t *files; |
| 53 | |
Linus Walleij | bb5322e | 2015-08-21 22:05:06 +0200 | [diff] [blame] | 54 | /* Get file listing. */ |
| 55 | files = LIBMTP_Get_Files_And_Folders(device, |
| 56 | storageid, |
| 57 | leaf); |
| 58 | if (files == NULL) { |
| 59 | LIBMTP_Dump_Errorstack(device); |
| 60 | LIBMTP_Clear_Errorstack(device); |
| 61 | } else { |
| 62 | LIBMTP_file_t *file, *tmp; |
| 63 | file = files; |
| 64 | while (file != NULL) { |
| 65 | /* Please don't print these */ |
| 66 | if (file->filetype == LIBMTP_FILETYPE_FOLDER) { |
| 67 | dump_files(device, storageid, file->item_id); |
| 68 | } else { |
| 69 | dump_fileinfo(file); |
| 70 | } |
| 71 | tmp = file; |
| 72 | file = file->next; |
| 73 | LIBMTP_destroy_file_t(tmp); |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | int main(int argc, char **argv) |
| 79 | { |
| 80 | LIBMTP_raw_device_t *rawdevices; |
| 81 | int numrawdevices; |
Linus Walleij | bb5322e | 2015-08-21 22:05:06 +0200 | [diff] [blame] | 82 | LIBMTP_error_number_t err; |
| 83 | int i; |
| 84 | |
tedbullock | cd9f499 | 2007-03-29 06:00:40 +0000 | [diff] [blame] | 85 | fprintf(stdout, "libmtp version: " LIBMTP_VERSION_STRING "\n\n"); |
| 86 | |
Linus Walleij | 6fd2f08 | 2006-03-28 07:19:22 +0000 | [diff] [blame] | 87 | LIBMTP_Init(); |
tedbullock | 5164920 | 2007-02-23 03:04:33 +0000 | [diff] [blame] | 88 | |
Linus Walleij | bb5322e | 2015-08-21 22:05:06 +0200 | [diff] [blame] | 89 | err = LIBMTP_Detect_Raw_Devices(&rawdevices, &numrawdevices); |
| 90 | switch(err) |
tedbullock | 5164920 | 2007-02-23 03:04:33 +0000 | [diff] [blame] | 91 | { |
tedbullock | 433d217 | 2007-02-23 22:39:12 +0000 | [diff] [blame] | 92 | case LIBMTP_ERROR_NO_DEVICE_ATTACHED: |
tedbullock | 5164920 | 2007-02-23 03:04:33 +0000 | [diff] [blame] | 93 | fprintf(stdout, "mtp-files: No Devices have been found\n"); |
| 94 | return 0; |
| 95 | case LIBMTP_ERROR_CONNECTING: |
| 96 | fprintf(stderr, "mtp-files: There has been an error connecting. Exit\n"); |
| 97 | return 1; |
| 98 | case LIBMTP_ERROR_MEMORY_ALLOCATION: |
| 99 | fprintf(stderr, "mtp-files: Memory Allocation Error. Exit\n"); |
| 100 | return 1; |
Linus Walleij | 71c7929 | 2011-03-05 22:08:07 +0100 | [diff] [blame] | 101 | |
tedbullock | 5164920 | 2007-02-23 03:04:33 +0000 | [diff] [blame] | 102 | /* Unknown general errors - This should never execute */ |
| 103 | case LIBMTP_ERROR_GENERAL: |
| 104 | default: |
| 105 | fprintf(stderr, "mtp-files: Unknown error, please report " |
| 106 | "this to the libmtp developers\n"); |
Linus Walleij | bb5322e | 2015-08-21 22:05:06 +0200 | [diff] [blame] | 107 | return 1; |
tedbullock | 5164920 | 2007-02-23 03:04:33 +0000 | [diff] [blame] | 108 | |
| 109 | /* Successfully connected at least one device, so continue */ |
| 110 | case LIBMTP_ERROR_NONE: |
| 111 | fprintf(stdout, "mtp-files: Successfully connected\n"); |
| 112 | fflush(stdout); |
Linus Walleij | bb5322e | 2015-08-21 22:05:06 +0200 | [diff] [blame] | 113 | break; |
Linus Walleij | 6fd2f08 | 2006-03-28 07:19:22 +0000 | [diff] [blame] | 114 | } |
Linus Walleij | 71c7929 | 2011-03-05 22:08:07 +0100 | [diff] [blame] | 115 | |
tedbullock | 5164920 | 2007-02-23 03:04:33 +0000 | [diff] [blame] | 116 | /* iterate through connected MTP devices */ |
Linus Walleij | bb5322e | 2015-08-21 22:05:06 +0200 | [diff] [blame] | 117 | for (i = 0; i < numrawdevices; i++) { |
| 118 | LIBMTP_mtpdevice_t *device; |
| 119 | LIBMTP_devicestorage_t *storage; |
tedbullock | 5164920 | 2007-02-23 03:04:33 +0000 | [diff] [blame] | 120 | char *friendlyname; |
Linus Walleij | 71c7929 | 2011-03-05 22:08:07 +0100 | [diff] [blame] | 121 | |
Linus Walleij | bb5322e | 2015-08-21 22:05:06 +0200 | [diff] [blame] | 122 | device = LIBMTP_Open_Raw_Device_Uncached(&rawdevices[i]); |
| 123 | if (device == NULL) { |
| 124 | fprintf(stderr, "Unable to open raw device %d\n", i); |
| 125 | continue; |
| 126 | } |
| 127 | |
tedbullock | 5164920 | 2007-02-23 03:04:33 +0000 | [diff] [blame] | 128 | /* Echo the friendly name so we know which device we are working with */ |
Linus Walleij | 7fe4a39 | 2012-01-12 22:16:19 +0100 | [diff] [blame] | 129 | friendlyname = LIBMTP_Get_Friendlyname(device); |
tedbullock | 5164920 | 2007-02-23 03:04:33 +0000 | [diff] [blame] | 130 | if (friendlyname == NULL) { |
| 131 | printf("Listing File Information on Device with name: (NULL)\n"); |
| 132 | } else { |
| 133 | printf("Listing File Information on Device with name: %s\n", friendlyname); |
| 134 | free(friendlyname); |
Linus Walleij | 6fd2f08 | 2006-03-28 07:19:22 +0000 | [diff] [blame] | 135 | } |
Linus Walleij | 71c7929 | 2011-03-05 22:08:07 +0100 | [diff] [blame] | 136 | |
Linus Walleij | bb5322e | 2015-08-21 22:05:06 +0200 | [diff] [blame] | 137 | LIBMTP_Dump_Errorstack(device); |
| 138 | LIBMTP_Clear_Errorstack(device); |
| 139 | |
| 140 | /* Loop over storages */ |
| 141 | for (storage = device->storage; storage != 0; storage = storage->next) { |
Stanisław Pitucha | 4c162fa | 2017-02-04 09:35:54 +1100 | [diff] [blame] | 142 | dump_files(device, storage->id, LIBMTP_FILES_AND_FOLDERS_ROOT); |
Linus Walleij | bb5322e | 2015-08-21 22:05:06 +0200 | [diff] [blame] | 143 | } |
| 144 | LIBMTP_Release_Device(device); |
Linus Walleij | 6fd2f08 | 2006-03-28 07:19:22 +0000 | [diff] [blame] | 145 | } |
Linus Walleij | 71c7929 | 2011-03-05 22:08:07 +0100 | [diff] [blame] | 146 | |
Linus Walleij | bb5322e | 2015-08-21 22:05:06 +0200 | [diff] [blame] | 147 | free(rawdevices); |
| 148 | |
Linus Walleij | 6fd2f08 | 2006-03-28 07:19:22 +0000 | [diff] [blame] | 149 | printf("OK.\n"); |
| 150 | exit (0); |
| 151 | } |