Skip to content

Commit 3b0d371

Browse files
committed
abstract futimesat and getting of atime/mtime
1 parent 59ece5f commit 3b0d371

7 files changed

+236
-39
lines changed

src/fs_base_futimesat.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
ISC License
3+
4+
Copyright (c) 2017, Antonio SJ Musumeci <[email protected]>
5+
6+
Permission to use, copy, modify, and/or distribute this software for any
7+
purpose with or without fee is hereby granted, provided that the above
8+
copyright notice and this permission notice appear in all copies.
9+
10+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17+
*/
18+
19+
#if __APPLE__
20+
# include "fs_base_futimesat_osx.icpp"
21+
#else
22+
# include "fs_base_futimesat_generic.icpp"
23+
#endif

src/fs_base_futimesat.hpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
ISC License
3+
4+
Copyright (c) 2017, Antonio SJ Musumeci <[email protected]>
5+
6+
Permission to use, copy, modify, and/or distribute this software for any
7+
purpose with or without fee is hereby granted, provided that the above
8+
copyright notice and this permission notice appear in all copies.
9+
10+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17+
*/
18+
19+
#ifndef __FS_BASE_FUTIMESAT_HPP__
20+
#define __FS_BASE_FUTIMESAT_HPP__
21+
22+
namespace fs
23+
{
24+
int
25+
futimesat(const int dirfd,
26+
const char *pathname,
27+
const struct timeval times[2]);
28+
}
29+
30+
#endif

src/fs_base_futimesat_generic.icpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
ISC License
3+
4+
Copyright (c) 2017, Antonio SJ Musumeci <[email protected]>
5+
6+
Permission to use, copy, modify, and/or distribute this software for any
7+
purpose with or without fee is hereby granted, provided that the above
8+
copyright notice and this permission notice appear in all copies.
9+
10+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17+
*/
18+
19+
#include <fcntl.h>
20+
#include <sys/time.h>
21+
22+
namespace fs
23+
{
24+
int
25+
futimesat(const int dirfd,
26+
const char *pathname,
27+
const struct timeval times[2])
28+
{
29+
return ::futimesat(dirfd,pathname,times);
30+
}
31+
}

src/fs_base_futimesat_osx.icpp

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
ISC License
3+
4+
Copyright (c) 2017, Antonio SJ Musumeci <[email protected]>
5+
6+
Permission to use, copy, modify, and/or distribute this software for any
7+
purpose with or without fee is hereby granted, provided that the above
8+
copyright notice and this permission notice appear in all copies.
9+
10+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17+
*/
18+
19+
#include <err.h>
20+
#include <fcntl.h>
21+
#include <stdio.h>
22+
#include <string.h>
23+
#include <sys/errno.h>
24+
#include <sys/param.h>
25+
#include <sys/stat.h>
26+
#include <sys/time.h>
27+
#include <unistd.h>
28+
29+
static
30+
int
31+
getpath(const int dirfd,
32+
const char *path,
33+
char *fullpath)
34+
{
35+
int rv;
36+
struct stat st;
37+
38+
rv = ::fstat(dirfd,&st);
39+
if(rv == -1)
40+
return -1;
41+
42+
if(!S_ISDIR(st.st_mode))
43+
return (errno=ENOTDIR,-1);
44+
45+
rv = ::fcntl(dirfd,F_GETPATH,fullpath);
46+
if(rv == -1)
47+
return -1;
48+
49+
rv = ::strlcat(fullpath,"/",MAXPATHLEN);
50+
if(rv > MAXPATHLEN)
51+
return (errno=ENAMETOOLONG,-1);
52+
53+
rv = ::strlcat(fullpath,path,MAXPATHLEN);
54+
if(rv > MAXPATHLEN)
55+
return (errno=ENAMETOOLONG,-1);
56+
57+
return 0;
58+
}
59+
60+
namespace fs
61+
{
62+
int
63+
futimesat(const int dirfd,
64+
const char *path,
65+
const struct timeval times[2])
66+
{
67+
char fullpath[MAXPATHLEN];
68+
69+
if((dirfd == AT_FDCWD) ||
70+
((path != NULL) && (path[0] == '/')))
71+
return ::utimes(path,times);
72+
73+
if(dirfd < 0)
74+
return (errno=EBADF,-1);
75+
76+
rv = getpath(dirfd,path,fullpath);
77+
if(rv == -1)
78+
return -1;
79+
80+
return ::utimes(fullpath,times);
81+
}
82+
}

src/fs_base_stat.hpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,56 @@ namespace fs
6262
{
6363
return ::fstat(fd,&st);
6464
}
65+
66+
static
67+
inline
68+
timespec *
69+
stat_atime(struct stat &st)
70+
{
71+
#if __APPLE__
72+
return &st.st_atimespec;
73+
#else
74+
return &st.st_atim;
75+
#endif
76+
}
77+
78+
static
79+
inline
80+
const
81+
timespec *
82+
stat_atime(const struct stat &st)
83+
{
84+
#if __APPLE__
85+
return &st.st_atimespec;
86+
#else
87+
return &st.st_atim;
88+
#endif
89+
}
90+
91+
static
92+
inline
93+
timespec *
94+
stat_mtime(struct stat &st)
95+
{
96+
#if __APPLE__
97+
return &st.st_mtimespec;
98+
#else
99+
return &st.st_mtim;
100+
#endif
101+
}
102+
103+
static
104+
inline
105+
const
106+
timespec *
107+
stat_mtime(const struct stat &st)
108+
{
109+
#if __APPLE__
110+
return &st.st_mtimespec;
111+
#else
112+
return &st.st_mtim;
113+
#endif
114+
}
65115
}
66116

67117
#endif

src/fs_base_utime.hpp

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
# include "fs_base_utime_generic.hpp"
2626
#endif
2727

28+
#include "fs_base_stat.hpp"
29+
2830
namespace fs
2931
{
3032
static
@@ -35,13 +37,8 @@ namespace fs
3537
{
3638
struct timespec times[2];
3739

38-
#if __APPLE__
39-
times[0] = st.st_atimespec;
40-
times[1] = st.st_mtimespec;
41-
#else
42-
times[0] = st.st_atim;
43-
times[1] = st.st_mtim;
44-
#endif
40+
times[0] = *fs::stat_atime(st);
41+
times[1] = *fs::stat_mtime(st);
4542

4643
return fs::utime(AT_FDCWD,path,times,0);
4744
}
@@ -54,13 +51,8 @@ namespace fs
5451
{
5552
struct timespec times[2];
5653

57-
#if __APPLE__
58-
times[0] = st.st_atimespec;
59-
times[1] = st.st_mtimespec;
60-
#else
61-
times[0] = st.st_atim;
62-
times[1] = st.st_mtim;
63-
#endif
54+
times[0] = *fs::stat_atime(st);
55+
times[1] = *fs::stat_mtime(st);
6456

6557
return fs::utime(fd,times);
6658
}

src/fs_base_utime_generic.hpp

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
#include <sys/stat.h>
2626
#include <sys/time.h>
2727

28-
#include "futimesat.hpp" /* futimesat replacement */
28+
#include "fs_base_futimesat.hpp"
29+
#include "fs_base_stat.hpp"
2930

3031
#ifndef UTIME_NOW
3132
# define UTIME_NOW ((1l << 30) - 1l)
@@ -125,7 +126,8 @@ _set_utime_omit_to_current_value(const int dirfd,
125126
{
126127
int rv;
127128
struct stat st;
128-
timespec *atime, *mtime;
129+
timespec *atime;
130+
timespec *mtime;
129131

130132
if(!_any_timespec_is_utime_omit(ts))
131133
return 0;
@@ -134,14 +136,9 @@ _set_utime_omit_to_current_value(const int dirfd,
134136
if(rv == -1)
135137
return -1;
136138

137-
#if __APPLE__
138-
atime = &st.st_atimespec;
139-
mtime = &st.st_mtimespec;
140-
#else
141-
atime = &st.st_atim;
142-
mtime = &st.st_mtim;
143-
#endif
144-
139+
atime = fs::stat_atime(st);
140+
mtime = fs::stat_mtime(st);
141+
145142
if(ts[0].tv_nsec == UTIME_OMIT)
146143
TIMESPEC_TO_TIMEVAL(&tv[0],atime);
147144
if(ts[1].tv_nsec == UTIME_OMIT)
@@ -159,7 +156,8 @@ _set_utime_omit_to_current_value(const int fd,
159156
{
160157
int rv;
161158
struct stat st;
162-
timespec *atime, *mtime;
159+
timespec *atime;
160+
timespec *mtime;
163161

164162
if(!_any_timespec_is_utime_omit(ts))
165163
return 0;
@@ -168,13 +166,8 @@ _set_utime_omit_to_current_value(const int fd,
168166
if(rv == -1)
169167
return -1;
170168

171-
#if __APPLE__
172-
atime = &st.st_atimespec;
173-
mtime = &st.st_mtimespec;
174-
#else
175-
atime = &st.st_atim;
176-
mtime = &st.st_mtim;
177-
#endif
169+
atime = fs::stat_atime(st);
170+
mtime = fs::stat_mtime(st);
178171

179172
if(ts[0].tv_nsec == UTIME_OMIT)
180173
TIMESPEC_TO_TIMEVAL(&tv[0],atime);
@@ -289,13 +282,9 @@ namespace fs
289282
if(rv == -1)
290283
return -1;
291284

292-
if((flags & AT_SYMLINK_NOFOLLOW) == 0) {
293-
#if __APPLE__
294-
return _futimesat(dirfd,path.c_str(),tvp);
295-
#else
296-
return ::futimesat(dirfd,path.c_str(),tvp);
297-
#endif
298-
}
285+
if((flags & AT_SYMLINK_NOFOLLOW) == 0)
286+
return fs::futimesat(dirfd,path.c_str(),tvp);
287+
299288
if(_can_call_lutimes(dirfd,path,flags))
300289
return ::lutimes(path.c_str(),tvp);
301290

0 commit comments

Comments
 (0)