ext/common/CachedFileStat.cpp in passenger-2.2.2 vs ext/common/CachedFileStat.cpp in passenger-2.2.3
- old
+ new
@@ -21,99 +21,42 @@
* 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.
*/
#include "CachedFileStat.h"
+#include "CachedFileStat.hpp"
-#include <map>
-#include <list>
+extern "C" {
-#include <boost/shared_ptr.hpp>
-#include <boost/thread.hpp>
-
-using namespace std;
-using namespace boost;
-using namespace Passenger;
-
-// CachedMultiFileStat is written in C++, with a C wrapper API around it.
-// I'm not going to reinvent my own linked list and hash table in C when I
-// can just use the STL.
-struct CachedMultiFileStat {
- struct Item {
- string filename;
- CachedFileStat cstat;
-
- Item(const string &filename)
- : cstat(filename)
- {
- this->filename = filename;
- }
- };
+struct CachedFileStat {
+ Passenger::CachedFileStat cfs;
- typedef shared_ptr<Item> ItemPtr;
- typedef list<ItemPtr> ItemList;
- typedef map<string, ItemList::iterator> ItemMap;
-
- unsigned int maxSize;
- ItemList items;
- ItemMap cache;
- boost::mutex lock;
-
- CachedMultiFileStat(unsigned int maxSize) {
- this->maxSize = maxSize;
- }
-
- int stat(const string &filename, struct stat *buf, unsigned int throttleRate = 0) {
- boost::unique_lock<boost::mutex> l(lock);
- ItemMap::iterator it(cache.find(filename));
- ItemPtr item;
- int ret;
-
- if (it == cache.end()) {
- // Filename not in cache.
- // If cache is full, remove the least recently used
- // cache entry.
- if (cache.size() == maxSize) {
- ItemList::iterator listEnd(items.end());
- listEnd--;
- string filename((*listEnd)->filename);
- items.pop_back();
- cache.erase(filename);
- }
-
- // Add to cache as most recently used.
- item = ItemPtr(new Item(filename));
- items.push_front(item);
- cache[filename] = items.begin();
- } else {
- // Cache hit.
- item = *it->second;
-
- // Mark this cache item as most recently used.
- items.erase(it->second);
- items.push_front(item);
- cache[filename] = items.begin();
- }
- ret = item->cstat.refresh(throttleRate);
- *buf = item->cstat.info;
- return ret;
- }
+ CachedFileStat(unsigned int maxSize): cfs(maxSize) { }
};
-CachedMultiFileStat *
-cached_multi_file_stat_new(unsigned int max_size) {
- return new CachedMultiFileStat(max_size);
+CachedFileStat *
+cached_file_stat_new(unsigned int max_size) {
+ return new CachedFileStat(max_size);
}
void
-cached_multi_file_stat_free(CachedMultiFileStat *mstat) {
- delete mstat;
+cached_file_stat_free(CachedFileStat *cstat) {
+ delete cstat;
}
int
-cached_multi_file_stat_perform(CachedMultiFileStat *mstat,
- const char *filename,
- struct stat *buf,
- unsigned int throttle_rate)
-{
- return mstat->stat(filename, buf, throttle_rate);
+cached_file_stat_perform(CachedFileStat *cstat,
+ const char *filename,
+ struct stat *buf,
+ unsigned int throttle_rate) {
+ try {
+ return cstat->cfs.stat(filename, buf, throttle_rate);
+ } catch (const Passenger::TimeRetrievalException &e) {
+ errno = e.code();
+ return -1;
+ } catch (const boost::thread_interrupted &) {
+ errno = EINTR;
+ return -1;
+ }
}
+
+} // extern "C"