coreutils icon indicating copy to clipboard operation
coreutils copied to clipboard

Touch Implementation

Open mike-ward opened this issue 1 year ago • 6 comments

Touch utility in V.

dates and times are iso8601. The original date parser in GNU is yacc'd and GPL'ed.

mike-ward avatar May 17 '24 15:05 mike-ward

touch.v should be touch.c.v, since it has C. code in it.

JalonSolov avatar May 17 '24 16:05 JalonSolov

renamed touch.v to touch.c.v

mike-ward avatar May 17 '24 17:05 mike-ward

You may need to pull & rebase. I took care of a lot of the "noise" from the CI (deprecated functions needed updates, etc.).

JalonSolov avatar May 17 '24 20:05 JalonSolov

Also looks like you'll need to run v fmt -w touch.c.v.

JalonSolov avatar May 17 '24 20:05 JalonSolov

I think that it needs only a windows implementation of fn lutime, and it will be good to merge.

spytheman avatar May 19 '24 00:05 spytheman

Looks like the Windows equivalent is https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-setfiletime

JalonSolov avatar May 19 '24 00:05 JalonSolov

Windows C programming is bit out of my wheelhouse. Maybe something like this?

#include <windows.h>

const generic_write = u32(0x40000000)
const open_existing = u32(0x00000003)
const file_attribute_normal = u32(0x00000080)
const null = unsafe { nil }

fn C.CreateFile(&char, u32, u32, voidptr, u32, u32, u32) u32
fn C.SetFileTime(u32, voidptr, voidptr, voidptr) bool

fn lutime(path string, acctime int, modtime int) ! {
	f_handle := C.CreateFile(&char(path.str), generic_write, null, open_existing, file_attribute_normal,
		u32(0))

	if f_handle == 0 {
		error('invalid file handle')
	}

	if !C.SetFileTime(f_handle, null, u64(acctime), u64(modtime)) {
		error('unable to set file times')
	}
}

mike-ward avatar May 20 '24 23:05 mike-ward

We have progress 🥳 : image

spytheman avatar May 21 '24 16:05 spytheman

I need to see what does the gnu coreutils tool do on windows.

spytheman avatar May 21 '24 16:05 spytheman

The test pass now on the CI, and locally too, when my timezone is set to UTC.

When it is not, the modification times differ:

Not all file systems can record creation and last access time and not all file systems record them in the same manner. For example, on NT FAT, create time has a resolution of 10 milliseconds, write time has a resolution of 2 seconds, and access time has a resolution of 1 day (really, the access date). On NTFS, access time has a resolution of 1 hour. Therefore, the GetFileTime function may not return the same file time information set using the SetFileTime function. Furthermore, FAT records times on disk in local time. However, NTFS records times on disk in UTC. For more information, see File Times.

I think that is a minor problem, and can be fixed later.

Excellent work @mike-ward . @JalonSolov thanks for the help.

spytheman avatar May 21 '24 17:05 spytheman

Now that I've gone through a pull request on this project, I know what to do so you guys won't have to fiddle as much with the code.

Thanks for your patience. Working on fmt now.

mike-ward avatar May 21 '24 17:05 mike-ward