libzypp 17.37.10

refresh.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
9#include "refresh.h"
10#include <zypp-media/ng/providespec.h>
11#include <zypp/ng/Context>
12#include <zypp/ng/workflows/contextfacade.h>
13#include <zypp/repo/RepoMirrorList.h>
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
41 zypp::filesystem::TmpDir tmpdir( zypp::filesystem::TmpDir::makeSibling( rawCachePath, 0755 ) );
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>
61 RefreshContext<ZyppContextRefType>::~RefreshContext()
62 {
63 MIL << "Deleting RefreshContext" << std::endl;
64 }
65
66 template<typename ZyppContextRefType>
67 void RefreshContext<ZyppContextRefType>::saveToRawCache()
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() ) {
74 for ( const auto & f : { zypp::repo::RepoMirrorList::cacheFileName(), zypp::repo::RepoMirrorList::cookieFileName() } ) {
75 zypp::filesystem::hardlinkCopy( (_rawCachePath / f), (_tmpDir.path() / f) );
76 }
77 }
78 zypp::filesystem::exchange( _tmpDir.path(), _rawCachePath );
79 }
80
81 template<typename ZyppContextRefType>
82 const zypp::Pathname &RefreshContext<ZyppContextRefType>::rawCachePath() const
83 {
84 return _rawCachePath;
85 }
86
87 template<typename ZyppContextRefType>
88 zypp::Pathname RefreshContext<ZyppContextRefType>::targetDir() const
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>
100 const zypp::RepoInfo &RefreshContext<ZyppContextRefType>::repoInfo() const
101 {
102 return _repoInfo;
103 }
104
105 template<typename ZyppContextRefType>
106 zypp::RepoInfo &RefreshContext<ZyppContextRefType>::repoInfo()
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>
118 const zypp::RepoManagerOptions &RefreshContext<ZyppContextRefType>::repoManagerOptions() const
119 {
120 return _repoManager->options();
121 }
122
123 template<typename ZyppContextRefType>
124 repo::RawMetadataRefreshPolicy RefreshContext<ZyppContextRefType>::policy() const
125 {
126 return _policy;
127 }
128
129 template<typename ZyppContextRefType>
130 void RefreshContext<ZyppContextRefType>::setPolicy(RawMetadataRefreshPolicy newPolicy)
131 {
132 _policy = newPolicy;
133 }
134
135 template<typename ZyppContextRefType>
136 const std::optional<typename RefreshContext<ZyppContextRefType>::PluginRepoverification> &RefreshContext<ZyppContextRefType>::pluginRepoverification() const
137 {
138 return _pluginRepoverification;
139 }
140
141 template<typename ZyppContextRefType>
142 void RefreshContext<ZyppContextRefType>::setProbedType(zypp::repo::RepoType rType)
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>
158 SignalProxy<void (zypp::repo::RepoType)> RefreshContext<ZyppContextRefType>::sigProbedTypeChanged()
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}
RepoMirrorList.h
zypp::Exception
Base class for Exception.
Definition Exception.h:153
zypp::RepoInfo
What is known about a repository.
Definition RepoInfo.h:72
zypp::filesystem::PathInfo
Wrapper class for stat/lstat.
Definition PathInfo.h:226
zypp::filesystem::Pathname
Pathname.
Definition Pathname.h:47
zypp::filesystem::Pathname::empty
bool empty() const
Test for an empty path.
Definition Pathname.h:116
zypp::filesystem::TmpDir
Provide a new empty temporary directory and recursively delete it when no longer needed.
Definition TmpPath.h:188
zypp::filesystem::TmpDir::makeSibling
static TmpDir makeSibling(const Pathname &sibling_r)
Provide a new empty temporary directory as sibling.
Definition TmpPath.cc:308
zypp::filesystem::TmpPath::path
Pathname path() const
Definition TmpPath.cc:152
zypp::repo::RepoMirrorList::cookieFileName
static constexpr const char * cookieFileName()
Definition RepoMirrorList.h:39
zypp::repo::RepoMirrorList::cacheFileName
static constexpr const char * cacheFileName()
Definition RepoMirrorList.h:44
zyppng::SignalProxy
Definition signals.h:325
zyppng::expected
Definition expected.h:27
zyppng::expected::success
static expected success(ConsParams &&...params)
Definition expected.h:115
zyppng::expected::error
static expected error(ConsParams &&...params)
Definition expected.h:126
zyppng::repo::RefreshContext
Definition refresh.h:43
zyppng::repo::RefreshContext::_repoInfo
zypp::RepoInfo _repoInfo
Definition refresh.h:128
zyppng::repo::RefreshContext::repoManagerOptions
const zypp::RepoManagerOptions & repoManagerOptions() const
Definition refresh.cc:118
zyppng::repo::RefreshContext::~RefreshContext
~RefreshContext() override
Definition refresh.cc:61
zyppng::repo::RefreshContext::repoManager
const RepoManagerRef< ZyppContextRefType > & repoManager() const
Definition refresh.cc:112
zyppng::repo::RefreshContext::setProbedType
void setProbedType(zypp::repo::RepoType rType)
Definition refresh.cc:142
zyppng::repo::RefreshContext::probedType
const std::optional< zypp::repo::RepoType > & probedType() const
Definition refresh.cc:152
zyppng::repo::RefreshContext::targetDir
zypp::Pathname targetDir() const
Definition refresh.cc:88
zyppng::repo::RefreshContext::_pluginRepoverification
std::optional< PluginRepoverification > _pluginRepoverification
Definition refresh.h:132
zyppng::repo::RefreshContext::_probedType
std::optional< zypp::repo::RepoType > _probedType
Definition refresh.h:134
zyppng::repo::RefreshContext::_tmpDir
zypp::filesystem::TmpDir _tmpDir
Definition refresh.h:130
zyppng::repo::RefreshContext::policy
RawMetadataRefreshPolicy policy() const
Definition refresh.cc:124
zyppng::repo::RefreshContext::pluginRepoverification
const std::optional< PluginRepoverification > & pluginRepoverification() const
Definition refresh.cc:136
zyppng::repo::RefreshContext::create
static expected< repo::RefreshContextRef< ZyppContextRefType > > create(ZyppContextRefType zyppContext, zypp::RepoInfo info, RepoManagerRef< ContextRefType > repoManager)
Definition refresh.cc:32
zyppng::repo::RefreshContext::saveToRawCache
void saveToRawCache()
Definition refresh.cc:67
zyppng::repo::RefreshContext::setPolicy
void setPolicy(RawMetadataRefreshPolicy newPolicy)
Definition refresh.cc:130
zyppng::repo::RefreshContext::zyppContext
const ZyppContextRefType & zyppContext() const
Definition refresh.cc:94
zyppng::repo::RefreshContext::_policy
repo::RawMetadataRefreshPolicy _policy
Definition refresh.h:131
zyppng::repo::RefreshContext::_zyppContext
ZyppContextRefType _zyppContext
Definition refresh.h:126
zyppng::repo::RefreshContext::repoInfo
const zypp::RepoInfo & repoInfo() const
Definition refresh.cc:100
zyppng::repo::RefreshContext::_repoManager
RepoManagerRef< ContextRefType > _repoManager
Definition refresh.h:127
zyppng::repo::RefreshContext::rawCachePath
const zypp::Pathname & rawCachePath() const
Definition refresh.cc:82
zyppng::repo::RefreshContext::_sigProbedTypeChanged
Signal< void(zypp::repo::RepoType)> _sigProbedTypeChanged
Definition refresh.h:135
zyppng::repo::RefreshContext::_rawCachePath
zypp::Pathname _rawCachePath
Definition refresh.h:129
zyppng::repo::RefreshContext::sigProbedTypeChanged
SignalProxy< void(zypp::repo::RepoType)> sigProbedTypeChanged()
Definition refresh.cc:158
contextfacade.h
zypp::filesystem::hardlinkCopy
int hardlinkCopy(const Pathname &oldpath, const Pathname &newpath)
Create newpath as hardlink or copy of oldpath.
Definition PathInfo.cc:888
zypp::filesystem::exchange
int exchange(const Pathname &lpath, const Pathname &rpath)
Exchanges two files or directories.
Definition PathInfo.cc:761
zyppng::operators
Definition asyncresult.h:279
zyppng::operators::and_then
auto and_then(Fun &&function)
Definition expected.h:623
zyppng::repo
Definition downloader.cc:25
zyppng::repo::RawMetadataRefreshPolicy
zypp::RepoManagerFlags::RawMetadataRefreshPolicy RawMetadataRefreshPolicy
Definition refresh.h:33
zyppng::rawcache_path_for_repoinfo
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
providespec.h
refresh.h
zypp::RepoManagerOptions
Repo manager settings.
Definition RepoManagerOptions.h:26
zypp::repo::RepoType
Repository type enumeration.
Definition RepoType.h:29
ZYPP_EXCPT_PTR
#define ZYPP_EXCPT_PTR(EXCPT)
Drops a logline and returns Exception as a std::exception_ptr.
Definition Exception.h:463
Gettext.h
_
#define _(MSG)
Definition Gettext.h:39
MIL
#define MIL
Definition Logger.h:100
PathInfo.h