libzypp: zypp/ng/repo/refresh.cc Source File

libzypp 17.38.14

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/repo/RepoMirrorList.h>
13#include <zypp-core/fs/PathInfo.h>
14#include <zypp-core/base/Gettext.h>
15
16namespace zyppng::repo {
17
18
19 RefreshContext::RefreshContext( private_constr_t, ContextRef &&zyppContext, zypp::RepoInfo &&info, zypp::Pathname &&rawCachePath, zypp::filesystem::TmpDir &&tempDir, RepoManagerRef &&repoManager )
20 : _zyppContext( std::move(zyppContext) )
21 , _repoManager( std::move(repoManager) )
22 , _repoInfo( std::move(info) )
23 , _rawCachePath( std::move(rawCachePath) )
24 , _tmpDir( std::move(tempDir) )
25 {
26 if ( _repoManager->pluginRepoverification().checkIfNeeded() )
27 _pluginRepoverification = _repoManager->pluginRepoverification();
28 }
29
30
31 expected<RefreshContextRef> RefreshContext::create( ContextRef zyppContext, zypp::RepoInfo info, RepoManagerRef repoManager )
32 {
33 using namespace operators;
34 using CtxType = RefreshContext;
35 using CtxRefType = RefreshContextRef;
36
37 return rawcache_path_for_repoinfo ( repoManager->options(), info )
38 | and_then( [&]( zypp::Pathname rawCachePath ) {
39
40 zypp::filesystem::TmpDir tmpdir( zypp::filesystem::TmpDir::makeSibling( rawCachePath, 0755 ) );
41 if( tmpdir.path().empty() && geteuid() != 0 ) {
42 tmpdir = zypp::filesystem::TmpDir(); // non-root user may not be able to write the cache
43 }
44 if( tmpdir.path().empty() ) {
45 return expected<CtxRefType>::error( ZYPP_EXCPT_PTR(zypp::Exception(_("Can't create metadata cache directory."))) );
46 }
47
48 MIL << "Creating RefreshContext " << std::endl;
49
50 return expected<CtxRefType>::success( std::make_shared<CtxType>( private_constr_t{}
51 , std::move(zyppContext)
52 , std::move(info)
53 , std::move(rawCachePath)
54 , std::move(tmpdir)
55 , std::move(repoManager)));
56 } );
57 }
58
59
60 RefreshContext::~RefreshContext()
61 {
62 MIL << "Deleting RefreshContext" << std::endl;
63 }
64
65
66 void RefreshContext::saveToRawCache()
67 {
68 // RepoMirrorList may have updated the mirrorlist file in the old
69 // metadata dir. We transfer it to the new dir.
70 zypp::Pathname oldCache = _rawCachePath / zypp::repo::RepoMirrorList::cacheFileName();
71 zypp::Pathname newCache = _tmpDir.path() / zypp::repo::RepoMirrorList::cacheFileName();
72 if ( zypp::PathInfo(oldCache).isExist() && not zypp::PathInfo(newCache).isExist() ) {
73 for ( const auto & f : { zypp::repo::RepoMirrorList::cacheFileName(), zypp::repo::RepoMirrorList::cookieFileName() } ) {
74 zypp::filesystem::hardlinkCopy( (_rawCachePath / f), (_tmpDir.path() / f) );
75 }
76 }
77 zypp::filesystem::exchange( _tmpDir.path(), _rawCachePath );
78 }
79
80
81 const zypp::Pathname &RefreshContext::rawCachePath() const
82 {
83 return _rawCachePath;
84 }
85
86
87 zypp::Pathname RefreshContext::targetDir() const
88 {
89 return _tmpDir.path();
90 }
91
92
93 const ContextRef &RefreshContext::zyppContext() const
94 {
95 return _zyppContext;
96 }
97
98
99 const zypp::RepoInfo &RefreshContext::repoInfo() const
100 {
101 return _repoInfo;
102 }
103
104
105 zypp::RepoInfo &RefreshContext::repoInfo()
106 {
107 return _repoInfo;
108 }
109
110
111 const RepoManagerRef &RefreshContext::repoManager() const
112 {
113 return _repoManager;
114 }
115
116
117 const zypp::RepoManagerOptions &RefreshContext::repoManagerOptions() const
118 {
119 return _repoManager->options();
120 }
121
122
123 repo::RawMetadataRefreshPolicy RefreshContext::policy() const
124 {
125 return _policy;
126 }
127
128
129 void RefreshContext::setPolicy(RawMetadataRefreshPolicy newPolicy)
130 {
131 _policy = newPolicy;
132 }
133
134
135 const std::optional<typename RefreshContext::PluginRepoverification> &RefreshContext::pluginRepoverification() const
136 {
137 return _pluginRepoverification;
138 }
139
140
141 void RefreshContext::setProbedType(zypp::repo::RepoType rType)
142 {
143 if ( _probedType && *_probedType == rType )
144 return;
145
146 _probedType = rType;
147 _sigProbedTypeChanged.emit(rType);
148 }
149
150
151 const std::optional<zypp::repo::RepoType> &RefreshContext::probedType() const
152 {
153 return _probedType;
154 }
155
156
157 SignalProxy<void (zypp::repo::RepoType)> RefreshContext::sigProbedTypeChanged()
158 {
159 return _sigProbedTypeChanged;
160 }
161
162}
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:103
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:48
zypp::filesystem::Pathname::empty
bool empty() const
Test for an empty path.
Definition Pathname.h:117
zypp::filesystem::TmpDir
Provide a new empty temporary directory and recursively delete it when no longer needed.
Definition TmpPath.h:173
zypp::filesystem::TmpDir::makeSibling
static TmpDir makeSibling(const Pathname &sibling_r)
Provide a new empty temporary directory as sibling.
Definition TmpPath.cc:250
zypp::filesystem::TmpPath::path
Pathname path() const
Definition TmpPath.cc:124
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:56
zyppng::expected::success
static expected success(ConsParams &&...params)
Definition expected.h:178
zyppng::expected::error
static expected error(ConsParams &&...params)
Definition expected.h:189
zyppng::repo::RefreshContext
Definition refresh.h:42
zyppng::repo::RefreshContext::setPolicy
void setPolicy(RawMetadataRefreshPolicy newPolicy)
Definition refresh.cc:129
zyppng::repo::RefreshContext::_repoManager
RepoManagerRef _repoManager
Definition refresh.h:122
zyppng::repo::RefreshContext::rawCachePath
const zypp::Pathname & rawCachePath() const
Definition refresh.cc:81
zyppng::repo::RefreshContext::targetDir
zypp::Pathname targetDir() const
Definition refresh.cc:87
zyppng::repo::RefreshContext::_repoInfo
zypp::RepoInfo _repoInfo
Definition refresh.h:123
zyppng::repo::RefreshContext::_rawCachePath
zypp::Pathname _rawCachePath
Definition refresh.h:124
zyppng::repo::RefreshContext::repoInfo
const zypp::RepoInfo & repoInfo() const
Definition refresh.cc:99
zyppng::repo::RefreshContext::policy
RawMetadataRefreshPolicy policy() const
Definition refresh.cc:123
zyppng::repo::RefreshContext::_zyppContext
ContextRef _zyppContext
Definition refresh.h:121
zyppng::repo::RefreshContext::pluginRepoverification
const std::optional< PluginRepoverification > & pluginRepoverification() const
Definition refresh.cc:135
zyppng::repo::RefreshContext::_policy
repo::RawMetadataRefreshPolicy _policy
Definition refresh.h:126
zyppng::repo::RefreshContext::_probedType
std::optional< zypp::repo::RepoType > _probedType
Definition refresh.h:129
zyppng::repo::RefreshContext::repoManager
const RepoManagerRef & repoManager() const
Definition refresh.cc:111
zyppng::repo::RefreshContext::_pluginRepoverification
std::optional< PluginRepoverification > _pluginRepoverification
Definition refresh.h:127
zyppng::repo::RefreshContext::sigProbedTypeChanged
SignalProxy< void(zypp::repo::RepoType)> sigProbedTypeChanged()
Definition refresh.cc:157
zyppng::repo::RefreshContext::create
static expected< repo::RefreshContextRef > create(ContextRef zyppContext, zypp::RepoInfo info, RepoManagerRef repoManager)
Definition refresh.cc:31
zyppng::repo::RefreshContext::_tmpDir
zypp::filesystem::TmpDir _tmpDir
Definition refresh.h:125
zyppng::repo::RefreshContext::repoManagerOptions
const zypp::RepoManagerOptions & repoManagerOptions() const
Definition refresh.cc:117
zyppng::repo::RefreshContext::setProbedType
void setProbedType(zypp::repo::RepoType rType)
Definition refresh.cc:141
zyppng::repo::RefreshContext::_sigProbedTypeChanged
Signal< void(zypp::repo::RepoType)> _sigProbedTypeChanged
Definition refresh.h:130
zyppng::repo::RefreshContext::zyppContext
const ContextRef & zyppContext() const
Definition refresh.cc:93
zyppng::repo::RefreshContext::saveToRawCache
void saveToRawCache()
Definition refresh.cc:66
zyppng::repo::RefreshContext::probedType
const std::optional< zypp::repo::RepoType > & probedType() const
Definition refresh.cc:151
zyppng::repo::RefreshContext::~RefreshContext
~RefreshContext() override
Definition refresh.cc:60
zypp::filesystem::hardlinkCopy
int hardlinkCopy(const Pathname &oldpath, const Pathname &newpath)
Create newpath as hardlink or copy of oldpath.
Definition PathInfo.cc:902
zypp::filesystem::exchange
int exchange(const Pathname &lpath, const Pathname &rpath)
Exchanges two files or directories.
Definition PathInfo.cc:775
zyppng::operators
Definition expected.h:699
zyppng::operators::and_then
auto and_then(Fun &&function)
Definition expected.h:708
zyppng::repo
Definition downloader.cc:22
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:166
providespec.h
refresh.h
zypp::RepoManagerOptions
Repo manager settings.
Definition RepoManagerOptions.h:26
zypp::repo::RepoType
Repository type enumeration.
Definition RepoType.h:29
PathInfo.h