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