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/Logger.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::major
240 ** FUNCTION TYPE : unsigned int
242 unsigned int PathInfo::major() const
244 return isBlk() || isChr() ? ::major(statbuf_C.st_rdev) : 0;
247 /******************************************************************
249 ** FUNCTION NAME : PathInfo::minor
250 ** FUNCTION TYPE : unsigned int
252 unsigned int PathInfo::minor() 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 ///////////////////////////////////////////////////////////////////
646 ///////////////////////////////////////////////////////////////////
648 int is_empty_dir( const Pathname & path_r )
650 return dirForEach( path_r,
651 [&]( const Pathname & dir_r, const char *const name_r )->bool
655 ///////////////////////////////////////////////////////////////////
657 // METHOD NAME : unlink
660 int unlink( const Pathname & path )
662 MIL << "unlink " << path;
663 if ( ::unlink( path.asString().c_str() ) == -1 ) {
664 return _Log_Result( errno );
666 return _Log_Result( 0 );
669 ///////////////////////////////////////////////////////////////////
671 // METHOD NAME : rename
674 int rename( const Pathname & oldpath, const Pathname & newpath )
676 MIL << "rename " << oldpath << " -> " << newpath;
677 if ( ::rename( oldpath.asString().c_str(), newpath.asString().c_str() ) == -1 ) {
678 return _Log_Result( errno );
680 return _Log_Result( 0 );
683 ///////////////////////////////////////////////////////////////////
685 // METHOD NAME : exchange
688 int exchange( const Pathname & lpath, const Pathname & rpath )
690 MIL << "exchange " << lpath << " <-> " << rpath;
691 if ( lpath.empty() || rpath.empty() )
692 return _Log_Result( EINVAL );
694 PathInfo linfo( lpath );
695 PathInfo rinfo( rpath );
697 if ( ! linfo.isExist() )
699 if ( ! rinfo.isExist() )
700 return _Log_Result( 0 ); // both don't exist.
702 // just rename rpath -> lpath
703 int ret = assert_dir( lpath.dirname() );
705 return _Log_Result( ret );
706 if ( ::rename( rpath.c_str(), lpath.c_str() ) == -1 ) {
707 return _Log_Result( errno );
709 return _Log_Result( 0 );
712 // HERE: lpath exists:
713 if ( ! rinfo.isExist() )
715 // just rename lpath -> rpath
716 int ret = assert_dir( rpath.dirname() );
718 return _Log_Result( ret );
719 if ( ::rename( lpath.c_str(), rpath.c_str() ) == -1 ) {
720 return _Log_Result( errno );
722 return _Log_Result( 0 );
726 TmpFile tmpfile( TmpFile::makeSibling( rpath ) );
728 return _Log_Result( errno );
729 Pathname tmp( tmpfile.path() );
730 ::unlink( tmp.c_str() );
732 if ( ::rename( lpath.c_str(), tmp.c_str() ) == -1 ) {
733 return _Log_Result( errno );
735 if ( ::rename( rpath.c_str(), lpath.c_str() ) == -1 ) {
736 ::rename( tmp.c_str(), lpath.c_str() );
737 return _Log_Result( errno );
739 if ( ::rename( tmp.c_str(), rpath.c_str() ) == -1 ) {
740 ::rename( lpath.c_str(), rpath.c_str() );
741 ::rename( tmp.c_str(), lpath.c_str() );
742 return _Log_Result( errno );
744 return _Log_Result( 0 );
747 ///////////////////////////////////////////////////////////////////
749 // METHOD NAME : copy
752 int copy( const Pathname & file, const Pathname & dest )
754 MIL << "copy " << file << " -> " << dest << ' ';
757 if ( !sp.isFile() ) {
758 return _Log_Result( EINVAL );
763 return _Log_Result( EISDIR );
766 const char *const argv[] = {
768 "--remove-destination",
770 file.asString().c_str(),
771 dest.asString().c_str(),
774 ExternalProgram prog( argv, ExternalProgram::Stderr_To_Stdout );
775 for ( string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() ) {
776 MIL << " " << output;
778 int ret = prog.close();
779 return _Log_Result( ret, "returned" );
782 ///////////////////////////////////////////////////////////////////
784 // METHOD NAME : symlink
787 int symlink( const Pathname & oldpath, const Pathname & newpath )
789 MIL << "symlink " << newpath << " -> " << oldpath;
790 if ( ::symlink( oldpath.asString().c_str(), newpath.asString().c_str() ) == -1 ) {
791 return _Log_Result( errno );
793 return _Log_Result( 0 );
796 ///////////////////////////////////////////////////////////////////
798 // METHOD NAME : hardlink
801 int hardlink( const Pathname & oldpath, const Pathname & newpath )
803 MIL << "hardlink " << newpath << " -> " << oldpath;
804 if ( ::link( oldpath.asString().c_str(), newpath.asString().c_str() ) == -1 ) {
805 return _Log_Result( errno );
807 return _Log_Result( 0 );
810 ///////////////////////////////////////////////////////////////////
812 // METHOD NAME : hardlink
815 int hardlinkCopy( const Pathname & oldpath, const Pathname & newpath )
817 MIL << "hardlinkCopy " << oldpath << " -> " << newpath;
819 PathInfo pi( oldpath, PathInfo::LSTAT );
822 // dont hardlink symlinks!
823 return copy( oldpath, newpath );
829 int res = unlink( newpath );
831 return _Log_Result( res );
834 // Here: no symlink, no newpath
835 if ( ::link( oldpath.asString().c_str(), newpath.asString().c_str() ) == -1 )
839 case EXDEV: // oldpath and newpath are not on the same mounted file system
840 return copy( oldpath, newpath );
843 return _Log_Result( errno );
845 return _Log_Result( 0 );
848 ///////////////////////////////////////////////////////////////////
850 // METHOD NAME : readlink
853 int readlink( const Pathname & symlink_r, Pathname & target_r )
855 static const ssize_t bufsiz = 2047;
856 static char buf[bufsiz+1];
857 ssize_t ret = ::readlink( symlink_r.c_str(), buf, bufsiz );
860 target_r = Pathname();
861 MIL << "readlink " << symlink_r;
862 return _Log_Result( errno );
869 ///////////////////////////////////////////////////////////////////
871 // METHOD NAME : expandlink
872 // METHOD TYPE : Pathname
874 Pathname expandlink( const Pathname & path_r )
876 static const unsigned int level_limit = 256;
877 static unsigned int count;
878 Pathname path(path_r);
879 PathInfo info(path_r, PathInfo::LSTAT);
881 for (count = level_limit; info.isLink() && count; count--)
883 DBG << "following symlink " << path;
884 path = path.dirname() / readlink(path);
885 DBG << "->" << path << std::endl;
886 info = PathInfo(path, PathInfo::LSTAT);
889 // expand limit reached
892 ERR << "Expand level limit reached. Probably a cyclic symbolic link." << endl;
896 else if (count < level_limit)
898 // check for a broken link
899 if (PathInfo(path).isExist())
901 // broken link, return an empty path
904 ERR << path << " is broken (expanded from " << path_r << ")" << endl;
909 // not a symlink, return the original pathname
910 DBG << "not a symlink" << endl;
914 ///////////////////////////////////////////////////////////////////
916 // METHOD NAME : copy_file2dir
919 int copy_file2dir( const Pathname & file, const Pathname & dest )
921 MIL << "copy_file2dir " << file << " -> " << dest << ' ';
924 if ( !sp.isFile() ) {
925 return _Log_Result( EINVAL );
930 return _Log_Result( ENOTDIR );
933 const char *const argv[] = {
936 file.asString().c_str(),
937 dest.asString().c_str(),
940 ExternalProgram prog( argv, ExternalProgram::Stderr_To_Stdout );
941 for ( string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() ) {
942 MIL << " " << output;
944 int ret = prog.close();
945 return _Log_Result( ret, "returned" );
948 ///////////////////////////////////////////////////////////////////
950 // METHOD NAME : md5sum
951 // METHOD TYPE : std::string
953 std::string md5sum( const Pathname & file )
955 if ( ! PathInfo( file ).isFile() ) {
958 std::ifstream istr( file.asString().c_str() );
962 return Digest::digest( "MD5", istr );
965 ///////////////////////////////////////////////////////////////////
967 // METHOD NAME : sha1sum
968 // METHOD TYPE : std::string
970 std::string sha1sum( const Pathname & file )
972 return checksum(file, "SHA1");
975 ///////////////////////////////////////////////////////////////////
977 // METHOD NAME : checksum
978 // METHOD TYPE : std::string
980 std::string checksum( const Pathname & file, const std::string &algorithm )
982 if ( ! PathInfo( file ).isFile() ) {
985 std::ifstream istr( file.asString().c_str() );
989 return Digest::digest( algorithm, istr );
992 bool is_checksum( const Pathname & file, const CheckSum &checksum )
994 return ( filesystem::checksum(file, checksum.type()) == checksum.checksum() );
997 ///////////////////////////////////////////////////////////////////
999 // METHOD NAME : erase
1000 // METHOD TYPE : int
1002 int erase( const Pathname & path )
1005 PathInfo p( path, PathInfo::LSTAT );
1009 res = recursive_rmdir( path );
1011 res = unlink( path );
1016 ///////////////////////////////////////////////////////////////////
1018 // METHOD NAME : chmod
1019 // METHOD TYPE : int
1021 int chmod( const Pathname & path, mode_t mode )
1023 MIL << "chmod " << path << ' ' << str::octstring( mode );
1024 if ( ::chmod( path.asString().c_str(), mode ) == -1 ) {
1025 return _Log_Result( errno );
1027 return _Log_Result( 0 );
1030 int addmod( const Pathname & path, mode_t mode )
1032 mode_t omode( PathInfo( path ).st_mode() );
1033 mode_t tmode( omode | mode );
1034 if ( omode != mode )
1035 return chmod( path, tmode );
1039 int delmod( const Pathname & path, mode_t mode )
1041 mode_t omode( PathInfo( path ).st_mode() );
1042 mode_t tmode( omode & ~mode );
1043 if ( omode != mode )
1044 return chmod( path, tmode );
1048 //////////////////////////////////////////////////////////////////
1050 // METHOD NAME : zipType
1051 // METHOD TYPE : ZIP_TYPE
1053 ZIP_TYPE zipType( const Pathname & file )
1055 ZIP_TYPE ret = ZT_NONE;
1057 int fd = open( file.asString().c_str(), O_RDONLY|O_CLOEXEC );
1060 const int magicSize = 3;
1061 unsigned char magic[magicSize];
1062 memset( magic, 0, magicSize );
1063 if ( read( fd, magic, magicSize ) == magicSize ) {
1064 if ( magic[0] == 0037 && magic[1] == 0213 ) {
1066 } else if ( magic[0] == 'B' && magic[1] == 'Z' && magic[2] == 'h' ) {
1076 ///////////////////////////////////////////////////////////////////
1079 // METHOD TYPE : ByteCount
1081 ByteCount df( const Pathname & path_r )
1083 ByteCount ret( -1 );
1085 if ( statvfs( path_r.c_str(), &sb ) == 0 )
1087 ret = sb.f_bfree * sb.f_bsize;
1092 ///////////////////////////////////////////////////////////////////
1094 // METHOD NAME : getUmask
1095 // METHOD TYPE : mode_t
1099 mode_t mask = ::umask( 0022 );
1104 ///////////////////////////////////////////////////////////////////
1106 // METHOD NAME : getUmask
1107 // METHOD TYPE : mode_t
1109 int assert_file( const Pathname & path, unsigned mode )
1111 int ret = assert_dir( path.dirname() );
1112 MIL << "assert_file " << str::octstring( mode ) << " " << path;
1114 return _Log_Result( ret );
1116 PathInfo pi( path );
1118 return _Log_Result( pi.isFile() ? 0 : EEXIST );
1120 int fd = ::creat( path.c_str(), mode );
1122 return _Log_Result( errno );
1125 return _Log_Result( 0 );
1128 ///////////////////////////////////////////////////////////////////
1130 // METHOD NAME : touch
1131 // METHOD TYPE : int
1133 int touch (const Pathname & path)
1135 MIL << "touch " << path;
1136 struct ::utimbuf times;
1137 times.actime = ::time( 0 );
1138 times.modtime = ::time( 0 );
1139 if ( ::utime( path.asString().c_str(), × ) == -1 ) {
1140 return _Log_Result( errno );
1142 return _Log_Result( 0 );
1145 /////////////////////////////////////////////////////////////////
1146 } // namespace filesystem
1147 ///////////////////////////////////////////////////////////////////
1148 /////////////////////////////////////////////////////////////////
1150 ///////////////////////////////////////////////////////////////////