path: signal last use of the file (touch)

To have a system-level overview of when was the last time the file was
used, update the file modification timestamp to to the current time.

This is needed to remove stale cache files of the system.

Access time is not used as it may be, on the system level, disabled.

Ticket: 7893
(cherry picked from commit fd3847db72)
pull/15003/head
Lukas Sismis 7 months ago committed by Victor Julien
parent f701a67d82
commit 763f883d5d

@ -277,3 +277,23 @@ bool SCPathContainsTraversal(const char *path)
#endif
return strstr(path, pattern) != NULL;
}
/**
* \brief Update access and modification time of an existing file to 'now'.
* \param path The file path to touch
* \retval 0 on success, -1 on failure
*/
int SCTouchFile(const char *path)
{
if (path == NULL || path[0] == '\0') {
errno = EINVAL;
return -1;
}
#ifndef OS_WIN32
struct utimbuf ub;
ub.actime = ub.modtime = time(NULL);
if (utime(path, &ub) == 0)
return 0;
#endif
return -1;
}

@ -59,5 +59,6 @@ bool SCIsRegularFile(const struct dirent *const dir_entry);
char *SCRealPath(const char *path, char *resolved_path);
const char *SCBasename(const char *path);
bool SCPathContainsTraversal(const char *path);
int SCTouchFile(const char *path);
#endif /* SURICATA_UTIL_PATH_H */

Loading…
Cancel
Save