Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions nob.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@
# include <sys/wait.h>
# include <sys/stat.h>
# include <unistd.h>
# include <utime.h>
# include <fcntl.h>
#endif

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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;
Expand Down
Loading