File: FileChangeChecker.h

package info (click to toggle)
ruby-passenger 4.0.53-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 28,668 kB
  • ctags: 70,512
  • sloc: cpp: 264,280; ruby: 25,606; sh: 22,815; ansic: 18,277; python: 767; makefile: 99; perl: 20
file content (209 lines) | stat: -rw-r--r-- 6,556 bytes parent folder | download
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
/*
 *  Phusion Passenger - https://www.phusionpassenger.com/
 *  Copyright (c) 2010 Phusion
 *
 *  "Phusion Passenger" is a trademark of Hongli Lai & Ninh Bui.
 *
 *  Permission is hereby granted, free of charge, to any person obtaining a copy
 *  of this software and associated documentation files (the "Software"), to deal
 *  in the Software without restriction, including without limitation the rights
 *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *  copies of the Software, and to permit persons to whom the Software is
 *  furnished to do so, subject to the following conditions:
 *
 *  The above copyright notice and this permission notice shall be included in
 *  all copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *  THE SOFTWARE.
 */
#ifndef _PASSENGER_CHANGE_FILE_CHECKER_H_
#define _PASSENGER_CHANGE_FILE_CHECKER_H_

#include <string>

#include <boost/thread.hpp>
#include <errno.h>

#include "CachedFileStat.hpp"
#include "SystemTime.h"

namespace Passenger {

using namespace std;
using namespace oxt;

/**
 * A utility class for checking for file changes. Example:
 *
 * @code
 * FileChangeChecker checker;
 * checker.changed("foo.txt");   // false
 * writeToFile("foo.txt");
 * checker.changed("foo.txt");   // true
 * checker.changed("foo.txt");   // false
 * @endcode
 *
 * FileChangeChecker uses stat() to retrieve file information. It also
 * supports throttling in order to limit the number of actual stat() calls.
 * This can improve performance on systems where disk I/O is a problem.
 */
class FileChangeChecker {
private:
	struct Entry {
		string filename;
		time_t lastMtime;
		time_t lastCtime;

		Entry(const string &filename) {
			this->filename  = filename;
			this->lastMtime = 0;
			this->lastCtime = 0;
		}
	};

	typedef boost::shared_ptr<Entry> EntryPtr;
	typedef list<EntryPtr> EntryList;
	typedef map<string, EntryList::iterator> EntryMap;

	CachedFileStat cstat;
	mutable boost::mutex lock;
	unsigned int maxSize;
	EntryList entries;
	EntryMap fileToEntry;

public:
	/**
	 * Create a FileChangeChecker object.
	 *
	 * @param maxSize The maximum size of the internal file list. A size of 0 means unlimited.
	 */
	FileChangeChecker(unsigned int maxSize = 0)
		: cstat(maxSize)
	{
		this->maxSize = maxSize;
	}

	/**
	 * Checks whether, since the last call to changed() with this filename,
	 * the file's timestamp has changed or whether the file has been created
	 * or removed. If the stat() call fails for any other reason (e.g. the
	 * directory is not readable) then this method will return false.
	 *
	 * If this method was called with this filename for the first time, or if
	 * information about this file has since been removed from the internal
	 * file list, then this method will return whether the file is stat()-able.
	 * That is, if the file doesn't exist then it will return false, but if
	 * the directory is not readable then it will also return false.
	 *
	 * @param filename The file to check. Note that two different filename
	 *                 strings are treated as two different files, so you should
	 *                 use absolute filenames if you change working directory
	 *                 often.
	 * @param throttleRate When set to a non-zero value, throttling will be
	 *                     enabled. stat() will be called at most once per
	 *                     throttleRate seconds.
 	 * @throws TimeRetrievalException Something went wrong while retrieving the
	 *         system time.
	 * @throws boost::thread_interrupted
	 */
	bool changed(const string &filename, unsigned int throttleRate = 0) {
		boost::unique_lock<boost::mutex> l(lock);
		EntryMap::iterator it(fileToEntry.find(filename));
		EntryPtr entry;
		struct stat buf;
		bool result, newEntry = false;
		int ret;

		if (it == fileToEntry.end()) {
			// Filename not in file list.
			// If file list is full, remove the least recently used
			// file list entry.
			if (maxSize != 0 && fileToEntry.size() == maxSize) {
				EntryList::iterator listEnd(entries.end());
				listEnd--;
				string filename((*listEnd)->filename);
				entries.pop_back();
				fileToEntry.erase(filename);
			}

			// Add to file list as most recently used.
			entry = EntryPtr(new Entry(filename));
			entries.push_front(entry);
			fileToEntry[filename] = entries.begin();
			newEntry = true;
		} else {
			// Filename is in file list.
			entry = *it->second;

			// Mark this entry as most recently used.
			entries.erase(it->second);
			entries.push_front(entry);
			fileToEntry[filename] = entries.begin();
		}

		ret = cstat.stat(filename, &buf, throttleRate);
		if (newEntry) {
			// The file's information isn't in the file list.
			if (ret == -1) {
				entry->lastMtime = 0;
				entry->lastCtime = 0;
				return false;
			} else {
				entry->lastMtime = buf.st_mtime;
				entry->lastCtime = buf.st_ctime;
				return true;
			}
		} else {
			// The file's information was already in the file list.
			if (ret == -1 && errno == ENOENT) {
				result = false;
				entry->lastMtime = 0;
				entry->lastCtime = 0;
			} else if (ret == -1) {
				result = false;
			} else {
				result = entry->lastMtime != buf.st_mtime || entry->lastCtime != buf.st_ctime;
				entry->lastMtime = buf.st_mtime;
				entry->lastCtime = buf.st_ctime;
			}
			return result;
		}
	}

	/**
	 * Change the maximum size of the internal file list.
	 *
	 * A size of 0 means unlimited.
	 */
	void setMaxSize(unsigned int maxSize) {
		boost::unique_lock<boost::mutex> l(lock);
		if (maxSize != 0) {
			int toRemove = fileToEntry.size() - maxSize;
			for (int i = 0; i < toRemove; i++) {
				string filename(entries.back()->filename);
				entries.pop_back();
				fileToEntry.erase(filename);
			}
		}
		this->maxSize = maxSize;
		cstat.setMaxSize(maxSize);
	}

	/**
	 * Returns whether <tt>filename</tt> is in the internal file list.
	 */
	bool knows(const string &filename) const {
		boost::unique_lock<boost::mutex> l(lock);
		return fileToEntry.find(filename) != fileToEntry.end();
	}
};

} // namespace Passenger

#endif /* _PASSENGER_CHANGE_FILE_CHECKER_H_ */