2016-08-05 05:10:02 +08:00
|
|
|
/*
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2020-08-18 04:29:22 +08:00
|
|
|
#include "errno.hpp"
|
|
|
|
#include "fs_close.hpp"
|
|
|
|
#include "fs_open.hpp"
|
|
|
|
#include "fs_ioctl.hpp"
|
2016-08-05 05:10:02 +08:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
2020-08-18 04:29:22 +08:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <linux/fs.h>
|
2016-09-14 20:36:06 +08:00
|
|
|
|
2016-08-05 05:10:02 +08:00
|
|
|
using std::string;
|
|
|
|
|
2020-12-20 04:06:08 +08:00
|
|
|
|
2016-08-05 05:10:02 +08:00
|
|
|
namespace fs
|
|
|
|
{
|
|
|
|
namespace attr
|
|
|
|
{
|
|
|
|
static
|
|
|
|
int
|
|
|
|
get_fs_ioc_flags(const int fd,
|
|
|
|
int &flags)
|
|
|
|
{
|
2017-04-04 09:11:20 +08:00
|
|
|
int rv;
|
|
|
|
|
|
|
|
rv = fs::ioctl(fd,FS_IOC_GETFLAGS,(void*)&flags);
|
|
|
|
if((rv == -1) && (errno == EINVAL))
|
|
|
|
errno = ENOTSUP;
|
|
|
|
|
|
|
|
return rv;
|
2016-08-05 05:10:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
int
|
|
|
|
get_fs_ioc_flags(const string &file,
|
|
|
|
int &flags)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
int rv;
|
|
|
|
const int openflags = O_RDONLY|O_NONBLOCK;
|
|
|
|
|
2016-10-19 21:28:06 +08:00
|
|
|
fd = fs::open(file,openflags);
|
2016-08-05 05:10:02 +08:00
|
|
|
if(fd == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
rv = get_fs_ioc_flags(fd,flags);
|
|
|
|
if(rv == -1)
|
|
|
|
{
|
|
|
|
int error = errno;
|
2016-10-19 21:28:06 +08:00
|
|
|
fs::close(fd);
|
2016-08-05 05:10:02 +08:00
|
|
|
errno = error;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-10-19 21:28:06 +08:00
|
|
|
return fs::close(fd);
|
2016-08-05 05:10:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
int
|
|
|
|
set_fs_ioc_flags(const int fd,
|
|
|
|
const int flags)
|
|
|
|
{
|
2017-04-04 09:11:20 +08:00
|
|
|
int rv;
|
|
|
|
|
|
|
|
rv = fs::ioctl(fd,FS_IOC_SETFLAGS,(void*)&flags);
|
|
|
|
if((rv == -1) && (errno == EINVAL))
|
|
|
|
errno = ENOTSUP;
|
|
|
|
|
|
|
|
return rv;
|
2016-08-05 05:10:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
int
|
|
|
|
set_fs_ioc_flags(const string &file,
|
|
|
|
const int flags)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
int rv;
|
|
|
|
const int openflags = O_RDONLY|O_NONBLOCK;
|
|
|
|
|
2016-10-19 21:28:06 +08:00
|
|
|
fd = fs::open(file,openflags);
|
2016-08-05 05:10:02 +08:00
|
|
|
if(fd == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
rv = set_fs_ioc_flags(fd,flags);
|
|
|
|
if(rv == -1)
|
|
|
|
{
|
|
|
|
int error = errno;
|
2016-10-19 21:28:06 +08:00
|
|
|
fs::close(fd);
|
2016-08-05 05:10:02 +08:00
|
|
|
errno = error;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-10-19 21:28:06 +08:00
|
|
|
return fs::close(fd);
|
2016-08-05 05:10:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
copy(const int fdin,
|
|
|
|
const int fdout)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
int flags;
|
|
|
|
|
|
|
|
rv = get_fs_ioc_flags(fdin,flags);
|
|
|
|
if(rv == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return set_fs_ioc_flags(fdout,flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
copy(const string &from,
|
|
|
|
const string &to)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
int flags;
|
|
|
|
|
|
|
|
rv = get_fs_ioc_flags(from,flags);
|
|
|
|
if(rv == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return set_fs_ioc_flags(to,flags);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|