Monitorare con le funzioni esterne del API di libgio-2.0 le modifiche apportate ad un file

Da Gambas-it.org - Wikipedia.

La libreria libgio-2.0 di GLIB consente, fra l'altro, di intercettare le eventuali modifiche apportate ad uno specifico file, ovvero ad un qualsiasi file all'interno di una cartella.

E' necessario avere installata nel sistema e richiamare in Gambas la libreria condivisa: "libgio-2.0.so.0.7200.4 "

Mostriamo un esempio pratico, nel quale monitoreremo il contenuto della cartella dei file temporanei /tmp :

Library "libgio-2.0:0.7200.4"

Private Enum G_FILE_MONITOR_NONE = 0, G_FILE_MONITOR_WATCH_MOUNTS, G_FILE_MONITOR_SEND_MOVED,
             G_FILE_MONITOR_WATCH_HARD_LINKS, G_FILE_MONITOR_WATCH_MOVES

' GFile * g_file_new_for_path (const char *path)
' Constructs a GFile for a given path.
Private Extern g_file_new_for_path(path As String) As Pointer

' GFileMonitor * g_file_monitor (GFile *file, GFileMonitorFlags flags, GCancellable *cancellable, GError **error)
' Obtains a file or directory monitor for the given file, depending on the type of the file.
Private Extern g_file_monitor(GFile As Pointer, flags As Integer, cancellable As Pointer, GError As Pointer) As Pointer

' gulong g_signal_connect_data (gpointer instance, const gchar *detailed_signal, GCallback c_handler, gpointer data, GClosureNotify destroy_data, GConnectFlags connect_flags)
' Connects a GCallback function to a signal for a particular object.
Private Extern g_signal_connect_data(instance As Pointer, detailed_signal As String, c_handler As Pointer, data As Pointer, destroy_data As Pointer, connect_flags As Integer) As Long

' GMainLoop * g_main_loop_new (GMainContext *context, gboolean is_running)
' Creates a new GMainLoop structure.
Private Extern g_main_loop_new(context As Pointer, is_running As Boolean) As Pointer

' void g_main_loop_run (GMainLoop *loop)
' Runs a main loop until g_main_loop_quit() is called on the loop.
Private Extern g_main_loop_run(gloop As Pointer)

' char * g_file_get_basename (GFile *file)
' Gets the base name (the last component of the path) for a given GFile.
Private Extern g_file_get_basename(GFile As Pointer) As String

' void g_main_loop_unref (GMainLoop *loop)
' Decreases the reference count on a GMainLoop object by one.
Private Extern g_main_loop_unref(gloop As Pointer)

' void g_object_unref (gpointer object)
' Decreases the reference count of object.
Private Extern g_object_unref(Gobject As Pointer)


Public Sub Main()
 
 Dim s As String
 Dim gf, mon, gml As Pointer
       
' Impostiamo la cartella da monitorare:
 s = "/tmp"
   
 gf = g_file_new_for_path(s)
 If gf == 0 Then Error.Raise("Impossibile impostare il percorso del file da monitorare !")
   
 mon = g_file_monitor(gf, G_FILE_MONITOR_SEND_MOVED, 0, 0)
 If mon == 0 Then Error.Raise("Impossibile monitorare " & s & " !")
   
' Funzione Callback che chiama la 'Funzione di Callback' per intercettare gli eventi sui file della cartella monitorata:
 g_signal_connect_data(mon, "changed", callback, 0, 0, 0)
   
 gml = g_main_loop_new(0, False)
 If gml == 0 Then Error.Raise("Errore !")
 
 g_main_loop_run(gml)
   
 g_main_loop_unref(gml)
 g_object_unref(mon)
 g_object_unref(gf)
  
End

Private Function callback(monitor As Pointer, primo As Pointer, secondo As Pointer, evento As Integer, udata As Pointer)
 
 Dim ris As String
 Dim eventi As String[]
 
 eventi = ["CHANGED", "CHANGES_DONE_HINT", "DELETED", "CREATED", "ATTRIBUTE_CHANGED",
           "PRE_UNMOUNT", "UNMOUNTED", "MOVED", "RENAMED", "MOVED_IN", "MOVED_OUT"]
 
' Descrive il tipo di evento ed il file interessato:
 ris = "Evento n. " & evento & " = '" & eventi[evento] & "'  -  " & " Nome primo file: " &
       Quote(IIf(primo > 0, g_file_get_basename(primo), "---")) &
       ",  Nome secondo file: " & Quote(IIf(secondo > 0, g_file_get_basename(secondo), "---"))
   
 Print ris
  
End


Riferimenti