diff --git a/nob.h b/nob.h index 737f61f..c2b8640 100644 --- a/nob.h +++ b/nob.h @@ -164,6 +164,7 @@ # include # include # include +# include # include #endif @@ -234,6 +235,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 +1646,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, 0644); + 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;