AuthorizationMBS issues

This code throws ‘Lasterror on Execute: -60031’

Any reason why?

a=new AuthorizationMBS
var fileToBeDeleted(0) as FolderItem
fileToBeDeleted(0) = SpecialFolder.Desktop.Child("test.txt")
dim s(0) as String
s(0)=ConvertEncoding(fileToBeDeleted(0).ShellPath,Encodings.UTF8)
if a.SimpleNewAuthorization then // create
  a.Execute("rm",s,true) // and run it
  if a.LastError<>0 then
    MsgBox "Lasterror on Execute: "+str(a.LastError)
  else
    a.MakeStreamAsyncron
  end if
end if

Try

a=new AuthorizationMBS
dim s(-1) as String
if a.SimpleNewAuthorization then // create
  a.Execute("rm " + SpecialFolder.Desktop.Child("test.txt").shellPath,s,true) // and run it
  if a.LastError<>0 then
    MsgBox "Lasterror on Execute: "+str(a.LastError)
  else
    a.MakeStreamAsyncron
  end if
end if
a.Execute("rm " + fileToBeDeleted(0).ShellPath,s,true)

Sadly the same error.

Please try full path for the rm command.

Not sure what you meant. I already use the full path.
When using that path in the Terminal directly, it works fine.

I haven’t used AuthorizationMBS with shell commands but we have used it to launch auto-update helper apps on macOS successfully for over 10 years.

Here is the code we use (which also supports MS-Windows).

Public Enum AuthoriseAndExecuteResult
OK
Cancelled
TimedOut
Failed
Function AuthoriseAndExecute(pExecutable As FolderItem, pOSXArgs() As String) As AuthoriseAndExecuteResult
  Dim tempInteger As Int32
  
  Return AuthoriseAndExecute(pExecutable, pOSXArgs, tempInteger)
End Function
Function AuthoriseAndExecute(pExecutable As FolderItem, pOSXArgs() As String, ByRef pOSErrorCode As Int32) As AuthoriseAndExecuteResult
  Dim theResult As AuthoriseAndExecuteResult
  Dim osErrorCode As Int32
  
  theResult = AuthoriseAndExecuteResult.Failed
  osErrorCode = 0
  
  #If TargetMacOS Then
    Dim authorisationObj As AuthorizationMBS
    
    authorisationObj = New AuthorizationMBS
    
    'first attempt to use the unix path to the executable as this should be more reliable than the shell path (needed for osx 10.5 support)
    If authorisationObj.SimpleNewAuthorization = True Then
      authorisationObj.Execute(pExecutable.UnixpathMBS, pOSXArgs)
      
      'if we received a -60031 error then the executable path could not be found so use the shell path instead
      If authorisationObj.LastError = -60031 Then
        authorisationObj.Execute(pExecutable.ShellPath, pOSXArgs)
      End If
      
      'convert the result into one of our enums
      Select Case authorisationObj.LastError
      Case 0
        theResult = AuthoriseAndExecuteResult.OK
      Case -60006
        theResult = AuthoriseAndExecuteResult.Cancelled
      Case -60005
        theResult = AuthoriseAndExecuteResult.TimedOut
      End Select
      
      osErrorCode = authorisationObj.LastError
    End If
  #Else
    Const SW_SHOWNORMAL = 1
    Const SW_SHOWNA = 8
    
    Dim info As New MemoryBlock(15 * 4)
    Dim verb As New MemoryBlock(32)
    Dim file As New MemoryBlock(260 * 2)
    
    Soft Declare Function ShellExecuteExW Lib "Shell32" (info As Ptr) As Boolean
    Soft Declare Function ShellExecuteExA Lib "Shell32" (info As Ptr) As Boolean
    
    info.Long(0) = info.Size
    info.Long(8) = 0
    If System.IsFunctionAvailable("ShellExecuteExW", "Shell32") = True Then
      verb.WString(0) = "runas"
      file.WString(0) = pExecutable.AbsolutePath
    Else
      verb.CString(0) = "runas"
      file.CString(0) = pExecutable.AbsolutePath
    End If
    info.Ptr(12) = verb
    info.Ptr(16) = file
    info.Long(28) = SW_SHOWNA
    
    If System.IsFunctionAvailable("ShellExecuteExW", "Shell32") = True Then
      If ShellExecuteExW(info) = True Then
        theResult = AuthoriseAndExecuteResult.OK
      End If
    Else
      If ShellExecuteExA(info) = True Then
        theResult = AuthoriseAndExecuteResult.OK
      End If
    End If
  #EndIf
  
  pOSErrorCode = osErrorCode
  
  Return theResult
End Function

Sorry, I keep getting Error -60031

/bin/rm

Thanks you all for the input! Appreciated!

/bin/rm also doesn’t work either.

Have you tried without any parameters as it might be the input file path it doesn’t like rather than the command path.

Please use NativePath.
Or try “/bin/echo” and let it output the parameters.

What file are you trying to rm? I’ve seen AuthorizationMBS fail when trying to update the hosts file. It could be a security issue.

It’s just a file from the desktop. Nothing special.

/bin/echo does not return the error.

I’m still investigating why it fails …

Has your app been given permission to access the desktop? If so, in what way?
Did you ever get the security / access dialog about your app accessing files?

Shell is not a terminal try:
“usr/bin/rm” as the command or lookup the path using “whereis rm” in the terminal, then ise the given path in your xojo shell.

That works! Thank you.

1 Like

Here they are in /bin as @kevin_g suggested.

tim@olmec ~ % whereis rm
/bin/rm
tim@olmec ~ % which rm
/bin/rm

So that definitely wouldn’t work here (macOS Big Sur, M1, Fresh install no migration) if you’re deploying this publicly.

A neat trick is to get the system PATH and set that in the shell (if interactive):

Var PATH As string = System.EnvironmentVariable("PATH")
sh = New Shell 
sh.Execute("export $PATH=" + PATH)

next time you call just “rm” it should work as the path variable is known now.

3 Likes