add_subdirs (GPtrArray *res,
             GFile     *parent,
             gboolean   recurse)
{
  g_autoptr(GFileEnumerator) dir_enum = NULL;

  dir_enum = g_file_enumerate_children (parent,
                                        G_FILE_ATTRIBUTE_STANDARD_NAME ","
                                        G_FILE_ATTRIBUTE_STANDARD_TYPE,
                                        G_FILE_QUERY_INFO_NONE,
                                        NULL, NULL);
  if (dir_enum == NULL)
    return;

  while (TRUE)
    {
      GFileInfo *info;
      GFile *path;

      if (!g_file_enumerator_iterate (dir_enum, &info, &path, NULL, NULL) ||
          info == NULL)
        break;

      /* Here we support either a plain repo or, if @recurse is TRUE, the root
       * directory of a USB created with "flatpak create-usb"
       */
      if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
        {
          g_autoptr(OstreeRepo) repo = ostree_repo_new (path);

          if (ostree_repo_open (repo, NULL, NULL))
            g_ptr_array_add (res, g_object_ref (path));
          else if (recurse)
            {
              g_autoptr(GFile) ostree_repo_subpath = NULL;
              g_autoptr(GFile) dot_ostree_repo_subpath = NULL;
              g_autoptr(GFile) dot_ostree_repo_d_subpath = NULL;
              g_autoptr(OstreeRepo) ostree_repo_subpath_repo = NULL;
              g_autoptr(OstreeRepo) dot_ostree_repo_subpath_repo = NULL;

              ostree_repo_subpath = g_file_resolve_relative_path (path, "ostree/repo");
              ostree_repo_subpath_repo = ostree_repo_new (ostree_repo_subpath);
              if (ostree_repo_open (ostree_repo_subpath_repo, NULL, NULL))
                g_ptr_array_add (res, g_object_ref (ostree_repo_subpath));

              dot_ostree_repo_subpath = g_file_resolve_relative_path (path, ".ostree/repo");
              dot_ostree_repo_subpath_repo = ostree_repo_new (dot_ostree_repo_subpath);
              if (ostree_repo_open (dot_ostree_repo_subpath_repo, NULL, NULL))
                g_ptr_array_add (res, g_object_ref (dot_ostree_repo_subpath));

              dot_ostree_repo_d_subpath = g_file_resolve_relative_path (path, ".ostree/repos.d");
              add_subdirs (res, dot_ostree_repo_d_subpath, FALSE);
            }
        }
    }
}
add_subdirs (GPtrArray *res,
             GFile     *parent,
             gboolean   recurse)
{
  g_autoptr(GFileEnumerator) dir_enum = NULL;

  dir_enum = g_file_enumerate_children (parent,
                                        G_FILE_ATTRIBUTE_STANDARD_NAME ","
                                        G_FILE_ATTRIBUTE_STANDARD_TYPE,
                                        G_FILE_QUERY_INFO_NONE,
                                        NULL, NULL);
  if (dir_enum == NULL)
    return;

  while (TRUE)
    {
      GFileInfo *info;
      GFile *path;

      if (!g_file_enumerator_iterate (dir_enum, &info, &path, NULL, NULL) ||
          info == NULL)
        break;

       
      if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
        {
          g_autoptr(OstreeRepo) repo = ostree_repo_new (path);

          if (ostree_repo_open (repo, NULL, NULL))
            g_ptr_array_add (res, g_object_ref (path));
          else if (recurse)
            {
              g_autoptr(GFile) ostree_repo_subpath = NULL;
              g_autoptr(GFile) dot_ostree_repo_subpath = NULL;
              g_autoptr(GFile) dot_ostree_repo_d_subpath = NULL;
              g_autoptr(OstreeRepo) ostree_repo_subpath_repo = NULL;
              g_autoptr(OstreeRepo) dot_ostree_repo_subpath_repo = NULL;

              ostree_repo_subpath = g_file_resolve_relative_path (path, "ostree/repo");
              ostree_repo_subpath_repo = ostree_repo_new (ostree_repo_subpath);
              if (ostree_repo_open (ostree_repo_subpath_repo, NULL, NULL))
                g_ptr_array_add (res, g_object_ref (ostree_repo_subpath));

              dot_ostree_repo_subpath = g_file_resolve_relative_path (path, ".ostree/repo");
              dot_ostree_repo_subpath_repo = ostree_repo_new (dot_ostree_repo_subpath);
              if (ostree_repo_open (dot_ostree_repo_subpath_repo, NULL, NULL))
                g_ptr_array_add (res, g_object_ref (dot_ostree_repo_subpath));

              dot_ostree_repo_d_subpath = g_file_resolve_relative_path (path, ".ostree/repos.d");
              add_subdirs (res, dot_ostree_repo_d_subpath, FALSE);
            }
        }
    }
}