2010-02-08 08:23:39 -08:00
|
|
|
/* Copyright (c) 2010 Wildfire Games
|
2009-04-18 10:00:33 -07:00
|
|
|
*
|
2010-02-08 08:23:39 -08:00
|
|
|
* 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.
|
2009-04-18 10:00:33 -07:00
|
|
|
*/
|
|
|
|
|
|
2004-08-01 13:14:14 -07:00
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
2007-05-03 23:21:21 -07:00
|
|
|
#include "lib/sysdep/sysdep.h"
|
2009-11-06 08:15:09 -08:00
|
|
|
#include "lib/path_util.h"
|
2010-03-03 06:14:26 -08:00
|
|
|
#include "lib/utf8.h"
|
2007-05-16 20:37:49 -07:00
|
|
|
#include "lib/sysdep/dir_watch.h"
|
2007-01-02 10:11:00 -08:00
|
|
|
#include "ps/CLogger.h"
|
2004-08-01 13:14:14 -07:00
|
|
|
|
2004-08-05 05:17:06 -07:00
|
|
|
#include <fam.h>
|
2004-08-01 13:14:14 -07:00
|
|
|
|
2009-11-14 11:12:09 -08:00
|
|
|
// FAMEvent is large (~4KB), so define a smaller structure to store events
|
2009-11-13 12:26:20 -08:00
|
|
|
struct NotificationEvent
|
|
|
|
|
{
|
2009-11-14 11:12:09 -08:00
|
|
|
std::string filename;
|
2009-11-13 12:26:20 -08:00
|
|
|
void *userdata;
|
|
|
|
|
FAMCodes code;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// To avoid deadlocks and slow synchronous reads, it's necessary to use a
|
|
|
|
|
// separate thread for reading events from FAM.
|
|
|
|
|
// So we just spawn a thread to push events into this list, then swap it out
|
|
|
|
|
// when someone calls dir_watch_Poll.
|
|
|
|
|
// (We assume STL memory allocation is thread-safe.)
|
|
|
|
|
static std::vector<NotificationEvent> g_notifications;
|
|
|
|
|
static pthread_t g_event_loop_thread;
|
|
|
|
|
|
|
|
|
|
// Mutex must wrap all accesses of g_notifications
|
|
|
|
|
// while the event loop thread is running
|
|
|
|
|
static pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;
|
2006-04-27 18:25:31 -07:00
|
|
|
|
|
|
|
|
// trool; -1 = init failed and all operations will be aborted silently.
|
|
|
|
|
// this is so that each dir_* call doesn't complain if the system's
|
|
|
|
|
// FAM is broken or unavailable.
|
|
|
|
|
static int initialized = 0;
|
2004-08-01 13:14:14 -07:00
|
|
|
|
2009-11-06 08:15:09 -08:00
|
|
|
static FAMConnection fc;
|
|
|
|
|
|
|
|
|
|
struct DirWatch
|
|
|
|
|
{
|
|
|
|
|
DirWatch()
|
|
|
|
|
: reqnum(-1)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~DirWatch()
|
|
|
|
|
{
|
2009-11-06 10:35:32 -08:00
|
|
|
debug_assert(initialized > 0);
|
2009-11-06 08:15:09 -08:00
|
|
|
|
|
|
|
|
FAMRequest req;
|
|
|
|
|
req.reqnum = reqnum;
|
|
|
|
|
FAMCancelMonitor(&fc, &req);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fs::wpath path;
|
|
|
|
|
int reqnum;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2006-04-02 18:00:45 -07:00
|
|
|
// for atexit
|
|
|
|
|
static void fam_deinit()
|
2005-05-17 22:32:09 -07:00
|
|
|
{
|
|
|
|
|
FAMClose(&fc);
|
2009-11-13 12:26:20 -08:00
|
|
|
|
|
|
|
|
pthread_cancel(g_event_loop_thread);
|
|
|
|
|
// NOTE: POSIX threads are (by default) only cancellable inside particular
|
|
|
|
|
// functions (like 'select'), so this should safely terminate while it's
|
|
|
|
|
// in select/FAMNextEvent/etc (and won't e.g. cancel while it's holding the
|
|
|
|
|
// mutex)
|
|
|
|
|
|
|
|
|
|
// Wait for the thread to finish
|
|
|
|
|
pthread_join(g_event_loop_thread, NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void fam_event_loop_process_events()
|
|
|
|
|
{
|
|
|
|
|
while(FAMPending(&fc) > 0)
|
|
|
|
|
{
|
|
|
|
|
FAMEvent e;
|
|
|
|
|
if(FAMNextEvent(&fc, &e) < 0)
|
|
|
|
|
{
|
|
|
|
|
debug_printf(L"FAMNextEvent error");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NotificationEvent ne;
|
2009-11-14 11:12:09 -08:00
|
|
|
ne.filename = e.filename;
|
2009-11-13 12:26:20 -08:00
|
|
|
ne.userdata = e.userdata;
|
|
|
|
|
ne.code = e.code;
|
|
|
|
|
|
|
|
|
|
pthread_mutex_lock(&g_mutex);
|
|
|
|
|
g_notifications.push_back(ne);
|
|
|
|
|
pthread_mutex_unlock(&g_mutex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void* fam_event_loop(void*)
|
|
|
|
|
{
|
|
|
|
|
int famfd = FAMCONNECTION_GETFD(&fc);
|
|
|
|
|
|
2009-11-14 11:12:09 -08:00
|
|
|
while(true)
|
2009-11-13 12:26:20 -08:00
|
|
|
{
|
|
|
|
|
fd_set fdrset;
|
|
|
|
|
FD_ZERO(&fdrset);
|
|
|
|
|
FD_SET(famfd, &fdrset);
|
|
|
|
|
|
|
|
|
|
// Block with select until there's events waiting
|
|
|
|
|
// (Mustn't just block inside FAMNextEvent since fam will deadlock)
|
|
|
|
|
while(select(famfd+1, &fdrset, NULL, NULL, NULL) < 0)
|
|
|
|
|
{
|
|
|
|
|
if(errno == EINTR)
|
|
|
|
|
{
|
|
|
|
|
// interrupted - try again
|
|
|
|
|
FD_ZERO(&fdrset);
|
|
|
|
|
FD_SET(famfd, &fdrset);
|
|
|
|
|
}
|
|
|
|
|
else if(errno == EBADF)
|
|
|
|
|
{
|
|
|
|
|
// probably just lost the connection to FAM - kill the thread
|
|
|
|
|
debug_printf(L"lost connection to FAM");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// oops
|
2009-11-14 11:12:09 -08:00
|
|
|
debug_printf(L"select error %d", errno);
|
2009-11-13 12:26:20 -08:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(FD_ISSET(famfd, &fdrset))
|
|
|
|
|
fam_event_loop_process_events();
|
|
|
|
|
}
|
2005-05-17 22:32:09 -07:00
|
|
|
}
|
|
|
|
|
|
2009-11-06 08:15:09 -08:00
|
|
|
LibError dir_watch_Add(const fs::wpath& path, PDirWatch& dirWatch)
|
2004-08-01 13:14:14 -07:00
|
|
|
{
|
2006-04-27 18:25:31 -07:00
|
|
|
// init already failed; don't try again or complain
|
|
|
|
|
if(initialized == -1)
|
2006-09-30 08:46:40 -07:00
|
|
|
return ERR::FAIL; // NOWARN
|
2006-04-27 18:25:31 -07:00
|
|
|
|
2004-08-01 13:14:14 -07:00
|
|
|
if(!initialized)
|
|
|
|
|
{
|
2009-11-13 12:26:20 -08:00
|
|
|
if(FAMOpen2(&fc, "lib_res"))
|
2006-04-27 18:25:31 -07:00
|
|
|
{
|
2009-11-13 12:26:20 -08:00
|
|
|
initialized = -1;
|
|
|
|
|
LOGERROR(L"Error initializing FAM; hotloading will be disabled");
|
|
|
|
|
return ERR::FAIL; // NOWARN
|
2006-04-27 18:25:31 -07:00
|
|
|
}
|
2009-11-13 12:26:20 -08:00
|
|
|
|
|
|
|
|
if (pthread_create(&g_event_loop_thread, NULL, &fam_event_loop, NULL))
|
2006-04-27 18:25:31 -07:00
|
|
|
{
|
|
|
|
|
initialized = -1;
|
2009-11-13 12:26:20 -08:00
|
|
|
LOGERROR(L"Error creating FAM event loop thread; hotloading will be disabled");
|
2006-09-30 08:46:40 -07:00
|
|
|
return ERR::FAIL; // NOWARN
|
2006-04-27 18:25:31 -07:00
|
|
|
}
|
2009-11-13 12:26:20 -08:00
|
|
|
|
|
|
|
|
initialized = 1;
|
|
|
|
|
atexit(fam_deinit);
|
2004-08-01 13:14:14 -07:00
|
|
|
}
|
|
|
|
|
|
2009-11-06 08:15:09 -08:00
|
|
|
PDirWatch tmpDirWatch(new DirWatch);
|
|
|
|
|
|
2009-11-13 12:26:20 -08:00
|
|
|
// NOTE: It would be possible to use FAMNoExists iff we're building with Gamin
|
|
|
|
|
// (not FAM), to avoid a load of boring notifications when we add a directory,
|
|
|
|
|
// but it would only save tens of milliseconds of CPU time, so it's probably
|
|
|
|
|
// not worthwhile
|
|
|
|
|
|
2009-11-06 08:15:09 -08:00
|
|
|
const fs::path path_c = path_from_wpath(path);
|
2004-08-01 13:14:14 -07:00
|
|
|
FAMRequest req;
|
2009-11-06 08:15:09 -08:00
|
|
|
if(FAMMonitorDirectory(&fc, path_c.string().c_str(), &req, tmpDirWatch.get()) < 0)
|
2004-08-01 13:14:14 -07:00
|
|
|
{
|
2009-11-06 10:35:32 -08:00
|
|
|
debug_warn(L"res_watch_dir failed!");
|
2006-09-30 08:46:40 -07:00
|
|
|
WARN_RETURN(ERR::FAIL); // no way of getting error code?
|
2004-08-01 13:14:14 -07:00
|
|
|
}
|
|
|
|
|
|
2009-11-06 08:15:09 -08:00
|
|
|
dirWatch.swap(tmpDirWatch);
|
|
|
|
|
dirWatch->path = path;
|
|
|
|
|
dirWatch->reqnum = req.reqnum;
|
|
|
|
|
|
2006-09-30 08:46:40 -07:00
|
|
|
return INFO::OK;
|
2004-08-01 13:14:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-11-06 08:15:09 -08:00
|
|
|
LibError dir_watch_Poll(DirWatchNotifications& notifications)
|
2004-08-01 13:14:14 -07:00
|
|
|
{
|
2006-04-27 18:25:31 -07:00
|
|
|
if(initialized == -1)
|
2006-09-30 08:46:40 -07:00
|
|
|
return ERR::FAIL; // NOWARN
|
2006-11-11 20:02:36 -08:00
|
|
|
if(!initialized) // XXX Fix Atlas instead of supressing the warning
|
|
|
|
|
return ERR::FAIL; //WARN_RETURN(ERR::LOGIC);
|
2004-08-01 13:14:14 -07:00
|
|
|
|
2009-11-13 12:26:20 -08:00
|
|
|
std::vector<NotificationEvent> polled_notifications;
|
|
|
|
|
|
|
|
|
|
pthread_mutex_lock(&g_mutex);
|
|
|
|
|
g_notifications.swap(polled_notifications);
|
|
|
|
|
pthread_mutex_unlock(&g_mutex);
|
|
|
|
|
|
|
|
|
|
for(size_t i = 0; i < polled_notifications.size(); ++i)
|
2006-04-02 18:00:45 -07:00
|
|
|
{
|
2009-11-13 12:26:20 -08:00
|
|
|
DirWatchNotification::EType type;
|
|
|
|
|
switch(polled_notifications[i].code)
|
2004-08-01 13:14:14 -07:00
|
|
|
{
|
2009-11-13 12:26:20 -08:00
|
|
|
case FAMChanged:
|
|
|
|
|
type = DirWatchNotification::Changed;
|
|
|
|
|
break;
|
|
|
|
|
case FAMCreated:
|
|
|
|
|
type = DirWatchNotification::Created;
|
|
|
|
|
break;
|
|
|
|
|
case FAMDeleted:
|
|
|
|
|
type = DirWatchNotification::Deleted;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
continue;
|
2004-08-01 13:14:14 -07:00
|
|
|
}
|
2009-11-13 12:26:20 -08:00
|
|
|
DirWatch* dirWatch = (DirWatch*)polled_notifications[i].userdata;
|
|
|
|
|
fs::wpath pathname = dirWatch->path/wstring_from_utf8(polled_notifications[i].filename);
|
|
|
|
|
notifications.push_back(DirWatchNotification(pathname, type));
|
2006-04-02 18:00:45 -07:00
|
|
|
}
|
2004-08-01 13:14:14 -07:00
|
|
|
|
2009-11-06 08:15:09 -08:00
|
|
|
// nothing new; try again later
|
|
|
|
|
return INFO::OK;
|
2004-08-01 13:14:14 -07:00
|
|
|
}
|