timestomping using utimensat()

0001-01-01

utimensat() is a POSIX function that is able to change a file’s timestamps with nanosecond precision. This can be used to timestomp files.

#define _POSIX_C_SOURCE 200809L
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <stdio.h>
#include <time.h>

int main() {
    const char *filename = "example.txt";

    struct timespec times[2];

    times[0].tv_sec = 1672531200; // Jan 1, 2023, 00:00:00 UTC
    times[0].tv_nsec = 0;         // No nanosecond precision

    times[1].tv_sec = 1672534800; // Jan 1, 2023, 01:00:00 UTC
    times[1].tv_nsec = 0;

    if (utimensat(AT_FDCWD, filename, times, 0) == -1) {
	perror("utimensat");
	return 1;
    }

    printf("Timestamps updated successfully.\n");
    return 0;
}

No notes link to this note