aboutsummaryrefslogtreecommitdiff
path: root/src/3rdParty/efsw/DirectorySnapshot.cpp
blob: 6049e4a7d0f82aadc2e51fa2a055b75465e9deca (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <efsw/DirectorySnapshot.hpp>
#include <efsw/FileSystem.hpp>

namespace efsw {

DirectorySnapshot::DirectorySnapshot() {}

DirectorySnapshot::DirectorySnapshot( std::string directory ) {
	init( directory );
}

DirectorySnapshot::~DirectorySnapshot() {}

void DirectorySnapshot::init( std::string directory ) {
	setDirectoryInfo( directory );
	initFiles();
}

bool DirectorySnapshot::exists() {
	return DirectoryInfo.exists();
}

void DirectorySnapshot::deleteAll( DirectorySnapshotDiff& Diff ) {
	FileInfo fi;

	for ( FileInfoMap::iterator it = Files.begin(); it != Files.end(); it++ ) {
		fi = it->second;

		if ( fi.isDirectory() ) {
			Diff.DirsDeleted.push_back( fi );
		} else {
			Diff.FilesDeleted.push_back( fi );
		}
	}

	Files.clear();
}

void DirectorySnapshot::setDirectoryInfo( std::string directory ) {
	DirectoryInfo = FileInfo( directory );
}

void DirectorySnapshot::initFiles() {
	Files = FileSystem::filesInfoFromPath( DirectoryInfo.Filepath );

	FileInfoMap::iterator it = Files.begin();
	std::list<std::string> eraseFiles;

	/// Remove all non regular files and non directories
	for ( ; it != Files.end(); it++ ) {
		if ( !it->second.isRegularFile() && !it->second.isDirectory() ) {
			eraseFiles.push_back( it->first );
		}
	}

	for ( std::list<std::string>::iterator eit = eraseFiles.begin(); eit != eraseFiles.end();
		  eit++ ) {
		Files.erase( *eit );
	}
}

DirectorySnapshotDiff DirectorySnapshot::scan() {
	DirectorySnapshotDiff Diff;

	Diff.clear();

	FileInfo curFI( DirectoryInfo.Filepath );

	Diff.DirChanged = DirectoryInfo != curFI;

	if ( Diff.DirChanged ) {
		DirectoryInfo = curFI;
	}

	/// If the directory was erased, create the events for files and directories deletion
	if ( !curFI.exists() ) {
		deleteAll( Diff );

		return Diff;
	}

	FileInfoMap files = FileSystem::filesInfoFromPath( DirectoryInfo.Filepath );

	if ( files.empty() && Files.empty() ) {
		return Diff;
	}

	FileInfo fi;
	FileInfoMap FilesCpy;
	FileInfoMap::iterator it;
	FileInfoMap::iterator fiIt;

	if ( Diff.DirChanged ) {
		FilesCpy = Files;
	}

	for ( it = files.begin(); it != files.end(); it++ ) {
		fi = it->second;

		/// File existed before?
		fiIt = Files.find( it->first );

		if ( fiIt != Files.end() ) {
			/// Erase from the file list copy
			FilesCpy.erase( it->first );

			/// File changed?
			if ( ( *fiIt ).second != fi ) {
				/// Update the new file info
				Files[it->first] = fi;

				/// handle modified event
				if ( fi.isDirectory() ) {
					Diff.DirsModified.push_back( fi );
				} else {
					Diff.FilesModified.push_back( fi );
				}
			}
		}
		/// Only add regular files or directories
		else if ( fi.isRegularFile() || fi.isDirectory() ) {
			/// New file found
			Files[it->first] = fi;

			FileInfoMap::iterator fit;
			std::string oldFile = "";

			/// Check if the same inode already existed
			if ( ( fit = nodeInFiles( fi ) ) != Files.end() ) {
				oldFile = fit->first;

				/// Avoid firing a Delete event
				FilesCpy.erase( fit->first );

				/// Delete the old file name
				Files.erase( fit->first );

				if ( fi.isDirectory() ) {
					Diff.DirsMoved.push_back( std::make_pair( oldFile, fi ) );
				} else {
					Diff.FilesMoved.push_back( std::make_pair( oldFile, fi ) );
				}
			} else {
				if ( fi.isDirectory() ) {
					Diff.DirsCreated.push_back( fi );
				} else {
					Diff.FilesCreated.push_back( fi );
				}
			}
		}
	}

	if ( !Diff.DirChanged ) {
		return Diff;
	}

	/// The files or directories that remains were deleted
	for ( it = FilesCpy.begin(); it != FilesCpy.end(); it++ ) {
		fi = it->second;

		if ( fi.isDirectory() ) {
			Diff.DirsDeleted.push_back( fi );
		} else {
			Diff.FilesDeleted.push_back( fi );
		}

		/// Remove the file or directory from the list of files
		Files.erase( it->first );
	}

	return Diff;
}

FileInfoMap::iterator DirectorySnapshot::nodeInFiles( FileInfo& fi ) {
	FileInfoMap::iterator it;

	if ( FileInfo::inodeSupported() ) {
		for ( it = Files.begin(); it != Files.end(); it++ ) {
			if ( it->second.sameInode( fi ) && it->second.Filepath != fi.Filepath ) {
				return it;
			}
		}
	}

	return Files.end();
}

void DirectorySnapshot::addFile( std::string path ) {
	std::string name( FileSystem::fileNameFromPath( path ) );
	Files[name] = FileInfo( path );
}

void DirectorySnapshot::removeFile( std::string path ) {
	std::string name( FileSystem::fileNameFromPath( path ) );

	FileInfoMap::iterator it = Files.find( name );

	if ( Files.end() != it ) {
		Files.erase( it );
	}
}

void DirectorySnapshot::moveFile( std::string oldPath, std::string newPath ) {
	removeFile( oldPath );
	addFile( newPath );
}

void DirectorySnapshot::updateFile( std::string path ) {
	addFile( path );
}

} // namespace efsw