libzypp  13.10.6
SoftLocksFile.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 
24 
25 using std::endl;
26 
28 namespace zypp
29 {
30  namespace target
32  {
33 
34  void SoftLocksFile::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 SoftLocksFile::store( const Pathname & file_r, const Data & data_r )
55  {
57  filesystem::chmod( tmp.path(), 0644 );
58 
59  std::ofstream outs( tmp.path().c_str() );
60  outs << "# zypp::SoftLocksFile generated " << Date::now() << endl;
61  dumpRange( outs, data_r.begin(), data_r.end(), "#", "\n", "\n", "\n", "#\n" );
62  outs.close();
63 
64  if ( outs.good() )
65  {
66  filesystem::rename( tmp.path(), file_r );
67  MIL << "Wrote " << PathInfo(file_r) << endl;
68  }
69  else
70  {
71  ERR << "Can't write " << PathInfo(tmp.path()) << endl;
72  }
73  }
74 
75  /******************************************************************
76  **
77  ** FUNCTION NAME : operator<<
78  ** FUNCTION TYPE : std::ostream &
79  */
80  std::ostream & operator<<( std::ostream & str, const SoftLocksFile & obj )
81  {
82  str << obj.file() << ' ';
83  if ( obj._dataPtr )
84  str << obj.data();
85  else
86  str << "(unloaded)";
87  return str;
88  }
89 
91  } // namespace target
94 } // namespace zypp
#define MIL
Definition: Logger.h:47
bool next()
Advance to next line.
Definition: IOStream.cc:72
Pathname path() const
Definition: TmpPath.cc:146
int chmod(const Pathname &path, mode_t mode)
Like &#39;chmod&#39;.
Definition: PathInfo.cc:1021
const Data & data() const
Return the data.
Definition: SoftLocksFile.h:55
Access to the sat-pools string space.
Definition: IdString.h:39
Simple lineparser: Traverse each line in a file.
Definition: IOStream.h:111
Provide a new empty temporary file and delete it when no longer needed.
Definition: TmpPath.h:126
static void load(const Pathname &file_r, Data &data_r)
Read Data from file_r.
static void store(const Pathname &file_r, const Data &data_r)
Write Data to file_r.
#define ERR
Definition: Logger.h:49
const Pathname & file() const
Return the file path.
Definition: SoftLocksFile.h:48
std::ostream & dumpRange(std::ostream &str, _Iterator begin, _Iterator 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:91
std::string trim(const std::string &s, const Trim trim_r)
Definition: String.cc:204
int rename(const Pathname &oldpath, const Pathname &newpath)
Like &#39;rename&#39;.
Definition: PathInfo.cc:674
const char * c_str() const
String representation.
Definition: Pathname.h:109
#define WAR
Definition: Logger.h:48
std::ostream & operator<<(std::ostream &str, const CommitPackageCache &obj)
scoped_ptr< Data > _dataPtr
Save and restore soft locks.
Definition: SoftLocksFile.h:35
std::tr1::unordered_set< IdString > Data
Definition: SoftLocksFile.h:39
static Date now()
Return the current time.
Definition: Date.h:76
static TmpFile makeSibling(const Pathname &sibling_r)
Provide a new empty temporary directory as sibling.
Definition: TmpPath.cc:218