int main (int argc, char **argv)
{

    int do_delete = 0;
    int opt;

    fprintf(stdout, "libmtp version: " LIBMTP_VERSION_STRING "\n\n");

    while ( (opt = getopt(argc, argv, "d")) != -1 ) {
        switch (opt) {
        case 'd':
            do_delete = 1;
            break;
        default:
            break;
        }
    }

    if(do_delete == 0) {
        printf("This is a dummy run. No folders will be deleted.\n");
        printf("To delete folders, use the '-d' option.\n");
    }

    LIBMTP_mtpdevice_t *device;
    LIBMTP_folder_t *folders;
    LIBMTP_file_t *files;

    LIBMTP_Init();
    device = LIBMTP_Get_First_Device();
    if (device == NULL) {
        printf("No devices.\n");
        exit (0);
    }


    files = LIBMTP_Get_Filelisting_With_Callback(device,NULL,NULL);


    folders = LIBMTP_Get_Folder_List(device);

    if(folders == NULL) {
        printf("No folders found\n");
    } else {
        prune_empty_folders(device,files,folders,do_delete);
    }

    LIBMTP_destroy_folder_t(folders);
    LIBMTP_destroy_file_t(files);

    LIBMTP_Release_Device(device);
    printf("OK.\n");
    exit (0);
}
Beispiel #2
0
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);
      free(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);
  }

  free(rawdevices);
  printf("OK.\n");

  return 0;
}
Beispiel #3
0
static void
delete_done_cb (LIBMTP_mtpdevice_t *device, TracksDeletedCallbackData *data)
{
	LIBMTP_folder_t *folders;
	LIBMTP_file_t *files;

	data->actually_free = FALSE;
	update_free_space_cb (device, RB_MTP_SOURCE (data->source));

	/* if any of the folders we just deleted from are now empty, delete them */
	folders = LIBMTP_Get_Folder_List (device);
	files = LIBMTP_Get_Filelisting_With_Callback (device, NULL, NULL);
	if (folders != NULL) {
		GHashTableIter iter;
		gpointer key;
		g_hash_table_iter_init (&iter, data->check_folders);
		while (g_hash_table_iter_next (&iter, &key, NULL)) {
			LIBMTP_folder_t *f;
			LIBMTP_folder_t *c;
			LIBMTP_file_t *file;
			uint32_t folder_id = GPOINTER_TO_UINT(key);

			while (folder_id != device->default_music_folder && folder_id != 0) {

				f = LIBMTP_Find_Folder (folders, folder_id);
				if (f == NULL) {
					rb_debug ("unable to find folder %u", folder_id);
					break;
				}

				/* don't delete folders with children that we didn't just delete */
				for (c = f->child; c != NULL; c = c->sibling) {
					if (g_hash_table_lookup (data->check_folders,
								 GUINT_TO_POINTER (c->folder_id)) == NULL) {
						break;
					}
				}
				if (c != NULL) {
					rb_debug ("folder %s has children", f->name);
					break;
				}

				/* don't delete folders that contain files */
				for (file = files; file != NULL; file = file->next) {
					if (file->parent_id == folder_id) {
						break;
					}
				}

				if (file != NULL) {
					rb_debug ("folder %s contains at least one file: %s", f->name, file->filename);
					break;
				}

				/* ok, the folder is empty */
				rb_debug ("deleting empty folder %s", f->name);
				LIBMTP_Delete_Object (device, f->folder_id);

				/* if the folder we just deleted has siblings, the parent
				 * can't be empty.
				 */
				if (f->sibling != NULL) {
					rb_debug ("folder %s has siblings, can't delete parent", f->name);
					break;
				}
				folder_id = f->parent_id;
			}
		}

		LIBMTP_destroy_folder_t (folders);
	} else {
		rb_debug ("unable to get device folder list");
	}

	/* clean up the file list */
	while (files != NULL) {
		LIBMTP_file_t *n;

		n = files->next;
		LIBMTP_destroy_file_t (files);
		files = n;
	}

	g_idle_add ((GSourceFunc) delete_done_idle_cb, data);
}