2016-10-19 21:28:06 +08:00
|
|
|
/*
|
|
|
|
ISC License
|
|
|
|
|
2020-08-18 04:29:22 +08:00
|
|
|
Copyright (c) 2020, Antonio SJ Musumeci <trapexit@spawn.link>
|
2016-10-19 21:28:06 +08:00
|
|
|
|
|
|
|
Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
purpose with or without fee is hereby granted, provided that the above
|
|
|
|
copyright notice and this permission notice appear in all copies.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2017-06-30 23:15:20 +08:00
|
|
|
#pragma once
|
2016-12-23 11:46:20 +08:00
|
|
|
|
2020-08-18 04:29:22 +08:00
|
|
|
#include "fs_lstat.hpp"
|
2019-01-25 20:03:46 +08:00
|
|
|
|
2016-10-19 21:28:06 +08:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2020-12-20 04:06:08 +08:00
|
|
|
|
2016-10-19 21:28:06 +08:00
|
|
|
namespace fs
|
|
|
|
{
|
|
|
|
static
|
|
|
|
inline
|
|
|
|
int
|
2020-08-18 04:29:22 +08:00
|
|
|
lchown(const char *pathname_,
|
|
|
|
const uid_t uid_,
|
|
|
|
const gid_t gid_)
|
2016-10-19 21:28:06 +08:00
|
|
|
{
|
2020-08-18 04:29:22 +08:00
|
|
|
return ::lchown(pathname_,uid_,gid_);
|
2016-10-19 21:28:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
inline
|
|
|
|
int
|
2020-08-18 04:29:22 +08:00
|
|
|
lchown(const std::string &pathname_,
|
|
|
|
const uid_t uid_,
|
|
|
|
const gid_t gid_)
|
2016-10-19 21:28:06 +08:00
|
|
|
{
|
2020-08-18 04:29:22 +08:00
|
|
|
return fs::lchown(pathname_.c_str(),uid_,gid_);
|
2016-10-19 21:28:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
inline
|
|
|
|
int
|
2020-08-18 04:29:22 +08:00
|
|
|
lchown(const char *pathname_,
|
|
|
|
const struct stat &st_)
|
2016-10-19 21:28:06 +08:00
|
|
|
{
|
2020-08-18 04:29:22 +08:00
|
|
|
return fs::lchown(pathname_,st_.st_uid,st_.st_gid);
|
2016-10-19 21:28:06 +08:00
|
|
|
}
|
|
|
|
|
2017-02-15 22:08:27 +08:00
|
|
|
static
|
|
|
|
inline
|
|
|
|
int
|
2020-08-18 04:29:22 +08:00
|
|
|
lchown(const std::string &pathname_,
|
2019-01-25 20:03:46 +08:00
|
|
|
const struct stat &st_)
|
2017-02-15 22:08:27 +08:00
|
|
|
{
|
2020-08-18 04:29:22 +08:00
|
|
|
return fs::lchown(pathname_.c_str(),st_);
|
2017-02-15 22:08:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
inline
|
|
|
|
int
|
2019-01-25 20:03:46 +08:00
|
|
|
lchown_check_on_error(const std::string &path_,
|
|
|
|
const struct stat &st_)
|
2017-02-15 22:08:27 +08:00
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
|
2019-01-25 20:03:46 +08:00
|
|
|
rv = fs::lchown(path_,st_);
|
2017-02-15 22:08:27 +08:00
|
|
|
if(rv == -1)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
struct stat tmpst;
|
|
|
|
|
|
|
|
error = errno;
|
2019-01-25 20:03:46 +08:00
|
|
|
rv = fs::lstat(path_,&tmpst);
|
2017-02-15 22:08:27 +08:00
|
|
|
if(rv == -1)
|
|
|
|
return -1;
|
|
|
|
|
2019-01-25 20:03:46 +08:00
|
|
|
if((st_.st_uid != tmpst.st_uid) ||
|
|
|
|
(st_.st_gid != tmpst.st_gid))
|
2017-02-15 22:08:27 +08:00
|
|
|
return (errno=error,-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2016-10-19 21:28:06 +08:00
|
|
|
}
|