tools zypp-pubkey: simple prog to list info about all gpg-pubkeys in the rpm database
[platform/upstream/libzypp.git] / tools / migrate-sources / migrate-sources.cc
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2
3 #include <iostream>
4 #include <fstream>
5
6 #include "zypp/base/Logger.h"
7 #include "zypp/ZYpp.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"
13
14 #include "zypp/parser/xmlstore/XMLSourceCacheParser.h"
15
16 #undef ZYPP_BASE_LOGGER_LOGGROUP
17 #define ZYPP_BASE_LOGGER_LOGGROUP "zypp-cache"
18
19
20 using namespace std;
21 using namespace zypp;
22
23 struct Options
24 {
25   Options()
26   : fake(false)
27   , root("/")
28   , sources_dir("/var/lib/zypp/db/sources")
29   {}
30
31   bool fake;
32   Pathname root;
33   Pathname sources_dir;
34 };
35
36 static void clear_cache( const Options &opt )
37 {
38   Pathname path = opt.root + "/var/lib/zypp/cache";
39   if ( PathInfo(path).isDir() )
40   {
41     cout << "Deleting old cache directory (" << path << ")." << endl;
42     if ( ! opt.fake )
43     {
44       if ( filesystem::recursive_rmdir(path) != 0 )
45         ERR << "Error removing cache directory" << path << endl;
46     }
47   }
48
49   path = opt.root + "/var/lib/zypp/db";
50   if ( PathInfo(path).isDir() )
51   {
52     cout << "Deleting old db directory (" << path << ")." << endl;
53     if ( ! opt.fake )
54     {
55       if ( filesystem::recursive_rmdir(path) != 0 )
56         ERR << "Error removing db directory" << path << endl;
57     }
58   }
59 }
60
61 static void migrate_sources( const Options &opt )
62 {
63   if ( getenv("YAST_IS_RUNNING") && (string(getenv("YAST_IS_RUNNING")) == "instsys" ))
64   {
65     MIL << "YaST is running in instsys. Not migrating old sources. YaST will do it." << endl;
66     return;
67   }
68   else
69   {
70     MIL << "YaST not running in instsys." << endl;
71   }
72
73   zypp::zypp_readonly_hack::IWantIt();
74   ZYpp::Ptr Z = zypp::getZYpp();
75   RepoManager manager;
76
77   Pathname source_p = opt.root + opt.sources_dir;
78
79   if ( ! PathInfo(source_p).isExist() )
80   {
81     cout << "No sources to migrate." << endl;
82     clear_cache( opt );
83     return;
84   }
85
86   RepoInfoList sources;
87   DBG << "Reading source cache in " << source_p << std::endl;
88
89   list<Pathname> entries;
90   if ( filesystem::readdir( entries, source_p, false ) != 0 )
91       ZYPP_THROW(Exception("failed to read directory"));
92
93   int i=0;
94   for ( list<Pathname>::const_iterator it = entries.begin(); it != entries.end(); ++it )
95   {
96
97     MIL << "Processing " << *it << endl;
98
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);
104       try {
105         data.setAlias(alias);
106         data.setEnabled(false);
107         cout << "Migrating repo: " << endl << data << endl;
108         if ( ! opt.fake )
109         {
110           manager.addRepository(data);
111         }
112         cout << "Deleting old source: " << *it << endl;
113         if ( ! opt.fake )
114         {
115           if ( filesystem::unlink(*it) != 0 )
116             ERR << "Error removing source " << *it << endl;
117           // delete old file
118         }
119         cout << "saved as " << alias << endl;
120         ++i;
121       }
122       catch ( const Exception &e )
123       {
124         cout << "Error adding repository: " << e.msg() << endl << data << endl;
125       }
126
127     }
128
129   }
130   cout << i << " sources migrated."<< endl;
131
132   // reread entries
133   if ( filesystem::readdir( entries, source_p, false ) != 0 )
134       ZYPP_THROW(Exception("failed to read directory"));
135   if ( entries.size() == 0 )
136   {
137     cout << "all sources migrated. deleting old source directory"<< endl;
138     if ( ! opt.fake )
139     {
140       if ( filesystem::recursive_rmdir(source_p) != 0 )
141         ERR << "Error removing source directory" << source_p << endl;
142
143       clear_cache( opt );
144     }
145   }
146   else
147   {
148     cout << "Not all sources migrated. leaving old source directory"<< endl;
149   }
150 }
151
152 void usage(int argc, char **argv)
153 {
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;
157 }
158
159 //-----------------------------------------------------------------------------
160
161 int
162 main (int argc, char **argv)
163 {
164   MIL << "-------------------------------------" << endl;
165   Options opt;
166   int i;
167   for ( i=1; i < argc; ++i )
168   {
169
170     if ( string(argv[i]) == "--help" )
171     {
172       usage(argc, argv);
173       return 0;
174     }
175     if ( string(argv[i]) == "--fake" )
176       opt.fake = true;
177     if ( string(argv[i]) == "--root" )
178       opt.root = argv[++i];
179     if ( string(argv[i]) == "--sp" )
180       opt.sources_dir = argv[++i];
181   }
182   migrate_sources(opt);
183
184   return 0;
185 }
186