mergerfs/src/fs_base_utime_generic.hpp

314 lines
6.6 KiB
C++
Raw Normal View History

2016-10-23 23:54:06 -04: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 11:15:20 -04:00
#pragma once
2016-12-22 22:46:20 -05:00
2019-01-25 07:03:46 -05:00
#include "fs_base_futimesat.hpp"
#include "fs_base_stat.hpp"
2016-10-23 23:54:06 -04:00
#include <string>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/time.h>
#ifndef UTIME_NOW
# define UTIME_NOW ((1l << 30) - 1l)
#endif
#ifndef UTIME_OMIT
# define UTIME_OMIT ((1l << 30) - 2l)
#endif
static
inline
bool
2019-01-25 07:03:46 -05:00
_can_call_lutimes(const int dirfd_,
const std::string &path_,
const int flags_)
2016-10-23 23:54:06 -04:00
{
2019-01-25 07:03:46 -05:00
return ((flags_ == AT_SYMLINK_NOFOLLOW) &&
((dirfd_ == AT_FDCWD) ||
(path_[0] == '/')));
2016-10-23 23:54:06 -04:00
}
static
inline
bool
2019-01-25 07:03:46 -05:00
_should_ignore(const struct timespec ts_[2])
2016-10-23 23:54:06 -04:00
{
2019-01-25 07:03:46 -05:00
return ((ts_ != NULL) &&
(ts_[0].tv_nsec == UTIME_OMIT) &&
(ts_[1].tv_nsec == UTIME_OMIT));
2016-10-23 23:54:06 -04:00
}
static
inline
bool
2019-01-25 07:03:46 -05:00
_should_be_set_to_now(const struct timespec ts_[2])
2016-10-23 23:54:06 -04:00
{
2019-01-25 07:03:46 -05:00
return ((ts_ == NULL) ||
((ts_[0].tv_nsec == UTIME_NOW) &&
(ts_[1].tv_nsec == UTIME_NOW)));
2016-10-23 23:54:06 -04:00
}
static
inline
bool
2019-01-25 07:03:46 -05:00
_timespec_invalid(const struct timespec &ts_)
2016-10-23 23:54:06 -04:00
{
2019-01-25 07:03:46 -05:00
return (((ts_.tv_nsec < 0) ||
(ts_.tv_nsec > 999999999)) &&
((ts_.tv_nsec != UTIME_NOW) &&
(ts_.tv_nsec != UTIME_OMIT)));
2016-10-23 23:54:06 -04:00
}
static
inline
bool
2019-01-25 07:03:46 -05:00
_timespec_invalid(const struct timespec ts_[2])
2016-10-23 23:54:06 -04:00
{
2019-01-25 07:03:46 -05:00
return ((ts_ != NULL) &&
(_timespec_invalid(ts_[0]) ||
_timespec_invalid(ts_[1])));
2016-10-23 23:54:06 -04:00
}
static
inline
bool
_flags_invalid(const int flags)
{
return ((flags & ~AT_SYMLINK_NOFOLLOW) != 0);
}
static
inline
bool
2019-01-25 07:03:46 -05:00
_any_timespec_is_utime_omit(const struct timespec ts_[2])
2016-10-23 23:54:06 -04:00
{
2019-01-25 07:03:46 -05:00
return ((ts_[0].tv_nsec == UTIME_OMIT) ||
(ts_[1].tv_nsec == UTIME_OMIT));
2016-10-23 23:54:06 -04:00
}
static
inline
bool
2019-01-25 07:03:46 -05:00
_any_timespec_is_utime_now(const struct timespec ts_[2])
2016-10-23 23:54:06 -04:00
{
2019-01-25 07:03:46 -05:00
return ((ts_[0].tv_nsec == UTIME_NOW) ||
(ts_[1].tv_nsec == UTIME_NOW));
2016-10-23 23:54:06 -04:00
}
static
inline
int
_set_utime_omit_to_current_value(const int dirfd,
const std::string &path,
2019-01-25 07:03:46 -05:00
const struct timespec ts_[2],
2016-10-23 23:54:06 -04:00
struct timeval tv[2],
const int flags)
{
int rv;
struct stat st;
2017-04-06 22:16:17 -04:00
timespec *atime;
timespec *mtime;
2016-10-23 23:54:06 -04:00
2019-01-25 07:03:46 -05:00
if(!_any_timespec_is_utime_omit(ts_))
2016-10-23 23:54:06 -04:00
return 0;
rv = ::fstatat(dirfd,path.c_str(),&st,flags);
if(rv == -1)
return -1;
2017-04-06 22:16:17 -04:00
atime = fs::stat_atime(st);
mtime = fs::stat_mtime(st);
2019-01-25 07:03:46 -05:00
if(ts_[0].tv_nsec == UTIME_OMIT)
2017-04-06 22:16:17 -04:00
TIMESPEC_TO_TIMEVAL(&tv[0],atime);
2019-01-25 07:03:46 -05:00
if(ts_[1].tv_nsec == UTIME_OMIT)
2017-04-06 22:16:17 -04:00
TIMESPEC_TO_TIMEVAL(&tv[1],mtime);
2016-10-23 23:54:06 -04:00
return 0;
}
static
inline
int
_set_utime_omit_to_current_value(const int fd,
2019-01-25 07:03:46 -05:00
const struct timespec ts_[2],
2016-10-23 23:54:06 -04:00
struct timeval tv[2])
{
int rv;
struct stat st;
2017-04-06 22:16:17 -04:00
timespec *atime;
timespec *mtime;
2016-10-23 23:54:06 -04:00
2019-01-25 07:03:46 -05:00
if(!_any_timespec_is_utime_omit(ts_))
2016-10-23 23:54:06 -04:00
return 0;
rv = ::fstat(fd,&st);
if(rv == -1)
return -1;
2017-04-06 22:16:17 -04:00
atime = fs::stat_atime(st);
mtime = fs::stat_mtime(st);
2019-01-25 07:03:46 -05:00
if(ts_[0].tv_nsec == UTIME_OMIT)
2017-04-06 22:59:28 -04:00
TIMESPEC_TO_TIMEVAL(&tv[0],atime);
2019-01-25 07:03:46 -05:00
if(ts_[1].tv_nsec == UTIME_OMIT)
2017-04-06 22:16:17 -04:00
TIMESPEC_TO_TIMEVAL(&tv[1],mtime);
2016-10-23 23:54:06 -04:00
return 0;
}
static
inline
int
2019-01-25 07:03:46 -05:00
_set_utime_now_to_now(const struct timespec ts_[2],
2016-10-23 23:54:06 -04:00
struct timeval tv[2])
{
int rv;
struct timeval now;
2019-01-25 07:03:46 -05:00
if(_any_timespec_is_utime_now(ts_))
2016-10-23 23:54:06 -04:00
return 0;
rv = ::gettimeofday(&now,NULL);
if(rv == -1)
return -1;
2019-01-25 07:03:46 -05:00
if(ts_[0].tv_nsec == UTIME_NOW)
2016-10-23 23:54:06 -04:00
tv[0] = now;
2019-01-25 07:03:46 -05:00
if(ts_[1].tv_nsec == UTIME_NOW)
2016-10-23 23:54:06 -04:00
tv[1] = now;
return 0;
}
static
inline
int
_convert_timespec_to_timeval(const int dirfd,
const std::string &path,
2019-01-25 07:03:46 -05:00
const struct timespec ts_[2],
2016-10-23 23:54:06 -04:00
struct timeval tv[2],
struct timeval *&tvp,
const int flags)
{
int rv;
2019-01-25 07:03:46 -05:00
if(_should_be_set_to_now(ts_))
2016-10-23 23:54:06 -04:00
return (tvp=NULL,0);
2019-01-25 07:03:46 -05:00
TIMESPEC_TO_TIMEVAL(&tv[0],&ts_[0]);
TIMESPEC_TO_TIMEVAL(&tv[1],&ts_[1]);
2016-10-23 23:54:06 -04:00
2019-01-25 07:03:46 -05:00
rv = _set_utime_omit_to_current_value(dirfd,path,ts_,tv,flags);
2016-10-23 23:54:06 -04:00
if(rv == -1)
return -1;
2019-01-25 07:03:46 -05:00
rv = _set_utime_now_to_now(ts_,tv);
2016-10-23 23:54:06 -04:00
if(rv == -1)
return -1;
return (tvp=tv,0);
}
static
inline
int
_convert_timespec_to_timeval(const int fd,
2019-01-25 07:03:46 -05:00
const struct timespec ts_[2],
2016-10-23 23:54:06 -04:00
struct timeval tv[2],
struct timeval *&tvp)
{
int rv;
2019-01-25 07:03:46 -05:00
if(_should_be_set_to_now(ts_))
2016-10-23 23:54:06 -04:00
return (tvp=NULL,0);
2019-01-25 07:03:46 -05:00
TIMESPEC_TO_TIMEVAL(&tv[0],&ts_[0]);
TIMESPEC_TO_TIMEVAL(&tv[1],&ts_[1]);
2016-10-23 23:54:06 -04:00
2019-01-25 07:03:46 -05:00
rv = _set_utime_omit_to_current_value(fd,ts_,tv);
2016-10-23 23:54:06 -04:00
if(rv == -1)
return -1;
2019-01-25 07:03:46 -05:00
rv = _set_utime_now_to_now(ts_,tv);
2016-10-23 23:54:06 -04:00
if(rv == -1)
return -1;
return (tvp=tv,0);
}
namespace fs
{
static
inline
int
utime(const int dirfd,
const std::string &path,
2019-01-25 07:03:46 -05:00
const struct timespec ts_[2],
2016-10-23 23:54:06 -04:00
const int flags)
{
int rv;
struct timeval tv[2];
struct timeval *tvp;
if(_flags_invalid(flags))
return (errno=EINVAL,-1);
2019-01-25 07:03:46 -05:00
if(_timespec_invalid(ts_))
2016-10-23 23:54:06 -04:00
return (errno=EINVAL,-1);
2019-01-25 07:03:46 -05:00
if(_should_ignore(ts_))
2016-10-23 23:54:06 -04:00
return 0;
2019-01-25 07:03:46 -05:00
rv = _convert_timespec_to_timeval(dirfd,path,ts_,tv,tvp,flags);
2016-10-23 23:54:06 -04:00
if(rv == -1)
return -1;
if((flags & AT_SYMLINK_NOFOLLOW) == 0)
2017-04-06 22:59:28 -04:00
return fs::futimesat(dirfd,path.c_str(),tvp);
2016-10-23 23:54:06 -04:00
if(_can_call_lutimes(dirfd,path,flags))
return ::lutimes(path.c_str(),tvp);
return (errno=ENOTSUP,-1);
}
static
inline
int
futimens(const int fd_,
const struct timespec ts_[2])
2016-10-23 23:54:06 -04:00
{
int rv;
struct timeval tv[2];
struct timeval *tvp;
2019-01-25 07:03:46 -05:00
if(_timespec_invalid(ts_))
2016-10-23 23:54:06 -04:00
return (errno=EINVAL,-1);
2019-01-25 07:03:46 -05:00
if(_should_ignore(ts_))
2016-10-23 23:54:06 -04:00
return 0;
2019-01-25 07:03:46 -05:00
rv = _convert_timespec_to_timeval(fd_,ts_,tv,tvp);
2016-10-23 23:54:06 -04:00
if(rv == -1)
return -1;
2019-01-25 07:03:46 -05:00
return ::futimes(fd_,tvp);
2016-10-23 23:54:06 -04:00
}
}