Skip to content
Snippets Groups Projects
Commit 4f9fb9aa authored by Isaiah's avatar Isaiah Committed by Brad King
Browse files

SystemTools: Fix Touch to avoid requiring file ownership

SystemTools::Touch should only require +w file permissions. The previous
implementation used `stat` to get the current time, and then passed that
time to one of utimensat/utimes/utime. However, utimes/utimensat only
permit an arbitrary-time argument to be passed by the file owner.

Therefore, per the docs, we pass NULL for the `times` argument to utimes
and utimensat, to indicate using the current time. This is permitted for
any UID with +w on the file.

Remove plain `utime` fallback that is no longer necessary.
parent 9e27254b
Branches fix_cm_16526
No related tags found
No related merge requests found
Pipeline #124809 passed
......@@ -1355,39 +1355,15 @@ bool SystemTools::Touch(const std::string& filename, bool create)
}
CloseHandle(h);
#elif KWSYS_CXX_HAS_UTIMENSAT
struct timespec times[2] = { { 0, UTIME_OMIT }, { 0, UTIME_NOW } };
if (utimensat(AT_FDCWD, filename.c_str(), times, 0) < 0) {
// utimensat is only available on newer Unixes and macOS 10.13+
if (utimensat(AT_FDCWD, filename.c_str(), NULL, 0) < 0) {
return false;
}
#else
struct stat st;
if (stat(filename.c_str(), &st) < 0) {
return false;
}
struct timeval mtime;
gettimeofday(&mtime, 0);
# if KWSYS_CXX_HAS_UTIMES
struct timeval atime;
# if KWSYS_CXX_STAT_HAS_ST_MTIM
atime.tv_sec = st.st_atim.tv_sec;
atime.tv_usec = st.st_atim.tv_nsec / 1000;
# elif KWSYS_CXX_STAT_HAS_ST_MTIMESPEC
atime.tv_sec = st.st_atimespec.tv_sec;
atime.tv_usec = st.st_atimespec.tv_nsec / 1000;
# else
atime.tv_sec = st.st_atime;
atime.tv_usec = 0;
# endif
struct timeval times[2] = { atime, mtime };
if (utimes(filename.c_str(), times) < 0) {
// fall back to utimes
if (utimes(filename.c_str(), NULL) < 0) {
return false;
}
# else
struct utimbuf times = { st.st_atime, mtime.tv_sec };
if (utime(filename.c_str(), &times) < 0) {
return false;
}
# endif
#endif
return true;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment