2014-07-03 11:32:21 +04:00
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
*
* Copyright ( C ) 2014 James Antil < james @ fedoraproject . org >
*
* This program 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 licence 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 License along with this library ; if not , write to the
* Free Software Foundation , Inc . , 59 Temple Place , Suite 330 ,
* Boston , MA 02111 - 1307 , USA .
*/
# include "config.h"
2014-12-02 01:38:42 +03:00
# include "rpmostree-db-builtins.h"
# include "rpmostree-rpm-util.h"
2014-11-26 00:42:37 +03:00
typedef struct {
const char * name ;
gboolean ( * fn ) ( int argc , char * * argv , GCancellable * cancellable , GError * * error ) ;
2014-12-02 01:38:42 +03:00
} RpmOstreeDbCommand ;
2014-11-26 00:42:37 +03:00
2014-12-02 01:38:42 +03:00
static RpmOstreeDbCommand rpm_subcommands [ ] = {
2014-12-02 01:38:08 +03:00
{ " diff " , rpmostree_db_builtin_diff } ,
{ " list " , rpmostree_db_builtin_list } ,
{ " version " , rpmostree_db_builtin_version } ,
2014-11-26 00:42:37 +03:00
{ NULL , NULL }
} ;
2014-07-03 11:32:21 +04:00
static char * opt_repo ;
2014-12-02 01:38:42 +03:00
static GOptionEntry global_entries [ ] = {
2014-11-26 00:42:37 +03:00
{ " repo " , ' r ' , 0 , G_OPTION_ARG_STRING , & opt_repo , " Path to OSTree repository (defaults to /sysroot/ostree/repo) " , " PATH " } ,
2014-07-03 11:32:21 +04:00
{ NULL }
} ;
2014-11-26 00:42:37 +03:00
static GOptionContext *
rpm_option_context_new_with_commands ( void )
{
2014-12-02 01:38:42 +03:00
RpmOstreeDbCommand * command = rpm_subcommands ;
2014-11-26 00:42:37 +03:00
GOptionContext * context ;
GString * summary ;
context = g_option_context_new ( " COMMAND " ) ;
2014-12-02 01:38:08 +03:00
summary = g_string_new ( " Builtin \" db \" Commands: " ) ;
2014-11-26 00:42:37 +03:00
while ( command - > name ! = NULL )
{
g_string_append_printf ( summary , " \n %s " , command - > name ) ;
command + + ;
}
g_option_context_set_summary ( context , summary - > str ) ;
g_string_free ( summary , TRUE ) ;
return context ;
}
gboolean
2014-12-02 01:38:08 +03:00
rpmostree_db_option_context_parse ( GOptionContext * context ,
const GOptionEntry * main_entries ,
int * argc , char * * * argv ,
OstreeRepo * * out_repo ,
GCancellable * cancellable , GError * * error )
2014-11-26 00:42:37 +03:00
{
gs_unref_object OstreeRepo * repo = NULL ;
gboolean success = FALSE ;
/* Entries are listed in --help output in the order added. We add the
* main entries ourselves so that we can add the - - repo entry first . */
2014-12-02 01:38:42 +03:00
g_option_context_add_main_entries ( context , global_entries , NULL ) ;
2014-11-26 00:42:37 +03:00
2015-08-05 19:39:07 +03:00
if ( ! rpmostree_option_context_parse ( context ,
main_entries ,
argc , argv ,
RPM_OSTREE_BUILTIN_FLAG_LOCAL_CMD ,
cancellable ,
NULL ,
error ) )
2014-11-26 00:42:37 +03:00
goto out ;
if ( opt_repo = = NULL )
{
gs_unref_object OstreeSysroot * sysroot = NULL ;
sysroot = ostree_sysroot_new_default ( ) ;
if ( ! ostree_sysroot_load ( sysroot , cancellable , error ) )
goto out ;
if ( ! ostree_sysroot_get_repo ( sysroot , & repo , cancellable , error ) )
goto out ;
}
2014-07-03 11:32:21 +04:00
else
{
2014-11-26 00:42:37 +03:00
gs_unref_object GFile * repo_file = g_file_new_for_path ( opt_repo ) ;
2014-07-03 11:32:21 +04:00
2014-11-26 00:42:37 +03:00
repo = ostree_repo_new ( repo_file ) ;
2014-07-03 11:32:21 +04:00
if ( ! ostree_repo_open ( repo , cancellable , error ) )
2014-11-26 00:42:37 +03:00
goto out ;
2014-07-03 11:32:21 +04:00
}
2014-11-26 00:42:37 +03:00
2014-07-03 11:32:21 +04:00
if ( rpmReadConfigFiles ( NULL , NULL ) )
2014-07-18 08:46:17 +04:00
{
g_set_error ( error , G_IO_ERROR , G_IO_ERROR_FAILED ,
2014-11-26 00:42:37 +03:00
" rpm failed to init: %s " , rpmlogMessage ( ) ) ;
2014-07-18 08:46:17 +04:00
goto out ;
}
2014-07-03 11:32:21 +04:00
2014-11-26 00:42:37 +03:00
gs_transfer_out_value ( out_repo , & repo ) ;
success = TRUE ;
out :
return success ;
}
gboolean
2014-12-02 01:38:08 +03:00
rpmostree_builtin_db ( int argc , char * * argv , GCancellable * cancellable , GError * * error )
2014-11-26 00:42:37 +03:00
{
2014-12-02 01:38:42 +03:00
RpmOstreeDbCommand * subcommand ;
2014-11-26 00:42:37 +03:00
const char * subcommand_name = NULL ;
gs_free char * prgname = NULL ;
gboolean ret = FALSE ;
int in , out ;
for ( in = 1 , out = 1 ; in < argc ; in + + , out + + )
2014-07-03 11:32:21 +04:00
{
2014-11-26 00:42:37 +03:00
/* The non-option is the command, take it out of the arguments */
if ( argv [ in ] [ 0 ] ! = ' - ' )
2014-07-25 07:38:51 +04:00
{
2014-11-26 00:42:37 +03:00
if ( subcommand_name = = NULL )
{
subcommand_name = argv [ in ] ;
out - - ;
continue ;
}
2014-07-25 07:38:51 +04:00
}
2014-07-03 11:32:21 +04:00
2014-11-26 00:42:37 +03:00
else if ( g_str_equal ( argv [ in ] , " -- " ) )
{
break ;
}
2014-07-03 11:32:21 +04:00
2014-11-26 00:42:37 +03:00
argv [ out ] = argv [ in ] ;
}
argc = out ;
2014-07-16 08:48:20 +04:00
2014-11-26 00:42:37 +03:00
subcommand = rpm_subcommands ;
while ( subcommand - > name )
{
if ( g_strcmp0 ( subcommand_name , subcommand - > name ) = = 0 )
break ;
subcommand + + ;
2014-07-03 11:32:21 +04:00
}
2014-11-26 00:42:37 +03:00
if ( ! subcommand - > name )
2014-07-03 11:32:21 +04:00
{
2014-11-26 00:42:37 +03:00
GOptionContext * context ;
gs_free char * help ;
context = rpm_option_context_new_with_commands ( ) ;
/* This will not return for some options (e.g. --version). */
2015-08-05 19:39:07 +03:00
if ( rpmostree_option_context_parse ( context , NULL ,
& argc , & argv ,
RPM_OSTREE_BUILTIN_FLAG_LOCAL_CMD ,
cancellable ,
NULL ,
error ) )
2014-11-26 00:42:37 +03:00
{
if ( subcommand_name = = NULL )
{
g_set_error_literal ( error , G_IO_ERROR , G_IO_ERROR_FAILED ,
2014-12-02 01:38:08 +03:00
" No \" db \" subcommand specified " ) ;
2014-11-26 00:42:37 +03:00
}
else
{
g_set_error ( error , G_IO_ERROR , G_IO_ERROR_FAILED ,
2014-12-02 01:38:08 +03:00
" Unknown \" db \" subcommand '%s' " , subcommand_name ) ;
2014-11-26 00:42:37 +03:00
}
}
help = g_option_context_get_help ( context , FALSE , NULL ) ;
g_printerr ( " %s " , help ) ;
g_option_context_free ( context ) ;
2014-07-03 11:32:21 +04:00
goto out ;
}
2014-11-26 00:42:37 +03:00
prgname = g_strdup_printf ( " %s %s " , g_get_prgname ( ) , subcommand_name ) ;
g_set_prgname ( prgname ) ;
if ( ! subcommand - > fn ( argc , argv , cancellable , error ) )
goto out ;
2014-07-03 11:32:21 +04:00
ret = TRUE ;
out :
return ret ;
}
2014-11-26 00:42:37 +03:00