Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: checking chalk permissions on self-extract #104

Merged
merged 1 commit into from
Dec 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions src/selfextract.nim
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ proc getSelfExtraction*(): Option[ChalkObj] =
# have a codec for this type of executable, avoid dupe errors.
once:
var
myPath = @[resolvePath(getMyAppPath())]
myPath = resolvePath(getMyAppPath())
cmd = getCommandName()

setCommandName("extract")
Expand All @@ -42,13 +42,44 @@ proc getSelfExtraction*(): Option[ChalkObj] =
# see if the mark is there, and reports whether it's found or not.
# This trace happens here mainly because we easily have the
# resolved path here.
trace("Checking chalk binary '" & myPath[0] & "' for embedded config")
trace("Checking chalk binary '" & myPath & "' for embedded config")

# Codecs by design fail gracefully as self config can be
# encoded using any codec (e.g. elf on linux but shell script on mac).
# Therefore if none of the codecs reads/parses config
# from the chalk binary, chalk will proceed running
# using default configs.
# There are 2 cases however where there are no embedded configs:
# * chalk binary was just compiled (before chalk load)
# * codecs could not read the chalk binary
# due to missing permissions but graceful error handling
# does not raise an exception
# In the case of missing permission, chalk using default
# configs is incorrect behavior therefore we explicitly
# check permission by opening a file stream.
# If that works, world is amazing and llamas have lots
# of lettus to enjoy ;D
# If not, we immediately exit with hopefully useful error message
# :fingerscrossed:
var canOpen = false
try:
let stream = newFileStream(myPath)
if stream != nil:
canOpen = true
stream.close()
except:
dumpExOnDebug()
error(getCurrentExceptionMsg())
if not canOpen:
cantLoad("Chalk is unable to read self-config. " &
"Ensure chalk has both read and execute permissions. " &
"To add permissions run: 'chmod +rx " & myPath & "'\n")

for codec in getAllCodecs():
if hostOS notin codec.nativeObjPlatforms:
continue
let
ai = ArtifactIterationInfo(filePaths: myPath)
ai = ArtifactIterationInfo(filePaths: @[myPath])
chalks = codec.scanArtifactLocations(ai)

if len(chalks) == 0:
Expand Down