@@ -922,44 +922,67 @@ void RepoSack::Impl::fix_group_missing_xml() {
922922 auto & logger = *base->get_logger ();
923923 auto & system_state = base->p_impl ->get_system_state ();
924924 auto comps_xml_dir = system_state.get_group_xml_dir ();
925+ auto comps_xml_dir_groups = comps_xml_dir / " groups" ;
926+ auto comps_xml_dir_environments = comps_xml_dir / " environments" ;
925927 bool directory_exists = true ;
926928 std::error_code ec;
927- std::filesystem::create_directories (comps_xml_dir , ec);
929+ std::filesystem::create_directories (comps_xml_dir_groups , ec);
928930 if (ec) {
929- logger.debug (" Failed to create directory \" {}\" : {}" , comps_xml_dir.string (), ec.message ());
931+ logger.debug (" Failed to create directory \" {}\" : {}" , comps_xml_dir_groups.string (), ec.message ());
932+ directory_exists = false ;
933+ }
934+ std::filesystem::create_directories (comps_xml_dir_environments, ec);
935+ if (ec) {
936+ logger.debug (" Failed to create directory \" {}\" : {}" , comps_xml_dir_environments.string (), ec.message ());
930937 directory_exists = false ;
931938 }
932939 if (!group_missing_xml.empty ()) {
933940 libdnf5::comps::GroupQuery available_groups (base);
934941 available_groups.filter_installed (false );
935942 for (const auto & group_id : group_missing_xml) {
936- bool xml_saved = false ;
943+ bool group_loaded = false ;
937944 if (directory_exists) {
938- // try to find the group id in availables
939- libdnf5::comps::GroupQuery group_query (available_groups);
940- group_query.filter_groupid (group_id);
941- if (group_query.size () == 1 ) {
942- // GroupQuery is basically a set thus iterators and `.get()` method
943- // return `const Group` objects.
944- // To call non-const serialize method we need to make a copy here.
945- libdnf5::comps::Group group = group_query.get ();
946- auto xml_file_name = comps_xml_dir / (group_id + " .xml" );
947- logger.debug (
948- " Re-creating installed group \" {}\" definition to file \" {}\" ." ,
949- group_id,
950- xml_file_name.string ());
951- try {
952- group.serialize (xml_file_name);
953- xml_saved = true ;
954- } catch (utils::xml::XMLSaveError & ex) {
955- logger.debug (ex.what ());
945+ auto xml_file_name = comps_xml_dir_groups / (group_id + " .xml" );
946+ auto xml_file_name_in_comps_dir = comps_xml_dir / (group_id + " .xml" );
947+ // Try to copy the xml file from the comps directory where it used to be stored.
948+ if (solv_repo.read_group_solvable_from_xml (xml_file_name_in_comps_dir)) {
949+ group_loaded = true ;
950+ std::filesystem::rename (xml_file_name_in_comps_dir, xml_file_name, ec);
951+ if (ec) {
952+ logger.debug (
953+ " Failed to move \" {}\" into directory \" {}\" : {}" ,
954+ xml_file_name_in_comps_dir.string (),
955+ comps_xml_dir_groups.string (),
956+ ec.message ());
956957 }
957- if (xml_saved) {
958- solv_repo.read_group_solvable_from_xml (xml_file_name);
958+ } else {
959+ // try to find the group id in availables
960+ libdnf5::comps::GroupQuery group_query (available_groups);
961+ group_query.filter_groupid (group_id);
962+ if (group_query.size () == 1 ) {
963+ // GroupQuery is basically a set thus iterators and `.get()` method
964+ // return `const Group` objects.
965+ // To call non-const serialize method we need to make a copy here.
966+ libdnf5::comps::Group group = group_query.get ();
967+ logger.debug (
968+ " Re-creating installed group \" {}\" definition to file \" {}\" ." ,
969+ group_id,
970+ xml_file_name.string ());
971+ bool xml_saved = false ;
972+ try {
973+ group.serialize (xml_file_name);
974+ xml_saved = true ;
975+ } catch (utils::xml::XMLSaveError & ex) {
976+ logger.debug (ex.what ());
977+ }
978+ if (xml_saved) {
979+ solv_repo.read_group_solvable_from_xml (xml_file_name);
980+ group_loaded = true ;
981+ }
959982 }
960983 }
961984 }
962- if (!xml_saved ) {
985+ if (!group_loaded ) {
963986 // fall-back to creating solvables only from system state
964987 solv_repo.create_group_solvable (group_id, system_state.get_group_state (group_id));
965988 }
@@ -969,30 +992,47 @@ void RepoSack::Impl::fix_group_missing_xml() {
969992 libdnf5::comps::EnvironmentQuery available_environments (base);
970993 available_environments.filter_installed (false );
971994 for (const auto & environment_id : environments_missing_xml) {
972- bool xml_saved = false ;
995+ bool environment_loaded = false ;
973996 if (directory_exists) {
974- // try to find the environment id in availables
975- libdnf5::comps::EnvironmentQuery environment_query (available_environments);
976- environment_query.filter_environmentid (environment_id);
977- if (environment_query.size () == 1 ) {
978- libdnf5::comps::Environment environment = environment_query.get ();
979- auto xml_file_name = comps_xml_dir / (environment_id + " .xml" );
980- logger.debug (
981- " Re-creating installed environmental group \" {}\" definition to file \" {}\" ." ,
982- environment_id,
983- xml_file_name.string ());
984- try {
985- environment.serialize (xml_file_name);
986- xml_saved = true ;
987- } catch (utils::xml::XMLSaveError & ex) {
988- logger.debug (ex.what ());
997+ auto xml_file_name = comps_xml_dir_groups / (environment_id + " .xml" );
998+ auto xml_file_name_in_comps_dir = comps_xml_dir / (environment_id + " .xml" );
999+ // Try to copy the xml file from the comps directory where it used to be stored.
1000+ if (solv_repo.read_group_solvable_from_xml (xml_file_name_in_comps_dir)) {
1001+ environment_loaded = true ;
1002+ std::filesystem::rename (xml_file_name_in_comps_dir, xml_file_name, ec);
1003+ if (ec) {
1004+ logger.debug (
1005+ " Failed to move \" {}\" into directory \" {}\" : {}" ,
1006+ xml_file_name_in_comps_dir.string (),
1007+ comps_xml_dir_groups.string (),
1008+ ec.message ());
9891009 }
990- if (xml_saved) {
991- solv_repo.read_group_solvable_from_xml (xml_file_name);
1010+ } else {
1011+ // try to find the environment id in availables
1012+ libdnf5::comps::EnvironmentQuery environment_query (available_environments);
1013+ environment_query.filter_environmentid (environment_id);
1014+ if (environment_query.size () == 1 ) {
1015+ libdnf5::comps::Environment environment = environment_query.get ();
1016+ auto xml_file_name = comps_xml_dir_environments / (environment_id + " .xml" );
1017+ logger.debug (
1018+ " Re-creating installed environmental group \" {}\" definition to file \" {}\" ." ,
1019+ environment_id,
1020+ xml_file_name.string ());
1021+ bool xml_saved = false ;
1022+ try {
1023+ environment.serialize (xml_file_name);
1024+ xml_saved = true ;
1025+ } catch (utils::xml::XMLSaveError & ex) {
1026+ logger.debug (ex.what ());
1027+ }
1028+ if (xml_saved) {
1029+ solv_repo.read_group_solvable_from_xml (xml_file_name);
1030+ environment_loaded = true ;
1031+ }
9921032 }
9931033 }
9941034 }
995- if (!xml_saved ) {
1035+ if (!environment_loaded ) {
9961036 // fall-back to creating solvables only from system state
9971037 solv_repo.create_environment_solvable (
9981038 environment_id, system_state.get_environment_state (environment_id));
0 commit comments