HomeForumSourceResearchGuide
Sign in to contribute to source. how it works
Component io.FileSystemEvents by barry
expand copy to clipboardexpand
data FSEvent {
	FileEventInfo evi
	byte type
}

interface FileEventsLib{
	int newEvents()
	FSEvent[] poll(int handle, int waitMS)
	bool listenFile(int handle, char path[])
	bool unlistenFile(int handle, char path[])
	bool listenDir(int handle, char path[], bool sub)
	bool unlistenDir(int handle, char path[])
	bool destroyEvents(int handle)
	}

const int POLL_WAIT_MS = 10

component provides io.FileSystemEvents(Destructor) requires native FileEventsLib lib, io.Output out {
	
	int platformHandle
	bool destroy
	Thread pThread

	void pollThread()
		{
		while (!destroy)
			{
			FSEvent events[] = lib.poll(platformHandle, POLL_WAIT_MS)

			for (int i = 0; i < events.arrayLength; i++)
				{
				if (events[i].type == 1)
					{
					emitevent fileEvent(events[i].evi)
					}
					else if (events[i].type == 2)
					{
					emitevent directoryEvent(events[i].evi)
					}
				}
			}
		}
	
	FileSystemEvents:FileSystemEvents()
		{
		platformHandle = lib.newEvents()
		if (platformHandle == 0) throw new Exception("failed to initialise file system events layer in native system")
		pThread = asynch::pollThread()
		}
	
	bool FileSystemEvents:listenFile(char path[])
		{
		return lib.listenFile(platformHandle, path)
		}
	
	void FileSystemEvents:unlistenFile(char path[])
		{
		lib.unlistenFile(platformHandle, path)
		}
	
	bool FileSystemEvents:listenDirectory(char path[])
		{
		return lib.listenDir(platformHandle, path, false)
		}
	
	void FileSystemEvents:unlistenDirectory(char path[])
		{
		lib.unlistenDir(platformHandle, path)
		}
	
	void Destructor:destroy()
		{
		if (platformHandle != 0)
			{
			destroy = true
			pThread.join()
			lib.destroyEvents(platformHandle)
			}
		}
	
	}
Revision history
To propose a new revision to this entity, use dana source put -uc your/new/version.dn -n io.FileSystemEvents -m "reason for update" -u yourUsername
Version 1 (this version) by barry
Notes for this version: Adds API to monitor changes to file system elements