User Tools

Site Tools


k19:k19.3:start

19.3.0 Class Stat

The Stat (gb) class provides you with all the information returned by the system about a particular file. It must be emphasised that a Stat object is unsuitable for making changes to file properties! The class cannot be created.

  • The Stat() function returns a Stat object with which you can read out information on file and directory properties.
  • The information is read out for exactly one selected file, whose path you must specify in the Path property → Table 19.3.0.1.1.
  • Attention: Six types of files are distinguished → Chapter 19.3.0.2.
  • Note that the information about a file is static when using this class because it has been read out at a certain point in time. If you want to record and evaluate certain changes to a selected file dynamically at runtime (via a task), then you will find the Watch class suitable for this in the gb.inotify component.

19.3.0.1 Properties

The Stat class has these properties:

PropertyData typeDescription
PathStringReturns the path of the file referenced by the Stat object.
AuthStringReturns the file permissions as a string; with the same syntax as CHMOD → Chapter 19.3.1.
GroupStringReturns the group to which the file belongs.
HiddenBooleanReturns True if the file is hidden.
LastAccessDateReturns the time for the last access to the file.
LastChangeDateReturns the time when the attributes of the file were changed for the last time.
LastModifiedDateOutputs the time when the content of the file was last modified.
TimeDateSynonym for the LastModified property.
LinkStringIf the file is a symbolic link, then the path of the file is returned.
ModeIntegerReturns the mode (access mask) of the file encoded as a number. You should use the more specific and easier to handle properties such as Perm or SetUID.
SetGIDBooleanReturns True if the SetGID bit is set in the Permissions mask.
SetUIDBooleanReturns True if the SetUID bit is set in the rights mask.
SizeLongReturns the file size.
StickyBooleanReturns True if the Sticky bit is set in the rights mask.
UserStringReturns the name of the user/owner who owns the file.
TypeIntegerReturns the type of a file.
Perm.Stat.PermReturns a virtual class describing the file permissions (permissions mask).

Table 19.3.0.1.1 : Properties of the class Stat

19.3.0.2 File types

The type of a file can be described by one of the following constants:

TypeTypeDescription
gb.File1Normal file (Regular file)
gb.Directory2Directory (Directory)
gb.Device3Special file for a device
gb.Pipe4Named pipe (FIFO file)
gb.Socket5Special file for a socket (Special file for a socket)
gb.Link6Symbolic link (Symbolic link)

Table 19.3.0.2.1 : File type constants of the Stat class

19.3.0.3 Class .Stat.Perm

The .Stat.Perm (gb) virtual class describes the file permissions for the selected file and can be used like a read-only array.

PropertyData typeDescription
.Stat.Perm.UserStringReturns the user/owner permission of the file as a string.
.Stat.Perm.GroupStringReturns the group permission of the file as a string.
.Stat.Perm.OtherStringReturns the default permissions as a string.

Table 19.3.0.3.1 : Properties of the .Stat.Perm class

In all three cases, the string contains the following characters:

  • r → for the read permission,
  • w → for the write permission,
  • x → for the execute right,

if the respective right has been granted. In particular, no character is output for missing permissions. This differs from the output of commands like 'ls -l', where missing permissions are noted with a minus sign at their fixed position.

19.3.0.4 Project

First, a temporary file is generated in the presented project and a string is inserted as content. Then the file permissions are explicitly changed. Then a Stat object is generated and the file information of the temporary file is read out and displayed in the console of the IDE.

The source code is given in full:

' Gambas class file
 
Public sFilePath As String = Temp(".datei")
 
Public Sub Form_Open()
  Dim sGroup As String
 
  FMain.Center
  FMain.Resizable = False
 
  Exec ["id", "-gn"] To sGroup ' Gruppe des aktuellen Benutzers ermitteln
  sGroup = Trim$(sGroup)
 
  Shell Subst$("touch &1; echo 'TEST TEXT TEXT' > &1", sFilePath) Wait
  Chown sFilePath To User.Name
  Chmod sFilePath To "rwxrw-r--"
  Chgrp sFilePath To sGroup
 
End
 
Public Sub btnGetInformation_Click()
  Dim FileInfo As Stat
  Dim vValue As Variant
  Dim aTypes As String[]
  Dim cCollection As Collection
 
  aTypes = ["Normale Datei", "Verzeichnis", "Gerät", "Pipe (FIFO)", "Socket", "Symbolischer Link"]
 
  FileInfo = Stat(sFilePath)
 
  cCollection = New Collection
' cCollection[Key As String] = Value As Variant → Zuerst der Schlüssel und dann der Wert
  cCollection["Pfad = "] = FileInfo.Path
  cCollection["Datei-Typ = "] = aTypes[FileInfo.Type - 1]
  cCollection["Rechte = "] = FileInfo.Auth
  cCollection["Datei-Modus = "] = FileInfo.Mode
  cCollection["Benutzer = "] = FileInfo.User
  cCollection["Gruppe = "] = FileInfo.Group
  cCollection["Versteckt? = "] = FileInfo.Hidden
  cCollection["Letzter Zugriff auf die Datei   = "] = FileInfo.LastAccess
  cCollection["Letzte Änderung Datei-Inhalt    = "] = FileInfo.LastModified
  cCollection["Letzte Änderung Datei-Attribute = "] = FileInfo.LastChange
  cCollection["Datei-Größe = "] = FileInfo.Size & " Byte"
  cCollection["Sticky-Bit gesetzt? = "] = FileInfo.Sticky
  If FileInfo.Link = Null Then
     cCollection["Symbolischer Link ? = "] = "Nein"
  Else
     cCollection["Symbolischer Link ? = "] = FileInfo.Link
  Endif
  cCollection["Benutzer-Rechte = "] = FileInfo.Perm.User
  cCollection["Gruppen-Rechte  = "] = FileInfo.Perm.Group
  cCollection["Andere-Rechte   = "] = FileInfo.Perm.Other
 
  For Each vValue In cCollection
    Print cCollection.Key, vValue
  Next
 
  Print "Pfad = ", sFilePath; "   Recht R+W  = ", Access(sFilePath, gb.Read Or gb.Write)
  Print "Pfad = ", sFilePath; "   Recht R    = ", Access(sFilePath, gb.Read)
  Print "Pfad = ", sFilePath; "   Recht W    = ", Access(sFilePath, gb.Write)
  Print "Pfad = ", sFilePath; "   Recht X    = ", Access(sFilePath, gb.Exec)
' gb.read ist Standard, wenn das optionale Mode-Argument fehlt
  Print "Pfad = ", sFilePath; "   Recht (R)  = ", Access(sFilePath)
 
End

Output in the console of the IDE:

Pfad =         /tmp/gambas.1000/6251/.datei.tmp
Datei-Typ =    Normale Datei
Rechte =       rwxrw-r--
Datei-Modus =  500
Benutzer = 	hans
Gruppe = 	hans
Versteckt? = 	True
Letzter Zugriff auf die Datei   = 01.10.2015 14:32:10
Letzte Änderung Datei-Inhalt    = 01.10.2015 14:32:10
Letzte Änderung Datei-Attribute = 01.10.2015 14:32:10
Datei-Größe = 	15 Byte
Sticky-Bit gesetzt? = False
Symbolischer Link ? = Nein
Benutzer-Rechte = 	rwx
Gruppen-Rechte  = 	rw
Andere-Rechte   = 	r
Pfad = 	/tmp/gambas.1000/6251/.datei.tmp  Recht R+W  = 	True
Pfad = 	/tmp/gambas.1000/6251/.datei.tmp  Recht R    = 	True
Pfad = 	/tmp/gambas.1000/6251/.datei.tmp  Recht W    = 	True
Pfad = 	/tmp/gambas.1000/6251/.datei.tmp  Recht X    = 	True
Pfad = 	/tmp/gambas.1000/6251/.datei.tmp  Recht (R)  = 	True

An alternative to the use of the virtual class .Stat.Perm is the Access function, about which you can read information at → http://gambaswiki.org/wiki/lang/access and which was also used selectively in the above project.

The three instructions CHGRP, CHMOD and CHOWN are available for changing file attributes → chapter 19.3.1.

Download

The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k19/k19.3/start.txt · Last modified: 13.10.2023 by emma

Page Tools