1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/PathInfo.cc
13 #include <sys/types.h> // for ::minor, ::major macros
14 #include <utime.h> // for ::utime
15 #include <sys/statvfs.h>
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"
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"
36 ///////////////////////////////////////////////////////////////////
38 { /////////////////////////////////////////////////////////////////
39 ///////////////////////////////////////////////////////////////////
41 { /////////////////////////////////////////////////////////////////
43 /******************************************************************
45 ** FUNCTION NAME : operator<<
46 ** FUNCTION TYPE : std::ostream &
48 std::ostream & operator<<( std::ostream & str, FileType obj )
51 #define EMUMOUT(T) case T: return str << #T; break
52 EMUMOUT( FT_NOT_AVAIL );
53 EMUMOUT( FT_NOT_EXIST );
56 EMUMOUT( FT_CHARDEV );
57 EMUMOUT( FT_BLOCKDEV );
66 ///////////////////////////////////////////////////////////////////
68 // METHOD NAME : StatMode::fileType
69 // METHOD TYPE : FileType
71 FileType StatMode::fileType() const
91 /******************************************************************
93 ** FUNCTION NAME : operator<<
94 ** FUNCTION TYPE : std::ostream &
96 std::ostream & operator<<( std::ostream & str, const StatMode & obj )
98 iostr::IosFmtFlagsSaver autoResoreState( str );
103 else if ( obj.isDir() )
105 else if ( obj.isLink() )
107 else if ( obj.isChr() )
109 else if ( obj.isBlk() )
111 else if ( obj.isFifo() )
113 else if ( obj.isSock() )
116 str << t << " " << std::setfill( '0' ) << std::setw( 4 ) << std::oct << obj.perm();
120 ///////////////////////////////////////////////////////////////////
124 ///////////////////////////////////////////////////////////////////
126 ///////////////////////////////////////////////////////////////////
128 // METHOD NAME : PathInfo::PathInfo
129 // METHOD TYPE : Constructor
136 ///////////////////////////////////////////////////////////////////
138 // METHOD NAME : PathInfo::PathInfo
139 // METHOD TYPE : Constructor
141 PathInfo::PathInfo( const Pathname & path, Mode initial )
149 ///////////////////////////////////////////////////////////////////
151 // METHOD NAME : PathInfo::PathInfo
152 // METHOD TYPE : Constructor
154 PathInfo::PathInfo( const std::string & path, Mode initial )
162 ///////////////////////////////////////////////////////////////////
164 // METHOD NAME : PathInfo::PathInfo
165 // METHOD TYPE : Constructor
167 PathInfo::PathInfo( const char * path, Mode initial )
175 ///////////////////////////////////////////////////////////////////
177 // METHOD NAME : PathInfo::~PathInfo
178 // METHOD TYPE : Destructor
180 PathInfo::~PathInfo()
184 ///////////////////////////////////////////////////////////////////
186 // METHOD NAME : PathInfo::operator()
187 // METHOD TYPE : bool
189 bool PathInfo::operator()()
191 if ( path_t.empty() ) {
196 error_i = ::stat( path_t.asString().c_str(), &statbuf_C );
199 error_i = ::lstat( path_t.asString().c_str(), &statbuf_C );
208 ///////////////////////////////////////////////////////////////////
210 // METHOD NAME : PathInfo::fileType
211 // METHOD TYPE : File_type
213 FileType PathInfo::fileType() const
216 return asStatMode().fileType();
220 ///////////////////////////////////////////////////////////////////
222 // METHOD NAME : PathInfo::userMay
223 // METHOD TYPE : mode_t
225 mode_t PathInfo::userMay() const
229 if ( owner() == geteuid() ) {
230 return( uperm()/0100 );
231 } else if ( group() == getegid() ) {
232 return( gperm()/010 );
237 /******************************************************************
239 ** FUNCTION NAME : PathInfo::devMajor
240 ** FUNCTION TYPE : unsigned int
242 unsigned int PathInfo::devMajor() const
244 return isBlk() || isChr() ? major(statbuf_C.st_rdev) : 0;
247 /******************************************************************
249 ** FUNCTION NAME : PathInfo::devMinor
250 ** FUNCTION TYPE : unsigned int
252 unsigned int PathInfo::devMinor() const
254 return isBlk() || isChr() ? minor(statbuf_C.st_rdev) : 0;
257 /******************************************************************
259 ** FUNCTION NAME : operator<<
260 ** FUNCTION TYPE : std::ostream &
262 std::ostream & operator<<( std::ostream & str, const PathInfo & obj )
264 iostr::IosFmtFlagsSaver autoResoreState( str );
266 str << obj.asString() << "{";
267 if ( !obj.isExist() ) {
268 str << Errno( obj.error() );
270 str << obj.asStatMode() << " " << std::dec << obj.owner() << "/" << obj.group();
273 str << " size " << obj.size();
279 ///////////////////////////////////////////////////////////////////
281 // filesystem utilities
283 ///////////////////////////////////////////////////////////////////
285 /******************************************************************
287 ** FUNCTION NAME : _Log_Result
288 ** FUNCTION TYPE : int
290 ** DESCRIPTION : Helper function to log return values.
292 #define _Log_Result MIL << endl, __Log_Result
293 inline int __Log_Result( const int res, const char * rclass = 0 /*errno*/ )
298 WAR << " FAILED: " << rclass << " " << res << endl;
300 WAR << " FAILED: " << str::strerror( res ) << endl;
305 ///////////////////////////////////////////////////////////////////
307 // METHOD NAME : PathInfo::mkdir
310 int mkdir( const Pathname & path, unsigned mode )
312 MIL << "mkdir " << path << ' ' << str::octstring( mode );
313 if ( ::mkdir( path.asString().c_str(), mode ) == -1 ) {
314 return _Log_Result( errno );
316 return _Log_Result( 0 );
319 ///////////////////////////////////////////////////////////////////
321 // METHOD NAME : assert_dir()
324 int assert_dir( const Pathname & path, unsigned mode )
329 { // Handle existing paths in advance.
337 string spath = path.asString()+"/";
338 string::size_type lastpos = ( path.relative() ? 2 : 1 ); // skip leasding './' or '/'
339 string::size_type pos = string::npos;
342 while ( (pos = spath.find('/',lastpos)) != string::npos )
344 string dir( spath.substr(0,pos) );
345 ret = ::mkdir( dir.c_str(), mode );
348 if ( errno == EEXIST ) // ignore errors about already existing paths
353 WAR << " FAILED: mkdir " << dir << ' ' << str::octstring( mode ) << " errno " << ret << endl;
358 MIL << "mkdir " << dir << ' ' << str::octstring( mode ) << endl;
366 ///////////////////////////////////////////////////////////////////
368 // METHOD NAME : rmdir
371 int rmdir( const Pathname & path )
373 MIL << "rmdir " << path;
374 if ( ::rmdir( path.asString().c_str() ) == -1 ) {
375 return _Log_Result( errno );
377 return _Log_Result( 0 );
380 ///////////////////////////////////////////////////////////////////
382 // METHOD NAME : recursive_rmdir
385 static int recursive_rmdir_1( const Pathname & dir )
390 if ( ! (dp = opendir( dir.c_str() )) )
391 return _Log_Result( errno );
393 while ( (d = readdir(dp)) )
395 std::string direntry = d->d_name;
396 if ( direntry == "." || direntry == ".." )
398 Pathname new_path( dir / d->d_name );
401 if ( ! lstat( new_path.c_str(), &st ) )
403 if ( S_ISDIR( st.st_mode ) )
404 recursive_rmdir_1( new_path );
406 ::unlink( new_path.c_str() );
411 if ( ::rmdir( dir.c_str() ) < 0 )
416 ///////////////////////////////////////////////////////////////////
417 int recursive_rmdir( const Pathname & path )
419 MIL << "recursive_rmdir " << path << ' ';
422 if ( !p.isExist() ) {
423 return _Log_Result( 0 );
427 return _Log_Result( ENOTDIR );
430 return _Log_Result( recursive_rmdir_1( path ) );
433 ///////////////////////////////////////////////////////////////////
435 // METHOD NAME : clean_dir
438 int clean_dir( const Pathname & path )
440 MIL << "clean_dir " << path << ' ';
443 if ( !p.isExist() ) {
444 return _Log_Result( 0 );
448 return _Log_Result( ENOTDIR );
451 string cmd( str::form( "cd '%s' && rm -rf --preserve-root -- *", path.asString().c_str() ) );
452 ExternalProgram prog( cmd, ExternalProgram::Stderr_To_Stdout );
453 for ( string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() ) {
454 MIL << " " << output;
456 int ret = prog.close();
457 return _Log_Result( ret, "returned" );
460 ///////////////////////////////////////////////////////////////////
462 // METHOD NAME : copy_dir
465 int copy_dir( const Pathname & srcpath, const Pathname & destpath )
467 MIL << "copy_dir " << srcpath << " -> " << destpath << ' ';
469 PathInfo sp( srcpath );
471 return _Log_Result( ENOTDIR );
474 PathInfo dp( destpath );
476 return _Log_Result( ENOTDIR );
479 PathInfo tp( destpath + srcpath.basename() );
480 if ( tp.isExist() ) {
481 return _Log_Result( EEXIST );
485 const char *const argv[] = {
489 srcpath.asString().c_str(),
490 destpath.asString().c_str(),
493 ExternalProgram prog( argv, ExternalProgram::Stderr_To_Stdout );
494 for ( string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() ) {
495 MIL << " " << output;
497 int ret = prog.close();
498 return _Log_Result( ret, "returned" );
501 ///////////////////////////////////////////////////////////////////
503 // METHOD NAME : copy_dir_content
506 int copy_dir_content(const Pathname & srcpath, const Pathname & destpath)
508 MIL << "copy_dir " << srcpath << " -> " << destpath << ' ';
510 PathInfo sp( srcpath );
512 return _Log_Result( ENOTDIR );
515 PathInfo dp( destpath );
517 return _Log_Result( ENOTDIR );
520 if ( srcpath == destpath ) {
521 return _Log_Result( EEXIST );
524 std::string src( srcpath.asString());
526 const char *const argv[] = {
531 destpath.asString().c_str(),
534 ExternalProgram prog( argv, ExternalProgram::Stderr_To_Stdout );
535 for ( string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() ) {
536 MIL << " " << output;
538 int ret = prog.close();
539 return _Log_Result( ret, "returned" );
542 ///////////////////////////////////////////////////////////////////////
544 ///////////////////////////////////////////////////////////////////////
546 const StrMatcher & matchNoDots()
548 static StrMatcher noDots( "[^.]*", Match::GLOB );
552 int dirForEach( const Pathname & dir_r, function<bool(const Pathname &, const char *const)> fnc_r )
557 AutoDispose<DIR *> dir( ::opendir( dir_r.c_str() ),
558 []( DIR * dir_r ) { if ( dir_r ) ::closedir( dir_r ); } );
560 MIL << "readdir " << dir_r << ' ';
562 return _Log_Result( errno );
563 MIL << endl; // close line before callbacks are invoked.
566 for ( struct dirent * entry = ::readdir( dir ); entry; entry = ::readdir( dir ) )
568 if ( entry->d_name[0] == '.' && ( entry->d_name[1] == '\0' || ( entry->d_name[1] == '.' && entry->d_name[2] == '\0' ) ) )
569 continue; // omitt . and ..
571 if ( ! fnc_r( dir_r, entry->d_name ) )
580 int dirForEach( const Pathname & dir_r, const StrMatcher & matcher_r, function<bool( const Pathname &, const char *const)> fnc_r )
585 bool nodots = ( &matcher_r == &matchNoDots() );
586 return dirForEach( dir_r,
587 [&]( const Pathname & dir_r, const char *const name_r )->bool
589 if ( ( nodots && name_r[0] == '.' ) || ! matcher_r( name_r ) )
591 return fnc_r( dir_r, name_r );
595 ///////////////////////////////////////////////////////////////////
597 ///////////////////////////////////////////////////////////////////
599 int readdir( std::list<std::string> & retlist_r, const Pathname & path_r, bool dots_r )
602 return dirForEach( path_r,
603 [&]( const Pathname & dir_r, const char *const name_r )->bool
605 if ( dots_r || name_r[0] != '.' )
606 retlist_r.push_back( name_r );
612 int readdir( std::list<Pathname> & retlist_r, const Pathname & path_r, bool dots_r )
615 return dirForEach( path_r,
616 [&]( const Pathname & dir_r, const char *const name_r )->bool
618 if ( dots_r || name_r[0] != '.' )
619 retlist_r.push_back( dir_r/name_r );
624 bool DirEntry::operator==( const DirEntry &rhs ) const
626 // if one of the types is not known, use the name only
627 if ( type == FT_NOT_AVAIL || rhs.type == FT_NOT_AVAIL )
628 return ( name == rhs.name );
629 return ((name == rhs.name ) && (type == rhs.type));
632 int readdir( DirContent & retlist_r, const Pathname & path_r, bool dots_r, PathInfo::Mode statmode_r )
635 return dirForEach( path_r,
636 [&]( const Pathname & dir_r, const char *const name_r )->bool
638 if ( dots_r || name_r[0] != '.' )
639 retlist_r.push_back( DirEntry( name_r, PathInfo( dir_r/name_r, statmode_r ).fileType() ) );
644 std::ostream & operator<<( std::ostream & str, const DirContent & obj )
645 { return dumpRange( str, obj.begin(), obj.end() ); }
647 ///////////////////////////////////////////////////////////////////
649 ///////////////////////////////////////////////////////////////////
651 int is_empty_dir( const Pathname & path_r )
653 return dirForEach( path_r,
654 [&]( const Pathname & dir_r, const char *const name_r )->bool
658 ///////////////////////////////////////////////////////////////////
660 // METHOD NAME : unlink
663 int unlink( const Pathname & path )
665 MIL << "unlink " << path;
666 if ( ::unlink( path.asString().c_str() ) == -1 ) {
667 return _Log_Result( errno );
669 return _Log_Result( 0 );
672 ///////////////////////////////////////////////////////////////////
674 // METHOD NAME : rename
677 int rename( const Pathname & oldpath, const Pathname & newpath )
679 MIL << "rename " << oldpath << " -> " << newpath;
680 if ( ::rename( oldpath.asString().c_str(), newpath.asString().c_str() ) == -1 ) {
681 return _Log_Result( errno );
683 return _Log_Result( 0 );
686 ///////////////////////////////////////////////////////////////////
688 // METHOD NAME : exchange
691 int exchange( const Pathname & lpath, const Pathname & rpath )
693 MIL << "exchange " << lpath << " <-> " << rpath;
694 if ( lpath.empty() || rpath.empty() )
695 return _Log_Result( EINVAL );
697 PathInfo linfo( lpath );
698 PathInfo rinfo( rpath );
700 if ( ! linfo.isExist() )
702 if ( ! rinfo.isExist() )
703 return _Log_Result( 0 ); // both don't exist.
705 // just rename rpath -> lpath
706 int ret = assert_dir( lpath.dirname() );
708 return _Log_Result( ret );
709 if ( ::rename( rpath.c_str(), lpath.c_str() ) == -1 ) {
710 return _Log_Result( errno );
712 return _Log_Result( 0 );
715 // HERE: lpath exists:
716 if ( ! rinfo.isExist() )
718 // just rename lpath -> rpath
719 int ret = assert_dir( rpath.dirname() );
721 return _Log_Result( ret );
722 if ( ::rename( lpath.c_str(), rpath.c_str() ) == -1 ) {
723 return _Log_Result( errno );
725 return _Log_Result( 0 );
729 TmpFile tmpfile( TmpFile::makeSibling( rpath ) );
731 return _Log_Result( errno );
732 Pathname tmp( tmpfile.path() );
733 ::unlink( tmp.c_str() );
735 if ( ::rename( lpath.c_str(), tmp.c_str() ) == -1 ) {
736 return _Log_Result( errno );
738 if ( ::rename( rpath.c_str(), lpath.c_str() ) == -1 ) {
739 ::rename( tmp.c_str(), lpath.c_str() );
740 return _Log_Result( errno );
742 if ( ::rename( tmp.c_str(), rpath.c_str() ) == -1 ) {
743 ::rename( lpath.c_str(), rpath.c_str() );
744 ::rename( tmp.c_str(), lpath.c_str() );
745 return _Log_Result( errno );
747 return _Log_Result( 0 );
750 ///////////////////////////////////////////////////////////////////
752 // METHOD NAME : copy
755 int copy( const Pathname & file, const Pathname & dest )
757 MIL << "copy " << file << " -> " << dest << ' ';
760 if ( !sp.isFile() ) {
761 return _Log_Result( EINVAL );
766 return _Log_Result( EISDIR );
769 const char *const argv[] = {
771 "--remove-destination",
773 file.asString().c_str(),
774 dest.asString().c_str(),
777 ExternalProgram prog( argv, ExternalProgram::Stderr_To_Stdout );
778 for ( string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() ) {
779 MIL << " " << output;
781 int ret = prog.close();
782 return _Log_Result( ret, "returned" );
785 ///////////////////////////////////////////////////////////////////
787 // METHOD NAME : symlink
790 int symlink( const Pathname & oldpath, const Pathname & newpath )
792 MIL << "symlink " << newpath << " -> " << oldpath;
793 if ( ::symlink( oldpath.asString().c_str(), newpath.asString().c_str() ) == -1 ) {
794 return _Log_Result( errno );
796 return _Log_Result( 0 );
799 ///////////////////////////////////////////////////////////////////
801 // METHOD NAME : hardlink
804 int hardlink( const Pathname & oldpath, const Pathname & newpath )
806 MIL << "hardlink " << newpath << " -> " << oldpath;
807 if ( ::link( oldpath.asString().c_str(), newpath.asString().c_str() ) == -1 ) {
808 return _Log_Result( errno );
810 return _Log_Result( 0 );
813 ///////////////////////////////////////////////////////////////////
815 // METHOD NAME : hardlink
818 int hardlinkCopy( const Pathname & oldpath, const Pathname & newpath )
820 MIL << "hardlinkCopy " << oldpath << " -> " << newpath;
822 PathInfo pi( oldpath, PathInfo::LSTAT );
825 // dont hardlink symlinks!
826 return copy( oldpath, newpath );
832 int res = unlink( newpath );
834 return _Log_Result( res );
837 // Here: no symlink, no newpath
838 if ( ::link( oldpath.asString().c_str(), newpath.asString().c_str() ) == -1 )
842 case EPERM: // /proc/sys/fs/protected_hardlink in proc(5)
843 case EXDEV: // oldpath and newpath are not on the same mounted file system
844 return copy( oldpath, newpath );
847 return _Log_Result( errno );
849 return _Log_Result( 0 );
852 ///////////////////////////////////////////////////////////////////
854 // METHOD NAME : readlink
857 int readlink( const Pathname & symlink_r, Pathname & target_r )
859 static const ssize_t bufsiz = 2047;
860 static char buf[bufsiz+1];
861 ssize_t ret = ::readlink( symlink_r.c_str(), buf, bufsiz );
864 target_r = Pathname();
865 MIL << "readlink " << symlink_r;
866 return _Log_Result( errno );
873 ///////////////////////////////////////////////////////////////////
875 // METHOD NAME : expandlink
876 // METHOD TYPE : Pathname
878 Pathname expandlink( const Pathname & path_r )
880 static const unsigned int level_limit = 256;
881 static unsigned int count;
882 Pathname path(path_r);
883 PathInfo info(path_r, PathInfo::LSTAT);
885 for (count = level_limit; info.isLink() && count; count--)
887 DBG << "following symlink " << path;
888 path = path.dirname() / readlink(path);
889 DBG << "->" << path << std::endl;
890 info = PathInfo(path, PathInfo::LSTAT);
893 // expand limit reached
896 ERR << "Expand level limit reached. Probably a cyclic symbolic link." << endl;
900 else if (count < level_limit)
902 // check for a broken link
903 if (PathInfo(path).isExist())
905 // broken link, return an empty path
908 ERR << path << " is broken (expanded from " << path_r << ")" << endl;
913 // not a symlink, return the original pathname
914 DBG << "not a symlink" << endl;
918 ///////////////////////////////////////////////////////////////////
920 // METHOD NAME : copy_file2dir
923 int copy_file2dir( const Pathname & file, const Pathname & dest )
925 MIL << "copy_file2dir " << file << " -> " << dest << ' ';
928 if ( !sp.isFile() ) {
929 return _Log_Result( EINVAL );
934 return _Log_Result( ENOTDIR );
937 const char *const argv[] = {
940 file.asString().c_str(),
941 dest.asString().c_str(),
944 ExternalProgram prog( argv, ExternalProgram::Stderr_To_Stdout );
945 for ( string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() ) {
946 MIL << " " << output;
948 int ret = prog.close();
949 return _Log_Result( ret, "returned" );
952 ///////////////////////////////////////////////////////////////////
954 // METHOD NAME : md5sum
955 // METHOD TYPE : std::string
957 std::string md5sum( const Pathname & file )
959 if ( ! PathInfo( file ).isFile() ) {
962 std::ifstream istr( file.asString().c_str() );
966 return Digest::digest( "MD5", istr );
969 ///////////////////////////////////////////////////////////////////
971 // METHOD NAME : sha1sum
972 // METHOD TYPE : std::string
974 std::string sha1sum( const Pathname & file )
976 return checksum(file, "SHA1");
979 ///////////////////////////////////////////////////////////////////
981 // METHOD NAME : checksum
982 // METHOD TYPE : std::string
984 std::string checksum( const Pathname & file, const std::string &algorithm )
986 if ( ! PathInfo( file ).isFile() ) {
989 std::ifstream istr( file.asString().c_str() );
993 return Digest::digest( algorithm, istr );
996 bool is_checksum( const Pathname & file, const CheckSum &checksum )
998 return ( filesystem::checksum(file, checksum.type()) == checksum.checksum() );
1001 ///////////////////////////////////////////////////////////////////
1003 // METHOD NAME : erase
1004 // METHOD TYPE : int
1006 int erase( const Pathname & path )
1009 PathInfo p( path, PathInfo::LSTAT );
1013 res = recursive_rmdir( path );
1015 res = unlink( path );
1020 ///////////////////////////////////////////////////////////////////
1022 // METHOD NAME : chmod
1023 // METHOD TYPE : int
1025 int chmod( const Pathname & path, mode_t mode )
1027 MIL << "chmod " << path << ' ' << str::octstring( mode );
1028 if ( ::chmod( path.asString().c_str(), mode ) == -1 ) {
1029 return _Log_Result( errno );
1031 return _Log_Result( 0 );
1034 int addmod( const Pathname & path, mode_t mode )
1036 mode_t omode( PathInfo( path ).st_mode() );
1037 mode_t tmode( omode | mode );
1038 if ( omode != mode )
1039 return chmod( path, tmode );
1043 int delmod( const Pathname & path, mode_t mode )
1045 mode_t omode( PathInfo( path ).st_mode() );
1046 mode_t tmode( omode & ~mode );
1047 if ( omode != mode )
1048 return chmod( path, tmode );
1052 //////////////////////////////////////////////////////////////////
1054 // METHOD NAME : zipType
1055 // METHOD TYPE : ZIP_TYPE
1057 ZIP_TYPE zipType( const Pathname & file )
1059 ZIP_TYPE ret = ZT_NONE;
1061 int fd = open( file.asString().c_str(), O_RDONLY|O_CLOEXEC );
1064 const int magicSize = 3;
1065 unsigned char magic[magicSize];
1066 memset( magic, 0, magicSize );
1067 if ( read( fd, magic, magicSize ) == magicSize ) {
1068 if ( magic[0] == 0037 && magic[1] == 0213 ) {
1070 } else if ( magic[0] == 'B' && magic[1] == 'Z' && magic[2] == 'h' ) {
1080 ///////////////////////////////////////////////////////////////////
1083 // METHOD TYPE : ByteCount
1085 ByteCount df( const Pathname & path_r )
1087 ByteCount ret( -1 );
1089 if ( statvfs( path_r.c_str(), &sb ) == 0 )
1091 ret = sb.f_bfree * sb.f_bsize;
1096 ///////////////////////////////////////////////////////////////////
1098 // METHOD NAME : getUmask
1099 // METHOD TYPE : mode_t
1103 mode_t mask = ::umask( 0022 );
1108 ///////////////////////////////////////////////////////////////////
1110 // METHOD NAME : getUmask
1111 // METHOD TYPE : mode_t
1113 int assert_file( const Pathname & path, unsigned mode )
1115 int ret = assert_dir( path.dirname() );
1116 MIL << "assert_file " << str::octstring( mode ) << " " << path;
1118 return _Log_Result( ret );
1120 PathInfo pi( path );
1122 return _Log_Result( pi.isFile() ? 0 : EEXIST );
1124 int fd = ::creat( path.c_str(), mode );
1126 return _Log_Result( errno );
1129 return _Log_Result( 0 );
1132 ///////////////////////////////////////////////////////////////////
1134 // METHOD NAME : touch
1135 // METHOD TYPE : int
1137 int touch (const Pathname & path)
1139 MIL << "touch " << path;
1140 struct ::utimbuf times;
1141 times.actime = ::time( 0 );
1142 times.modtime = ::time( 0 );
1143 if ( ::utime( path.asString().c_str(), × ) == -1 ) {
1144 return _Log_Result( errno );
1146 return _Log_Result( 0 );
1149 /////////////////////////////////////////////////////////////////
1150 } // namespace filesystem
1151 ///////////////////////////////////////////////////////////////////
1152 /////////////////////////////////////////////////////////////////
1154 ///////////////////////////////////////////////////////////////////