code: drawterm

Download patch

ref: 0f3766f44e554d73c7a3d8bab55cc32dc13f1c26
parent: 333472c8c87c11eefb62cb1d5eab4472e5a8d2a9
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Sat Feb 25 18:03:59 EST 2017

implement setting atime and mtime in fswstat for unix and windows

--- a/kern/devfs-posix.c
+++ b/kern/devfs-posix.c
@@ -1,5 +1,6 @@
 #include	"u.h"
 #include	<sys/types.h>
+#include	<sys/time.h>
 #include	<sys/stat.h>
 #include	<dirent.h>
 #include	<fcntl.h>
@@ -406,7 +407,14 @@
 		uif->mode &= ~0777;
 		uif->mode |= d.mode&0777;
 	}
-
+	if(~d.atime != 0 || ~d.mtime != 0){
+		struct timeval t[2];
+		t[0].tv_sec = ~d.atime != 0 ? d.atime : stbuf->st_atime;
+		t[0].tv_usec = 0;
+		t[1].tv_sec = ~d.mtime != 0 ? d.mtime : stbuf->st_mtime;
+		t[1].tv_usec = 0;
+		utimes(uif->path, t);
+	}
 	if(d.name[0] && strcmp(d.name, lastelem(uif->path)) != 0) {
 		char *base, *newpath;
 
--- a/kern/devfs-win32.c
+++ b/kern/devfs-win32.c
@@ -61,6 +61,17 @@
 	return ((t<0)?(-1 - (-t - 1)) : t)/10000000;
 }
 
+static FILETIME
+filetime(ulong ut)
+{
+	FILETIME ft;
+	vlong t = (vlong)ut * 10000000LL;
+	t += 116444736000000000LL;
+	ft.dwLowDateTime = t;
+	ft.dwHighDateTime = t >> 32;
+	return ft;
+}
+
 static uvlong
 pathhash(wchar_t *p)
 {
@@ -578,6 +589,29 @@
 	uif = c->aux;
 	if(pathtype(uif->path) != TPATH_FILE)
 		error(Ebadstat);
+	if(~d.atime != 0 || ~d.mtime != 0){
+		FILETIME ta, *pta = NULL;
+		FILETIME tm, *ptm = NULL;
+		HANDLE h;
+		if(~d.atime != 0){
+			ta = filetime(d.atime);
+			pta = &ta;
+		}
+		if(~d.mtime != 0){
+			tm = filetime(d.mtime);
+			ptm = &tm;
+		}
+		if((h = CreateFile(uif->path,
+			FILE_WRITE_ATTRIBUTES,
+			FILE_SHARE_READ | FILE_SHARE_WRITE,
+			NULL,
+			OPEN_EXISTING,
+			FILE_ATTRIBUTE_NORMAL,
+			0)) != INVALID_HANDLE_VALUE){
+			SetFileTime(h, NULL, pta, ptm);
+			CloseHandle(h);
+		}
+	}
 	/* change name */
 	if(d.name[0]){
 		wchar_t *base, *newpath;