GB3- Récupérer l'inode d'un fichier avec gambas
Bonjour,
Aujourd'hui je vous propose un code gambas pour récupérer l'inode d'un fichier au travers de la librairie externe libc6.
1- Problème posé.
J'a besoin de connaître le numéro d'inode de mon fichier présent sur le disque.
Pour ce faire je peux utiliser la commande
ls -I monfichier
Mais je souhaite connaître la procédure pour récupérer l'inode au travers de la librairie libc6.
2- Déclaration de la librairie.
Déclarons la librairie comme ceci :
Library "libc:6"
Ensuite, la librairie dispose de plusieurs variables que nous allons regrouper dans une structure nommée stat__
Voici le code :
Public Struct stat_ st_dev As Long st_ino As Long st_nlink As Long st_mode As Integer st_uid As Integer st_gid As Integer __pad0 As Integer st_rdev As Long st_size As Long st_blksize As Long st_blocks As Long st_atime As Long st_atimensec As Long st_mtime As Long st_mtimensec As Long st_ctime As Long st_ctimensec As Long __glibc_reserved[3] As Long End Struct
Nous déclarons une méthode externe nommée __xstat (contenue dans la libc6) qui aura trois paramètres et nous renverra un Integer.
Voici le code :
Private Extern __xstat(_STAT_VER As Integer, __path As String, __statbuf As Stat_) As Integer
3- Récupération du numéro d'inode.
Enfin, il ne nous reste plus qu'à utiliser une simple variable Integer (nommée i par exemple) dans notre code pour extraire le numéro de l'inode à partir du chemin que nous aurons défini dans la variable __path comme ceci :
Dim i As Integer Dim st As New Stat_ i = __xstat(_STAT_VER_LINUX, Dialog.Path, st)
Remarque: Dans le cas ou la méthode __xstat nous renverrait une erreur, i serait egal à la valeur : -1
Nous pouvons donc ajouter cette ligne en fin de code :
If i < 0 Then Error.Raise("Error !")
4- Exemple graphique
Pour concrétiser ce code, j'ai réalisé un petit exemple graphique avec une textbox, un bouton et un label.
Lorsque je clique sur un bouton j'ouvre une boite de dialogue pour sélectionner un fichier.
Lors d'un clic sur OK, j'affiche le chemin sélectionné dans la textbox et affiche le numéro d'inode dans un label.
Voici le code complet de l'exemple :
Library "libc:6" Private Const _STAT_VER_LINUX As Integer = 1 ' int __xstat (_STAT_VER, __path, __statbuf) ' Get file attributes for FILE and put them in BUF. Public Struct stat_ st_dev As Long st_ino As Long st_nlink As Long st_mode As Integer st_uid As Integer st_gid As Integer __pad0 As Integer st_rdev As Long st_size As Long st_blksize As Long st_blocks As Long st_atime As Long st_atimensec As Long st_mtime As Long st_mtimensec As Long st_ctime As Long st_ctimensec As Long __glibc_reserved[3] As Long End Struct Private Extern __xstat(_STAT_VER As Integer, __path As String, __statbuf As Stat_) As Integer Public Sub Button1_Click() Dim i As Integer Dim st As New Stat_ Dialog.Filter = ["*.jpg", "Fichiers JPEG", ".png", "Fichiers PNG", ".xpm", "Fichiers XPM"] Dialog.Title = "Ouvrir unfichier" Dialog.Path = Application.Path If Dialog.OpenFile() Then Return i = __xstat(_STAT_VER_LINUX, Dialog.Path, st) If i < 0 Then Error.Raise("Error !") Label1.Text = st.st_ino tfile.Text = Dialog.Path End Public Sub Form_Open() Me.Center() End
Merci à tous pour votre attention.