2004-08-01 22:14:14 +02:00
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
#include <string>
|
|
|
|
|
2006-06-02 04:10:27 +02:00
|
|
|
#include "lib/lib.h"
|
|
|
|
#include "lib/res/file/file.h"
|
2004-08-01 22:14:14 +02:00
|
|
|
|
2004-08-05 14:17:06 +02:00
|
|
|
#include <fam.h>
|
2004-08-01 22:14:14 +02:00
|
|
|
|
|
|
|
static FAMConnection fc;
|
2006-04-28 03:25:31 +02: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 22:14:14 +02:00
|
|
|
|
|
|
|
static std::map<intptr_t, std::string> dirs;
|
|
|
|
|
2006-04-03 03:00:45 +02:00
|
|
|
// for atexit
|
|
|
|
static void fam_deinit()
|
2005-05-18 07:32:09 +02:00
|
|
|
{
|
|
|
|
FAMClose(&fc);
|
|
|
|
}
|
|
|
|
|
2006-04-03 03:00:45 +02:00
|
|
|
LibError dir_add_watch(const char* const n_full_path, intptr_t* const watch)
|
2004-08-01 22:14:14 +02:00
|
|
|
{
|
2006-04-28 03:25:31 +02:00
|
|
|
// init already failed; don't try again or complain
|
|
|
|
if(initialized == -1)
|
2006-09-30 17:46:40 +02:00
|
|
|
return ERR::FAIL; // NOWARN
|
2006-04-28 03:25:31 +02:00
|
|
|
|
2004-08-01 22:14:14 +02:00
|
|
|
if(!initialized)
|
|
|
|
{
|
2006-04-28 03:25:31 +02:00
|
|
|
// success
|
|
|
|
if(FAMOpen2(&fc, "lib_res") == 0)
|
|
|
|
{
|
|
|
|
initialized = 1;
|
|
|
|
atexit(fam_deinit);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
initialized = -1;
|
|
|
|
DISPLAY_ERROR(L"Error initializing FAM; hotloading will be disabled");
|
2006-09-30 17:46:40 +02:00
|
|
|
return ERR::FAIL; // NOWARN
|
2006-04-28 03:25:31 +02:00
|
|
|
}
|
2004-08-01 22:14:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
FAMRequest req;
|
|
|
|
if(FAMMonitorDirectory(&fc, n_full_path, &req, (void*)0) < 0)
|
|
|
|
{
|
|
|
|
*watch = -1;
|
|
|
|
debug_warn("res_watch_dir failed!");
|
2006-09-30 17:46:40 +02:00
|
|
|
WARN_RETURN(ERR::FAIL); // no way of getting error code?
|
2004-08-01 22:14:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
*watch = (intptr_t)req.reqnum;
|
|
|
|
dirs[*watch] = n_full_path;
|
2006-09-30 17:46:40 +02:00
|
|
|
return INFO::OK;
|
2004-08-01 22:14:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-04-03 03:00:45 +02:00
|
|
|
LibError dir_cancel_watch(const intptr_t watch)
|
2004-08-01 22:14:14 +02:00
|
|
|
{
|
2006-04-28 03:25:31 +02:00
|
|
|
if(initialized == -1)
|
2006-09-30 17:46:40 +02:00
|
|
|
return ERR::FAIL; // NOWARN
|
2004-08-01 22:14:14 +02:00
|
|
|
if(!initialized)
|
2006-09-30 17:46:40 +02:00
|
|
|
WARN_RETURN(ERR::LOGIC);
|
2004-08-01 22:14:14 +02:00
|
|
|
|
|
|
|
FAMRequest req;
|
|
|
|
req.reqnum = (int)watch;
|
2006-04-20 03:33:57 +02:00
|
|
|
RETURN_ERR(FAMCancelMonitor(&fc, &req));
|
2006-09-30 17:46:40 +02:00
|
|
|
return INFO::OK;
|
2004-08-01 22:14:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int dir_get_changed_file(char* fn)
|
|
|
|
{
|
2006-04-28 03:25:31 +02:00
|
|
|
if(initialized == -1)
|
2006-09-30 17:46:40 +02:00
|
|
|
return ERR::FAIL; // NOWARN
|
2004-08-01 22:14:14 +02:00
|
|
|
if(!initialized)
|
2006-09-30 17:46:40 +02:00
|
|
|
WARN_RETURN(ERR::LOGIC);
|
2004-08-01 22:14:14 +02:00
|
|
|
|
|
|
|
FAMEvent e;
|
|
|
|
while(FAMPending(&fc) > 0)
|
2006-04-03 03:00:45 +02:00
|
|
|
{
|
2004-08-01 22:14:14 +02:00
|
|
|
if(FAMNextEvent(&fc, &e) >= 0)
|
|
|
|
{
|
2005-01-17 05:52:02 +01:00
|
|
|
if (e.code == FAMChanged || e.code == FAMCreated || e.code == FAMDeleted)
|
|
|
|
{
|
|
|
|
char n_path[PATH_MAX];
|
|
|
|
const char* dir = dirs[e.fr.reqnum].c_str();
|
|
|
|
snprintf(n_path, PATH_MAX, "%s%c%s", dir, DIR_SEP, e.filename);
|
2006-04-22 23:21:42 +02:00
|
|
|
RETURN_ERR(file_make_portable_path(n_path, fn));
|
2006-09-30 17:46:40 +02:00
|
|
|
return INFO::OK;
|
2005-01-17 05:52:02 +01:00
|
|
|
}
|
2004-08-01 22:14:14 +02:00
|
|
|
}
|
2006-04-03 03:00:45 +02:00
|
|
|
}
|
2004-08-01 22:14:14 +02:00
|
|
|
|
2006-04-28 03:25:31 +02:00
|
|
|
// just nothing new; try again later
|
2006-09-30 17:46:40 +02:00
|
|
|
return ERR::AGAIN; // NOWARN
|
2004-08-01 22:14:14 +02:00
|
|
|
}
|