Skip to content
Snippets Groups Projects
Commit 330b50d7 authored by Henri Hannetel's avatar Henri Hannetel Committed by Jeremy Soller
Browse files

Add UTIME_OMIT and UTIME_NOW

parent 2c7ec0dc
No related branches found
No related tags found
1 merge request!427Add UTIME_OMIT and UTIME_NOW
......@@ -37,6 +37,9 @@ pub const S_ISUID: c_int = 0o4_000;
pub const S_ISGID: c_int = 0o2_000;
pub const S_ISVTX: c_int = 0o1_000;
pub const UTIME_NOW: useconds_t = (1 << 30) - 1;
pub const UTIME_OMIT: useconds_t = (1 << 30) - 2;
#[repr(C)]
#[derive(Default)]
pub struct stat {
......
......@@ -6,8 +6,31 @@
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
int check_mtime(char *path, int expected_sec, int expected_nsec, int err_gap) {
// Checks whether the moditication time of the file located at *path* match the provided times.
// When err_gap is set, only checks for a match on sec with a margin of error of +/- err_gap.
struct stat sb;
if (stat(path, &sb) != 0) {
fprintf(stderr, "stat: %s\n", strerror(errno));
return -1;
}
if (err_gap > 0) {
if (sb.st_mtim.tv_sec < expected_sec + err_gap && sb.st_mtim.tv_sec > expected_sec - err_gap) {
return 0;
}
} else {
if (sb.st_mtim.tv_sec == expected_sec && sb.st_mtim.tv_nsec == expected_nsec) {
return 0;
}
}
fprintf(stderr, "Wrong modified time: %d.%d\n", sb.st_mtim.tv_sec, sb.st_mtim.tv_nsec);
return -1;
}
int main(int argc, char** argv) {
char temp[] = "/tmp/stattest-XXXXXX";
const char file[] = "/mkfifo_fifo";
......@@ -47,21 +70,28 @@ int main(int argc, char** argv) {
fprintf(stderr, "futimens: %s\n", strerror(errno));
exit(1);
}
if (check_mtime(path, 20, 0, 0) != 0) {
exit(1);
}
// Access times are not flushed to disk, so atime checks can't be (currently) performed
struct stat sb;
if (stat(path, &sb) != 0) {
fprintf(stderr, "stat: %s\n", strerror(errno));
const struct timespec omit_times[] = { { 25, UTIME_OMIT }, { 12, UTIME_OMIT } };
if (futimens(fd, omit_times) == -1) {
fprintf(stderr, "futimens: %s\n", strerror(errno));
exit(1);
}
if (check_mtime(path, 20, 0, 0) != 0) {
exit(1);
}
if (sb.st_mtim.tv_sec != 20 || sb.st_mtim.tv_nsec != 0) {
fprintf(stderr, "Wrong modified time: %d.%d\n", sb.st_mtim.tv_sec, sb.st_mtim.tv_nsec);
const struct timespec now_times[] = { { 25, UTIME_NOW }, { 12, UTIME_NOW } };
if (futimens(fd, now_times) == -1) {
fprintf(stderr, "futimens: %s\n", strerror(errno));
exit(1);
}
int now_ts = time(NULL);
if (check_mtime(path, now_ts, 0, 1) != 0) {
exit(1);
}
// Access times are not flushed to disk, so this check can't be (currently) performed
/*
* if (sb.st_atim.tv_sec != 10 || sb.st_atim.tv_nsec != 0) {
* fprintf(stderr, "Wrong accessed time: %d.%d\n", sb.st_atim.tv_sec, sb.st_atim.tv_nsec);
* exit(1);
* }
*/
}
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