Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine if file can be deleted on windows in go

Tags:

windows

go

Is there any reasonable way to determine if a file can be successfully deleted on the Windows platform using the go language? I've tried using various flags calling os.OpenFile including using os.O_TRUNC all the calls are successful yet remove will fail when the file is in use. I've also tried using the locking mechanisms specific to Windows (i.e. kern.NewProc("LockFileEx") etc) and these seem to get exclusive locks successfully yet os.Remove (after unlocking) will fail. I realize that if between the test and the remove call if another process opens the file delete will still fail but this isn't the case here, with my tests I know the file is in use yet I can't find a test to tell me that (other than actually calling os.Remove)

like image 695
mikedoy Avatar asked Oct 17 '22 21:10

mikedoy


1 Answers

On Windows, the Go os.Remove function uses the Windows DeleteFile function. You must satisfy certain conditions for a Windows file to be deleted. For example, "The DeleteFile function marks a file for deletion on close. Therefore, the file deletion does not occur until the last handle to the file is closed", "The DeleteFile function fails if an application attempts to delete a file that has other handles open for normal I/O", and so on.

like image 124
peterSO Avatar answered Oct 20 '22 06:10

peterSO