Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

open(f: var File, filehandle: FileHandle) does not work on Windows #85

Open
Clonkk opened this issue Oct 26, 2020 · 3 comments
Open

open(f: var File, filehandle: FileHandle) does not work on Windows #85

Clonkk opened this issue Oct 26, 2020 · 3 comments

Comments

@Clonkk
Copy link

Clonkk commented Oct 26, 2020

Opening a File from a FileHandle using

proc open*(f: var File, filehandle: FileHandle,
           mode: FileMode = fmRead): bool {.tags: [], raises: [], benign.}

does not work on Windows but does on Linux.

Example

  import std/ioutils

  let file1 = open("dummy.txt", mode=fmWrite)
  let dupfile1fd = duplicate(file1.getFileHandle())
  var f : File
  doAssert(isNil(f))
  let res = open(f, dupfile1fd, mode=fmWrite)
  # Both fails
  doAssert(not isNil(f)) # f == nil
  doAssert(res) # res == false
  let msg = "This is a test message that will be displayed ! \n"
  f.write(msg)
  f.close()

Current Output

Error: unhandled exception: test.nim(9, 9) `not isNil(f)`  [AssertionDefect]
Error: execution of an external program failed'

Expected Output

This is a test message that will be displayed ! 

Additional Information

Works on Linux

On branch devel commit ea6c28249ae7107fba954f90dac31132564dcaad

@timotheecour
Copy link
Member

  • doAssert(res) # res == -1
    => you mean res == false

  • can you check whether it returns false directly here:

  when not defined(nimInheritHandles) and declared(setInheritable):
    let oshandle = when defined(windows): FileHandle getOsfhandle(filehandle) else: filehandle
    if not setInheritable(oshandle, false):
      return false
  • f = c_fdopen(filehandle, FormatOpen[mode])
    if this fails, you need to check errno (and strerror) => what are the values here?

@Clonkk
Copy link
Author

Clonkk commented Oct 27, 2020

Modified tests to include errno :

var errno {.importc, header: "<errno.h>".}: cint ## error variable
proc strerror(errnum: cint): cstring {.importc, header: "<string.h>".}

proc proc0()=
  let file1 = open("dummy.txt", mode=fmWrite)
  let dupfile1fd = duplicate(file1.getFileHandle())

  var f : File
  doAssert(isNil(f))
  let res = open(f, dupfile1fd, mode=fmWrite)
  let msg = "errno: " & $errno & " `" & $strerror(errno) & "`"

  doAssert(not isNil(f))
  doAssert(res)

  write(f, msg)
  close(f)

proc0()

Output
errno: 22 `Invalid argument.

I tried using getOsFileHandle instead of getFileHandle as well

let dupfile1fd = duplicate(file1.getOsFileHandle())

Output:

Error: unhandled exception: Bad file descriptor [IOError]

@ringabout
Copy link
Member

Since ioutils have moved to fusion, could the examples posted be changed? Or transfer this issue to fusion repo.

@ringabout ringabout transferred this issue from nim-lang/Nim Mar 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants