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

Add getLastErrno and use it where appropriate #41

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
23 changes: 19 additions & 4 deletions libssh2/src/Network/SSH/Client/LibSSH2/Errors.chs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module Network.SSH.Client.LibSSH2.Errors
IntResult (..),

-- * Functions
getLastErrno,
getLastError,
getLastSftpError,
handleInt,
Expand Down Expand Up @@ -94,6 +95,13 @@ data ErrorCode =

instance Exception ErrorCode

data SessionError = SessionError {
code :: ErrorCode,
msg :: String }
deriving (Show)

instance Exception SessionError

error2int :: (Num i) => ErrorCode -> i
error2int = fromIntegral . negate . fromEnum

Expand Down Expand Up @@ -131,15 +139,20 @@ instance IntResult CLong where
instance IntResult CLLong where
intResult = fromIntegral

{# fun session_last_errno as getLastErrno
{ toPointer `Session' } -> `Int' #}

{# fun session_last_error as getLastError_
{ toPointer `Session',
alloca- `String' peekCStringPtr*,
castPtr `Ptr Int',
`Int' } -> `Int' #}

-- | Get last error information.
getLastError :: Session -> IO (Int, String)
getLastError s = getLastError_ s nullPtr 0
getLastError :: Session -> IO (ErrorCode, String)
getLastError s = do
(r, m) <- getLastError_ s nullPtr 0
return (int2error r, m)

-- | Throw an exception if negative value passed,
-- or return unchanged value.
Expand Down Expand Up @@ -172,7 +185,7 @@ handleNullPtr m_ctx fromPointer io = do
Nothing -> throw NULL_POINTER
Just ctx -> do
let session = getSession ctx
(r, _) <- getLastError session
r <- getLastErrno session
case int2error r of
EAGAIN -> threadWaitSession (Just session) >> handleNullPtr m_ctx fromPointer io
err -> throwCtxSpecificError ctx err
Expand Down Expand Up @@ -245,7 +258,9 @@ class SshCtx a where

instance SshCtx Session where
getSession = id
throwCtxSpecificError _ er = throw er
throwCtxSpecificError ctx er = do
(r, s) <- getLastError $ getSession ctx
throw $ SessionError (assert (r == er) er) s

instance SshCtx Sftp where
getSession = sftpSession
Expand Down