libzypp  17.32.4
RepomdFileCollector.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
9 
10 #include "RepomdFileCollector.h"
11 #include <zypp/ZConfig.h>
12 #include <zypp/PathInfo.h>
13 #include <zypp/RepoInfo.h>
14 #include <solv/solvversion.h>
15 
16 namespace zypp::env
17 {
19  {
20  static bool val = [](){
21  const char * env = getenv("ZYPP_REPOMD_WITH_OTHER");
22  return( env && zypp::str::strToBool( env, true ) );
23  }();
24  return val;
25  }
26 
28  {
29  static bool val = [](){
30  const char * env = getenv("ZYPP_REPOMD_WITH_FILELISTS");
31  return( env && zypp::str::strToBool( env, true ) );
32  }();
33  return val;
34  }
35 }
36 
37 namespace zypp::repo::yum
38 {
39 
40  namespace
41  {
42  inline OnMediaLocation loc_with_path_prefix( OnMediaLocation loc_r, const Pathname & prefix_r )
43  {
44  if ( ! prefix_r.empty() && prefix_r != "/" )
45  loc_r.changeFilename( prefix_r / loc_r.filename() );
46  return loc_r;
47  }
48 
49  // search old repository file to run the delta algorithm on
50  Pathname search_deltafile( const Pathname & dir, const Pathname & file )
51  {
53  if ( ! PathInfo(dir).isDir() )
54  return deltafile;
55 
56  // Strip the checksum preceding the file stem so we can look for an
57  // old *-primary.xml which may contain some reusable blocks.
58  std::string base { file.basename() };
59  size_t hypoff = base.find( '-' );
60  if ( hypoff != std::string::npos )
61  base.replace( 0, hypoff + 1, "" );
62 
63  std::list<std::string> retlist;
64  if ( ! filesystem::readdir( retlist, dir, false ) )
65  {
66  for ( const auto & fn : retlist )
67  {
68  if ( str::endsWith( fn, base ) )
69  deltafile = fn;
70  }
71  }
72  if ( !deltafile.empty() )
73  return dir/deltafile;
74 
75  return deltafile;
76  }
77  } // namespace
78 
79 
93  : _destDir { destDir_r }
94  {
95  addWantedLocale( ZConfig::instance().textLocale() );
96  for ( const Locale & it : ZConfig::instance().repoRefreshLocales() )
97  addWantedLocale( it );
98  }
99 
101  { }
102 
107  bool RepomdFileCollector::collect(const OnMediaLocation &loc_r, const std::string &typestr_r)
108  {
109  if ( str::endsWith( typestr_r, "_db" ) )
110  return true; // skip sqlitedb
111 
112  bool zchk { str::endsWith( typestr_r, "_zck" ) };
113 #if defined(LIBSOLVEXT_FEATURE_ZCHUNK_COMPRESSION)
114  const std::string & basetype { zchk ? typestr_r.substr( 0, typestr_r.size()-4 ) : typestr_r };
115 #else
116  if ( zchk )
117  return true; // skip zchunk if not supported by libsolv
118  const std::string & basetype { typestr_r };
119 #endif
120 
121  // filter well known resource types
122  if ( basetype == "other" && not env::ZYPP_REPOMD_WITH_OTHER() )
123  return true; // skip it
124 
125  if ( basetype == "filelists" && not env::ZYPP_REPOMD_WITH_FILELISTS() )
126  return true; // skip it
127 
128  // filter localized susedata
129  if ( str::startsWith( basetype, "susedata." ) )
130  {
131  // susedata.LANG
132  if ( ! wantLocale( Locale(basetype.c_str()+9) ) )
133  return true; // skip it
134  }
135 
136  // may take it... (prefer zchnk)
137  if ( zchk || !_wantedFiles.count( basetype ) )
138  _wantedFiles[basetype] = loc_r;
139 
140  return true;
141  }
142 
144  {
145  // schedule fileS for download
146  for ( const auto & el : _wantedFiles ) {
147  const OnMediaLocation & loc { el.second };
148  const OnMediaLocation & loc_with_path { loc_with_path_prefix( loc, repoInfo().path() ) };
149  cb( OnMediaLocation(loc_with_path).setDeltafile( search_deltafile( deltaDir()/"repodata", loc.filename() ) ) );
150  }
151  }
152 
153  bool RepomdFileCollector::wantLocale(const Locale &locale_r) const
154  { return _wantedLocales.count( locale_r ); }
155 
157  {
158  while ( locale_r )
159  {
160  _wantedLocales.insert( locale_r );
161  locale_r = locale_r.fallback();
162  }
163  }
164 
165 }
Pathname deltafile
std::function< void(const OnMediaLocation &file)> FinalizeCb
Definition: Env.h:22
bool ZYPP_REPOMD_WITH_OTHER()
Describes a resource file located on a medium.
static ZConfig & instance()
Singleton ctor.
Definition: ZConfig.cc:925
virtual const Pathname & deltaDir() const =0
Locale fallback() const
Return the fallback locale for this locale, if no fallback exists the empty Locale::noCode.
Definition: Locale.cc:208
bool collect(const OnMediaLocation &loc_r, const std::string &typestr_r)
The callback invoked by the RepomdFileReader.
std::string basename() const
Return the last component of this path.
Definition: Pathname.h:128
LocaleSet _wantedLocales
Locales do download.
bool ZYPP_REPOMD_WITH_FILELISTS()
bool empty() const
Test for an empty path.
Definition: Pathname.h:114
virtual const RepoInfo & repoInfo() const =0
bool startsWith(const C_Str &str_r, const C_Str &prefix_r)
alias for hasPrefix
Definition: String.h:1085
const Pathname & filename() const
The path to the resource on the medium.
bool endsWith(const C_Str &str_r, const C_Str &prefix_r)
alias for hasSuffix
Definition: String.h:1092
RepomdFileCollector(const Pathname &destDir_r)
&#39;Language[_Country]&#39; codes.
Definition: Locale.h:49
int readdir(std::list< std::string > &retlist_r, const Pathname &path_r, bool dots_r)
Return content of directory via retlist.
Definition: PathInfo.cc:606
bool wantLocale(const Locale &locale_r) const
bool strToBool(const C_Str &str, bool default_r)
Parse str into a bool depending on the default value.
Definition: String.h:429
Wrapper class for ::stat/::lstat.
Definition: PathInfo.h:221
std::map< std::string, OnMediaLocation > _wantedFiles
OnMediaLocation & changeFilename(Pathname filename_r)
Individual manipulation of filename (prefer setLocation).