aboutsummaryrefslogtreecommitdiff
path: root/src/3rdParty/efsw/FileWatcherGeneric.cpp
blob: 074cff11286e1dd6164d26b03292aecab8eac9ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <efsw/FileSystem.hpp>
#include <efsw/FileWatcherGeneric.hpp>
#include <efsw/Lock.hpp>
#include <efsw/System.hpp>

namespace efsw {

FileWatcherGeneric::FileWatcherGeneric( FileWatcher* parent ) :
	FileWatcherImpl( parent ), mThread( NULL ), mLastWatchID( 0 ) {
	mInitOK = true;
	mIsGeneric = true;
}

FileWatcherGeneric::~FileWatcherGeneric() {
	mInitOK = false;

	efSAFE_DELETE( mThread );

	/// Delete the watches
	WatchList::iterator it = mWatches.begin();

	for ( ; it != mWatches.end(); ++it ) {
		efSAFE_DELETE( ( *it ) );
	}
}

WatchID FileWatcherGeneric::addWatch( const std::string& directory, FileWatchListener* watcher,
									  bool recursive ) {
	std::string dir( directory );

	FileSystem::dirAddSlashAtEnd( dir );

	FileInfo fi( dir );

	if ( !fi.isDirectory() ) {
		return Errors::Log::createLastError( Errors::FileNotFound, dir );
	} else if ( !fi.isReadable() ) {
		return Errors::Log::createLastError( Errors::FileNotReadable, dir );
	} else if ( pathInWatches( dir ) ) {
		return Errors::Log::createLastError( Errors::FileRepeated, dir );
	}

	std::string curPath;
	std::string link( FileSystem::getLinkRealPath( dir, curPath ) );

	if ( "" != link ) {
		if ( pathInWatches( link ) ) {
			return Errors::Log::createLastError( Errors::FileRepeated, dir );
		} else if ( !linkAllowed( curPath, link ) ) {
			return Errors::Log::createLastError( Errors::FileOutOfScope, dir );
		} else {
			dir = link;
		}
	}

	mLastWatchID++;

	WatcherGeneric* pWatch = new WatcherGeneric( mLastWatchID, dir, watcher, this, recursive );

	Lock lock( mWatchesLock );
	mWatches.push_back( pWatch );

	return pWatch->ID;
}

void FileWatcherGeneric::removeWatch( const std::string& directory ) {
	WatchList::iterator it = mWatches.begin();

	for ( ; it != mWatches.end(); ++it ) {
		if ( ( *it )->Directory == directory ) {
			WatcherGeneric* watch = ( *it );

			Lock lock( mWatchesLock );

			mWatches.erase( it );

			efSAFE_DELETE( watch );

			return;
		}
	}
}

void FileWatcherGeneric::removeWatch( WatchID watchid ) {
	WatchList::iterator it = mWatches.begin();

	for ( ; it != mWatches.end(); ++it ) {
		if ( ( *it )->ID == watchid ) {
			WatcherGeneric* watch = ( *it );

			Lock lock( mWatchesLock );

			mWatches.erase( it );

			efSAFE_DELETE( watch );

			return;
		}
	}
}

void FileWatcherGeneric::watch() {
	if ( NULL == mThread ) {
		mThread = new Thread( &FileWatcherGeneric::run, this );
		mThread->launch();
	}
}

void FileWatcherGeneric::run() {
	do {
		{
			Lock lock( mWatchesLock );

			WatchList::iterator it = mWatches.begin();

			for ( ; it != mWatches.end(); ++it ) {
				( *it )->watch();
			}
		}

		if ( mInitOK )
			System::sleep( 1000 );
	} while ( mInitOK );
}

void FileWatcherGeneric::handleAction( Watcher*, const std::string&, unsigned long, std::string ) {
	/// Not used
}

std::list<std::string> FileWatcherGeneric::directories() {
	std::list<std::string> dirs;

	Lock lock( mWatchesLock );

	WatchList::iterator it = mWatches.begin();

	for ( ; it != mWatches.end(); ++it ) {
		dirs.push_back( ( *it )->Directory );
	}

	return dirs;
}

bool FileWatcherGeneric::pathInWatches( const std::string& path ) {
	WatchList::iterator it = mWatches.begin();

	for ( ; it != mWatches.end(); ++it ) {
		if ( ( *it )->Directory == path || ( *it )->pathInWatches( path ) ) {
			return true;
		}
	}

	return false;
}

} // namespace efsw