libzypp  17.37.5
refresh.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
9 #include "refresh.h"
11 #include <zypp/ng/Context>
14 #include <zypp-core/fs/PathInfo.h>
15 #include <zypp-core/base/Gettext.h>
16 
17 namespace zyppng::repo {
18 
19  template<typename ZyppContextRefType>
20  RefreshContext<ZyppContextRefType>::RefreshContext( private_constr_t, ZyppContextRefType &&zyppContext, zypp::RepoInfo &&info, zypp::Pathname &&rawCachePath, zypp::filesystem::TmpDir &&tempDir, RepoManagerRef<ContextRefType> &&repoManager )
21  : _zyppContext( std::move(zyppContext) )
22  , _repoManager( std::move(repoManager) )
23  , _repoInfo( std::move(info) )
24  , _rawCachePath( std::move(rawCachePath) )
25  , _tmpDir( std::move(tempDir) )
26  {
27  if ( _repoManager->pluginRepoverification().checkIfNeeded() )
28  _pluginRepoverification = _repoManager->pluginRepoverification();
29  }
30 
31  template<typename ZyppContextRefType>
32  expected<RefreshContextRef<ZyppContextRefType>> RefreshContext<ZyppContextRefType>::create( ZyppContextRefType zyppContext, zypp::RepoInfo info, RepoManagerRef<ZyppContextRefType> repoManager )
33  {
34  using namespace operators;
35  using CtxType = RefreshContext<ZyppContextRefType>;
36  using CtxRefType = RefreshContextRef<ZyppContextRefType>;
37 
38  return rawcache_path_for_repoinfo ( repoManager->options(), info )
39  | and_then( [&]( zypp::Pathname rawCachePath ) {
40 
42  if( tmpdir.path().empty() && geteuid() != 0 ) {
43  tmpdir = zypp::filesystem::TmpDir(); // non-root user may not be able to write the cache
44  }
45  if( tmpdir.path().empty() ) {
46  return expected<CtxRefType>::error( ZYPP_EXCPT_PTR(zypp::Exception(_("Can't create metadata cache directory."))) );
47  }
48 
49  MIL << "Creating RefreshContext " << std::endl;
50 
51  return expected<CtxRefType>::success( std::make_shared<CtxType>( private_constr_t{}
52  , std::move(zyppContext)
53  , std::move(info)
54  , std::move(rawCachePath)
55  , std::move(tmpdir)
56  , std::move(repoManager)));
57  } );
58  }
59 
60  template<typename ZyppContextRefType>
62  {
63  MIL << "Deleting RefreshContext" << std::endl;
64  }
65 
66  template<typename ZyppContextRefType>
68  {
69  // RepoMirrorList may have updated the mirrorlist file in the old
70  // metadata dir. We transfer it to the new dir.
71  zypp::Pathname oldCache = _rawCachePath / zypp::repo::RepoMirrorList::cacheFileName();
72  zypp::Pathname newCache = _tmpDir.path() / zypp::repo::RepoMirrorList::cacheFileName();
73  if ( zypp::PathInfo(oldCache).isExist() && not zypp::PathInfo(newCache).isExist() ) {
75  zypp::filesystem::hardlinkCopy( (_rawCachePath / f), (_tmpDir.path() / f) );
76  }
77  }
78  zypp::filesystem::exchange( _tmpDir.path(), _rawCachePath );
79  }
80 
81  template<typename ZyppContextRefType>
83  {
84  return _rawCachePath;
85  }
86 
87  template<typename ZyppContextRefType>
89  {
90  return _tmpDir.path();
91  }
92 
93  template<typename ZyppContextRefType>
94  const ZyppContextRefType &RefreshContext<ZyppContextRefType>::zyppContext() const
95  {
96  return _zyppContext;
97  }
98 
99  template<typename ZyppContextRefType>
101  {
102  return _repoInfo;
103  }
104 
105  template<typename ZyppContextRefType>
107  {
108  return _repoInfo;
109  }
110 
111  template<typename ZyppContextRefType>
112  const RepoManagerRef<ZyppContextRefType> &RefreshContext<ZyppContextRefType>::repoManager() const
113  {
114  return _repoManager;
115  }
116 
117  template<typename ZyppContextRefType>
119  {
120  return _repoManager->options();
121  }
122 
123  template<typename ZyppContextRefType>
125  {
126  return _policy;
127  }
128 
129  template<typename ZyppContextRefType>
131  {
132  _policy = newPolicy;
133  }
134 
135  template<typename ZyppContextRefType>
137  {
138  return _pluginRepoverification;
139  }
140 
141  template<typename ZyppContextRefType>
143  {
144  if ( _probedType && *_probedType == rType )
145  return;
146 
147  _probedType = rType;
148  _sigProbedTypeChanged.emit(rType);
149  }
150 
151  template<typename ZyppContextRefType>
152  const std::optional<zypp::repo::RepoType> &RefreshContext<ZyppContextRefType>::probedType() const
153  {
154  return _probedType;
155  }
156 
157  template<typename ZyppContextRefType>
159  {
160  return _sigProbedTypeChanged;
161  }
162 
163  // explicitely intantiate the template types we want to work with
164  template class RefreshContext<SyncContextRef>;
165  template class RefreshContext<ContextRef>;
166 
167 }
#define MIL
Definition: Logger.h:100
int exchange(const Pathname &lpath, const Pathname &rpath)
Exchanges two files or directories.
Definition: PathInfo.cc:761
#define _(MSG)
Definition: Gettext.h:39
static TmpDir makeSibling(const Pathname &sibling_r)
Provide a new empty temporary directory as sibling.
Definition: TmpPath.cc:308
Definition: Arch.h:363
What is known about a repository.
Definition: RepoInfo.h:71
Pathname path() const
Definition: TmpPath.cc:152
#define ZYPP_EXCPT_PTR(EXCPT)
Drops a logline and returns Exception as a std::exception_ptr.
Definition: Exception.h:463
ZyppContextRefType _zyppContext
int hardlinkCopy(const Pathname &oldpath, const Pathname &newpath)
Create newpath as hardlink or copy of oldpath.
Definition: PathInfo.cc:888
Repo manager settings.
bool empty() const
Test for an empty path.
Definition: Pathname.h:116
Provide a new empty temporary directory and recursively delete it when no longer needed.
Definition: TmpPath.h:187
static constexpr const char * cacheFileName()
static expected success(ConsParams &&...params)
Definition: expected.h:115
zypp::RepoManagerFlags::CacheBuildPolicy _policy
Base class for Exception.
Definition: Exception.h:152
zypp_private::repo::PluginRepoverification PluginRepoverification
Definition: repomanager.h:41
auto and_then(Fun &&function)
Definition: expected.h:623
static constexpr const char * cookieFileName()
Wrapper class for ::stat/::lstat.
Definition: PathInfo.h:225
expected< zypp::Pathname > rawcache_path_for_repoinfo(const RepoManagerOptions &opt, const RepoInfo &info)
Calculates the raw cache path for a repository, this is usually /var/cache/zypp/alias.
Definition: repomanager.h:172
Repository type enumeration.
Definition: RepoType.h:28