haskell / file-io

File IO (read/write/open) for OsPath API
11 stars 4 forks source link

readFile creates new files on Windows #14

Closed Bodigrim closed 9 months ago

Bodigrim commented 9 months ago

Win 10, GHC 9.6.3:

$ cabal repl -b file-io 
ghci> :set -XQuasiQuotes
ghci> System.File.OsPath.readFile [System.OsPath.osp|does-not-exist|]
""

What's worse is that readFile also creates an empty does-not-exist file.

hasufell commented 9 months ago

Ouch.

We're using the Win32 API proper, which is a bit peculiar.

I'll look at it this weekend.

hasufell commented 9 months ago

From a quick glance it seems I was tricked by the posix functions behaving in a certain way:

The solution seems to be to use openExistingFile for all read operations. On posix it doesn't matter, on windows it does.

hasufell commented 9 months ago

Or we just change this

https://github.com/hasufell/file-io/blob/9b1cc99165ae1cd9d4845e25b208cb2bc478b9ac/windows/System/File/Platform.hs#L48

to Win32.oPEN_EXISTING.

hasufell commented 9 months ago

This is fixed here: https://github.com/hasufell/file-io/pull/16/commits/9436ee8bf36ee93297ab810172c1f87ee0f55def