remove decades-deprecated Win32 API *A function support (#21315)
This commit is contained in:
parent
9b5ae2b2eb
commit
612abda4f4
13 changed files with 173 additions and 414 deletions
|
|
@ -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 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 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 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
|
||||
|
|
@ -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
|
||||
|
||||
|
|
@ -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:
|
||||
|
|
@ -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:
|
||||
|
|
@ -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.
|
||||
|
|
@ -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
|
||||
)
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
|
|
@ -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
|
||||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -84,10 +84,7 @@ proc getFilePermissions*(filename: string): set[FilePermission] {.
|
|||
if (a.st_mode and S_IWOTH.Mode) != 0.Mode: result.incl(fpOthersWrite)
|
||||
if (a.st_mode and S_IXOTH.Mode) != 0.Mode: result.incl(fpOthersExec)
|
||||
else:
|
||||
when useWinUnicode:
|
||||
wrapUnary(res, getFileAttributesW, filename)
|
||||
else:
|
||||
var res = getFileAttributesA(filename)
|
||||
wrapUnary(res, getFileAttributesW, filename)
|
||||
if res == -1'i32: raiseOSError(osLastError(), filename)
|
||||
if (res and FILE_ATTRIBUTE_READONLY) != 0'i32:
|
||||
result = {fpUserExec, fpUserRead, fpGroupExec, fpGroupRead,
|
||||
|
|
@ -136,19 +133,13 @@ proc setFilePermissions*(filename: string, permissions: set[FilePermission],
|
|||
if chmod(filename, cast[Mode](p)) != 0:
|
||||
raiseOSError(osLastError(), $(filename, permissions))
|
||||
else:
|
||||
when useWinUnicode:
|
||||
wrapUnary(res, getFileAttributesW, filename)
|
||||
else:
|
||||
var res = getFileAttributesA(filename)
|
||||
wrapUnary(res, getFileAttributesW, filename)
|
||||
if res == -1'i32: raiseOSError(osLastError(), filename)
|
||||
if fpUserWrite in permissions:
|
||||
res = res and not FILE_ATTRIBUTE_READONLY
|
||||
else:
|
||||
res = res or FILE_ATTRIBUTE_READONLY
|
||||
when useWinUnicode:
|
||||
wrapBinary(res2, setFileAttributesW, filename, res)
|
||||
else:
|
||||
var res2 = setFileAttributesA(filename, res)
|
||||
wrapBinary(res2, setFileAttributesW, filename, res)
|
||||
if res2 == - 1'i32: raiseOSError(osLastError(), $(filename, permissions))
|
||||
|
||||
|
||||
|
|
@ -221,14 +212,10 @@ proc copyFile*(source, dest: string, options = {cfSymlinkFollow}) {.rtl,
|
|||
if isSymlink and (cfSymlinkIgnore in options or defined(windows)):
|
||||
return
|
||||
when defined(windows):
|
||||
when useWinUnicode:
|
||||
let s = newWideCString(source)
|
||||
let d = newWideCString(dest)
|
||||
if copyFileW(s, d, 0'i32) == 0'i32:
|
||||
raiseOSError(osLastError(), $(source, dest))
|
||||
else:
|
||||
if copyFileA(source, dest, 0'i32) == 0'i32:
|
||||
raiseOSError(osLastError(), $(source, dest))
|
||||
let s = newWideCString(source)
|
||||
let d = newWideCString(dest)
|
||||
if copyFileW(s, d, 0'i32) == 0'i32:
|
||||
raiseOSError(osLastError(), $(source, dest))
|
||||
else:
|
||||
if isSymlink and cfSymlinkAsIs in options:
|
||||
createSymlink(expandSymlink(source), dest)
|
||||
|
|
@ -334,14 +321,9 @@ when not declared(ENOENT) and not defined(windows):
|
|||
var ENOENT {.importc, header: "<errno.h>".}: cint
|
||||
|
||||
when defined(windows) and not weirdTarget:
|
||||
when useWinUnicode:
|
||||
template deleteFile(file: untyped): untyped = deleteFileW(file)
|
||||
template setFileAttributes(file, attrs: untyped): untyped =
|
||||
setFileAttributesW(file, attrs)
|
||||
else:
|
||||
template deleteFile(file: untyped): untyped = deleteFileA(file)
|
||||
template setFileAttributes(file, attrs: untyped): untyped =
|
||||
setFileAttributesA(file, attrs)
|
||||
template deleteFile(file: untyped): untyped = deleteFileW(file)
|
||||
template setFileAttributes(file, attrs: untyped): untyped =
|
||||
setFileAttributesW(file, attrs)
|
||||
|
||||
proc tryRemoveFile*(file: string): bool {.rtl, extern: "nos$1", tags: [WriteDirEffect], noWeirdTarget.} =
|
||||
## Removes the `file`.
|
||||
|
|
@ -358,10 +340,7 @@ proc tryRemoveFile*(file: string): bool {.rtl, extern: "nos$1", tags: [WriteDirE
|
|||
## * `moveFile proc`_
|
||||
result = true
|
||||
when defined(windows):
|
||||
when useWinUnicode:
|
||||
let f = newWideCString(file)
|
||||
else:
|
||||
let f = file
|
||||
let f = newWideCString(file)
|
||||
if deleteFile(f) == 0:
|
||||
result = false
|
||||
let err = getLastError()
|
||||
|
|
|
|||
|
|
@ -849,30 +849,17 @@ when not defined(nimscript):
|
|||
doAssert false, "use -d:nodejs to have `getCurrentDir` defined"
|
||||
elif defined(windows):
|
||||
var bufsize = MAX_PATH.int32
|
||||
when useWinUnicode:
|
||||
var res = newWideCString("", bufsize)
|
||||
while true:
|
||||
var L = getCurrentDirectoryW(bufsize, res)
|
||||
if L == 0'i32:
|
||||
raiseOSError(osLastError())
|
||||
elif L > bufsize:
|
||||
res = newWideCString("", L)
|
||||
bufsize = L
|
||||
else:
|
||||
result = res$L
|
||||
break
|
||||
else:
|
||||
result = newString(bufsize)
|
||||
while true:
|
||||
var L = getCurrentDirectoryA(bufsize, result)
|
||||
if L == 0'i32:
|
||||
raiseOSError(osLastError())
|
||||
elif L > bufsize:
|
||||
result = newString(L)
|
||||
bufsize = L
|
||||
else:
|
||||
setLen(result, L)
|
||||
break
|
||||
var res = newWideCString("", bufsize)
|
||||
while true:
|
||||
var L = getCurrentDirectoryW(bufsize, res)
|
||||
if L == 0'i32:
|
||||
raiseOSError(osLastError())
|
||||
elif L > bufsize:
|
||||
res = newWideCString("", L)
|
||||
bufsize = L
|
||||
else:
|
||||
result = res$L
|
||||
break
|
||||
else:
|
||||
var bufsize = 1024 # should be enough
|
||||
result = newString(bufsize)
|
||||
|
|
|
|||
|
|
@ -48,14 +48,10 @@ proc createSymlink*(src, dest: string) {.noWeirdTarget.} =
|
|||
const SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE = 2
|
||||
# allows anyone with developer mode on to create a link
|
||||
let flag = dirExists(src).int32 or SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE
|
||||
when useWinUnicode:
|
||||
var wSrc = newWideCString(src)
|
||||
var wDst = newWideCString(dest)
|
||||
if createSymbolicLinkW(wDst, wSrc, flag) == 0 or getLastError() != 0:
|
||||
raiseOSError(osLastError(), $(src, dest))
|
||||
else:
|
||||
if createSymbolicLinkA(dest, src, flag) == 0 or getLastError() != 0:
|
||||
raiseOSError(osLastError(), $(src, dest))
|
||||
var wSrc = newWideCString(src)
|
||||
var wDst = newWideCString(dest)
|
||||
if createSymbolicLinkW(wDst, wSrc, flag) == 0 or getLastError() != 0:
|
||||
raiseOSError(osLastError(), $(src, dest))
|
||||
else:
|
||||
if symlink(src, dest) != 0:
|
||||
raiseOSError(osLastError(), $(src, dest))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue