Browse Source

Merge pull request #334 from trapexit/freebsd

changes to allow compiling on FreeBSD
pull/336/head
Antonio SJ Musumeci 8 years ago
committed by GitHub
parent
commit
12b9fc1750
  1. 6
      Makefile
  2. 5
      src/fs_base_fsync.hpp
  3. 38
      src/fs_base_utime.hpp
  4. 298
      src/fs_base_utime_generic.hpp
  5. 13
      src/fs_base_utime_utimensat.hpp
  6. 62
      src/fs_clonefile.cpp
  7. 22
      src/fs_clonepath.cpp
  8. 21
      src/fs_time.cpp
  9. 22
      src/fs_time.hpp
  10. 33
      src/fs_time_futimes.icpp
  11. 4
      src/utimens.cpp
  12. 2
      tools/cppfind

6
Makefile

@ -143,14 +143,16 @@ distclean: clean
install: install-base install-mount.mergerfs install-man install: install-base install-mount.mergerfs install-man
install-base: $(TARGET) install-base: $(TARGET)
$(INSTALL) -v -m 0755 -D "$(TARGET)" "$(INSTALLBINDIR)/$(TARGET)"
$(MKDIR) -p "$(INSTALLBINDIR)"
$(INSTALL) -v -m 0755 "$(TARGET)" "$(INSTALLBINDIR)/$(TARGET)"
install-mount.mergerfs: mount.mergerfs install-mount.mergerfs: mount.mergerfs
$(MKDIR) -p "$(INSTALLBINDIR)" $(MKDIR) -p "$(INSTALLBINDIR)"
$(CP) -a "$<" "$(INSTALLBINDIR)/$<" $(CP) -a "$<" "$(INSTALLBINDIR)/$<"
install-man: $(MANPAGE) install-man: $(MANPAGE)
$(INSTALL) -v -m 0644 -D "man/$(MANPAGE)" "$(INSTALLMAN1DIR)/$(MANPAGE)"
$(MKDIR) -p "$(INSTALLMAN1DIR)"
$(INSTALL) -v -m 0644 "man/$(MANPAGE)" "$(INSTALLMAN1DIR)/$(MANPAGE)"
install-strip: install-base install-strip: install-base
$(STRIP) "$(INSTALLBINDIR)/$(TARGET)" $(STRIP) "$(INSTALLBINDIR)/$(TARGET)"

5
src/fs_base_fsync.hpp

@ -16,6 +16,7 @@
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/ */
#include <errno.h>
#include <unistd.h> #include <unistd.h>
namespace fs namespace fs
@ -33,6 +34,10 @@ namespace fs
int int
fdatasync(const int fd) fdatasync(const int fd)
{ {
#if _POSIX_SYNCHRONIZED_IO > 0
return ::fdatasync(fd); return ::fdatasync(fd);
#else
return (errno=ENOSYS,-1);
#endif
} }
} }

38
src/fs_time_futimens.icpp → src/fs_base_utime.hpp

@ -14,13 +14,20 @@
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/ */
#include <fcntl.h>
#include <sys/stat.h>
#ifdef __linux__
# include "fs_base_utime_utimensat.hpp"
#elif __FreeBSD__ >= 11
# include "fs_base_utime_utimensat.hpp"
#else
# include "fs_base_utime_generic.hpp"
#endif
namespace fs namespace fs
{ {
static
inline
int int
utimes(const int fd,
utime(const std::string &path,
const struct stat &st) const struct stat &st)
{ {
struct timespec times[2]; struct timespec times[2];
@ -28,6 +35,29 @@ namespace fs
times[0] = st.st_atim; times[0] = st.st_atim;
times[1] = st.st_mtim; times[1] = st.st_mtim;
return ::futimens(fd,times);
return fs::utime(AT_FDCWD,path,times,0);
}
static
inline
int
utime(const int fd,
const struct stat &st)
{
struct timespec times[2];
times[0] = st.st_atim;
times[1] = st.st_mtim;
return fs::utime(fd,times);
}
static
inline
int
lutime(const std::string &path,
const struct timespec times[2])
{
return fs::utime(AT_FDCWD,path,times,AT_SYMLINK_NOFOLLOW);
} }
} }

298
src/fs_base_utime_generic.hpp

@ -0,0 +1,298 @@
/*
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.
*/
#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
_can_call_lutimes(const int dirfd,
const std::string &path,
const int flags)
{
return ((flags == AT_SYMLINK_NOFOLLOW) &&
((dirfd == AT_FDCWD) ||
(path[0] == '/')));
}
static
inline
bool
_should_ignore(const struct timespec ts[2])
{
return ((ts != NULL) &&
(ts[0].tv_nsec == UTIME_OMIT) &&
(ts[1].tv_nsec == UTIME_OMIT));
}
static
inline
bool
_should_be_set_to_now(const struct timespec ts[2])
{
return ((ts == NULL) ||
((ts[0].tv_nsec == UTIME_NOW) &&
(ts[1].tv_nsec == UTIME_NOW)));
}
static
inline
bool
_timespec_invalid(const struct timespec &ts)
{
return (((ts.tv_nsec < 0) ||
(ts.tv_nsec > 999999999)) &&
((ts.tv_nsec != UTIME_NOW) &&
(ts.tv_nsec != UTIME_OMIT)));
}
static
inline
bool
_timespec_invalid(const struct timespec ts[2])
{
return ((ts != NULL) &&
(_timespec_invalid(ts[0]) ||
_timespec_invalid(ts[1])));
}
static
inline
bool
_flags_invalid(const int flags)
{
return ((flags & ~AT_SYMLINK_NOFOLLOW) != 0);
}
static
inline
bool
_any_timespec_is_utime_omit(const struct timespec ts[2])
{
return ((ts[0].tv_nsec == UTIME_OMIT) ||
(ts[1].tv_nsec == UTIME_OMIT));
}
static
inline
bool
_any_timespec_is_utime_now(const struct timespec ts[2])
{
return ((ts[0].tv_nsec == UTIME_NOW) ||
(ts[1].tv_nsec == UTIME_NOW));
}
static
inline
int
_set_utime_omit_to_current_value(const int dirfd,
const std::string &path,
const struct timespec ts[2],
struct timeval tv[2],
const int flags)
{
int rv;
struct stat st;
if(!_any_timespec_is_utime_omit(ts))
return 0;
rv = ::fstatat(dirfd,path.c_str(),&st,flags);
if(rv == -1)
return -1;
if(ts[0].tv_nsec == UTIME_OMIT)
TIMESPEC_TO_TIMEVAL(&tv[0],&st.st_atim);
if(ts[1].tv_nsec == UTIME_OMIT)
TIMESPEC_TO_TIMEVAL(&tv[1],&st.st_mtim);
return 0;
}
static
inline
int
_set_utime_omit_to_current_value(const int fd,
const struct timespec ts[2],
struct timeval tv[2])
{
int rv;
struct stat st;
if(!_any_timespec_is_utime_omit(ts))
return 0;
rv = ::fstat(fd,&st);
if(rv == -1)
return -1;
if(ts[0].tv_nsec == UTIME_OMIT)
TIMESPEC_TO_TIMEVAL(&tv[0],&st.st_atim);
if(ts[1].tv_nsec == UTIME_OMIT)
TIMESPEC_TO_TIMEVAL(&tv[1],&st.st_mtim);
return 0;
}
static
inline
int
_set_utime_now_to_now(const struct timespec ts[2],
struct timeval tv[2])
{
int rv;
struct timeval now;
if(_any_timespec_is_utime_now(ts))
return 0;
rv = ::gettimeofday(&now,NULL);
if(rv == -1)
return -1;
if(ts[0].tv_nsec == UTIME_NOW)
tv[0] = now;
if(ts[1].tv_nsec == UTIME_NOW)
tv[1] = now;
return 0;
}
static
inline
int
_convert_timespec_to_timeval(const int dirfd,
const std::string &path,
const struct timespec ts[2],
struct timeval tv[2],
struct timeval *&tvp,
const int flags)
{
int rv;
if(_should_be_set_to_now(ts))
return (tvp=NULL,0);
TIMESPEC_TO_TIMEVAL(&tv[0],&ts[0]);
TIMESPEC_TO_TIMEVAL(&tv[1],&ts[1]);
rv = _set_utime_omit_to_current_value(dirfd,path,ts,tv,flags);
if(rv == -1)
return -1;
rv = _set_utime_now_to_now(ts,tv);
if(rv == -1)
return -1;
return (tvp=tv,0);
}
static
inline
int
_convert_timespec_to_timeval(const int fd,
const struct timespec ts[2],
struct timeval tv[2],
struct timeval *&tvp)
{
int rv;
if(_should_be_set_to_now(ts))
return (tvp=NULL,0);
TIMESPEC_TO_TIMEVAL(&tv[0],&ts[0]);
TIMESPEC_TO_TIMEVAL(&tv[1],&ts[1]);
rv = _set_utime_omit_to_current_value(fd,ts,tv);
if(rv == -1)
return -1;
rv = _set_utime_now_to_now(ts,tv);
if(rv == -1)
return -1;
return (tvp=tv,0);
}
namespace fs
{
static
inline
int
utime(const int dirfd,
const std::string &path,
const struct timespec ts[2],
const int flags)
{
int rv;
struct timeval tv[2];
struct timeval *tvp;
if(_flags_invalid(flags))
return (errno=EINVAL,-1);
if(_timespec_invalid(ts))
return (errno=EINVAL,-1);
if(_should_ignore(ts))
return 0;
rv = _convert_timespec_to_timeval(dirfd,path,ts,tv,tvp,flags);
if(rv == -1)
return -1;
if((flags & AT_SYMLINK_NOFOLLOW) == 0)
return ::futimesat(dirfd,path.c_str(),tvp);
if(_can_call_lutimes(dirfd,path,flags))
return ::lutimes(path.c_str(),tvp);
return (errno=ENOTSUP,-1);
}
static
inline
int
utime(const int fd,
const struct timespec ts[2])
{
int rv;
struct timeval tv[2];
struct timeval *tvp;
if(_timespec_invalid(ts))
return (errno=EINVAL,-1);
if(_should_ignore(ts))
return 0;
rv = _convert_timespec_to_timeval(fd,ts,tv,tvp);
if(rv == -1)
return -1;
return ::futimes(fd,tvp);
}
}

13
src/fs_base_utimensat.hpp → src/fs_base_utime_utimensat.hpp

@ -26,7 +26,7 @@ namespace fs
static static
inline inline
int int
utimensat(const int dirfd,
utime(const int dirfd,
const std::string &path, const std::string &path,
const struct timespec times[2], const struct timespec times[2],
const int flags) const int flags)
@ -37,14 +37,9 @@ namespace fs
static static
inline inline
int int
utimensat(const std::string &path,
const struct stat &st)
utime(const int fd,
const struct timespec times[2])
{ {
struct timespec times[2];
times[0] = st.st_atim;
times[1] = st.st_mtim;
return fs::utimensat(AT_FDCWD,path,times,0);
return ::futimens(fd,times);
} }
} }

62
src/fs_clonefile.cpp

@ -30,23 +30,29 @@
#include "fs_base_open.hpp" #include "fs_base_open.hpp"
#include "fs_base_read.hpp" #include "fs_base_read.hpp"
#include "fs_base_stat.hpp" #include "fs_base_stat.hpp"
#include "fs_base_utime.hpp"
#include "fs_base_write.hpp" #include "fs_base_write.hpp"
#include "fs_fadvise.hpp" #include "fs_fadvise.hpp"
#include "fs_fallocate.hpp" #include "fs_fallocate.hpp"
#include "fs_sendfile.hpp" #include "fs_sendfile.hpp"
#include "fs_time.hpp"
#include "fs_xattr.hpp" #include "fs_xattr.hpp"
#ifndef O_LARGEFILE
# define O_LARGEFILE 0
#endif
#ifndef O_NOATIME
# define O_NOATIME 0
#endif
using std::string; using std::string;
using std::vector; using std::vector;
namespace fs
{
int
writen(const int fd,
int
writen(const int fd,
const char *buf, const char *buf,
const size_t count) const size_t count)
{
{
size_t nleft; size_t nleft;
ssize_t nwritten; ssize_t nwritten;
@ -66,15 +72,15 @@ namespace fs
} }
return count; return count;
}
}
static
int
copyfile_rw(const int fdin,
static
int
copyfile_rw(const int fdin,
const int fdout, const int fdout,
const size_t count, const size_t count,
const size_t blocksize) const size_t blocksize)
{
{
ssize_t nr; ssize_t nr;
ssize_t nw; ssize_t nw;
ssize_t bufsize; ssize_t bufsize;
@ -105,15 +111,15 @@ namespace fs
} }
return count; return count;
}
}
static
int
copydata(const int fdin,
static
int
copydata(const int fdin,
const int fdout, const int fdout,
const size_t count, const size_t count,
const size_t blocksize) const size_t blocksize)
{
{
int rv; int rv;
fs::fadvise(fdin,0,count,POSIX_FADV_WILLNEED); fs::fadvise(fdin,0,count,POSIX_FADV_WILLNEED);
@ -123,15 +129,15 @@ namespace fs
rv = fs::sendfile(fdin,fdout,count); rv = fs::sendfile(fdin,fdout,count);
if((rv == -1) && ((errno == EINVAL) || (errno == ENOSYS))) if((rv == -1) && ((errno == EINVAL) || (errno == ENOSYS)))
return fs::copyfile_rw(fdin,fdout,count,blocksize);
return ::copyfile_rw(fdin,fdout,count,blocksize);
return rv; return rv;
}
}
static
bool
ignorable_error(const int err)
{
static
bool
ignorable_error(const int err)
{
switch(err) switch(err)
{ {
case ENOTTY: case ENOTTY:
@ -143,8 +149,10 @@ namespace fs
} }
return false; return false;
}
}
namespace fs
{
int int
clonefile(const int fdin, clonefile(const int fdin,
const int fdout) const int fdout)
@ -156,16 +164,16 @@ namespace fs
if(rv == -1) if(rv == -1)
return -1; return -1;
rv = copydata(fdin,fdout,stin.st_size,stin.st_blksize);
rv = ::copydata(fdin,fdout,stin.st_size,stin.st_blksize);
if(rv == -1) if(rv == -1)
return -1; return -1;
rv = fs::attr::copy(fdin,fdout); rv = fs::attr::copy(fdin,fdout);
if(rv == -1 && !ignorable_error(errno))
if((rv == -1) && !ignorable_error(errno))
return -1; return -1;
rv = fs::xattr::copy(fdin,fdout); rv = fs::xattr::copy(fdin,fdout);
if(rv == -1 && !ignorable_error(errno))
if((rv == -1) && !ignorable_error(errno))
return -1; return -1;
rv = fs::fchown(fdout,stin); rv = fs::fchown(fdout,stin);
@ -176,7 +184,7 @@ namespace fs
if(rv == -1) if(rv == -1)
return -1; return -1;
rv = fs::utimes(fdout,stin);
rv = fs::utime(fdout,stin);
if(rv == -1) if(rv == -1)
return -1; return -1;

22
src/fs_clonepath.cpp

@ -22,18 +22,16 @@
#include "fs_base_chown.hpp" #include "fs_base_chown.hpp"
#include "fs_base_mkdir.hpp" #include "fs_base_mkdir.hpp"
#include "fs_base_stat.hpp" #include "fs_base_stat.hpp"
#include "fs_base_utimensat.hpp"
#include "fs_base_utime.hpp"
#include "fs_path.hpp" #include "fs_path.hpp"
#include "fs_xattr.hpp" #include "fs_xattr.hpp"
using std::string; using std::string;
namespace fs
static
bool
ignorable_error(const int err)
{ {
static
bool
ignorable_error(const int err)
{
switch(err) switch(err)
{ {
case ENOTTY: case ENOTTY:
@ -45,8 +43,10 @@ namespace fs
} }
return false; return false;
}
}
namespace fs
{
int int
clonepath(const string &fromsrc, clonepath(const string &fromsrc,
const string &tosrc, const string &tosrc,
@ -72,7 +72,7 @@ namespace fs
if(rv == -1) if(rv == -1)
return -1; return -1;
else if(!S_ISDIR(st.st_mode)) else if(!S_ISDIR(st.st_mode))
return (errno = ENOTDIR,-1);
return (errno=ENOTDIR,-1);
fs::path::make(&tosrc,relative,topath); fs::path::make(&tosrc,relative,topath);
rv = fs::mkdir(topath,st.st_mode); rv = fs::mkdir(topath,st.st_mode);
@ -88,18 +88,18 @@ namespace fs
// It may not support it... it's fine... // It may not support it... it's fine...
rv = fs::attr::copy(frompath,topath); rv = fs::attr::copy(frompath,topath);
if(rv == -1 && !ignorable_error(errno))
if((rv == -1) && !ignorable_error(errno))
return -1; return -1;
rv = fs::xattr::copy(frompath,topath); rv = fs::xattr::copy(frompath,topath);
if(rv == -1 && !ignorable_error(errno))
if((rv == -1) && !ignorable_error(errno))
return -1; return -1;
rv = fs::chown(topath,st); rv = fs::chown(topath,st);
if(rv == -1) if(rv == -1)
return -1; return -1;
rv = fs::utimensat(topath,st);
rv = fs::utime(topath,st);
if(rv == -1) if(rv == -1)
return -1; return -1;

21
src/fs_time.cpp

@ -1,21 +0,0 @@
/*
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.
*/
#ifdef __linux__
# include "fs_time_futimens.icpp"
#else
# include "fs_time_futimes.icpp"
#endif

22
src/fs_time.hpp

@ -1,22 +0,0 @@
/*
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.
*/
namespace fs
{
int
utimes(const int fd,
const struct stat &st);
}

33
src/fs_time_futimes.icpp

@ -1,33 +0,0 @@
/*
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.
*/
#include <sys/stat.h>
#include <sys/time.h>
namespace fs
{
int
utimes(const int fd,
const struct stat &st)
{
struct timeval times[2];
TIMESPEC_TO_TIMEVAL(&times[0],&st.st_atim);
TIMESPEC_TO_TIMEVAL(&times[1],&st.st_mtim);
return ::futimes(fd,times);
}
}

4
src/utimens.cpp

@ -23,7 +23,7 @@
#include "config.hpp" #include "config.hpp"
#include "errno.hpp" #include "errno.hpp"
#include "fs_base_utimensat.hpp"
#include "fs_base_utime.hpp"
#include "fs_path.hpp" #include "fs_path.hpp"
#include "rv.hpp" #include "rv.hpp"
#include "rwlock.hpp" #include "rwlock.hpp"
@ -45,7 +45,7 @@ _utimens_loop_core(const string *basepath,
fs::path::make(basepath,fusepath,fullpath); fs::path::make(basepath,fusepath,fullpath);
rv = fs::utimensat(AT_FDCWD,fullpath,ts,AT_SYMLINK_NOFOLLOW);
rv = fs::lutime(fullpath,ts);
return calc_error(rv,error,errno); return calc_error(rv,error,errno);
} }

2
tools/cppfind

@ -2,6 +2,6 @@
FUSE_CFLAGS="$(pkg-config --cflags fuse) -DFUSE_USE_VERSION=29" FUSE_CFLAGS="$(pkg-config --cflags fuse) -DFUSE_USE_VERSION=29"
echo "#include <fuse.h>" | cpp ${FUSE_CFLAGS} | grep -q "${1}"
echo "#include <fuse.h>" | cpp ${FUSE_CFLAGS} | grep "${1}" > /dev/null
[ "$?" != "0" ]; echo $? [ "$?" != "0" ]; echo $?
Loading…
Cancel
Save