Skip to content

Commit 9e971a1

Browse files
committed
Fix handling of symbolic links on Linux
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 #127
1 parent 7957048 commit 9e971a1

File tree

2 files changed

+32
-17
lines changed

2 files changed

+32
-17
lines changed

src/nimblepkg/checksums.nim

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,36 @@ proc updateSha1Checksum(checksum: var Sha1State, fileName, filePath: string) =
2525
# directory from which no files are being installed.
2626
return
2727
checksum.update(fileName)
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
37-
defer: close(file)
38-
const bufferSize = 8192
39-
var buffer = newString(bufferSize)
40-
while true:
41-
var bytesRead = readChars(file, buffer)
42-
if bytesRead == 0: break
43-
checksum.update(buffer.toOpenArray(0, bytesRead - 1))
28+
if symlinkExists(filePath):
29+
# Check whether a file is a symbolic link and if so update the checksum with
30+
# the path to the file that the link points to.
31+
var linkPath: string
32+
try:
33+
linkPath = expandSymlink(filePath)
34+
except OSError:
35+
displayWarning(&"Cannot expand symbolic link \"{filePath}\".\n" &
36+
"Skipping it in the calculation of the checksum.")
37+
return
38+
checksum.update(linkPath)
39+
else:
40+
# Otherwise this is an ordinary file and we are adding its content to the
41+
# checksum.
42+
var file: File
43+
try:
44+
file = filePath.open(fmRead)
45+
except IOError:
46+
## If the file cannot be open for reading do not count its content in the
47+
## checksum.
48+
displayWarning(&"The file \"{filePath}\" cannot be open for reading.\n" &
49+
"Skipping it in the calculation of the checksum.")
50+
return
51+
defer: close(file)
52+
const bufferSize = 8192
53+
var buffer = newString(bufferSize)
54+
while true:
55+
var bytesRead = readChars(file, buffer)
56+
if bytesRead == 0: break
57+
checksum.update(buffer.toOpenArray(0, bytesRead - 1))
4458

4559
proc calculateDirSha1Checksum*(dir: string): Sha1Hash =
4660
## Recursively calculates the sha1 checksum of the contents of the directory

src/nimblepkg/vcstools.nim

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,8 @@ proc getVcsRevision*(dir: Path): Sha1Hash =
203203
proc getPackageFileListWithoutVcs(dir: Path): seq[string] =
204204
## Recursively walks the directory `dir` and returns a list of files in it and
205205
## its subdirectories.
206-
for file in walkDirRec($dir, relative = true):
206+
for file in walkDirRec($dir, yieldFilter = {pcFile, pcLinkToFile},
207+
relative = true):
207208
result.add file
208209

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

0 commit comments

Comments
 (0)