Skip to content

Commit

Permalink
remove decades-deprecated Win32 API *A function support (#21315)
Browse files Browse the repository at this point in the history
  • Loading branch information
tersec authored Mar 2, 2023
1 parent 9b5ae2b commit 612abda
Show file tree
Hide file tree
Showing 13 changed files with 179 additions and 420 deletions.
11 changes: 3 additions & 8 deletions lib/pure/asyncfile.nim
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,9 @@ proc openAsync*(filename: string, mode = fmRead): AsyncFile =
let flags = FILE_FLAG_OVERLAPPED or FILE_ATTRIBUTE_NORMAL
let desiredAccess = getDesiredAccess(mode)
let creationDisposition = getCreationDisposition(mode, filename)
when useWinUnicode:
let fd = createFileW(newWideCString(filename), desiredAccess,
FILE_SHARE_READ,
nil, creationDisposition, flags, 0)
else:
let fd = createFileA(filename, desiredAccess,
FILE_SHARE_READ,
nil, creationDisposition, flags, 0)
let fd = createFileW(newWideCString(filename), desiredAccess,
FILE_SHARE_READ,
nil, creationDisposition, flags, 0)

if fd == INVALID_HANDLE_VALUE:
raiseOSError(osLastError())
Expand Down
2 changes: 1 addition & 1 deletion lib/pure/browsers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ when defined(nimPreviewSlimSystem):

when defined(windows):
import winlean
when useWinUnicode and defined(nimPreviewSlimSystem):
when defined(nimPreviewSlimSystem):
import std/widestrs
from os import absolutePath
else:
Expand Down
7 changes: 2 additions & 5 deletions lib/pure/memfiles.nim
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

when defined(windows):
import winlean
when useWinUnicode and defined(nimPreviewSlimSystem):
when defined(nimPreviewSlimSystem):
import std/widestrs
elif defined(posix):
import posix
Expand Down Expand Up @@ -199,10 +199,7 @@ proc open*(filename: string, mode: FileMode = fmRead,
else: FILE_ATTRIBUTE_NORMAL or flags,
0)

when useWinUnicode:
result.fHandle = callCreateFile(createFileW, newWideCString(filename))
else:
result.fHandle = callCreateFile(createFileA, filename)
result.fHandle = callCreateFile(createFileW, newWideCString(filename))

if result.fHandle == INVALID_HANDLE_VALUE:
fail(osLastError(), "error opening file")
Expand Down
93 changes: 29 additions & 64 deletions lib/pure/os.nim
Original file line number Diff line number Diff line change
Expand Up @@ -422,32 +422,18 @@ proc expandFilename*(filename: string): string {.rtl, extern: "nos$1",
## Raises `OSError` in case of an error. Follows symlinks.
when defined(windows):
var bufsize = MAX_PATH.int32
when useWinUnicode:
var unused: WideCString = nil
var res = newWideCString("", bufsize)
while true:
var L = getFullPathNameW(newWideCString(filename), bufsize, res, unused)
if L == 0'i32:
raiseOSError(osLastError(), filename)
elif L > bufsize:
res = newWideCString("", L)
bufsize = L
else:
result = res$L
break
else:
var unused: cstring = nil
result = newString(bufsize)
while true:
var L = getFullPathNameA(filename, bufsize, result, unused)
if L == 0'i32:
raiseOSError(osLastError(), filename)
elif L > bufsize:
result = newString(L)
bufsize = L
else:
setLen(result, L)
break
var unused: WideCString = nil
var res = newWideCString("", bufsize)
while true:
var L = getFullPathNameW(newWideCString(filename), bufsize, res, unused)
if L == 0'i32:
raiseOSError(osLastError(), filename)
elif L > bufsize:
res = newWideCString("", L)
bufsize = L
else:
result = res$L
break
# getFullPathName doesn't do case corrections, so we have to use this convoluted
# way of retrieving the true filename
for x in walkFiles(result):
Expand Down Expand Up @@ -483,14 +469,10 @@ proc createHardlink*(src, dest: string) {.noWeirdTarget.} =
## See also:
## * `createSymlink proc`_
when defined(windows):
when useWinUnicode:
var wSrc = newWideCString(src)
var wDst = newWideCString(dest)
if createHardLinkW(wDst, wSrc, nil) == 0:
raiseOSError(osLastError(), $(src, dest))
else:
if createHardLinkA(dest, src, nil) == 0:
raiseOSError(osLastError(), $(src, dest))
var wSrc = newWideCString(src)
var wDst = newWideCString(dest)
if createHardLinkW(wDst, wSrc, nil) == 0:
raiseOSError(osLastError(), $(src, dest))
else:
if link(src, dest) != 0:
raiseOSError(osLastError(), $(src, dest))
Expand Down Expand Up @@ -655,32 +637,18 @@ proc getAppFilename*(): string {.rtl, extern: "nos$1", tags: [ReadIOEffect], noW
# /proc/<pid>/path/a.out (complete pathname)
when defined(windows):
var bufsize = int32(MAX_PATH)
when useWinUnicode:
var buf = newWideCString("", bufsize)
while true:
var L = getModuleFileNameW(0, buf, bufsize)
if L == 0'i32:
result = "" # error!
break
elif L > bufsize:
buf = newWideCString("", L)
bufsize = L
else:
result = buf$L
break
else:
result = newString(bufsize)
while true:
var L = getModuleFileNameA(0, result, bufsize)
if L == 0'i32:
result = "" # error!
break
elif L > bufsize:
result = newString(L)
bufsize = L
else:
setLen(result, L)
break
var buf = newWideCString("", bufsize)
while true:
var L = getModuleFileNameW(0, buf, bufsize)
if L == 0'i32:
result = "" # error!
break
elif L > bufsize:
buf = newWideCString("", L)
bufsize = L
else:
result = buf$L
break
elif defined(macosx):
var size = cuint32(0)
getExecPath1(nil, size)
Expand Down Expand Up @@ -977,10 +945,7 @@ proc isHidden*(path: string): bool {.noWeirdTarget.} =
assert ".foo/".isHidden

when defined(windows):
when useWinUnicode:
wrapUnary(attributes, getFileAttributesW, path)
else:
var attributes = getFileAttributesA(path)
wrapUnary(attributes, getFileAttributesW, path)
if attributes != -1'i32:
result = (attributes and FILE_ATTRIBUTE_HIDDEN) != 0'i32
else:
Expand Down
35 changes: 12 additions & 23 deletions lib/pure/osproc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -714,22 +714,15 @@ when defined(windows) and not defined(useNimRtl):
if len(workingDir) > 0: wd = workingDir
if env != nil: e = buildEnv(env)
if poEchoCmd in options: echo($cmdl)
when useWinUnicode:
var tmp = newWideCString(cmdl)
var ee =
if e.str.isNil: newWideCString(cstring(nil))
else: newWideCString(e.str, e.len)
var wwd = newWideCString(wd)
var flags = NORMAL_PRIORITY_CLASS or CREATE_UNICODE_ENVIRONMENT
if poDaemon in options: flags = flags or CREATE_NO_WINDOW
success = winlean.createProcessW(nil, tmp, nil, nil, 1, flags,
ee, wwd, si, procInfo)
else:
var ee =
if e.str.isNil: cstring(nil)
else: cstring(e.str)
success = winlean.createProcessA(nil,
cmdl, nil, nil, 1, NORMAL_PRIORITY_CLASS, ee, wd, si, procInfo)
var tmp = newWideCString(cmdl)
var ee =
if e.str.isNil: newWideCString(cstring(nil))
else: newWideCString(e.str, e.len)
var wwd = newWideCString(wd)
var flags = NORMAL_PRIORITY_CLASS or CREATE_UNICODE_ENVIRONMENT
if poDaemon in options: flags = flags or CREATE_NO_WINDOW
success = winlean.createProcessW(nil, tmp, nil, nil, 1, flags,
ee, wwd, si, procInfo)
let lastError = osLastError()

if poParentStreams notin options:
Expand Down Expand Up @@ -870,13 +863,9 @@ when defined(windows) and not defined(useNimRtl):
si.hStdError = getStdHandle(STD_ERROR_HANDLE)
si.hStdInput = getStdHandle(STD_INPUT_HANDLE)
si.hStdOutput = getStdHandle(STD_OUTPUT_HANDLE)
when useWinUnicode:
var c = newWideCString(command)
var res = winlean.createProcessW(nil, c, nil, nil, 0,
NORMAL_PRIORITY_CLASS, nil, nil, si, procInfo)
else:
var res = winlean.createProcessA(nil, command, nil, nil, 0,
NORMAL_PRIORITY_CLASS, nil, nil, si, procInfo)
var c = newWideCString(command)
var res = winlean.createProcessW(nil, c, nil, nil, 0,
NORMAL_PRIORITY_CLASS, nil, nil, si, procInfo)
if res == 0:
raiseOSError(osLastError())
else:
Expand Down
5 changes: 1 addition & 4 deletions lib/std/cmdline.nim
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@ else:
# Needed by windows in order to obtain the command line for targets
# other than command line targets
when defined(windows) and not weirdTarget:
when useWinUnicode:
template getCommandLine*(): untyped = getCommandLineW()
else:
template getCommandLine*(): untyped = getCommandLineA()
template getCommandLine*(): untyped = getCommandLineW()


proc parseCmdLine*(c: string): seq[string] {.
Expand Down
67 changes: 21 additions & 46 deletions lib/std/private/oscommon.nim
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,17 @@ else:


when defined(windows) and not weirdTarget:
when useWinUnicode:
template wrapUnary*(varname, winApiProc, arg: untyped) =
var varname = winApiProc(newWideCString(arg))

template wrapBinary*(varname, winApiProc, arg, arg2: untyped) =
var varname = winApiProc(newWideCString(arg), arg2)
proc findFirstFile*(a: string, b: var WIN32_FIND_DATA): Handle =
result = findFirstFileW(newWideCString(a), b)
template findNextFile*(a, b: untyped): untyped = findNextFileW(a, b)

template getFilename*(f: untyped): untyped =
$cast[WideCString](addr(f.cFileName[0]))
else:
template findFirstFile*(a, b: untyped): untyped = findFirstFileA(a, b)
template findNextFile*(a, b: untyped): untyped = findNextFileA(a, b)
template wrapUnary*(varname, winApiProc, arg: untyped) =
var varname = winApiProc(newWideCString(arg))

template wrapBinary*(varname, winApiProc, arg, arg2: untyped) =
var varname = winApiProc(newWideCString(arg), arg2)
proc findFirstFile*(a: string, b: var WIN32_FIND_DATA): Handle =
result = findFirstFileW(newWideCString(a), b)
template findNextFile*(a, b: untyped): untyped = findNextFileW(a, b)

template getFilename*(f: untyped): untyped = $cast[cstring](addr f.cFileName)
template getFilename*(f: untyped): untyped =
$cast[WideCString](addr(f.cFileName[0]))

proc skipFindData*(f: WIN32_FIND_DATA): bool {.inline.} =
# Note - takes advantage of null delimiter in the cstring
Expand Down Expand Up @@ -104,12 +98,9 @@ proc tryMoveFSObject*(source, dest: string, isDir: bool): bool {.noWeirdTarget.}
## In case of other errors `OSError` is raised.
## Returns true in case of success.
when defined(windows):
when useWinUnicode:
let s = newWideCString(source)
let d = newWideCString(dest)
result = moveFileExW(s, d, MOVEFILE_COPY_ALLOWED or MOVEFILE_REPLACE_EXISTING) != 0'i32
else:
result = moveFileExA(source, dest, MOVEFILE_COPY_ALLOWED or MOVEFILE_REPLACE_EXISTING) != 0'i32
let s = newWideCString(source)
let d = newWideCString(dest)
result = moveFileExW(s, d, MOVEFILE_COPY_ALLOWED or MOVEFILE_REPLACE_EXISTING) != 0'i32
else:
result = c_rename(source, dest) == 0'i32

Expand Down Expand Up @@ -137,10 +128,7 @@ proc fileExists*(filename: string): bool {.rtl, extern: "nos$1",
## * `dirExists proc`_
## * `symlinkExists proc`_
when defined(windows):
when useWinUnicode:
wrapUnary(a, getFileAttributesW, filename)
else:
var a = getFileAttributesA(filename)
wrapUnary(a, getFileAttributesW, filename)
if a != -1'i32:
result = (a and FILE_ATTRIBUTE_DIRECTORY) == 0'i32
else:
Expand All @@ -157,10 +145,7 @@ proc dirExists*(dir: string): bool {.rtl, extern: "nos$1", tags: [ReadDirEffect]
## * `fileExists proc`_
## * `symlinkExists proc`_
when defined(windows):
when useWinUnicode:
wrapUnary(a, getFileAttributesW, dir)
else:
var a = getFileAttributesA(dir)
wrapUnary(a, getFileAttributesW, dir)
if a != -1'i32:
result = (a and FILE_ATTRIBUTE_DIRECTORY) != 0'i32
else:
Expand All @@ -178,10 +163,7 @@ proc symlinkExists*(link: string): bool {.rtl, extern: "nos$1",
## * `fileExists proc`_
## * `dirExists proc`_
when defined(windows):
when useWinUnicode:
wrapUnary(a, getFileAttributesW, link)
else:
var a = getFileAttributesA(link)
wrapUnary(a, getFileAttributesW, link)
if a != -1'i32:
# xxx see: bug #16784 (bug9); checking `IO_REPARSE_TAG_SYMLINK`
# may also be needed.
Expand All @@ -197,15 +179,8 @@ when defined(windows) and not weirdTarget:
flags = flags or FILE_FLAG_OPEN_REPARSE_POINT
let access = if writeAccess: GENERIC_WRITE else: 0'i32

when useWinUnicode:
result = createFileW(
newWideCString(path), access,
FILE_SHARE_DELETE or FILE_SHARE_READ or FILE_SHARE_WRITE,
nil, OPEN_EXISTING, flags, 0
)
else:
result = createFileA(
path, access,
FILE_SHARE_DELETE or FILE_SHARE_READ or FILE_SHARE_WRITE,
nil, OPEN_EXISTING, flags, 0
)
result = createFileW(
newWideCString(path), access,
FILE_SHARE_DELETE or FILE_SHARE_READ or FILE_SHARE_WRITE,
nil, OPEN_EXISTING, flags, 0
)
17 changes: 4 additions & 13 deletions lib/std/private/osdirs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -328,10 +328,7 @@ iterator walkDirRec*(dir: string,

proc rawRemoveDir(dir: string) {.noWeirdTarget.} =
when defined(windows):
when useWinUnicode:
wrapUnary(res, removeDirectoryW, dir)
else:
var res = removeDirectoryA(dir)
wrapUnary(res, removeDirectoryW, dir)
let lastError = osLastError()
if res == 0'i32 and lastError.int32 != 3'i32 and
lastError.int32 != 18'i32 and lastError.int32 != 2'i32:
Expand Down Expand Up @@ -396,10 +393,7 @@ proc rawCreateDir(dir: string): bool {.noWeirdTarget.} =
#echo res
raiseOSError(osLastError(), dir)
else:
when useWinUnicode:
wrapUnary(res, createDirectoryW, dir)
else:
let res = createDirectoryA(dir)
wrapUnary(res, createDirectoryW, dir)

if res != 0'i32:
result = true
Expand Down Expand Up @@ -561,10 +555,7 @@ proc setCurrentDir*(newDir: string) {.inline, tags: [], noWeirdTarget.} =
## * `getTempDir proc`_
## * `getCurrentDir proc`_
when defined(windows):
when useWinUnicode:
if setCurrentDirectoryW(newWideCString(newDir)) == 0'i32:
raiseOSError(osLastError(), newDir)
else:
if setCurrentDirectoryA(newDir) == 0'i32: raiseOSError(osLastError(), newDir)
if setCurrentDirectoryW(newWideCString(newDir)) == 0'i32:
raiseOSError(osLastError(), newDir)
else:
if chdir(newDir) != 0'i32: raiseOSError(osLastError(), newDir)
Loading

0 comments on commit 612abda

Please sign in to comment.