3 * Example program that lists all files on a device.
5 * Copyright (C) 2005-2012 Linus Walleij <triad@df.lth.se>
6 * Copyright (C) 2007 Ted Bullock <tbullock@canada.com>
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.
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.
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.
26 static void dump_fileinfo(LIBMTP_file_t *file)
28 printf("File ID: %u\n", file->item_id);
29 if (file->filename != NULL)
30 printf(" Filename: %s\n", file->filename);
32 // This is sort of special...
33 if (file->filesize == (uint32_t) -1) {
34 printf(" None. (abstract file, size = -1)\n");
37 printf(" File size %llu (0x%016I64X) bytes\n", file->filesize, file->filesize);
39 printf(" File size %llu (0x%016llX) bytes\n",
40 (long long unsigned int) file->filesize,
41 (long long unsigned int) file->filesize);
44 printf(" Parent ID: %u\n", file->parent_id);
45 printf(" Storage ID: 0x%08X\n", file->storage_id);
46 printf(" Filetype: %s\n", LIBMTP_Get_Filetype_Description(file->filetype));
50 dump_files(LIBMTP_mtpdevice_t *device, uint32_t storageid, int leaf)
54 /* Get file listing. */
55 files = LIBMTP_Get_Files_And_Folders(device,
59 LIBMTP_Dump_Errorstack(device);
60 LIBMTP_Clear_Errorstack(device);
62 LIBMTP_file_t *file, *tmp;
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);
73 LIBMTP_destroy_file_t(tmp);
78 int main(int argc, char **argv)
80 LIBMTP_raw_device_t *rawdevices;
82 LIBMTP_error_number_t err;
85 fprintf(stdout, "libmtp version: " LIBMTP_VERSION_STRING "\n\n");
89 err = LIBMTP_Detect_Raw_Devices(&rawdevices, &numrawdevices);
92 case LIBMTP_ERROR_NO_DEVICE_ATTACHED:
93 fprintf(stdout, "mtp-files: No Devices have been found\n");
95 case LIBMTP_ERROR_CONNECTING:
96 fprintf(stderr, "mtp-files: There has been an error connecting. Exit\n");
98 case LIBMTP_ERROR_MEMORY_ALLOCATION:
99 fprintf(stderr, "mtp-files: Memory Allocation Error. Exit\n");
102 /* Unknown general errors - This should never execute */
103 case LIBMTP_ERROR_GENERAL:
105 fprintf(stderr, "mtp-files: Unknown error, please report "
106 "this to the libmtp developers\n");
109 /* Successfully connected at least one device, so continue */
110 case LIBMTP_ERROR_NONE:
111 fprintf(stdout, "mtp-files: Successfully connected\n");
116 /* iterate through connected MTP devices */
117 for (i = 0; i < numrawdevices; i++) {
118 LIBMTP_mtpdevice_t *device;
119 LIBMTP_devicestorage_t *storage;
122 device = LIBMTP_Open_Raw_Device_Uncached(&rawdevices[i]);
123 if (device == NULL) {
124 fprintf(stderr, "Unable to open raw device %d\n", i);
128 /* Echo the friendly name so we know which device we are working with */
129 friendlyname = LIBMTP_Get_Friendlyname(device);
130 if (friendlyname == NULL) {
131 printf("Listing File Information on Device with name: (NULL)\n");
133 printf("Listing File Information on Device with name: %s\n", friendlyname);
137 LIBMTP_Dump_Errorstack(device);
138 LIBMTP_Clear_Errorstack(device);
140 /* Loop over storages */
141 for (storage = device->storage; storage != 0; storage = storage->next) {
142 dump_files(device, storage->id, 0);
144 LIBMTP_Release_Device(device);