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 class has these properties:
Property | Data type | Description |
---|---|---|
Path | String | Returns the path of the file referenced by the Stat object. |
Auth | String | Returns the file permissions as a string; with the same syntax as CHMOD → Chapter 19.3.1. |
Group | String | Returns the group to which the file belongs. |
Hidden | Boolean | Returns True if the file is hidden. |
LastAccess | Date | Returns the time for the last access to the file. |
LastChange | Date | Returns the time when the attributes of the file were changed for the last time. |
LastModified | Date | Outputs the time when the content of the file was last modified. |
Time | Date | Synonym for the LastModified property. |
Link | String | If the file is a symbolic link, then the path of the file is returned. |
Mode | Integer | Returns 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. |
SetGID | Boolean | Returns True if the SetGID bit is set in the Permissions mask. |
SetUID | Boolean | Returns True if the SetUID bit is set in the rights mask. |
Size | Long | Returns the file size. |
Sticky | Boolean | Returns True if the Sticky bit is set in the rights mask. |
User | String | Returns the name of the user/owner who owns the file. |
Type | Integer | Returns the type of a file. |
Perm | .Stat.Perm | Returns a virtual class describing the file permissions (permissions mask). |
Table 19.3.0.1.1 : Properties of the class Stat
The type of a file can be described by one of the following constants:
Type | Type | Description |
---|---|---|
gb.File | 1 | Normal file (Regular file) |
gb.Directory | 2 | Directory (Directory) |
gb.Device | 3 | Special file for a device |
gb.Pipe | 4 | Named pipe (FIFO file) |
gb.Socket | 5 | Special file for a socket (Special file for a socket) |
gb.Link | 6 | Symbolic link (Symbolic link) |
Table 19.3.0.2.1 : File type constants of the Stat class
The .Stat.Perm (gb) virtual class describes the file permissions for the selected file and can be used like a read-only array.
Property | Data type | Description |
---|---|---|
.Stat.Perm.User | String | Returns the user/owner permission of the file as a string. |
.Stat.Perm.Group | String | Returns the group permission of the file as a string. |
.Stat.Perm.Other | String | Returns 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:
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.
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.