2016-10-19 21:28:06 +08:00
|
|
|
/*
|
|
|
|
ISC License
|
|
|
|
|
|
|
|
Copyright (c) 2016, Antonio SJ Musumeci <trapexit@spawn.link>
|
|
|
|
|
|
|
|
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
|
|
|
|
2017-02-15 22:08:27 +08:00
|
|
|
#include "fs_base_stat.hpp"
|
|
|
|
|
2019-01-25 20:03:46 +08:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2017-02-15 22:08:27 +08:00
|
|
|
#define MODE_BITS (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)
|
|
|
|
|
2016-10-19 21:28:06 +08:00
|
|
|
namespace fs
|
|
|
|
{
|
|
|
|
static
|
|
|
|
inline
|
|
|
|
int
|
2019-01-25 20:03:46 +08:00
|
|
|
chmod(const std::string &path_,
|
|
|
|
const mode_t mode_)
|
2016-10-19 21:28:06 +08:00
|
|
|
{
|
2019-01-25 20:03:46 +08:00
|
|
|
return ::chmod(path_.c_str(),mode_);
|
2016-10-19 21:28:06 +08:00
|
|
|
}
|
|
|
|
|
2017-02-15 22:08:27 +08:00
|
|
|
static
|
|
|
|
inline
|
|
|
|
int
|
2019-01-25 20:03:46 +08:00
|
|
|
chmod_check_on_error(const std::string &path_,
|
|
|
|
const mode_t mode_)
|
2017-02-15 22:08:27 +08:00
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
|
2019-01-25 20:03:46 +08:00
|
|
|
rv = fs::chmod(path_,mode_);
|
2017-02-15 22:08:27 +08:00
|
|
|
if(rv == -1)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
error = errno;
|
2019-01-25 20:03:46 +08:00
|
|
|
rv = fs::stat(path_,&st);
|
2017-02-15 22:08:27 +08:00
|
|
|
if(rv == -1)
|
|
|
|
return -1;
|
|
|
|
|
2019-01-25 20:03:46 +08:00
|
|
|
if((st.st_mode & MODE_BITS) != (mode_ & MODE_BITS))
|
2017-02-15 22:08:27 +08:00
|
|
|
return (errno=error,-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2016-10-19 21:28:06 +08:00
|
|
|
}
|