From 7ccc37f2ae012a0689de87647f042bca5a76b7a7 Mon Sep 17 00:00:00 2001 From: Mikko Marttila <13412395+mikmart@users.noreply.github.com> Date: Sat, 23 Aug 2025 11:50:42 +0100 Subject: [PATCH 1/2] Introduce `nob_touch_file()` --- nob.h | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/nob.h b/nob.h index 737f61fa..6bbaac39 100644 --- a/nob.h +++ b/nob.h @@ -234,6 +234,7 @@ NOBDEF bool nob_read_entire_dir(const char *parent, Nob_File_Paths *children); NOBDEF bool nob_write_entire_file(const char *path, const void *data, size_t size); NOBDEF Nob_File_Type nob_get_file_type(const char *path); NOBDEF bool nob_delete_file(const char *path); +NOBDEF bool nob_touch_file(const char *path); #define nob_return_defer(value) do { result = (value); goto defer; } while(0) @@ -1644,6 +1645,47 @@ NOBDEF bool nob_delete_file(const char *path) #endif // _WIN32 } +NOBDEF bool nob_touch_file(const char *path) +{ + nob_log(NOB_INFO, "touching %s", path); +#ifdef _WIN32 + HANDLE FileHandle = CreateFile(path, FILE_WRITE_ATTRIBUTES, 0, 0, OPEN_ALWAYS, 0, 0); + if (FileHandle == INVALID_HANDLE_VALUE) { + nob_log(NOB_ERROR, "Could not touch file %s: %s", path, nob_win32_error_message(GetLastError())); + return false; + } + + FILETIME Now; + GetSystemTimeAsFileTime(&Now); + + if (!SetFileTime(FileHandle, NULL, NULL, &Now)) { + nob_log(NOB_ERROR, "Could not touch file %s: %s", path, nob_win32_error_message(GetLastError())); + CloseHandle(FileHandle); + return false; + } + + CloseHandle(FileHandle); + return true; +#else + if (utime(path, NULL) == -1) { + if (errno != ENOENT) { + nob_log(NOB_ERROR, "Could not touch file %s: %s", path, strerror(errno)); + return false; + } else { + int fd = creat(path); + if (fd == -1) { + nob_log(NOB_ERROR, "Could not touch file %s: %s", path, strerror(errno)); + return false; + } else { + close(fd); + return true; + } + } + } + return true; +#endif // _WIN32 +} + NOBDEF bool nob_copy_directory_recursively(const char *src_path, const char *dst_path) { bool result = true; From 2eb370c671ece3a0eddc86d7463353f632ac4388 Mon Sep 17 00:00:00 2001 From: Mikko Marttila <13412395+mikmart@users.noreply.github.com> Date: Sat, 23 Aug 2025 12:03:31 +0100 Subject: [PATCH 2/2] Fix nob_touch_file() on POSIX --- nob.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/nob.h b/nob.h index 6bbaac39..c2b86404 100644 --- a/nob.h +++ b/nob.h @@ -164,6 +164,7 @@ # include # include # include +# include # include #endif @@ -1672,7 +1673,7 @@ NOBDEF bool nob_touch_file(const char *path) nob_log(NOB_ERROR, "Could not touch file %s: %s", path, strerror(errno)); return false; } else { - int fd = creat(path); + int fd = creat(path, 0644); if (fd == -1) { nob_log(NOB_ERROR, "Could not touch file %s: %s", path, strerror(errno)); return false;