files.c
#include "common.h"
#include <stdlib.h>
static void dump_fileinfo(LIBMTP_file_t *file)
{
printf("File ID: %u\n", file->item_id);
if (file->filename != NULL)
printf(" Filename: %s\n", file->filename);
// This is sort of special...
if (file->filesize == (uint32_t) -1) {
printf(" None. (abstract file, size = -1)\n");
} else {
#ifdef __WIN32__
printf(" File size %llu (0x%016I64X) bytes\n", file->filesize, file->filesize);
#else
printf(" File size %llu (0x%016llX) bytes\n",
(long long unsigned int) file->filesize,
(long long unsigned int) file->filesize);
#endif
}
printf(" Parent ID: %u\n", file->parent_id);
printf(" Storage ID: 0x%08X\n", file->storage_id);
printf(" Filetype: %s\n", LIBMTP_Get_Filetype_Description(file->filetype));
}
static void
dump_files(LIBMTP_mtpdevice_t *device, uint32_t storageid, int leaf)
{
LIBMTP_file_t *files;
/* Get file listing. */
files = LIBMTP_Get_Files_And_Folders(device,
storageid,
leaf);
if (files == NULL) {
LIBMTP_Dump_Errorstack(device);
LIBMTP_Clear_Errorstack(device);
} else {
LIBMTP_file_t *file, *tmp;
file = files;
while (file != NULL) {
/* Please don't print these */
if (file->filetype == LIBMTP_FILETYPE_FOLDER) {
dump_files(device, storageid, file->item_id);
} else {
dump_fileinfo(file);
}
tmp = file;
file = file->next;
LIBMTP_destroy_file_t(tmp);
}
}
}
int main(int argc, char **argv)
{
LIBMTP_raw_device_t *rawdevices;
int numrawdevices;
LIBMTP_error_number_t err;
int i;
fprintf(stdout, "libmtp version: " LIBMTP_VERSION_STRING "\n\n");
LIBMTP_Init();
err = LIBMTP_Detect_Raw_Devices(&rawdevices, &numrawdevices);
switch(err)
{
case LIBMTP_ERROR_NO_DEVICE_ATTACHED:
fprintf(stdout, "mtp-files: No Devices have been found\n");
return 0;
case LIBMTP_ERROR_CONNECTING:
fprintf(stderr, "mtp-files: There has been an error connecting. Exit\n");
return 1;
case LIBMTP_ERROR_MEMORY_ALLOCATION:
fprintf(stderr, "mtp-files: Memory Allocation Error. Exit\n");
return 1;
/* Unknown general errors - This should never execute */
case LIBMTP_ERROR_GENERAL:
default:
fprintf(stderr, "mtp-files: Unknown error, please report "
"this to the libmtp developers\n");
return 1;
/* Successfully connected at least one device, so continue */
case LIBMTP_ERROR_NONE:
fprintf(stdout, "mtp-files: Successfully connected\n");
fflush(stdout);
break;
}
/* iterate through connected MTP devices */
for (i = 0; i < numrawdevices; i++) {
LIBMTP_mtpdevice_t *device;
LIBMTP_devicestorage_t *storage;
char *friendlyname;
char *serialnr;
device = LIBMTP_Open_Raw_Device_Uncached(&rawdevices[i]);
if (device == NULL) {
fprintf(stderr, "Unable to open raw device %d\n", i);
continue;
}
/* Echo the friendly name so we know which device we are working with */
friendlyname = LIBMTP_Get_Friendlyname(device);
serialnr = LIBMTP_Get_Serialnumber(device);
if (friendlyname == NULL) {
printf("Listing File Information on Device with name: (NULL) [SN:%s]\n",
serialnr);
} else {
printf("Listing File Information on Device with name: %s [SN:%s]\n",
friendlyname, serialnr);
LIBMTP_FreeMemory(friendlyname);
}
LIBMTP_Dump_Errorstack(device);
LIBMTP_Clear_Errorstack(device);
/* Loop over storages */
for (storage = device->storage; storage != 0; storage = storage->next) {
dump_files(device, storage->id, LIBMTP_FILES_AND_FOLDERS_ROOT);
}
LIBMTP_Release_Device(device);
}
LIBMTP_FreeMemory(rawdevices);
printf("OK.\n");
exit (0);
}
LIBMTP_FreeMemory
void LIBMTP_FreeMemory(void *mem)
Definition libmtp.c:9472
LIBMTP_Open_Raw_Device_Uncached
LIBMTP_mtpdevice_t * LIBMTP_Open_Raw_Device_Uncached(LIBMTP_raw_device_t *rawdevice)
Definition libmtp.c:1921
LIBMTP_Release_Device
void LIBMTP_Release_Device(LIBMTP_mtpdevice_t *device)
Definition libmtp.c:2513
LIBMTP_Dump_Errorstack
void LIBMTP_Dump_Errorstack(LIBMTP_mtpdevice_t *device)
Definition libmtp.c:2659
LIBMTP_Clear_Errorstack
void LIBMTP_Clear_Errorstack(LIBMTP_mtpdevice_t *device)
Definition libmtp.c:2632
LIBMTP_Get_Friendlyname
char * LIBMTP_Get_Friendlyname(LIBMTP_mtpdevice_t *device)
Definition libmtp.c:3745
LIBMTP_Get_Serialnumber
char * LIBMTP_Get_Serialnumber(LIBMTP_mtpdevice_t *device)
Definition libmtp.c:3704
LIBMTP_Detect_Raw_Devices
LIBMTP_error_number_t LIBMTP_Detect_Raw_Devices(LIBMTP_raw_device_t **, int *)
Definition libopenusb1-glue.c:592
LIBMTP_destroy_file_t
void LIBMTP_destroy_file_t(LIBMTP_file_t *file)
Definition libmtp.c:4323
LIBMTP_Get_Files_And_Folders
LIBMTP_file_t * LIBMTP_Get_Files_And_Folders(LIBMTP_mtpdevice_t *device, uint32_t const storage, uint32_t const parent)
Definition libmtp.c:4593
LIBMTP_Get_Filetype_Description
char const * LIBMTP_Get_Filetype_Description(LIBMTP_filetype_t intype)
Definition libmtp.c:807
LIBMTP_Init
void LIBMTP_Init(void)
Definition libmtp.c:774
LIBMTP_error_number_t
LIBMTP_error_number_t
Definition libmtp.h:447
LIBMTP_devicestorage_struct
Definition libmtp.h:790
LIBMTP_devicestorage_struct::next
LIBMTP_devicestorage_t * next
Definition libmtp.h:800
LIBMTP_devicestorage_struct::id
uint32_t id
Definition libmtp.h:791
LIBMTP_file_struct
Definition libmtp.h:694
LIBMTP_file_struct::next
LIBMTP_file_t * next
Definition libmtp.h:702
LIBMTP_file_struct::filename
char * filename
Definition libmtp.h:698
LIBMTP_file_struct::filesize
uint64_t filesize
Definition libmtp.h:699
LIBMTP_file_struct::parent_id
uint32_t parent_id
Definition libmtp.h:696
LIBMTP_file_struct::item_id
uint32_t item_id
Definition libmtp.h:695
LIBMTP_file_struct::storage_id
uint32_t storage_id
Definition libmtp.h:697
LIBMTP_file_struct::filetype
LIBMTP_filetype_t filetype
Definition libmtp.h:701
LIBMTP_mtpdevice_struct
Definition libmtp.h:635
LIBMTP_mtpdevice_struct::storage
LIBMTP_devicestorage_t * storage
Definition libmtp.h:656
LIBMTP_raw_device_struct
Definition libmtp.h:544