libmtp: folders.c

libmtp 1.1.23

folders.c
#include "common.h"
#include <stdlib.h>
static void dump_folder_list(LIBMTP_folder_t *folderlist, int level)
{
int i;
if(folderlist==NULL) {
return;
}
printf("%u\t", folderlist->folder_id);
for(i=0;i<level;i++) printf(" ");
printf("%s\n", folderlist->name);
dump_folder_list(folderlist->child, level+1);
dump_folder_list(folderlist->sibling, level);
}
int main (int argc, char **argv)
{
LIBMTP_raw_device_t *rawdevices;
int numrawdevices;
int i;
LIBMTP_Init();
printf("Attempting to connect device(s)\n");
switch (LIBMTP_Detect_Raw_Devices(&rawdevices, &numrawdevices)) {
case LIBMTP_ERROR_NO_DEVICE_ATTACHED:
printf("mtp-folders: no devices found\n");
return 0;
case LIBMTP_ERROR_CONNECTING:
fprintf(stderr, "mtp-folders: There has been an error connecting. Exit\n");
return 1;
case LIBMTP_ERROR_MEMORY_ALLOCATION:
fprintf(stderr, "mtp-folders: Memory Allocation Error. Exit\n");
return 1;
/* Unknown general errors - This should never execute */
case LIBMTP_ERROR_GENERAL:
default:
fprintf(stderr, "mtp-folders: Unknown error, please report "
"this to the libmtp developers\n");
return 1;
/* Successfully connected at least one device, so continue */
case LIBMTP_ERROR_NONE:
printf("mtp-folders: Successfully connected\n");
}
/* iterate through connected MTP devices */
for (i = 0; i < numrawdevices; i++) {
LIBMTP_mtpdevice_t *device;
LIBMTP_devicestorage_t *storage;
char *friendlyname;
int ret;
device = LIBMTP_Open_Raw_Device(&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);
if (friendlyname == NULL) {
printf("Friendly name: (NULL)\n");
} else {
printf("Friendly name: %s\n", friendlyname);
LIBMTP_FreeMemory(friendlyname);
}
LIBMTP_Dump_Errorstack(device);
LIBMTP_Clear_Errorstack(device);
/* Get all storages for this device */
ret = LIBMTP_Get_Storage(device, LIBMTP_STORAGE_SORTBY_NOTSORTED);
if (ret != 0) {
perror("LIBMTP_Get_Storage()\n");
LIBMTP_Dump_Errorstack(device);
LIBMTP_Clear_Errorstack(device);
continue;
}
/* Loop over storages, dump folder for each one */
for (storage = device->storage; storage != 0; storage = storage->next) {
LIBMTP_folder_t *folders;
printf("Storage: %s\n", storage->StorageDescription);
folders = LIBMTP_Get_Folder_List_For_Storage(device, storage->id);
if (folders == NULL) {
fprintf(stdout, "No folders found\n");
LIBMTP_Dump_Errorstack(device);
LIBMTP_Clear_Errorstack(device);
} else {
dump_folder_list(folders,0);
}
LIBMTP_destroy_folder_t(folders);
}
LIBMTP_Release_Device(device);
}
LIBMTP_FreeMemory(rawdevices);
printf("OK.\n");
return 0;
}
LIBMTP_FreeMemory
void LIBMTP_FreeMemory(void *mem)
Definition libmtp.c:9472
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_Storage
int LIBMTP_Get_Storage(LIBMTP_mtpdevice_t *device, int const sortby)
Definition libmtp.c:4193
LIBMTP_Detect_Raw_Devices
LIBMTP_error_number_t LIBMTP_Detect_Raw_Devices(LIBMTP_raw_device_t **, int *)
Definition libopenusb1-glue.c:592
LIBMTP_Get_Folder_List_For_Storage
LIBMTP_folder_t * LIBMTP_Get_Folder_List_For_Storage(LIBMTP_mtpdevice_t *device, uint32_t const storage)
Definition libmtp.c:7454
LIBMTP_destroy_folder_t
void LIBMTP_destroy_folder_t(LIBMTP_folder_t *folder)
Definition libmtp.c:7354
LIBMTP_Init
void LIBMTP_Init(void)
Definition libmtp.c:774
LIBMTP_folder_t
struct LIBMTP_folder_struct LIBMTP_folder_t
Definition libmtp.h:469
LIBMTP_devicestorage_t
struct LIBMTP_devicestorage_struct LIBMTP_devicestorage_t
Definition libmtp.h:471
LIBMTP_mtpdevice_t
struct LIBMTP_mtpdevice_struct LIBMTP_mtpdevice_t
Definition libmtp.h:464
LIBMTP_raw_device_t
struct LIBMTP_raw_device_struct LIBMTP_raw_device_t
Definition libmtp.h:460
LIBMTP_devicestorage_struct::StorageDescription
char * StorageDescription
Definition libmtp.h:798
LIBMTP_devicestorage_struct::next
LIBMTP_devicestorage_t * next
Definition libmtp.h:800
LIBMTP_devicestorage_struct::id
uint32_t id
Definition libmtp.h:791
LIBMTP_folder_struct::folder_id
uint32_t folder_id
Definition libmtp.h:767
LIBMTP_folder_struct::sibling
LIBMTP_folder_t * sibling
Definition libmtp.h:771
LIBMTP_folder_struct::child
LIBMTP_folder_t * child
Definition libmtp.h:772
LIBMTP_folder_struct::name
char * name
Definition libmtp.h:770
LIBMTP_mtpdevice_struct::storage
LIBMTP_devicestorage_t * storage
Definition libmtp.h:656