Move everything to negative errno return types (#1499)
Some checks failed
CodeQL / Analyze (push) Has been cancelled
mkdocs / deploy (push) Has been cancelled

This commit is contained in:
trapexit 2025-07-25 17:53:48 -05:00 • committed by GitHub
parent 2b500f194a
commit e8cd74cab0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
177 changed files with 5952 additions and 5843 deletions

View file

@ -14,6 +14,8 @@
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "fs_clonepath.hpp"
#include "errno.h"
#include "fs_attr.hpp"
#include "fs_clonepath.hpp"
@ -30,131 +32,124 @@
using std::string;
namespace l
static
bool
_ignorable_error(const int err_)
{
static
bool
ignorable_error(const int err_)
{
switch(err_)
{
case ENOTTY:
case ENOTSUP:
#if ENOTSUP != EOPNOTSUPP
case EOPNOTSUPP:
#endif
return true;
}
return false;
}
}
namespace fs
{
/*
Attempts to clone a path.
The directories which already exist are left alone.
The new directories have metadata set to match the original if
possible. Optionally ignore errors on metadata copies.
*/
int
clonepath(const string &fromsrc_,
const string &tosrc_,
const char *relative_,
const bool return_metadata_errors_)
{
int rv;
struct stat st;
string topath;
string frompath;
string dirname;
if((relative_ == NULL) || (relative_[0] == '\0'))
return 0;
dirname = fs::path::dirname(relative_);
if(dirname != "/")
{
rv = fs::clonepath(fromsrc_,tosrc_,dirname,return_metadata_errors_);
if(rv == -1)
return -1;
}
frompath = fs::path::make(fromsrc_,relative_);
rv = fs::lstat(frompath,&st);
if(rv == -1)
return -1;
else if(!S_ISDIR(st.st_mode))
return (errno=ENOTDIR,-1);
topath = fs::path::make(tosrc_,relative_);
rv = fs::mkdir(topath,st.st_mode);
if(rv == -1)
{
if(errno == EEXIST)
return 0;
else
return -1;
}
// it may not support it... it's fine...
rv = fs::attr::copy(frompath,topath);
if(return_metadata_errors_ && (rv < 0) && !l::ignorable_error(-rv))
return -1;
// it may not support it... it's fine...
rv = fs::xattr::copy(frompath,topath);
if(return_metadata_errors_ && (rv == -1) && !l::ignorable_error(errno))
return -1;
rv = fs::lchown_check_on_error(topath,st);
if(return_metadata_errors_ && (rv == -1))
return -1;
rv = fs::lutimens(topath,st);
if(return_metadata_errors_ && (rv == -1))
return -1;
return 0;
}
int
clonepath(const string &from_,
const string &to_,
const string &relative_,
const bool return_metadata_errors_)
{
return fs::clonepath(from_,
to_,
relative_.c_str(),
return_metadata_errors_);
}
int
clonepath_as_root(const string &from_,
const string &to_,
const char *relative_,
const bool return_metadata_errors_)
{
if((relative_ == NULL) || (relative_[0] == '\0'))
return 0;
if(from_ == to_)
return 0;
switch(err_)
{
const ugid::SetRootGuard ugidGuard;
return fs::clonepath(from_,to_,relative_,return_metadata_errors_);
case ENOTTY:
case ENOTSUP:
#if ENOTSUP != EOPNOTSUPP
case EOPNOTSUPP:
#endif
return true;
}
}
int
clonepath_as_root(const string &from_,
const string &to_,
const string &relative_,
const bool return_metadata_errors_)
{
return fs::clonepath_as_root(from_,to_,relative_.c_str(),return_metadata_errors_);
}
return false;
}
/*
Attempts to clone a path.
The directories which already exist are left alone.
The new directories have metadata set to match the original if
possible. Optionally ignore errors on metadata copies.
*/
int
fs::clonepath(const string &srcpath_,
const string &dstpath_,
const char *relpath_,
const bool return_metadata_errors_)
{
int rv;
struct stat st;
string dstpath;
string srcpath;
string dirname;
if((relpath_ == NULL) || (relpath_[0] == '\0'))
return 0;
dirname = fs::path::dirname(relpath_);
if(dirname != "/")
{
rv = fs::clonepath(srcpath_,dstpath_,dirname,return_metadata_errors_);
if(rv < 0)
return rv;
}
srcpath = fs::path::make(srcpath_,relpath_);
rv = fs::lstat(srcpath,&st);
if(rv < 0)
return rv;
else if(!S_ISDIR(st.st_mode))
return -ENOTDIR;
dstpath = fs::path::make(dstpath_,relpath_);
rv = fs::mkdir(dstpath,st.st_mode);
if(rv < 0)
return ((rv == -EEXIST) ? 0 : rv);
// it may not support it... it's fine...
rv = fs::attr::copy(srcpath,dstpath);
if(return_metadata_errors_ && (rv < 0) && !::_ignorable_error(-rv))
return rv;
// it may not support it... it's fine...
rv = fs::xattr::copy(srcpath,dstpath);
if(return_metadata_errors_ && (rv < 0) && !::_ignorable_error(-rv))
return rv;
rv = fs::lchown_check_on_error(dstpath,st);
if(return_metadata_errors_ && (rv < 0))
return rv;
rv = fs::lutimens(dstpath,st);
if(return_metadata_errors_ && (rv < 0))
return rv;
return 0;
}
int
fs::clonepath(const string &srcpath_,
const string &dstpath_,
const string &relpath_,
const bool return_metadata_errors_)
{
return fs::clonepath(srcpath_,
dstpath_,
relpath_.c_str(),
return_metadata_errors_);
}
int
fs::clonepath_as_root(const string &srcpath_,
const string &dstpath_,
const char *relpath_,
const bool return_metadata_errors_)
{
if((relpath_ == NULL) || (relpath_[0] == '\0'))
return 0;
if(srcpath_ == dstpath_)
return 0;
const ugid::SetRootGuard ugid_guard;
return fs::clonepath(srcpath_,
dstpath_,
relpath_,
return_metadata_errors_);
}
int
fs::clonepath_as_root(const string &srcpath_,
const string &dstpath_,
const string &relpath_,
const bool return_metadata_errors_)
{
return fs::clonepath_as_root(srcpath_,
dstpath_,
relpath_.c_str(),
return_metadata_errors_);
}