1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
6 #include "zypp/base/Logger.h"
8 #include "zypp/zypp_detail/ZYppReadOnlyHack.h"
9 #include "zypp/ZYppFactory.h"
10 #include "zypp/PathInfo.h"
11 #include "zypp/RepoManager.h"
12 #include "zypp/cache/CacheFSCK.h"
14 #include "zypp/parser/xmlstore/XMLSourceCacheParser.h"
16 #undef ZYPP_BASE_LOGGER_LOGGROUP
17 #define ZYPP_BASE_LOGGER_LOGGROUP "zypp-cache"
28 , sources_dir("/var/lib/zypp/db/sources")
36 static void clear_cache( const Options &opt )
38 Pathname path = opt.root + "/var/lib/zypp/cache";
39 if ( PathInfo(path).isDir() )
41 cout << "Deleting old cache directory (" << path << ")." << endl;
44 if ( filesystem::recursive_rmdir(path) != 0 )
45 ERR << "Error removing cache directory" << path << endl;
49 path = opt.root + "/var/lib/zypp/db";
50 if ( PathInfo(path).isDir() )
52 cout << "Deleting old db directory (" << path << ")." << endl;
55 if ( filesystem::recursive_rmdir(path) != 0 )
56 ERR << "Error removing db directory" << path << endl;
61 static void migrate_sources( const Options &opt )
63 if ( getenv("YAST_IS_RUNNING") && (string(getenv("YAST_IS_RUNNING")) == "instsys" ))
65 MIL << "YaST is running in instsys. Not migrating old sources. YaST will do it." << endl;
70 MIL << "YaST not running in instsys." << endl;
73 zypp::zypp_readonly_hack::IWantIt();
74 ZYpp::Ptr Z = zypp::getZYpp();
77 Pathname source_p = opt.root + opt.sources_dir;
79 if ( ! PathInfo(source_p).isExist() )
81 cout << "No sources to migrate." << endl;
87 DBG << "Reading source cache in " << source_p << std::endl;
89 list<Pathname> entries;
90 if ( filesystem::readdir( entries, source_p, false ) != 0 )
91 ZYPP_THROW(Exception("failed to read directory"));
94 for ( list<Pathname>::const_iterator it = entries.begin(); it != entries.end(); ++it )
97 MIL << "Processing " << *it << endl;
99 std::ifstream anIstream((*it).c_str());
100 zypp::parser::xmlstore::XMLSourceCacheParser iter(anIstream, "");
101 for (; ! iter.atEnd(); ++iter) {
102 RepoInfo data = **iter;
103 string alias = "migrated_" + str::numstring(i);
105 data.setAlias(alias);
106 data.setEnabled(false);
107 cout << "Migrating repo: " << endl << data << endl;
110 manager.addRepository(data);
112 cout << "Deleting old source: " << *it << endl;
115 if ( filesystem::unlink(*it) != 0 )
116 ERR << "Error removing source " << *it << endl;
119 cout << "saved as " << alias << endl;
122 catch ( const Exception &e )
124 cout << "Error adding repository: " << e.msg() << endl << data << endl;
130 cout << i << " sources migrated."<< endl;
133 if ( filesystem::readdir( entries, source_p, false ) != 0 )
134 ZYPP_THROW(Exception("failed to read directory"));
135 if ( entries.size() == 0 )
137 cout << "all sources migrated. deleting old source directory"<< endl;
140 if ( filesystem::recursive_rmdir(source_p) != 0 )
141 ERR << "Error removing source directory" << source_p << endl;
148 cout << "Not all sources migrated. leaving old source directory"<< endl;
152 void usage(int argc, char **argv)
154 cout << argv[0] << ". Migrates old sources to 10.3 repositories." << endl;
155 cout << "Usage:" << endl;
156 cout << argv[0] << " [--root root-path] [--fake] [--sp sources-path]" << endl;
159 //-----------------------------------------------------------------------------
162 main (int argc, char **argv)
164 MIL << "-------------------------------------" << endl;
167 for ( i=1; i < argc; ++i )
170 if ( string(argv[i]) == "--help" )
175 if ( string(argv[i]) == "--fake" )
177 if ( string(argv[i]) == "--root" )
178 opt.root = argv[++i];
179 if ( string(argv[i]) == "--sp" )
180 opt.sources_dir = argv[++i];
182 migrate_sources(opt);