108c9600aSClemens Zeidler /* 208c9600aSClemens Zeidler * Copyright 2011, Haiku, Inc. All rights reserved. 308c9600aSClemens Zeidler * Distributed under the terms of the MIT License. 408c9600aSClemens Zeidler * 508c9600aSClemens Zeidler * Authors: 608c9600aSClemens Zeidler * Clemens Zeidler <haiku@clemens-zeidler.de> 708c9600aSClemens Zeidler */ 808c9600aSClemens Zeidler #ifndef FILE_MONITOR_H 908c9600aSClemens Zeidler #define FILE_MONITOR_H 1008c9600aSClemens Zeidler 1108c9600aSClemens Zeidler 1208c9600aSClemens Zeidler #include <map> 1308c9600aSClemens Zeidler #include <vector> 1408c9600aSClemens Zeidler 1508c9600aSClemens Zeidler #include <Entry.h> 1608c9600aSClemens Zeidler #include <Node.h> 1708c9600aSClemens Zeidler 1808c9600aSClemens Zeidler #include "NodeMonitorHandler.h" 1908c9600aSClemens Zeidler 2008c9600aSClemens Zeidler 2108c9600aSClemens Zeidler struct WatchedFile { 2208c9600aSClemens Zeidler entry_ref entry; 2308c9600aSClemens Zeidler node_ref node; 2408c9600aSClemens Zeidler /*! Don't use it as the primary cookie storage. To be set in EntryCreated 2508c9600aSClemens Zeidler in EntryViewInterface. */ 2608c9600aSClemens Zeidler void* cookie; 2708c9600aSClemens Zeidler }; 2808c9600aSClemens Zeidler 2908c9600aSClemens Zeidler 3008c9600aSClemens Zeidler class NodeRefComp { 3108c9600aSClemens Zeidler public: 3208c9600aSClemens Zeidler bool operator()33*92eeb4afSPascal Abresch operator()(const node_ref& a, const node_ref& b) const 3408c9600aSClemens Zeidler { 3508c9600aSClemens Zeidler return a.node < b.node; 3608c9600aSClemens Zeidler } 3708c9600aSClemens Zeidler }; 3808c9600aSClemens Zeidler 3908c9600aSClemens Zeidler 4008c9600aSClemens Zeidler typedef std::map<node_ref, WatchedFile, NodeRefComp> WatchedFileList; 4108c9600aSClemens Zeidler 4208c9600aSClemens Zeidler 4308c9600aSClemens Zeidler class EntryViewInterface { 4408c9600aSClemens Zeidler public: ~EntryViewInterface()4508c9600aSClemens Zeidler virtual ~EntryViewInterface() {}; 4608c9600aSClemens Zeidler EntryCreated(WatchedFile * file)4708c9600aSClemens Zeidler virtual void EntryCreated(WatchedFile* file) {}; EntryRemoved(WatchedFile * file)4808c9600aSClemens Zeidler virtual void EntryRemoved(WatchedFile* file) {}; EntryMoved(WatchedFile * file)4908c9600aSClemens Zeidler virtual void EntryMoved(WatchedFile* file) {}; StatChanged(WatchedFile * file)5008c9600aSClemens Zeidler virtual void StatChanged(WatchedFile* file) {}; AttrChanged(WatchedFile * file)5108c9600aSClemens Zeidler virtual void AttrChanged(WatchedFile* file) {}; 5208c9600aSClemens Zeidler EntriesCleared()5308c9600aSClemens Zeidler virtual void EntriesCleared() {}; 5408c9600aSClemens Zeidler }; 5508c9600aSClemens Zeidler 5608c9600aSClemens Zeidler 5708c9600aSClemens Zeidler const uint32 kMsgAddRefs = '&adr'; 5808c9600aSClemens Zeidler const uint32 kMsgCleared = '&clr'; 5908c9600aSClemens Zeidler 6008c9600aSClemens Zeidler 6108c9600aSClemens Zeidler typedef std::vector<entry_ref> RefList; 6208c9600aSClemens Zeidler 6308c9600aSClemens Zeidler 6408c9600aSClemens Zeidler class ReadThread; 6508c9600aSClemens Zeidler 6608c9600aSClemens Zeidler 6708c9600aSClemens Zeidler class FileMonitor : public NodeMonitorHandler { 6808c9600aSClemens Zeidler public: 6908c9600aSClemens Zeidler FileMonitor(EntryViewInterface* listener); 7008c9600aSClemens Zeidler ~FileMonitor(); 7108c9600aSClemens Zeidler 7208c9600aSClemens Zeidler void SetReadThread(ReadThread* readThread); 7308c9600aSClemens Zeidler 7408c9600aSClemens Zeidler void Reset(); 7508c9600aSClemens Zeidler 7608c9600aSClemens Zeidler virtual void MessageReceived(BMessage* message); 7708c9600aSClemens Zeidler 7808c9600aSClemens Zeidler virtual void EntryCreated(const char *name, ino_t directory, 7908c9600aSClemens Zeidler dev_t device, ino_t node); 8008c9600aSClemens Zeidler virtual void EntryRemoved(const char *name, ino_t directory, 8108c9600aSClemens Zeidler dev_t device, ino_t node); 8208c9600aSClemens Zeidler virtual void EntryMoved(const char *name, 8308c9600aSClemens Zeidler const char *fromName, ino_t fromDirectory, 8408c9600aSClemens Zeidler ino_t toDirectory, dev_t device, 8508c9600aSClemens Zeidler ino_t node, dev_t nodeDevice); 8608c9600aSClemens Zeidler virtual void StatChanged(ino_t node, dev_t device, 8708c9600aSClemens Zeidler int32 statFields); 8808c9600aSClemens Zeidler virtual void AttrChanged(ino_t node, dev_t device); 8908c9600aSClemens Zeidler 9008c9600aSClemens Zeidler private: 9108c9600aSClemens Zeidler WatchedFile* _FindFile(dev_t device, ino_t node); 9208c9600aSClemens Zeidler 9308c9600aSClemens Zeidler EntryViewInterface* fListener; 9408c9600aSClemens Zeidler WatchedFileList fWatchedFileList; 9508c9600aSClemens Zeidler 9608c9600aSClemens Zeidler ReadThread* fReadThread; 9708c9600aSClemens Zeidler RefList* fCurrentReadList; 9808c9600aSClemens Zeidler uint32 fCurrentReadIndex; 9908c9600aSClemens Zeidler }; 10008c9600aSClemens Zeidler 10108c9600aSClemens Zeidler 10208c9600aSClemens Zeidler class ReadThread { 10308c9600aSClemens Zeidler public: 10408c9600aSClemens Zeidler ReadThread(FileMonitor* target); ~ReadThread()10508c9600aSClemens Zeidler virtual ~ReadThread() {} 10608c9600aSClemens Zeidler 10708c9600aSClemens Zeidler status_t Run(); 10808c9600aSClemens Zeidler bool Running(); 10908c9600aSClemens Zeidler status_t Wait(); 11008c9600aSClemens Zeidler 11108c9600aSClemens Zeidler void Stop(); 11208c9600aSClemens Zeidler bool Stopped(); 11308c9600aSClemens Zeidler 11408c9600aSClemens Zeidler RefList* ReadRefList(); 11508c9600aSClemens Zeidler void ReadDone(); 11608c9600aSClemens Zeidler 11708c9600aSClemens Zeidler protected: 11808c9600aSClemens Zeidler virtual bool ReadNextEntry(entry_ref& entry) = 0; 11908c9600aSClemens Zeidler 12008c9600aSClemens Zeidler int32 Process(); 12108c9600aSClemens Zeidler 12208c9600aSClemens Zeidler friend int32 ReadThreadFunction(void *data); 12308c9600aSClemens Zeidler 12408c9600aSClemens Zeidler BHandler* fTarget; 12508c9600aSClemens Zeidler 12608c9600aSClemens Zeidler RefList fRefList1; 12708c9600aSClemens Zeidler RefList fRefList2; 12808c9600aSClemens Zeidler RefList* fWriteRefList; 12908c9600aSClemens Zeidler RefList* fReadRefList; 13008c9600aSClemens Zeidler bool fReading; 13108c9600aSClemens Zeidler 13208c9600aSClemens Zeidler private: 13308c9600aSClemens Zeidler void _SwapLists(); 13408c9600aSClemens Zeidler inline void _PublishEntrys(BMessenger& messenger); 13508c9600aSClemens Zeidler 13608c9600aSClemens Zeidler bool fStopped; 13708c9600aSClemens Zeidler thread_id fThreadId; 13808c9600aSClemens Zeidler 13908c9600aSClemens Zeidler int16 fNReaded; 14008c9600aSClemens Zeidler bool fRunning; 14108c9600aSClemens Zeidler }; 14208c9600aSClemens Zeidler 14308c9600aSClemens Zeidler 14408c9600aSClemens Zeidler #endif // FILE_MONITOR_H 145