libzypp 17.37.10

SolvIdentFile.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
12#include <iostream>
13#include <fstream>
14
15#include <zypp/base/LogTools.h>
16#include <zypp/base/IOStream.h>
17#include <zypp/base/String.h>
18
19#include <zypp/PathInfo.h>
20#include <zypp/TmpPath.h>
21#include <zypp/Date.h>
22
23#include <zypp/target/SolvIdentFile.h>
24
25 using std::endl;
26
28 namespace zypp
29{
31 namespace target
32 {
33
34 void SolvIdentFile::load( const Pathname & file_r, Data & data_r )
35 {
36 PathInfo pi( file_r );
37 if ( ! pi.isFile() )
38 {
39 WAR << "Can't read " << pi << endl;
40 return;
41 }
42 std::ifstream infile( file_r.c_str() );
43 for( iostr::EachLine in( infile ); in; in.next() )
44 {
45 std::string l( str::trim(*in) );
46 if ( ! l.empty() && l[0] != '#' )
47 {
48 data_r.insert( IdString(l) );
49 }
50 }
51 MIL << "Read " << pi << endl;
52 }
53
54 void SolvIdentFile::store( const Pathname & file_r, const Data & data_r )
55 {
56 filesystem::TmpFile tmp( filesystem::TmpFile::makeSibling( file_r ) );
57 filesystem::chmod( tmp.path(), 0644 );
58
59 std::ofstream outs( tmp.path().c_str() );
60 outs << "# " << file_r.basename() << " generated " << Date::now() << endl;
61 if ( true )
62 {
63 std::set<IdString> sorted( data_r.begin(), data_r.end() );
64 dumpRange( outs, sorted.begin(), sorted.end(), "#", "\n", "\n", "\n", "#\n" );
65 }
66 else
67 {
68 dumpRange( outs, data_r.begin(), data_r.end(), "#", "\n", "\n", "\n", "#\n" );
69 }
70 outs.close();
71
72 if ( outs.good() )
73 {
74 filesystem::rename( tmp.path(), file_r );
75 MIL << "Wrote " << PathInfo(file_r) << endl;
76 }
77 else
78 {
79 ERR << "Can't write " << PathInfo(tmp.path()) << endl;
80 }
81 }
82
83 /******************************************************************
84 **
85 ** FUNCTION NAME : operator<<
86 ** FUNCTION TYPE : std::ostream &
87 */
88 std::ostream & operator<<( std::ostream & str, const SolvIdentFile & obj )
89 {
90 str << obj.file() << ' ';
91 if ( obj._dataPtr )
92 str << obj.data();
93 else
94 str << "(unloaded)";
95 return str;
96 }
97
99 } // namespace target
102} // namespace zypp
SolvIdentFile.h
zypp::Date::now
static Date now()
Return the current time.
Definition Date.h:78
zypp::IdString
Access to the sat-pools string space.
Definition IdString.h:44
zypp::filesystem::PathInfo
Wrapper class for stat/lstat.
Definition PathInfo.h:226
zypp::filesystem::PathInfo::isFile
bool isFile() const
Definition PathInfo.h:295
zypp::filesystem::Pathname
Pathname.
Definition Pathname.h:47
zypp::filesystem::Pathname::c_str
const char * c_str() const
String representation.
Definition Pathname.h:112
zypp::filesystem::Pathname::basename
std::string basename() const
Return the last component of this path.
Definition Pathname.h:130
zypp::filesystem::TmpFile
Provide a new empty temporary file and delete it when no longer needed.
Definition TmpPath.h:128
zypp::filesystem::TmpFile::makeSibling
static TmpFile makeSibling(const Pathname &sibling_r)
Provide a new empty temporary directory as sibling.
Definition TmpPath.cc:222
zypp::filesystem::TmpPath::path
Pathname path() const
Definition TmpPath.cc:152
zypp::iostr::EachLine
Simple lineparser: Traverse each line in a file.
Definition IOStream.h:113
zypp::iostr::EachLine::next
bool next()
Advance to next line.
Definition IOStream.cc:72
zypp::target::SolvIdentFile::SolvIdentFile
SolvIdentFile(Pathname file_r)
Ctor taking the file to read/write.
Definition SolvIdentFile.h:42
zypp::target::SolvIdentFile::load
static void load(const Pathname &file_r, Data &data_r)
Read Data from file_r.
Definition SolvIdentFile.cc:34
zypp::target::SolvIdentFile::data
const Data & data() const
Return the data.
Definition SolvIdentFile.h:54
zypp::target::SolvIdentFile::Data
std::unordered_set< IdString > Data
Definition SolvIdentFile.h:38
zypp::target::SolvIdentFile::_dataPtr
scoped_ptr< Data > _dataPtr
Definition SolvIdentFile.h:103
zypp::target::SolvIdentFile::store
static void store(const Pathname &file_r, const Data &data_r)
Write Data to file_r.
Definition SolvIdentFile.cc:54
zypp::target::SolvIdentFile::file
const Pathname & file() const
Return the file path.
Definition SolvIdentFile.h:47
str
String related utilities and Regular expression matching.
zypp::filesystem::chmod
int chmod(const Pathname &path, mode_t mode)
Like 'chmod'.
Definition PathInfo.cc:1097
zypp::filesystem::rename
int rename(const Pathname &oldpath, const Pathname &newpath)
Like 'rename'.
Definition PathInfo.cc:747
zypp::str::trim
std::string trim(const std::string &s, const Trim trim_r)
Definition String.cc:226
zypp::target
Definition KeyRing.h:325
zypp::target::operator<<
std::ostream & operator<<(std::ostream &str, const CommitPackageCache &obj)
Definition CommitPackageCache.cc:160
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition Application.cc:20
zypp::dumpRange
std::ostream & dumpRange(std::ostream &str, TIterator begin, TIterator end, const std::string &intro="{", const std::string &pfx="\n ", const std::string &sep="\n ", const std::string &sfx="\n", const std::string &extro="}")
Print range defined by iterators (multiline style).
Definition LogTools.h:120
MIL
#define MIL
Definition Logger.h:100
ERR
#define ERR
Definition Logger.h:102
WAR
#define WAR
Definition Logger.h:101
Date.h
PathInfo.h
TmpPath.h
IOStream.h
LogTools.h
String.h