Skip to content

Commit

Permalink
Fix handling of symbolic links on Linux
Browse files Browse the repository at this point in the history
When a file is a symbolic link to some other file the proper behavior
when calculating the checksum of a package is to use the link path
instead of the content of the pointed file.

Additionally the `getPackageFileListWithoutVcs` procedure is fixed to
return also the symbolic link files.

Related to nim-lang#127
  • Loading branch information
bobeff authored and CyberTailor committed Dec 12, 2021
1 parent 8c783ca commit 928fe89
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
46 changes: 30 additions & 16 deletions src/nimblepkg/checksums.nim
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,36 @@ proc updateSha1Checksum(checksum: var Sha1State, fileName, filePath: string) =
# directory from which no files are being installed.
return
checksum.update(fileName)
var file: File
try:
file = filePath.open(fmRead)
except IOError:
## If the file cannot be open for reading do not count its content in the
## checksum.
displayWarning(&"The file \"{filePath}\" cannot be open for reading.\n" &
"Skipping it in the calculation of the checksum.")
return
defer: close(file)
const bufferSize = 8192
var buffer = newString(bufferSize)
while true:
var bytesRead = readChars(file, buffer)
if bytesRead == 0: break
checksum.update(buffer.toOpenArray(0, bytesRead - 1))
if symlinkExists(filePath):
# Check whether a file is a symbolic link and if so update the checksum with
# the path to the file that the link points to.
var linkPath: string
try:
linkPath = expandSymlink(filePath)
except OSError:
displayWarning(&"Cannot expand symbolic link \"{filePath}\".\n" &
"Skipping it in the calculation of the checksum.")
return
checksum.update(linkPath)
else:
# Otherwise this is an ordinary file and we are adding its content to the
# checksum.
var file: File
try:
file = filePath.open(fmRead)
except IOError:
## If the file cannot be open for reading do not count its content in the
## checksum.
displayWarning(&"The file \"{filePath}\" cannot be open for reading.\n" &
"Skipping it in the calculation of the checksum.")
return
defer: close(file)
const bufferSize = 8192
var buffer = newString(bufferSize)
while true:
var bytesRead = readChars(file, buffer)
if bytesRead == 0: break
checksum.update(buffer.toOpenArray(0, bytesRead - 1))

proc calculateDirSha1Checksum*(dir: string): Sha1Hash =
## Recursively calculates the sha1 checksum of the contents of the directory
Expand Down
3 changes: 2 additions & 1 deletion src/nimblepkg/vcstools.nim
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ proc getVcsRevision*(dir: Path): Sha1Hash =
proc getPackageFileListWithoutVcs(dir: Path): seq[string] =
## Recursively walks the directory `dir` and returns a list of files in it and
## its subdirectories.
for file in walkDirRec($dir, relative = true):
for file in walkDirRec($dir, yieldFilter = {pcFile, pcLinkToFile},
relative = true):
result.add file

proc getPackageFileList*(dir: Path): seq[string] =
Expand Down

0 comments on commit 928fe89

Please sign in to comment.