Ooops
March 01, 2021, 10:41:01 PM

Author Topic: [bmx] Detect file/dir changes by Zeke [ 1+ years ago ]  (Read 551 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bmx] Detect file/dir changes by Zeke [ 1+ years ago ]
« on: June 29, 2017, 12:28:39 AM »
Title : Detect file/dir changes
Author : Zeke
Posted : 1+ years ago

Description : This app will detect all kind file/directory changes (add,delete,modify,write,access etc).

Code :
Code: BlitzMax
  1. 'Source Code created on 21 Jan 2010 09:21:21 with Logic Gui Version 5.1 Build 422
  2. SuperStrict
  3.  
  4. Import MaxGui.Drivers
  5.  
  6. Extern "win32"
  7.         Function ReadDirectoryChangesW:Int(hDirectory:Int, lpBuffer:Byte Ptr, nBufferLength:Int, bWatchSubtree:Int, dwNotifyFilter:Int, lpBytesReturned:Int, lpOverlapped:Byte Ptr, lpCompletionRoutine:Byte Ptr)
  8.         Function CreateFileA:Int(lpFileName$z, dwDesiredAccess:Int, dwShareMode:Int, lpSecurityAttributes:Byte Ptr, dwCreationDisposition:Int, dwFlagsAndAttributes:Int, hTemplateFile:Int)
  9.         Function CloseHandle:Int(hObject:Int)
  10.         Function WaitForSingleObject:Int(hHandle:Int, dwMilliseconds:Int)
  11.         Function CreateEventA:Int(lbEventAttributes:Int, bManualReset:Int, bInitialState:Int, lpName$z)
  12.         Function ResetEvent:Int(hEvent:Int)
  13. End Extern
  14.  
  15. 'Types
  16. Type OVERLAPPED
  17.         Field Internal:Int
  18.         Field InternalHigh:Int
  19.         Field offset:Int
  20.         Field OffsetHight:Int
  21.         Field hEvent:Int
  22. End Type
  23.  
  24. Type FILE_NOTIFY_INFORMATION
  25.         Field NextEntryOffset:Int '4
  26.         Field Action:Int
  27.         Field FileNameLength:Int '4
  28.         Field fileName:Int, filename_:Int, Filename1:Double, FileName2:Double, FileName3:Double
  29.         Field Filename4:Double, FileName5:Double, FileName6:Double, FileName7:Double
  30.         Field Filename8:Double, FileName9:Double, FileName10:Double, FileName11:Double
  31.         Field Filename12:Double, FileName13:Double, FileName14:Double, FileName15:Double
  32.         Field Filename16:Double, FileName17:Double, FileName18:Double, FileName19:Double
  33.         Field Filename20:Double, FileName21:Double, FileName22:Double, FileName23:Double
  34.         Field Filename24:Double, FileName25:Double, FileName26:Double, FileName27:Double
  35.         Field Filename28:Double, FileName29:Double, FileName30:Double, FileName31:Double
  36.         Field Filename32:Double, FileName33:Double, FileName34:Double, FileName35:Double
  37.         Field Filename36:Double, FileName37:Double, FileName38:Double, FileName39:Double
  38.         Field Filename40:Double, FileName41:Double, FileName42:Double, FileName43:Double
  39.         Field Filename44:Double, FileName45:Double, FileName46:Double, FileName47:Double
  40.         Field Filename48:Double, FileName49:Double, FileName50:Double, FileName51:Double
  41.         Field Filename52:Double, FileName53:Double, FileName54:Double, FileName55:Double
  42.         Field Filename56:Double, FileName57:Double, FileName58:Double, FileName59:Double
  43.         Field Filename60:Double , FileName61:Double , FileName62:Double , FileName63:Double
  44.         '^^ filename 512 bytes
  45. End Type
  46.  
  47. 'Consts
  48. Const TIME_OUT:Int = $102
  49.  
  50. Const FILE_SHARE_DELETE:Int = $4
  51. Const FILE_SHARE_READ:Int = $1
  52. Const FILE_SHARE_WRITE:Int = $2
  53. Const FILE_ALL_ACCESS:Int = $1FF
  54. Const FILE_LIST_DIRECTORY:Int = $1
  55. Const OPEN_EXISTING:Int = $3
  56. Const FILE_FLAG_BACKUP_SEMANTICS:Int = $2000000
  57. Const FILE_FLAG_OVERLAPPED:Int = $40000000
  58. Const FILE_ATTRIBUTE_NORMAL:Int = $80
  59.  
  60. 'FILE_NOTIFY_CHANGE
  61. Const FILE_NOTIFY_CHANGE_FILE_NAME:Int = $1
  62. Const FILE_NOTIFY_CHANGE_DIR_NAME:Int = $2
  63. Const FILE_NOTIFY_CHANGE_ATTRIBUTES:Int = $4
  64. Const FILE_NOTIFY_CHANGE_SIZE:Int = $8
  65. Const FILE_NOTIFY_CHANGE_LAST_WRITE:Int = $10
  66. Const FILE_NOTIFY_CHANGE_LAST_ACCESS:Int = $20
  67. Const FILE_NOTIFY_CHANGE_CREATION:Int = $40
  68. Const FILE_NOTIFY_CHANGE_SECURITY:Int = $100
  69.  
  70. 'FILE_ACTION
  71. Const FILE_ACTION_ADDED:Int = $1
  72. Const FILE_ACTION_REMOVED:Int = $2
  73. Const FILE_ACTION_MODIFIED:Int = $3
  74. Const FILE_ACTION_RENAMED_OLD_NAME:Int = $4
  75. Const FILE_ACTION_RENAMED_NEW_NAME:Int = $5
  76.  
  77.  
  78. 'Globals
  79. Global running:Int = 0
  80. Global flags:Int = 0
  81. Global subtree:Int = 0
  82. Global folder:String = ""
  83. Global nUsed:Int = 0
  84. Global hDir:Int = 0
  85. Global hEvent:Int = 0
  86. Global oLap:OVERLAPPED = New OVERLAPPED
  87. Global buffer:Byte Ptr = MemAlloc(65535)
  88. Global dirBuf:FILE_NOTIFY_INFORMATION = New FILE_NOTIFY_INFORMATION
  89.  
  90.  
  91. Global Window:TGadget = CreateWindow("Detect file/directory changes",352,239,516,316,Null,WINDOW_TITLEBAR|WINDOW_RESIZABLE |WINDOW_STATUS |WINDOW_CLIENTCOORDS )
  92.         Global Group1:TGadget = CreatePanel(8,61,108,249,Window,PANEL_GROUP,"Filter")
  93.         SetGadgetLayout(Group1, 1, 0, 1, 0)
  94.                 Global chkFileName:TGadget = CreateButton("File Name",3,3,100,20,Group1,BUTTON_CHECKBOX)
  95.                         SetButtonState( chkFileName,0 )
  96.                         SetGadgetToolTip( chkFileName, "Any file name change in the watched directory or subtree causes a change notification wait operation to return. Changes include renaming, creating, or deleting a file." )
  97.                 Global chkDirName:TGadget = CreateButton("Dir Name",3,28,100,20,Group1,BUTTON_CHECKBOX)
  98.                         SetButtonState( chkDirName,0 )
  99.                         SetGadgetToolTip( chkDirName, "Any directory-name change in the watched directory or subtree causes a change notification wait operation to return. Changes include creating or deleting a directory." )
  100.                 Global chkAttributes:TGadget = CreateButton("Attributes",3,53,100,20,Group1,BUTTON_CHECKBOX)
  101.                         SetButtonState( chkAttributes,0 )
  102.                         SetGadgetToolTip( chkAttributes, "Any attribute change in the watched directory or subtree causes a change notification wait operation to return." )
  103.                 Global chkSize:TGadget = CreateButton("Size",3,78,100,20,Group1,BUTTON_CHECKBOX)
  104.                         SetButtonState( chkSize,0 )
  105.                         SetGadgetToolTip( chkSize, "Any file-size change in the watched directory or subtree causes a change notification wait operation to return. The operating system detects a change in file size only when the file is written to the disk. For operating systems that use extensive caching, detection occurs only when the cache is sufficiently flushed." )
  106.                 Global chkLastWrite:TGadget = CreateButton("Last Write",3,103,100,20,Group1,BUTTON_CHECKBOX)
  107.                         SetButtonState( chkLastWrite,0 )
  108.                         SetGadgetToolTip( chkLastWrite, "Any change to the last write-time of files in the watched directory or subtree causes a change notification wait operation to return. The operating system detects a change to the last write-time only when the file is written to the disk. For operating systems that use extensive caching, detection occurs only when the cache is sufficiently flushed." )
  109.                 Global chkLastAccess:TGadget = CreateButton("Last Access",3,128,100,20,Group1,BUTTON_CHECKBOX)
  110.                         SetButtonState( chkLastAccess,0 )
  111.                         SetGadgetToolTip( chkLastAccess, "Any change to the last access time of files in the watched directory or subtree causes a change notification wait operation to return." )
  112.                 Global chkCreation:TGadget = CreateButton("Creation",3,153,100,20,Group1,BUTTON_CHECKBOX)
  113.                         SetButtonState( chkCreation,0 )
  114.                         SetGadgetToolTip( chkCreation, "Any change to the creation time of files in the watched directory or subtree causes a change notification wait operation to return." )
  115.                 Global chkSecurity:TGadget = CreateButton("Security",3,178,100,20,Group1,BUTTON_CHECKBOX)
  116.                         SetButtonState( chkSecurity,0 )
  117.                         SetGadgetToolTip( chkSecurity, "Any security-descriptor change in the watched directory or subtree causes a change notification wait operation to return." )
  118.                 Global chkWatchSubDirs:TGadget = CreateButton("Watch Sub Dirs",3,208,100,20,Group1,BUTTON_CHECKBOX)
  119.                         SetButtonState( chkWatchSubDirs,0 )
  120.                         SetGadgetToolTip(chkWatchSubDirs, "If CHECKED, the application monitors the directory tree rooted at the specified directory. If UNCHECKED, the application monitors only the selected directory.")
  121.         Local Label2:TGadget = CreateLabel("Log Window:", 131, 61, 142, 20, Window, Null)
  122.         SetGadgetLayout Label2, 1, 0, 1, 0
  123.         Global btnWatch:TGadget = CreateButton("Start",448,13,63,20,Window,BUTTON_PUSH)
  124.                 SetGadgetToolTip(btnWatch, "Start monitoring selected directory.")
  125.                 SetGadgetLayout btnWatch, 0, 1, 1, 0
  126.         Global btnExit:TGadget = CreateButton("Exit", 448, 50, 63, 20, Window, BUTTON_PUSH)
  127.         SetGadgetLayout btnExit, 0, 1, 1, 0
  128.         Global txtLog:TGadget = CreateTextArea(131, 81, 380, 229, Window, Null)
  129.         SetGadgetLayout txtLog, 1, 1, 1, 1
  130.                 SetTextAreaText( txtLog , "" )
  131.         Local Label1:TGadget = CreateLabel("Selected Directory:", 8, 13, 203, 17, Window, Null)
  132.         SetGadgetLayout Label1, 1, 0, 1, 0
  133.         Global txtDir:TGadget = CreateTextField(8, 31, 387, 20, Window, Null)
  134.         SetGadgetLayout txtDir, 1, 1, 1, 0
  135.                 SetGadgetText( txtDir,"")
  136.         Global btnSelectDir:TGadget = CreateButton("...",398,30,24,20,Window,BUTTON_PUSH)
  137.                 SetGadgetToolTip( btnSelectDir, "Select directory to be monitored." )
  138.         SetGadgetLayout btnSelectDir, 0, 1, 1, 0
  139.  
  140. '//MainLoop
  141. Repeat
  142.         Delay 10
  143.         If PollEvent() 
  144.                 Select EventID()
  145.                         Case EVENT_WINDOWCLOSE
  146.                                 Select EventSource()
  147.                                         Case Window     Window_WC( Window )
  148.                                 End Select
  149.        
  150.                         Case EVENT_GADGETACTION
  151.                                 Select EventSource()
  152.                                         Case chkFileName chkFileName_GA(chkFileName, EventData())
  153.                                         Case chkDirName chkDirName_GA(chkDirName, EventData())
  154.                                         Case chkAttributes chkAttributes_GA(chkAttributes, EventData())
  155.                                         Case chkSize chkSize_GA(chkSize, EventData())
  156.                                         Case chkLastWrite chkLastWrite_GA(chkLastWrite, EventData())
  157.                                         Case chkLastAccess chkLastAccess_GA(chkLastAccess, EventData())
  158.                                         Case chkCreation chkCreation_GA(chkCreation, EventData())
  159.                                         Case chkSecurity chkSecurity_GA(chkSecurity, EventData())
  160.                                         Case chkWatchSubDirs chkWatchSubDirs_GA(chkWatchSubDirs, EventData())
  161.                                         Case btnWatch   btnWatch_GA( btnWatch )
  162.                                         Case btnExit    btnExit_GA( btnExit )
  163.                                         Case txtLog     txtLog_GA( txtLog )
  164.                                         Case txtDir     txtDir_GA( txtDir )
  165.                                         Case btnSelectDir       btnSelectDir_GA( btnSelectDir )
  166.                                 End Select
  167.                 End Select
  168.         EndIf
  169.         If Not Running Then Continue
  170.         Local Pos:Int = 0
  171.         Local ret:Int = waitforsingleobject(hEvent, 50) '50ms delay
  172.        
  173.         If ret <> TIME_OUT
  174.                 MemCopy(dirBuf, buffer, SizeOf(dirBuf))
  175.                
  176.                 Select dirBuf.Action
  177.                         Case FILE_ACTION_MODIFIED
  178.                                 AddLog "File Modified"
  179.                         Case FILE_ACTION_ADDED
  180.                                 AddLog "File Added"
  181.                         Case FILE_ACTION_REMOVED
  182.                                 AddLog "File Deleted"
  183.                         Case FILE_ACTION_RENAMED_NEW_NAME
  184.                                 AddLog "File Renamed New filename"
  185.                         Case FILE_ACTION_RENAMED_OLD_NAME
  186.                                 AddLog "File Renamed Old filename"
  187.                         Default
  188.                                 AddLog "Unkown File Action= " + dirBuf.Action
  189.                 End Select
  190.                
  191.                 Local file:String = String.FromShorts(Short Ptr(Byte Ptr(Varptr(dirBuf.fileName))), dirBuf.FileNameLength / 2)
  192.                
  193.                 AddLog "~tFileName=" + file
  194.  
  195.                 While dirBuf.NextEntryOffset <> 0 'process all actions
  196.                         Pos:+dirBuf.NextEntryOffset
  197.                         MemCopy dirBuf, buffer + Pos, SizeOf(dirBuf)
  198.                        
  199.                         Select dirBuf.Action
  200.                                 Case FILE_ACTION_MODIFIED
  201.                                         AddLog "File/Dir Modified"
  202.                                 Case FILE_ACTION_ADDED
  203.                                         AddLog "File Added"
  204.                                 Case FILE_ACTION_REMOVED
  205.                                         AddLog "File Deleted"
  206.                                 Case FILE_ACTION_RENAMED_NEW_NAME
  207.                                         AddLog "File Renamed New name"
  208.                                 Case FILE_ACTION_RENAMED_OLD_NAME
  209.                                         AddLog "File Renamed Old name"
  210.                                 Default
  211.                                         AddLog "Unkown File Action= " + dirBuf.Action
  212.                         End Select
  213.                         Local file:String = String.FromShorts(Short Ptr(Byte Ptr(Varptr(dirBuf.fileName))), dirBuf.FileNameLength / 2)
  214.                        
  215.                         AddLog "~tFileName=" + file
  216.                 Wend
  217.                
  218.                 'Reset event
  219.                 ResetEvent hEvent
  220.                 ReadDirectoryChangesW(hDir, buffer, 65535, subtree, flags, nUsed, oLap, Null)
  221.         End If
  222. Forever
  223.  
  224. Function Window_WC( Window:TGadget )
  225.         EndApp
  226. End Function
  227.  
  228. Function chkFileName_GA(Button:TGadget, State:Int)
  229.         flags:+$1 * (State = 1) - $1 * (State = 0)
  230. End Function
  231.  
  232. Function chkDirName_GA( Button:TGadget, State:Int )
  233.         flags:+$2 * (State = 1) - $2 * (State = 0)
  234. End Function
  235.  
  236. Function chkAttributes_GA( Button:TGadget, State:Int )
  237.         flags:+$4 * (State = 1) - $4 * (State = 0)
  238. End Function
  239.  
  240. Function chkSize_GA(Button:TGadget, State:Int)
  241.         flags:+$8 * (State = 1) - $8 * (State = 0)
  242. End Function
  243.  
  244. Function chkLastWrite_GA( Button:TGadget, State:Int )
  245.         flags:+$10 * (State = 1) - $10 * (State = 0)
  246. End Function
  247.  
  248. Function chkLastAccess_GA( Button:TGadget, State:Int )
  249.         flags:+$20 * (State = 1) - $20 * (State = 0)
  250. End Function
  251.  
  252. Function chkCreation_GA( Button:TGadget, State:Int )
  253.         flags:+$40 * (State = 1) - $40 * (State = 0)
  254. End Function
  255.  
  256. Function chkSecurity_GA( Button:TGadget, State:Int )
  257.         flags:+$100 * (State = 1) - $100 * (State = 0)
  258. End Function
  259.  
  260. Function chkWatchSubDirs_GA( Button:TGadget, State:Int )
  261.         subtree = State
  262. End Function
  263.  
  264. Function btnWatch_GA( Button:TGadget )
  265.         If GadgetText(Button) = "Start" Then
  266.                 StartMonitor
  267.         Else
  268.                 StopMonitor
  269.         EndIf
  270. End Function
  271.  
  272. Function btnExit_GA( Button:TGadget )
  273.         EndApp
  274. End Function
  275.  
  276. Function txtLog_GA( TextArea:TGadget )
  277. End Function
  278.  
  279. Function txtDir_GA( TextField:TGadget )
  280.         folder = TextField.getText()
  281. End Function
  282.  
  283. Function btnSelectDir_GA:Int( Button:TGadget )
  284.         Local dir:String = RequestDir("Select directory to be monitored", "")
  285.         If dir = "" Then Return 0
  286.         If Right(dir, 1) <> "" Then dir:+""
  287.         SetGadgetText(txtDir , dir)
  288.         folder=dir
  289. End Function
  290.  
  291. '// Functions
  292.  
  293. Function EndApp()
  294.         MemFree buffer
  295.         If hEvent CloseHandle hEvent
  296.         If hDir CloseHandle hDir
  297.         End
  298. End Function
  299.  
  300. Function StartMonitor:Int()
  301.         If folder = "" Then
  302.                 AddLog "Select directory first"
  303.                 Return 0
  304.         End If
  305.         If FileType(folder) <> 2 Then
  306.                 AddLog "Invalid directory"
  307.                 Return 0
  308.         End If
  309.         If Right(folder,1)<>"" Then folder:+""
  310.         SetGadgetText(btnWatch, "Stop")
  311.         Running = 1
  312.         DisableGadget(Group1)
  313.         SetTextAreaText(txtLog , "")
  314.        
  315.         If hDir Then CloseHandle hDir
  316.         hDir = CreateFileA(folder, FILE_LIST_DIRECTORY, FILE_SHARE_READ | FILE_SHARE_WRITE, Null, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED, Null)
  317.         If Not hDir
  318.                 AddLog "Error~n"
  319.                 Return 0
  320.         End If
  321.         If hEvent Then CloseHandle hEvent
  322.         hEvent = CreateEventA(0, True, True, Null)
  323.         oLap.hEvent = hEvent
  324.         ReadDirectoryChangesW(hDir, buffer, 65535, subtree, flags, nUsed, oLap, Null)
  325.         AddLog "Monitoring started...~n"
  326. End Function
  327.  
  328. Function StopMonitor()
  329.         SetGadgetText(btnWatch, "Start")
  330.         Running = 0
  331.         EnableGadget(Group1)
  332.         If hEvent CloseHandle hEvent
  333.         If hDir CloseHandle hDir
  334.         AddLog "Monitoring stopped...~n"
  335. End Function
  336.  
  337. Function AddLog(text:String)
  338.         AddTextAreaText(txtLog, text + "~n")
  339.         PollSystem
  340. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal