From ea50981352bb5c7ab48663edaeb2df1ddd66953e Mon Sep 17 00:00:00 2001 From: Michael Andres <ma@suse.de> Date: Tue, 10 Dec 2019 14:02:18 +0100 Subject: [PATCH] PathInfo: new assert_file_mode(), ::chmod even if the file already exists --- zypp/PathInfo.cc | 27 +++++++++++++++++++++++++++ zypp/PathInfo.h | 4 ++++ 2 files changed, 31 insertions(+) diff --git a/zypp/PathInfo.cc b/zypp/PathInfo.cc index cf1efa723..add273ea0 100644 --- a/zypp/PathInfo.cc +++ b/zypp/PathInfo.cc @@ -1149,6 +1149,33 @@ namespace zypp return logResult( 0 ); } + int assert_file_mode( const Pathname & path, unsigned mode ) + { + int ret = assert_dir( path.dirname() ); + MIL << "assert_file_mode " << str::octstring( mode ) << " " << path; + if ( ret != 0 ) + return logResult( ret ); + + PathInfo pi( path ); + if ( pi.isExist() ) + { + if ( ! pi.isFile() ) + return logResult( EEXIST ); + + mode = applyUmaskTo( mode ); + if ( pi.st_mode() != mode ) + return chmod( path, mode ); + + return logResult( 0 ); + } + + int fd = ::creat( path.c_str(), mode ); + if ( fd == -1 ) + return logResult( errno ); + ::close( fd ); + return logResult( 0 ); + } + /////////////////////////////////////////////////////////////////// // // METHOD NAME : touch diff --git a/zypp/PathInfo.h b/zypp/PathInfo.h index 74baf030e..3324897c5 100644 --- a/zypp/PathInfo.h +++ b/zypp/PathInfo.h @@ -581,6 +581,10 @@ namespace zypp * @return 0 on success, errno on failure **/ int assert_file( const Pathname & path, unsigned mode = 0644 ); + /** + * Like \ref assert_file but enforce \a mode even if the file already exists. + */ + int assert_file_mode( const Pathname & path, unsigned mode = 0644 ); /** * Change file's modification and access times.