[Gambas-user] Stat() Auth and Mode (misunderstanding or bug?)

Willy Raets willy at ...2734...
Fri Sep 19 02:42:35 CEST 2014


On do, 2014-09-18 at 14:15 +0200, Tobias Boege wrote:
> On Thu, 18 Sep 2014, Willy Raets wrote:
> > On do, 2014-09-18 at 01:53 +0200, Beno??t Minisini wrote:
> > > Le 18/09/2014 01:50, Willy Raets a ??crit :
> > > > Hi all,
> > > >
> > > > Following was brought to my attention:
> > > >
> > > > To reproduce make a file named test in your home folder.
> > > > Create a console project in IDE with any name you like
> > > >
> > > > Public Sub Main()
> > > >
> > > >    Dim sPath As String
> > > >    sPath = User.Home &/ "test"
> > > >    With Stat(sPath)
> > > >      Print "Auth: " & .Auth
> > > >      Print "Mode: " & .Mode
> > > >   End With
> > > >
> > > > End
> > > >
> > > > Run the project.
> > > > Output is:
> > > >
> > > > Auth: rw-r--r--
> > > > Mode: 420
> > > >
> > > > Should mode not be 644 or do I misunderstand Stat().Mode?
> > > >
> > > > user rw- = 6
> > > > group r-- = 4
> > > > other r-- = 4
> > > >
> > > 
> > > 644 is octal. In decimal it is 420.
> > 
> > That explains a lot and at the same time it is confusing as I got used
> > to octal representation when running chmod on linux.
> > 
> > I guess I'll have to rewire my brain when using Stat().Mode in Gambas :)
> > 
> 
> Think of it as: Mode returns an Integer. Integers only have *values*, they
> do *not* come with specific representation information, like a number base.

Here is a function that converts a decimal to octal and returns it as an
Integer :)
Isn't that what we can do with Gambas ;)

Just copy/paste into an empty console project, run the project and sit
back and enjoy the mode looking familiar to CHMOD in linux :)

=== THE CODE ========================

'Function: Octal
'Description: Convert decimal to octal
'Author: W.J.L. Raets
'License: GNU - GPL version 3

Public Sub Main()

  Dim sPath As String
  sPath = User.Home
  Print "Path: " & sPath
  With Stat(sPath)
    Print "Auth: " & .Auth
    Print "Mode: " & Octal(.Mode)
  End With

Private Function Octal(Decimal As String) As Integer
  
  Dim iPos, iX, iTot As Integer
  Dim sTot As String
  iTot = 0
  If Len(Decimal) <> 0 Then
    iPos = Val(Decimal)
    For iX = 1 To Len(Decimal)
      iTot = iPos / 8 ^ (Len(Decimal) - iX)
      iPos = iPos - 8 ^ (Len(Decimal) - iX) * iTot
      sTot &= iTot
    Next
  Else
    sTot = "0"
  Endif
  Return Val(sTot)
  
End

=== END CODE ========================

-- 
Kind regards,

Willy (aka gbWilly)

http://gambasshowcase.org/
http://howtogambas.org
http://gambos.org








More information about the User mailing list