From 42d454ac271bcceb2d956f13fed696d8efd4cdca Mon Sep 17 00:00:00 2001 From: Antonio SJ Musumeci Date: Thu, 6 Apr 2017 22:59:28 -0400 Subject: [PATCH] abstract futimesat --- src/fs_base_futimesat.cpp | 23 ++++++++ src/fs_base_futimesat.hpp | 30 +++++++++++ src/fs_base_futimesat_generic.icpp | 31 +++++++++++ src/fs_base_futimesat_osx.icpp | 84 ++++++++++++++++++++++++++++++ src/fs_base_utime_generic.hpp | 5 +- 5 files changed, 171 insertions(+), 2 deletions(-) create mode 100644 src/fs_base_futimesat.cpp create mode 100644 src/fs_base_futimesat.hpp create mode 100644 src/fs_base_futimesat_generic.icpp create mode 100644 src/fs_base_futimesat_osx.icpp diff --git a/src/fs_base_futimesat.cpp b/src/fs_base_futimesat.cpp new file mode 100644 index 00000000..c254f4b9 --- /dev/null +++ b/src/fs_base_futimesat.cpp @@ -0,0 +1,23 @@ +/* + ISC License + + Copyright (c) 2017, Antonio SJ Musumeci + + 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. +*/ + +#if __APPLE__ +# include "fs_base_futimesat_osx.icpp" +#else +# include "fs_base_futimesat_generic.icpp" +#endif diff --git a/src/fs_base_futimesat.hpp b/src/fs_base_futimesat.hpp new file mode 100644 index 00000000..20f0938a --- /dev/null +++ b/src/fs_base_futimesat.hpp @@ -0,0 +1,30 @@ +/* + ISC License + + Copyright (c) 2017, Antonio SJ Musumeci + + 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. +*/ + +#ifndef __FS_BASE_FUTIMESAT_HPP__ +#define __FS_BASE_FUTIMESAT_HPP__ + +namespace fs +{ + int + futimesat(const int dirfd, + const char *pathname, + const struct timeval times[2]); +} + +#endif diff --git a/src/fs_base_futimesat_generic.icpp b/src/fs_base_futimesat_generic.icpp new file mode 100644 index 00000000..bfcd5e57 --- /dev/null +++ b/src/fs_base_futimesat_generic.icpp @@ -0,0 +1,31 @@ +/* + ISC License + + Copyright (c) 2017, Antonio SJ Musumeci + + 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 +#include + +namespace fs +{ + int + futimesat(const int dirfd, + const char *pathname, + const struct timeval times[2]) + { + return ::futimesat(dirfd,pathname,times); + } +} diff --git a/src/fs_base_futimesat_osx.icpp b/src/fs_base_futimesat_osx.icpp new file mode 100644 index 00000000..1efc5d8f --- /dev/null +++ b/src/fs_base_futimesat_osx.icpp @@ -0,0 +1,84 @@ +/* + ISC License + + Copyright (c) 2017, Antonio SJ Musumeci + + 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 +#include +#include +#include +#include +#include +#include +#include +#include + +static +int +getpath(const int dirfd, + const char *path, + char *fullpath) +{ + int rv; + struct stat st; + + rv = ::fstat(dirfd,&st); + if(rv == -1) + return -1; + + if(!S_ISDIR(st.st_mode)) + return (errno=ENOTDIR,-1); + + rv = ::fcntl(dirfd,F_GETPATH,fullpath); + if(rv == -1) + return -1; + + rv = ::strlcat(fullpath,"/",MAXPATHLEN); + if(rv > MAXPATHLEN) + return (errno=ENAMETOOLONG,-1); + + rv = ::strlcat(fullpath,path,MAXPATHLEN); + if(rv > MAXPATHLEN) + return (errno=ENAMETOOLONG,-1); + + return 0; +} + +namespace fs +{ + int + futimesat(const int dirfd, + const char *pathname, + const struct timeval times[2]) + { + int rv; + char fullpath[MAXPATHLEN]; + + if((dirfd == AT_FDCWD) || + ((pathname != NULL) && + (pathname[0] == '/'))) + return ::utimes(pathname,times); + + if(dirfd < 0) + return (errno=EBADF,-1); + + rv = getpath(dirfd,pathname,fullpath); + if(rv == -1) + return -1; + + return ::utimes(fullpath,times); + } +} diff --git a/src/fs_base_utime_generic.hpp b/src/fs_base_utime_generic.hpp index 0734d8a1..c49169ad 100644 --- a/src/fs_base_utime_generic.hpp +++ b/src/fs_base_utime_generic.hpp @@ -25,6 +25,7 @@ #include #include +#include "fs_base_futimesat.hpp" #include "fs_base_stat.hpp" #ifndef UTIME_NOW @@ -169,7 +170,7 @@ _set_utime_omit_to_current_value(const int fd, mtime = fs::stat_mtime(st); if(ts[0].tv_nsec == UTIME_OMIT) - TIMESPEC_TO_TIMEVAL(&tv[0],atime; + TIMESPEC_TO_TIMEVAL(&tv[0],atime); if(ts[1].tv_nsec == UTIME_OMIT) TIMESPEC_TO_TIMEVAL(&tv[1],mtime); @@ -282,7 +283,7 @@ namespace fs return -1; if((flags & AT_SYMLINK_NOFOLLOW) == 0) - return ::futimesat(dirfd,path.c_str(),tvp); + return fs::futimesat(dirfd,path.c_str(),tvp); if(_can_call_lutimes(dirfd,path,flags)) return ::lutimes(path.c_str(),tvp);