Skip to content

Commit 8c783ca

Browse files
bobeffCyberTailor
authored andcommitted
Fix a crash when a package file cannot be open
Sometimes when downloading a package some symbolic links files cannot be open on Windows when Nimble is run as administrator. For this reason, add exception handling to the `open` procedure to avoid the crash and display a warning message that the file content will not be count in the calculation of the package's checksum. Related to nim-lang#127
1 parent a125d3d commit 8c783ca

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/nimblepkg/checksums.nim

+10-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# BSD License. Look at license.txt for more info.
33

44
import os, std/sha1, strformat, algorithm
5-
import common, version, sha1hashes, vcstools, paths
5+
import common, version, sha1hashes, vcstools, paths, cli
66

77
type
88
ChecksumError* = object of NimbleError
@@ -25,7 +25,15 @@ proc updateSha1Checksum(checksum: var Sha1State, fileName, filePath: string) =
2525
# directory from which no files are being installed.
2626
return
2727
checksum.update(fileName)
28-
let file = filePath.open(fmRead)
28+
var file: File
29+
try:
30+
file = filePath.open(fmRead)
31+
except IOError:
32+
## If the file cannot be open for reading do not count its content in the
33+
## checksum.
34+
displayWarning(&"The file \"{filePath}\" cannot be open for reading.\n" &
35+
"Skipping it in the calculation of the checksum.")
36+
return
2937
defer: close(file)
3038
const bufferSize = 8192
3139
var buffer = newString(bufferSize)

0 commit comments

Comments
 (0)