libzypp  17.28.4
PathInfo.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
13 #include <utime.h> // for ::utime
14 #include <sys/statvfs.h>
15 #include <sys/sysmacros.h> // for ::minor, ::major macros
16 
17 #include <iostream>
18 #include <fstream>
19 #include <iomanip>
20 
21 #include <zypp-core/fs/PathInfo.h>
22 #include <zypp-core/base/LogTools.h>
23 #include <zypp-core/base/String.h>
24 #include <zypp-core/base/IOStream.h>
25 #include <zypp-core/base/Errno.h>
26 
27 #include <zypp-core/AutoDispose.h>
28 #include <zypp-core/ExternalProgram.h>
29 #include <zypp-core/Digest.h>
30 #include <zypp-core/fs/TmpPath.h>
31 
32 using std::endl;
33 using std::string;
34 
36 namespace zypp
37 {
39  namespace filesystem
40  {
41 
42  /******************************************************************
43  **
44  ** FUNCTION NAME : operator<<
45  ** FUNCTION TYPE : std::ostream &
46  */
47  std::ostream & operator<<( std::ostream & str, FileType obj )
48  {
49  switch ( obj ) {
50 #define EMUMOUT(T) case T: return str << #T; break
53  EMUMOUT( FT_FILE );
54  EMUMOUT( FT_DIR );
57  EMUMOUT( FT_FIFO );
58  EMUMOUT( FT_LINK );
59  EMUMOUT( FT_SOCKET );
60 #undef EMUMOUT
61  }
62  return str;
63  }
64 
66  //
67  // METHOD NAME : StatMode::fileType
68  // METHOD TYPE : FileType
69  //
71  {
72  if ( isFile() )
73  return FT_FILE;
74  if ( isDir() )
75  return FT_DIR;
76  if ( isLink() )
77  return FT_LINK;
78  if ( isChr() )
79  return FT_CHARDEV;
80  if ( isBlk() )
81  return FT_BLOCKDEV;
82  if ( isFifo() )
83  return FT_FIFO;
84  if ( isSock() )
85  return FT_SOCKET ;
86 
87  return FT_NOT_AVAIL;
88  }
89 
90  /******************************************************************
91  **
92  ** FUNCTION NAME : operator<<
93  ** FUNCTION TYPE : std::ostream &
94  */
95  std::ostream & operator<<( std::ostream & str, const StatMode & obj )
96  {
97  iostr::IosFmtFlagsSaver autoResoreState( str );
98 
99  char t = '?';
100  if ( obj.isFile() )
101  t = '-';
102  else if ( obj.isDir() )
103  t = 'd';
104  else if ( obj.isLink() )
105  t = 'l';
106  else if ( obj.isChr() )
107  t = 'c';
108  else if ( obj.isBlk() )
109  t = 'b';
110  else if ( obj.isFifo() )
111  t = 'p';
112  else if ( obj.isSock() )
113  t = 's';
114 
115  str << t << " " << std::setfill( '0' ) << std::setw( 4 ) << std::oct << obj.perm();
116  return str;
117  }
118 
120  //
121  // Class : PathInfo
122  //
124 
126  //
127  // METHOD NAME : PathInfo::PathInfo
128  // METHOD TYPE : Constructor
129  //
131  : mode_e( STAT )
132  , error_i( -1 )
133  {}
134 
136  //
137  // METHOD NAME : PathInfo::PathInfo
138  // METHOD TYPE : Constructor
139  //
140  PathInfo::PathInfo( const Pathname & path, Mode initial )
141  : path_t( path )
142  , mode_e( initial )
143  , error_i( -1 )
144  {
145  operator()();
146  }
147 
149  //
150  // METHOD NAME : PathInfo::PathInfo
151  // METHOD TYPE : Constructor
152  //
153  PathInfo::PathInfo( const std::string & path, Mode initial )
154  : path_t( path )
155  , mode_e( initial )
156  , error_i( -1 )
157  {
158  operator()();
159  }
160 
162  //
163  // METHOD NAME : PathInfo::PathInfo
164  // METHOD TYPE : Constructor
165  //
166  PathInfo::PathInfo( const char * path, Mode initial )
167  : path_t( path )
168  , mode_e( initial )
169  , error_i( -1 )
170  {
171  operator()();
172  }
173 
175  //
176  // METHOD NAME : PathInfo::~PathInfo
177  // METHOD TYPE : Destructor
178  //
180  {
181  }
182 
184  //
185  // METHOD NAME : PathInfo::operator()
186  // METHOD TYPE : bool
187  //
189  {
190  if ( path_t.empty() ) {
191  error_i = -1;
192  } else {
193  switch ( mode_e ) {
194  case STAT:
195  error_i = ::stat( path_t.asString().c_str(), &statbuf_C );
196  break;
197  case LSTAT:
198  error_i = ::lstat( path_t.asString().c_str(), &statbuf_C );
199  break;
200  }
201  if ( error_i == -1 )
202  error_i = errno;
203  }
204  return !error_i;
205  }
206 
208  //
209  // METHOD NAME : PathInfo::fileType
210  // METHOD TYPE : File_type
211  //
213  {
214  if ( isExist() )
215  return asStatMode().fileType();
216  return FT_NOT_EXIST;
217  }
218 
220  //
221  // METHOD NAME : PathInfo::userMay
222  // METHOD TYPE : mode_t
223  //
224  mode_t PathInfo::userMay() const
225  {
226  if ( !isExist() )
227  return 0;
228  if ( owner() == geteuid() ) {
229  return( uperm()/0100 );
230  } else if ( group() == getegid() ) {
231  return( gperm()/010 );
232  }
233  return operm();
234  }
235 
236  /******************************************************************
237  **
238  ** FUNCTION NAME : PathInfo::devMajor
239  ** FUNCTION TYPE : unsigned int
240  */
241  unsigned int PathInfo::devMajor() const
242  {
243  return isBlk() || isChr() ? major(statbuf_C.st_rdev) : 0;
244  }
245 
246  /******************************************************************
247  **
248  ** FUNCTION NAME : PathInfo::devMinor
249  ** FUNCTION TYPE : unsigned int
250  */
251  unsigned int PathInfo::devMinor() const
252  {
253  return isBlk() || isChr() ? minor(statbuf_C.st_rdev) : 0;
254  }
255 
256  /******************************************************************
257  **
258  ** FUNCTION NAME : operator<<
259  ** FUNCTION TYPE : std::ostream &
260  */
261  std::ostream & operator<<( std::ostream & str, const PathInfo & obj )
262  {
263  iostr::IosFmtFlagsSaver autoResoreState( str );
264 
265  str << obj.asString() << "{";
266  if ( !obj.isExist() ) {
267  str << Errno( obj.error() );
268  } else {
269  str << obj.asStatMode() << " " << std::dec << obj.owner() << "/" << obj.group();
270 
271  if ( obj.isFile() )
272  str << " size " << obj.size();
273  }
274 
275  return str << "}";
276  }
277 
279  //
280  // filesystem utilities
281  //
283 
284 #define logResult MIL << endl, doLogResult
285  namespace {
287  inline int doLogResult( const int res, const char * rclass = 0 /*errno*/ )
288  {
289  if ( res )
290  {
291  if ( rclass )
292  WAR << " FAILED: " << rclass << " " << res << endl;
293  else
294  WAR << " FAILED: " << str::strerror( res ) << endl;
295  }
296  return res;
297  }
298  } // namespace
299 
301  //
302  // METHOD NAME : PathInfo::mkdir
303  // METHOD TYPE : int
304  //
305  int mkdir( const Pathname & path, unsigned mode )
306  {
307  MIL << "mkdir " << path << ' ' << str::octstring( mode );
308  if ( ::mkdir( path.asString().c_str(), mode ) == -1 ) {
309  return logResult( errno );
310  }
311  return logResult( 0 );
312  }
313 
315  //
316  // METHOD NAME : assert_dir()
317  // METHOD TYPE : int
318  //
319  int assert_dir( const Pathname & path, unsigned mode )
320  {
321  if ( path.empty() )
322  return ENOENT;
323 
324  { // Handle existing paths in advance.
325  PathInfo pi( path );
326  if ( pi.isDir() )
327  return 0;
328  if ( pi.isExist() )
329  return EEXIST;
330  }
331 
332  string spath = path.asString()+"/";
333  std::string::size_type lastpos = ( path.relative() ? 2 : 1 ); // skip leasding './' or '/'
334  std::string::size_type pos = std::string::npos;
335  int ret = 0;
336 
337  while ( (pos = spath.find('/',lastpos)) != std::string::npos )
338  {
339  string dir( spath.substr(0,pos) );
340  ret = ::mkdir( dir.c_str(), mode );
341  if ( ret == -1 )
342  {
343  if ( errno == EEXIST ) // ignore errors about already existing paths
344  ret = 0;
345  else
346  {
347  ret = errno;
348  WAR << " FAILED: mkdir " << dir << ' ' << str::octstring( mode ) << " errno " << ret << endl;
349  }
350  }
351  else
352  {
353  MIL << "mkdir " << dir << ' ' << str::octstring( mode ) << endl;
354  }
355  lastpos = pos+1;
356  }
357 
358  return ret;
359  }
360 
362  //
363  // METHOD NAME : rmdir
364  // METHOD TYPE : int
365  //
366  int rmdir( const Pathname & path )
367  {
368  MIL << "rmdir " << path;
369  if ( ::rmdir( path.asString().c_str() ) == -1 ) {
370  return logResult( errno );
371  }
372  return logResult( 0 );
373  }
374 
376  //
377  // METHOD NAME : recursive_rmdir
378  // METHOD TYPE : int
379  //
380  static int recursive_rmdir_1( const Pathname & dir, bool removeDir = true )
381  {
382  DIR * dp;
383  struct dirent * d;
384 
385  if ( ! (dp = opendir( dir.c_str() )) )
386  return logResult( errno );
387 
388  while ( (d = readdir(dp)) )
389  {
390  std::string direntry = d->d_name;
391  if ( direntry == "." || direntry == ".." )
392  continue;
393  Pathname new_path( dir / d->d_name );
394 
395  struct stat st;
396  if ( ! lstat( new_path.c_str(), &st ) )
397  {
398  if ( S_ISDIR( st.st_mode ) )
399  recursive_rmdir_1( new_path );
400  else
401  ::unlink( new_path.c_str() );
402  }
403  }
404  closedir( dp );
405 
406  if ( removeDir && ::rmdir( dir.c_str() ) < 0 )
407  return errno;
408 
409  return 0;
410  }
412  int recursive_rmdir( const Pathname & path )
413  {
414  MIL << "recursive_rmdir " << path << ' ';
415  PathInfo p( path );
416 
417  if ( !p.isExist() ) {
418  return logResult( 0 );
419  }
420 
421  if ( !p.isDir() ) {
422  return logResult( ENOTDIR );
423  }
424 
425  p.lstat(); // get dir symlinks
426  if ( !p.isDir() ) {
427  MIL << "unlink symlink ";
428  if ( ::unlink( path.asString().c_str() ) == -1 ) {
429  return logResult( errno );
430  }
431  return logResult( 0 );
432  }
433 
434  return logResult( recursive_rmdir_1( path ) );
435  }
436 
438  //
439  // METHOD NAME : clean_dir
440  // METHOD TYPE : int
441  //
442  int clean_dir( const Pathname & path )
443  {
444  MIL << "clean_dir " << path << ' ';
445  PathInfo p( path );
446 
447  if ( !p.isExist() ) {
448  return logResult( 0 );
449  }
450 
451  if ( !p.isDir() ) {
452  return logResult( ENOTDIR );
453  }
454 
455  return logResult( recursive_rmdir_1( path, false/* don't remove path itself */ ) );
456  }
457 
459  //
460  // METHOD NAME : copy_dir
461  // METHOD TYPE : int
462  //
463  int copy_dir( const Pathname & srcpath, const Pathname & destpath )
464  {
465  MIL << "copy_dir " << srcpath << " -> " << destpath << ' ';
466 
467  PathInfo sp( srcpath );
468  if ( !sp.isDir() ) {
469  return logResult( ENOTDIR );
470  }
471 
472  PathInfo dp( destpath );
473  if ( !dp.isDir() ) {
474  return logResult( ENOTDIR );
475  }
476 
477  PathInfo tp( destpath + srcpath.basename() );
478  if ( tp.isExist() ) {
479  return logResult( EEXIST );
480  }
481 
482 
483  const char *const argv[] = {
484  "/bin/cp",
485  "-dR",
486  "--",
487  srcpath.asString().c_str(),
488  destpath.asString().c_str(),
489  NULL
490  };
492  for ( string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() ) {
493  MIL << " " << output;
494  }
495  int ret = prog.close();
496  return logResult( ret, "returned" );
497  }
498 
500  //
501  // METHOD NAME : copy_dir_content
502  // METHOD TYPE : int
503  //
504  int copy_dir_content(const Pathname & srcpath, const Pathname & destpath)
505  {
506  MIL << "copy_dir " << srcpath << " -> " << destpath << ' ';
507 
508  PathInfo sp( srcpath );
509  if ( !sp.isDir() ) {
510  return logResult( ENOTDIR );
511  }
512 
513  PathInfo dp( destpath );
514  if ( !dp.isDir() ) {
515  return logResult( ENOTDIR );
516  }
517 
518  if ( srcpath == destpath ) {
519  return logResult( EEXIST );
520  }
521 
522  std::string src( srcpath.asString());
523  src += "/.";
524  const char *const argv[] = {
525  "/bin/cp",
526  "-dR",
527  "--",
528  src.c_str(),
529  destpath.asString().c_str(),
530  NULL
531  };
533  for ( string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() ) {
534  MIL << " " << output;
535  }
536  int ret = prog.close();
537  return logResult( ret, "returned" );
538  }
539 
541  // dirForEach
543 
544  int dirForEach( const Pathname & dir_r, function<bool(const Pathname &, const char *const)> fnc_r )
545  {
546  if ( ! fnc_r )
547  return 0;
548 
549  AutoDispose<DIR *> dir( ::opendir( dir_r.c_str() ),
550  []( DIR * dir_r ) { if ( dir_r ) ::closedir( dir_r ); } );
551 
552  MIL << "readdir " << dir_r << ' ';
553  if ( ! dir )
554  return logResult( errno );
555  MIL << endl; // close line before callbacks are invoked.
556 
557  int ret = 0;
558  for ( struct dirent * entry = ::readdir( dir ); entry; entry = ::readdir( dir ) )
559  {
560  if ( entry->d_name[0] == '.' && ( entry->d_name[1] == '\0' || ( entry->d_name[1] == '.' && entry->d_name[2] == '\0' ) ) )
561  continue; // omitt . and ..
562 
563  if ( ! fnc_r( dir_r, entry->d_name ) )
564  {
565  ret = -1;
566  break;
567  }
568  }
569  return ret;
570  }
571 
573  // readdir
575 
576  int readdir( std::list<std::string> & retlist_r, const Pathname & path_r, bool dots_r )
577  {
578  retlist_r.clear();
579  return dirForEach( path_r,
580  [&]( const Pathname & dir_r, const char *const name_r )->bool
581  {
582  if ( dots_r || name_r[0] != '.' )
583  retlist_r.push_back( name_r );
584  return true;
585  } );
586  }
587 
588 
589  int readdir( std::list<Pathname> & retlist_r, const Pathname & path_r, bool dots_r )
590  {
591  retlist_r.clear();
592  return dirForEach( path_r,
593  [&]( const Pathname & dir_r, const char *const name_r )->bool
594  {
595  if ( dots_r || name_r[0] != '.' )
596  retlist_r.push_back( dir_r/name_r );
597  return true;
598  } );
599  }
600 
601  bool DirEntry::operator==( const DirEntry &rhs ) const
602  {
603  // if one of the types is not known, use the name only
604  if ( type == FT_NOT_AVAIL || rhs.type == FT_NOT_AVAIL )
605  return ( name == rhs.name );
606  return ((name == rhs.name ) && (type == rhs.type));
607  }
608 
609  int readdir( DirContent & retlist_r, const Pathname & path_r, bool dots_r, PathInfo::Mode statmode_r )
610  {
611  retlist_r.clear();
612  return dirForEach( path_r,
613  [&]( const Pathname & dir_r, const char *const name_r )->bool
614  {
615  if ( dots_r || name_r[0] != '.' )
616  retlist_r.push_back( DirEntry( name_r, PathInfo( dir_r/name_r, statmode_r ).fileType() ) );
617  return true;
618  } );
619  }
620 
621  std::ostream & operator<<( std::ostream & str, const DirContent & obj )
622  { return dumpRange( str, obj.begin(), obj.end() ); }
623 
625  // is_empty_dir
627 
628  int is_empty_dir( const Pathname & path_r )
629  {
630  return dirForEach( path_r,
631  [&]( const Pathname & dir_r, const char *const name_r )->bool
632  { return false; } );
633  }
634 
636  //
637  // METHOD NAME : unlink
638  // METHOD TYPE : int
639  //
640  int unlink( const Pathname & path )
641  {
642  MIL << "unlink " << path;
643  if ( ::unlink( path.asString().c_str() ) == -1 ) {
644  return logResult( errno );
645  }
646  return logResult( 0 );
647  }
648 
650  namespace
651  {
652  int safe_rename( const Pathname & oldpath, const Pathname & newpath )
653  {
654  int ret = ::rename( oldpath.asString().c_str(), newpath.asString().c_str() );
655 
656  // rename(2) can fail on OverlayFS. Fallback to using mv(1), which is
657  // explicitly mentioned in the kernel docs to deal correctly with OverlayFS.
658  if ( ret == -1 && errno == EXDEV ) {
659  const char *const argv[] = {
660  "/usr/bin/mv",
661  oldpath.asString().c_str(),
662  newpath.asString().c_str(),
663  NULL
664  };
665  ExternalProgram prog( argv, ExternalProgram::Stderr_To_Stdout );
666  for ( string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() ) {
667  MIL << " " << output;
668  }
669  ret = prog.close();
670  }
671 
672  return ret;
673  }
674  } // namespace
676 
678  //
679  // METHOD NAME : rename
680  // METHOD TYPE : int
681  //
682  int rename( const Pathname & oldpath, const Pathname & newpath )
683  {
684  MIL << "rename " << oldpath << " -> " << newpath;
685  if ( safe_rename( oldpath.asString().c_str(), newpath.asString().c_str() ) == -1 ) {
686  return logResult( errno );
687  }
688  return logResult( 0 );
689  }
690 
692  //
693  // METHOD NAME : exchange
694  // METHOD TYPE : int
695  //
696  int exchange( const Pathname & lpath, const Pathname & rpath )
697  {
698  MIL << "exchange " << lpath << " <-> " << rpath;
699  if ( lpath.empty() || rpath.empty() )
700  return logResult( EINVAL );
701 
702  PathInfo linfo( lpath );
703  PathInfo rinfo( rpath );
704 
705  if ( ! linfo.isExist() )
706  {
707  if ( ! rinfo.isExist() )
708  return logResult( 0 ); // both don't exist.
709 
710  // just rename rpath -> lpath
711  int ret = assert_dir( lpath.dirname() );
712  if ( ret != 0 )
713  return logResult( ret );
714  if ( safe_rename( rpath.c_str(), lpath.c_str() ) == -1 ) {
715  return logResult( errno );
716  }
717  return logResult( 0 );
718  }
719 
720  // HERE: lpath exists:
721  if ( ! rinfo.isExist() )
722  {
723  // just rename lpath -> rpath
724  int ret = assert_dir( rpath.dirname() );
725  if ( ret != 0 )
726  return logResult( ret );
727  if ( safe_rename( lpath.c_str(), rpath.c_str() ) == -1 ) {
728  return logResult( errno );
729  }
730  return logResult( 0 );
731  }
732 
733  // HERE: both exist
734  TmpFile tmpfile( TmpFile::makeSibling( rpath ) );
735  if ( ! tmpfile )
736  return logResult( errno );
737  Pathname tmp( tmpfile.path() );
738  ::unlink( tmp.c_str() );
739 
740  if ( safe_rename( lpath.c_str(), tmp.c_str() ) == -1 ) {
741  return logResult( errno );
742  }
743  if ( safe_rename( rpath.c_str(), lpath.c_str() ) == -1 ) {
744  safe_rename( tmp.c_str(), lpath.c_str() );
745  return logResult( errno );
746  }
747  if ( safe_rename( tmp.c_str(), rpath.c_str() ) == -1 ) {
748  safe_rename( lpath.c_str(), rpath.c_str() );
749  safe_rename( tmp.c_str(), lpath.c_str() );
750  return logResult( errno );
751  }
752  return logResult( 0 );
753  }
754 
756  //
757  // METHOD NAME : copy
758  // METHOD TYPE : int
759  //
760  int copy( const Pathname & file, const Pathname & dest )
761  {
762  MIL << "copy " << file << " -> " << dest << ' ';
763 
764  PathInfo sp( file );
765  if ( !sp.isFile() ) {
766  return logResult( EINVAL );
767  }
768 
769  PathInfo dp( dest );
770  if ( dp.isDir() ) {
771  return logResult( EISDIR );
772  }
773 
774  const char *const argv[] = {
775  "/bin/cp",
776  "--remove-destination",
777  "--",
778  file.asString().c_str(),
779  dest.asString().c_str(),
780  NULL
781  };
783  for ( string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() ) {
784  MIL << " " << output;
785  }
786  int ret = prog.close();
787  return logResult( ret, "returned" );
788  }
789 
791  //
792  // METHOD NAME : symlink
793  // METHOD TYPE : int
794  //
795  int symlink( const Pathname & oldpath, const Pathname & newpath )
796  {
797  MIL << "symlink " << newpath << " -> " << oldpath;
798  if ( ::symlink( oldpath.asString().c_str(), newpath.asString().c_str() ) == -1 ) {
799  return logResult( errno );
800  }
801  return logResult( 0 );
802  }
803 
805  //
806  // METHOD NAME : hardlink
807  // METHOD TYPE : int
808  //
809  int hardlink( const Pathname & oldpath, const Pathname & newpath )
810  {
811  MIL << "hardlink " << newpath << " -> " << oldpath;
812  if ( ::link( oldpath.asString().c_str(), newpath.asString().c_str() ) == -1 ) {
813  return logResult( errno );
814  }
815  return logResult( 0 );
816  }
817 
819  //
820  // METHOD NAME : hardlink
821  // METHOD TYPE : int
822  //
823  int hardlinkCopy( const Pathname & oldpath, const Pathname & newpath )
824  {
825  MIL << "hardlinkCopy " << oldpath << " -> " << newpath;
826 
827  PathInfo pi( oldpath, PathInfo::LSTAT );
828  if ( pi.isLink() )
829  {
830  // dont hardlink symlinks!
831  MIL << " => copy" << endl;
832  return copy( oldpath, newpath );
833  }
834 
835  pi.lstat( newpath );
836  if ( pi.isExist() )
837  {
838  int res = unlink( newpath );
839  if ( res != 0 )
840  return logResult( res );
841  }
842 
843  // Here: no symlink, no newpath
844  if ( ::link( oldpath.asString().c_str(), newpath.asString().c_str() ) == -1 )
845  {
846  switch ( errno )
847  {
848  case EPERM: // /proc/sys/fs/protected_hardlink in proc(5)
849  case EXDEV: // oldpath and newpath are not on the same mounted file system
850  MIL << " => copy" << endl;
851  return copy( oldpath, newpath );
852  break;
853  }
854  return logResult( errno );
855  }
856  return logResult( 0 );
857  }
858 
860  //
861  // METHOD NAME : readlink
862  // METHOD TYPE : int
863  //
864  int readlink( const Pathname & symlink_r, Pathname & target_r )
865  {
866  static const ssize_t bufsiz = 2047;
867  static char buf[bufsiz+1];
868  ssize_t ret = ::readlink( symlink_r.c_str(), buf, bufsiz );
869  if ( ret == -1 )
870  {
871  target_r = Pathname();
872  MIL << "readlink " << symlink_r;
873  return logResult( errno );
874  }
875  buf[ret] = '\0';
876  target_r = buf;
877  return 0;
878  }
879 
881  //
882  // METHOD NAME : expandlink
883  // METHOD TYPE : Pathname
884  //
885  Pathname expandlink( const Pathname & path_r )
886  {
887  static const unsigned int level_limit = 256;
888  static unsigned int count;
889  Pathname path(path_r);
890  PathInfo info(path_r, PathInfo::LSTAT);
891 
892  for (count = level_limit; info.isLink() && count; count--)
893  {
894  DBG << "following symlink " << path;
895  path = path.dirname() / readlink(path);
896  DBG << "->" << path << std::endl;
897  info = PathInfo(path, PathInfo::LSTAT);
898  }
899 
900  // expand limit reached
901  if (count == 0)
902  {
903  ERR << "Expand level limit reached. Probably a cyclic symbolic link." << endl;
904  return Pathname();
905  }
906  // symlink
907  else if (count < level_limit)
908  {
909  // check for a broken link
910  if (PathInfo(path).isExist())
911  return path;
912  // broken link, return an empty path
913  else
914  {
915  ERR << path << " is broken (expanded from " << path_r << ")" << endl;
916  return Pathname();
917  }
918  }
919 
920  // not a symlink, return the original pathname
921  DBG << "not a symlink" << endl;
922  return path;
923  }
924 
926  //
927  // METHOD NAME : copy_file2dir
928  // METHOD TYPE : int
929  //
930  int copy_file2dir( const Pathname & file, const Pathname & dest )
931  {
932  MIL << "copy_file2dir " << file << " -> " << dest << ' ';
933 
934  PathInfo sp( file );
935  if ( !sp.isFile() ) {
936  return logResult( EINVAL );
937  }
938 
939  PathInfo dp( dest );
940  if ( !dp.isDir() ) {
941  return logResult( ENOTDIR );
942  }
943 
944  const char *const argv[] = {
945  "/bin/cp",
946  "--",
947  file.asString().c_str(),
948  dest.asString().c_str(),
949  NULL
950  };
952  for ( string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() ) {
953  MIL << " " << output;
954  }
955  int ret = prog.close();
956  return logResult( ret, "returned" );
957  }
958 
960  //
961  // METHOD NAME : md5sum
962  // METHOD TYPE : std::string
963  //
964  std::string md5sum( const Pathname & file )
965  {
966  if ( ! PathInfo( file ).isFile() ) {
967  return string();
968  }
969  std::ifstream istr( file.asString().c_str() );
970  if ( ! istr ) {
971  return string();
972  }
973  return Digest::digest( "MD5", istr );
974  }
975 
977  //
978  // METHOD NAME : sha1sum
979  // METHOD TYPE : std::string
980  //
981  std::string sha1sum( const Pathname & file )
982  {
983  return checksum(file, "SHA1");
984  }
985 
987  //
988  // METHOD NAME : checksum
989  // METHOD TYPE : std::string
990  //
991  std::string checksum( const Pathname & file, const std::string &algorithm )
992  {
993  if ( ! PathInfo( file ).isFile() ) {
994  return string();
995  }
996  std::ifstream istr( file.asString().c_str() );
997  if ( ! istr ) {
998  return string();
999  }
1000  return Digest::digest( algorithm, istr );
1001  }
1002 
1003  bool is_checksum( const Pathname & file, const CheckSum &checksum )
1004  {
1005  return ( filesystem::checksum(file, checksum.type()) == checksum.checksum() );
1006  }
1007 
1009  //
1010  // METHOD NAME : erase
1011  // METHOD TYPE : int
1012  //
1013  int erase( const Pathname & path )
1014  {
1015  int res = 0;
1016  PathInfo p( path, PathInfo::LSTAT );
1017  if ( p.isExist() )
1018  {
1019  if ( p.isDir() )
1020  res = recursive_rmdir( path );
1021  else
1022  res = unlink( path );
1023  }
1024  return res;
1025  }
1026 
1028  //
1029  // METHOD NAME : chmod
1030  // METHOD TYPE : int
1031  //
1032  int chmod( const Pathname & path, mode_t mode )
1033  {
1034  MIL << "chmod " << path << ' ' << str::octstring( mode );
1035  if ( ::chmod( path.asString().c_str(), mode ) == -1 ) {
1036  return logResult( errno );
1037  }
1038  return logResult( 0 );
1039  }
1040 
1041  int addmod( const Pathname & path, mode_t mode )
1042  {
1043  mode_t omode( PathInfo( path ).st_mode() );
1044  mode_t tmode( omode | mode );
1045  if ( omode != mode )
1046  return chmod( path, tmode );
1047  return 0;
1048  }
1049 
1050  int delmod( const Pathname & path, mode_t mode )
1051  {
1052  mode_t omode( PathInfo( path ).st_mode() );
1053  mode_t tmode( omode & ~mode );
1054  if ( omode != mode )
1055  return chmod( path, tmode );
1056  return 0;
1057  }
1058 
1060  //
1061  // METHOD NAME : zipType
1062  // METHOD TYPE : ZIP_TYPE
1063  //
1064  ZIP_TYPE zipType( const Pathname & file )
1065  {
1066  ZIP_TYPE ret = ZT_NONE;
1067 
1068  int fd = open( file.asString().c_str(), O_RDONLY|O_CLOEXEC );
1069 
1070  if ( fd != -1 ) {
1071  const int magicSize = 5;
1072  unsigned char magic[magicSize];
1073  memset( magic, 0, magicSize );
1074  if ( read( fd, magic, magicSize ) == magicSize ) {
1075  if ( magic[0] == 0037 && magic[1] == 0213 ) {
1076  ret = ZT_GZ;
1077  } else if ( magic[0] == 'B' && magic[1] == 'Z' && magic[2] == 'h' ) {
1078  ret = ZT_BZ2;
1079  } else if ( magic[0] == '\0' && magic[1] == 'Z' && magic[2] == 'C' && magic[3] == 'K' && magic[4] == '1') {
1080  ret = ZT_ZCHNK;
1081 
1082  }
1083  }
1084  close( fd );
1085  }
1086 
1087  return ret;
1088  }
1089 
1091  //
1092  // METHOD NAME : df
1093  // METHOD TYPE : ByteCount
1094  //
1095  ByteCount df( const Pathname & path_r )
1096  {
1097  ByteCount ret( -1 );
1098  struct statvfs sb;
1099  if ( statvfs( path_r.c_str(), &sb ) == 0 )
1100  {
1101  ret = sb.f_bfree * sb.f_bsize;
1102  }
1103  return ret;
1104  }
1105 
1107  //
1108  // METHOD NAME : getUmask
1109  // METHOD TYPE : mode_t
1110  //
1111  mode_t getUmask()
1112  {
1113  mode_t mask = ::umask( 0022 );
1114  ::umask( mask );
1115  return mask;
1116  }
1117 
1119  //
1120  // METHOD NAME : getUmask
1121  // METHOD TYPE : mode_t
1122  //
1123  int assert_file( const Pathname & path, unsigned mode )
1124  {
1125  int ret = assert_dir( path.dirname() );
1126  MIL << "assert_file " << str::octstring( mode ) << " " << path;
1127  if ( ret != 0 )
1128  return logResult( ret );
1129 
1130  PathInfo pi( path );
1131  if ( pi.isExist() )
1132  return logResult( pi.isFile() ? 0 : EEXIST );
1133 
1134  int fd = ::creat( path.c_str(), mode );
1135  if ( fd == -1 )
1136  return logResult( errno );
1137 
1138  ::close( fd );
1139  return logResult( 0 );
1140  }
1141 
1142  int assert_file_mode( const Pathname & path, unsigned mode )
1143  {
1144  int ret = assert_dir( path.dirname() );
1145  MIL << "assert_file_mode " << str::octstring( mode ) << " " << path;
1146  if ( ret != 0 )
1147  return logResult( ret );
1148 
1149  PathInfo pi( path );
1150  if ( pi.isExist() )
1151  {
1152  if ( ! pi.isFile() )
1153  return logResult( EEXIST );
1154 
1155  mode = applyUmaskTo( mode );
1156  if ( pi.st_mode() != mode )
1157  return chmod( path, mode );
1158 
1159  return logResult( 0 );
1160  }
1161 
1162  int fd = ::creat( path.c_str(), mode );
1163  if ( fd == -1 )
1164  return logResult( errno );
1165  ::close( fd );
1166  return logResult( 0 );
1167  }
1168 
1170  //
1171  // METHOD NAME : touch
1172  // METHOD TYPE : int
1173  //
1174  int touch (const Pathname & path)
1175  {
1176  MIL << "touch " << path;
1177  struct ::utimbuf times;
1178  times.actime = ::time( 0 );
1179  times.modtime = ::time( 0 );
1180  if ( ::utime( path.asString().c_str(), &times ) == -1 ) {
1181  return logResult( errno );
1182  }
1183  return logResult( 0 );
1184  }
1185 
1187  } // namespace filesystem
1190 } // namespace zypp
Reference counted access to a Tp object calling a custom Dispose function when the last AutoDispose h...
Definition: AutoDispose.h:93
Store and operate with byte count.
Definition: ByteCount.h:31
std::string digest()
get hex string representation of the digest
Definition: Digest.cc:175
Convenience errno wrapper.
Definition: Errno.h:26
Execute a program and give access to its io An object of this class encapsulates the execution of an ...
int close()
Wait for the progamm to complete.
std::string receiveLine()
Read one line from the input stream.
Wrapper class for ::stat/::lstat.
Definition: PathInfo.h:219
const std::string & asString() const
Return current Pathname as String.
Definition: PathInfo.h:246
bool lstat(const Pathname &path)
LSTAT path.
Definition: PathInfo.h:262
mode_t uperm() const
Definition: PathInfo.h:319
StatMode asStatMode() const
Return st_mode() as filesystem::StatMode.
Definition: PathInfo.h:328
bool stat()
STAT current path.
Definition: PathInfo.h:267
mode_t st_mode() const
Definition: PathInfo.h:324
bool operator()()
Restat current path using current mode.
Definition: PathInfo.cc:188
Mode
stat() or lstat()
Definition: PathInfo.h:224
unsigned int devMinor() const
Definition: PathInfo.cc:251
FileType fileType() const
Definition: PathInfo.cc:212
mode_t gperm() const
Definition: PathInfo.h:320
bool lstat()
LSTAT current path.
Definition: PathInfo.h:269
mode_t userMay() const
Returns current users permission ([0-7])
Definition: PathInfo.cc:224
bool isExist() const
Return whether valid stat info exists.
Definition: PathInfo.h:279
mode_t operm() const
Definition: PathInfo.h:321
unsigned int devMajor() const
Definition: PathInfo.cc:241
int error() const
Return error returned from last stat/lstat call.
Definition: PathInfo.h:252
Pathname dirname() const
Return all but the last component od this path.
Definition: Pathname.h:124
const std::string & asString() const
String representation.
Definition: Pathname.h:91
std::string basename() const
Return the last component of this path.
Definition: Pathname.h:128
bool empty() const
Test for an empty path.
Definition: Pathname.h:114
const char * c_str() const
String representation.
Definition: Pathname.h:110
bool relative() const
Test for a relative path.
Definition: Pathname.h:118
Wrapper class for mode_t values as derived from ::stat.
Definition: PathInfo.h:79
mode_t perm() const
Definition: PathInfo.h:154
FileType fileType() const
Definition: PathInfo.cc:70
Provide a new empty temporary file and delete it when no longer needed.
Definition: TmpPath.h:128
static TmpFile makeSibling(const Pathname &sibling_r)
Provide a new empty temporary directory as sibling.
Definition: TmpPath.cc:218
Pathname path() const
Definition: TmpPath.cc:146
String related utilities and Regular expression matching.
std::map< std::string, std::string > read(const Pathname &_path)
Read sysconfig file path_r and return (key,valye) pairs.
Definition: Sysconfig.cc:34
int chmod(const Pathname &path, mode_t mode)
Like 'chmod'.
Definition: PathInfo.cc:1032
std::string checksum(const Pathname &file, const std::string &algorithm)
Compute a files checksum.
Definition: PathInfo.cc:991
int delmod(const Pathname &path, mode_t mode)
Remove the mode bits from the file given by path.
Definition: PathInfo.cc:1050
static int recursive_rmdir_1(const Pathname &dir, bool removeDir=true)
Definition: PathInfo.cc:380
FileType
File type information.
Definition: PathInfo.h:54
Pathname expandlink(const Pathname &path_r)
Recursively follows the symlink pointed to by path_r and returns the Pathname to the real file or dir...
Definition: PathInfo.cc:885
int is_empty_dir(const Pathname &path_r)
Check if the specified directory is empty.
Definition: PathInfo.cc:628
mode_t applyUmaskTo(mode_t mode_r)
Modify mode_r according to the current umask ( mode_r & ~getUmask() ).
Definition: PathInfo.h:780
int readdir(std::list< std::string > &retlist_r, const Pathname &path_r, bool dots_r)
Return content of directory via retlist.
Definition: PathInfo.cc:576
ByteCount df(const Pathname &path_r)
Report free disk space on a mounted file system.
Definition: PathInfo.cc:1095
int unlink(const Pathname &path)
Like 'unlink'.
Definition: PathInfo.cc:640
int rename(const Pathname &oldpath, const Pathname &newpath)
Like 'rename'.
Definition: PathInfo.cc:682
int mkdir(const Pathname &path, unsigned mode)
Like 'mkdir'.
Definition: PathInfo.cc:305
int rmdir(const Pathname &path)
Like 'rmdir'.
Definition: PathInfo.cc:366
bool is_checksum(const Pathname &file, const CheckSum &checksum)
check files checksum
Definition: PathInfo.cc:1003
int assert_file(const Pathname &path, unsigned mode)
Create an empty file if it does not yet exist.
Definition: PathInfo.cc:1123
int recursive_rmdir(const Pathname &path)
Like 'rm -r DIR'.
Definition: PathInfo.cc:412
int copy_dir(const Pathname &srcpath, const Pathname &destpath)
Like 'cp -a srcpath destpath'.
Definition: PathInfo.cc:463
int copy_dir_content(const Pathname &srcpath, const Pathname &destpath)
Like 'cp -a srcpath/.
Definition: PathInfo.cc:504
int dirForEach(const Pathname &dir_r, const StrMatcher &matcher_r, function< bool(const Pathname &, const char *const)> fnc_r)
Definition: PathInfo.cc:32
int erase(const Pathname &path)
Erase whatever happens to be located at path (file or directory).
Definition: PathInfo.cc:1013
int addmod(const Pathname &path, mode_t mode)
Add the mode bits to the file given by path.
Definition: PathInfo.cc:1041
int hardlink(const Pathname &oldpath, const Pathname &newpath)
Like '::link'.
Definition: PathInfo.cc:809
int readlink(const Pathname &symlink_r, Pathname &target_r)
Like 'readlink'.
Definition: PathInfo.cc:864
int assert_dir(const Pathname &path, unsigned mode)
Like 'mkdir -p'.
Definition: PathInfo.cc:319
int copy_file2dir(const Pathname &file, const Pathname &dest)
Like 'cp file dest'.
Definition: PathInfo.cc:930
int assert_file_mode(const Pathname &path, unsigned mode)
Like assert_file but enforce mode even if the file already exists.
Definition: PathInfo.cc:1142
mode_t getUmask()
Get the current umask (file mode creation mask)
Definition: PathInfo.cc:1111
int touch(const Pathname &path)
Change file's modification and access times.
Definition: PathInfo.cc:1174
std::ostream & operator<<(std::ostream &str, const Glob &obj)
Definition: Glob.cc:53
int hardlinkCopy(const Pathname &oldpath, const Pathname &newpath)
Create newpath as hardlink or copy of oldpath.
Definition: PathInfo.cc:823
std::list< DirEntry > DirContent
Returned by readdir.
Definition: PathInfo.h:514
std::string md5sum(const Pathname &file)
Compute a files md5sum.
Definition: PathInfo.cc:964
int exchange(const Pathname &lpath, const Pathname &rpath)
Exchanges two files or directories.
Definition: PathInfo.cc:696
std::string sha1sum(const Pathname &file)
Compute a files sha1sum.
Definition: PathInfo.cc:981
ZIP_TYPE zipType(const Pathname &file)
Definition: PathInfo.cc:1064
int copy(const Pathname &file, const Pathname &dest)
Like 'cp file dest'.
Definition: PathInfo.cc:760
ZIP_TYPE
Test whether a file is compressed (gzip/bzip2).
Definition: PathInfo.h:745
int symlink(const Pathname &oldpath, const Pathname &newpath)
Like 'symlink'.
Definition: PathInfo.cc:795
int clean_dir(const Pathname &path)
Like 'rm -r DIR/ *'.
Definition: PathInfo.cc:442
boost::io::ios_base_all_saver IosFmtFlagsSaver
Save and restore streams width, precision and fmtflags.
Definition: IOStream.h:35
SolvableIdType size_type
Definition: PoolMember.h:126
std::string octstring(char n, int w=4)
Definition: String.h:348
std::string strerror(int errno_r)
Return string describing the error_r code.
Definition: String.cc:53
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:2
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:91
Listentry returned by readdir.
Definition: PathInfo.h:499
bool operator==(const DirEntry &rhs) const
Definition: PathInfo.cc:601
#define DBG
Definition: Logger.h:95
#define MIL
Definition: Logger.h:96
#define ERR
Definition: Logger.h:98
#define WAR
Definition: Logger.h:97
#define EMUMOUT(T)
#define logResult
Definition: PathInfo.cc:284