2017-09-15 18:05:12 +03:00
/*
* Copyright © 2017 Endless Mobile , Inc .
2022-02-17 22:12:18 +03:00
* Copyright ( C ) 2022 Igalia S . L .
2017-09-15 18:05:12 +03:00
*
2018-01-30 22:26:26 +03:00
* SPDX - License - Identifier : LGPL - 2.0 +
*
2017-09-15 18:05:12 +03:00
* This library is free software ; you can redistribute it and / or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation ; either
* version 2 of the License , or ( at your option ) any later version .
*
* This library is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the GNU
* Lesser General Public License for more details .
*
* You should have received a copy of the GNU Lesser General Public
2021-12-07 04:20:55 +03:00
* License along with this library . If not , see < https : //www.gnu.org/licenses/>.
2017-09-15 18:05:12 +03:00
*
* Authors :
* - Philip Withnall < withnall @ endlessm . com >
*/
# include "config.h"
# include <gio/gio.h>
# include <glib.h>
# include <glib-object.h>
2018-06-06 23:41:37 +03:00
# include <libglnx.h>
2017-09-15 18:05:12 +03:00
# include <locale.h>
# include "ostree-autocleanups.h"
# include "ostree-remote-private.h"
# include "ostree-repo-finder.h"
# include "ostree-repo-finder-mount.h"
# include "ostree-types.h"
# include "test-mock-gio.h"
static void
result_cb ( GObject * source_object ,
GAsyncResult * result ,
gpointer user_data )
{
GAsyncResult * * result_out = user_data ;
* result_out = g_object_ref ( result ) ;
}
static void
collection_ref_free0 ( OstreeCollectionRef * ref )
{
2022-02-17 22:12:18 +03:00
g_clear_pointer ( & ref , ostree_collection_ref_free ) ;
2017-09-15 18:05:12 +03:00
}
int
main ( int argc , char * * argv )
{
g_autoptr ( GError ) error = NULL ;
setlocale ( LC_ALL , " " ) ;
if ( argc < 5 | | ( argc % 2 ) ! = 1 )
{
g_printerr ( " Usage: %s REPO MOUNT-ROOT COLLECTION-ID REF-NAME [COLLECTION-ID REF-NAME …] \n " , argv [ 0 ] ) ;
return 1 ;
}
g_autoptr ( GMainContext ) context = g_main_context_new ( ) ;
g_main_context_push_thread_default ( context ) ;
g_autoptr ( OstreeRepo ) parent_repo = ostree_repo_open_at ( AT_FDCWD , argv [ 1 ] , NULL , & error ) ;
g_assert_no_error ( error ) ;
/* Set up a mock volume. */
g_autoptr ( GFile ) mount_root = g_file_new_for_commandline_arg ( argv [ 2 ] ) ;
g_autoptr ( GMount ) mount = G_MOUNT ( ostree_mock_mount_new ( " mount " , mount_root ) ) ;
g_autoptr ( GList ) mounts = g_list_prepend ( NULL , mount ) ;
g_autoptr ( GVolumeMonitor ) monitor = ostree_mock_volume_monitor_new ( mounts , NULL ) ;
g_autoptr ( OstreeRepoFinderMount ) finder = ostree_repo_finder_mount_new ( monitor ) ;
/* Resolve the refs. */
g_autoptr ( GPtrArray ) refs = g_ptr_array_new_with_free_func ( ( GDestroyNotify ) collection_ref_free0 ) ;
for ( gsize i = 3 ; i < argc ; i + = 2 )
{
const char * collection_id = argv [ i ] ;
const char * ref_name = argv [ i + 1 ] ;
g_ptr_array_add ( refs , ostree_collection_ref_new ( collection_id , ref_name ) ) ;
}
g_ptr_array_add ( refs , NULL ) ; /* NULL terminator */
2020-09-13 00:51:40 +03:00
g_autoptr ( GAsyncResult ) async_result = NULL ;
2017-09-15 18:05:12 +03:00
ostree_repo_finder_resolve_async ( OSTREE_REPO_FINDER ( finder ) ,
( const OstreeCollectionRef * const * ) refs - > pdata ,
2020-09-13 00:51:40 +03:00
parent_repo , NULL , result_cb , & async_result ) ;
2017-09-15 18:05:12 +03:00
2020-09-13 00:51:40 +03:00
while ( async_result = = NULL )
2017-09-15 18:05:12 +03:00
g_main_context_iteration ( context , TRUE ) ;
g_autoptr ( GPtrArray ) results = ostree_repo_finder_resolve_finish ( OSTREE_REPO_FINDER ( finder ) ,
2020-09-13 00:51:40 +03:00
async_result , & error ) ;
2017-09-15 18:05:12 +03:00
g_assert_no_error ( error ) ;
/* Check that the results are correct: the invalid refs should have been
* ignored , and the valid results canonicalised and deduplicated . */
for ( gsize i = 0 ; i < results - > len ; i + + )
{
const OstreeRepoFinderResult * result = g_ptr_array_index ( results , i ) ;
GHashTableIter iter ;
OstreeCollectionRef * ref ;
const gchar * checksum ;
g_hash_table_iter_init ( & iter , result - > ref_to_checksum ) ;
while ( g_hash_table_iter_next ( & iter , ( gpointer * ) & ref , ( gpointer * ) & checksum ) )
g_print ( " % " G_GSIZE_FORMAT " %s %s %s %s \n " ,
i , ostree_remote_get_name ( result - > remote ) ,
ref - > collection_id , ref - > ref_name ,
checksum ) ;
}
g_main_context_pop_thread_default ( context ) ;
return 0 ;
}