int luv_new_fs_watcher (lua_State* L) { int before = lua_gettop(L); const char* filename = luaL_checkstring(L, 1); luv_ref_t* ref; uv_fs_event_t* handle = (uv_fs_event_t*)lua_newuserdata(L, sizeof(uv_fs_event_t)); uv_fs_event_init(luv_get_loop(L), handle, filename, luv_on_fs_event, 0); /* Set metatable for type */ luaL_getmetatable(L, "luv_fs_watcher"); lua_setmetatable(L, -2); /* Create a local environment for storing stuff */ lua_newtable(L); lua_setfenv (L, -2); /* Store a reference to the userdata in the handle */ ref = (luv_ref_t*)malloc(sizeof(luv_ref_t)); ref->L = L; lua_pushvalue(L, -1); /* duplicate so we can _ref it */ ref->r = luaL_ref(L, LUA_REGISTRYINDEX); handle->data = ref; assert(lua_gettop(L) == before + 1); /* return the userdata */ return 1; }
/* _unix_watch_dir(): initialize directory */ static void _unix_watch_dir(u3_udir* dir_u, u3_udir* par_u, c3_c* pax_c) { // initialize dir_u dir_u->dir = c3y; dir_u->dry = c3n; dir_u->pax_c = pax_c; dir_u->par_u = par_u; dir_u->nex_u = NULL; dir_u->kid_u = NULL; if ( par_u ) { dir_u->nex_u = par_u->kid_u; par_u->kid_u = (u3_unod*) dir_u; } // stuff dir_u into libuv // note that we're doing something tricky here // see comment in _unix_fs_event_cb c3_w ret_w = uv_fs_event_init(u3L, &dir_u->was_u); if (0 != ret_w) { uL(fprintf(uH, "directory event init: %s\n", uv_strerror(ret_w))); c3_assert(0); } ret_w = uv_fs_event_start(&dir_u->was_u, _unix_fs_event_cb, pax_c, 0); if (0 != ret_w) { uL(fprintf(uH, "directory event start: %s\n", uv_strerror(ret_w))); c3_assert(0); } }
/* _unix_file_watch(): create file tracker (from filesystem) */ static void _unix_file_watch(u2_ufil* fil_u, u2_udir* dir_u, c3_c* pax_c, mpz_t mod_mp) { uv_fs_event_init(u2L, &fil_u->was_u, pax_c, _unix_fs_event_cb, 0); // uL(fprintf(uH, "file: got: %s (handle %d)\n", pax_c, fil_u->was_u.type)); fil_u->non = u2_no; fil_u->dry = u2_no; fil_u->pax_c = pax_c; { c3_c* dot_c = strrchr(pax_c, '.'); c3_c* fas_c = strrchr(pax_c, '/'); fil_u->dot_c = dot_c ? (fas_c ? ((dot_c > fas_c) ? dot_c : 0) : dot_c) : 0; } fil_u->par_u = dir_u; mpz_init_set(fil_u->mod_mp, mod_mp); fil_u->nex_u = 0; c3_assert(!fil_u->dot_c || (fil_u->dot_c > fil_u->pax_c)); }
/* _unix_dir_watch(): instantiate directory tracker. */ static void _unix_dir_watch(u2_udir* dir_u, u2_udir* par_u, c3_c* pax_c) { // (1) build data structure // dir_u->yes = u2_yes; dir_u->dry = u2_no; dir_u->pax_c = pax_c; dir_u->par_u = par_u; dir_u->dis_u = 0; dir_u->fil_u = 0; dir_u->nex_u = 0; // (2) stuff data structure into libuv // c3_w ret_w = uv_fs_event_init(u2L, &dir_u->was_u ); if (0 != ret_w){ uL(fprintf(uH, "event init: %s\n", uv_strerror(ret_w))); c3_assert(0); } // note that we're doing something tricky here; see comment in _unix_fs_event_cb // ret_w = uv_fs_event_start(&dir_u->was_u, _unix_fs_event_cb, pax_c, 0); if (0 != ret_w){ uL(fprintf(uH, "event start: %s\n", uv_strerror(ret_w))); c3_assert(0); } }
/* _unix_dir_forge: instantiate directory tracker (and make directory). */ static void _unix_dir_forge(u2_udir* dir_u, u2_udir* par_u, u2_noun tet) { dir_u->yes = u2_yes; dir_u->dry = u2_no; { c3_c* tet_c = u2_cr_string(tet); c3_w pax_w = strlen(par_u->pax_c); c3_w tet_w = strlen(tet_c); c3_c* pax_c = c3_malloc(pax_w + 1 + tet_w + 1); strncpy(pax_c, par_u->pax_c, pax_w + 1); pax_c[pax_w] = '/'; strncpy(pax_c + pax_w + 1, tet_c, tet_w + 1); pax_c[pax_w + tet_w + 1] = '\0'; free(tet_c); u2z(tet); uv_fs_event_init(u2L, &dir_u->was_u, pax_c, _unix_fs_event_cb, 0); _unix_mkdir(pax_c); dir_u->pax_c = pax_c; } dir_u->par_u = par_u; dir_u->dis_u = 0; dir_u->fil_u = 0; dir_u->nex_u = 0; }
/* _unix_watch_file(): initialize file */ static void _unix_watch_file(u3_ufil* fil_u, u3_udir* par_u, c3_c* pax_c) { // initialize fil_u fil_u->dir = c3n; fil_u->dry = c3n; fil_u->pax_c = pax_c; fil_u->par_u = par_u; fil_u->nex_u = NULL; fil_u->mug_w = 0; fil_u->gum_w = 0; if ( par_u ) { fil_u->nex_u = par_u->kid_u; par_u->kid_u = (u3_unod*) fil_u; } // stuff fil_u into libuv // note that we're doing something tricky here // see comment in _unix_fs_event_cb c3_w ret_w = uv_fs_event_init(u3L, &fil_u->was_u); if (0 != ret_w) { uL(fprintf(uH, "file event init: %s\n", uv_strerror(ret_w))); c3_assert(0); } ret_w = uv_fs_event_start(&fil_u->was_u, _unix_fs_event_cb, pax_c, 0); if ( 0 != ret_w ) { uL(fprintf(uH, "file event start %s: %s\n", fil_u->pax_c, uv_strerror(ret_w))); c3_assert(0); } }
uv_fs_event_t* bdg_new_watcher(uv_loop_t* loop, void* callback) { uv_fs_event_t* handle = malloc(sizeof(uv_fs_event_t)); uv_fs_event_init(loop, handle); handle->data = callback; return handle; }
static int luv_new_fs_event(lua_State* L) { uv_fs_event_t* handle = lua_newuserdata(L, sizeof(*handle)); int ret = uv_fs_event_init(luv_loop(L), handle); if (ret < 0) { lua_pop(L, 1); return luv_error(L, ret); } handle->data = luv_setup_handle(L); return 1; }
static void timer_cb(uv_timer_t* handle) { int r; r = uv_fs_event_init(handle->loop, &fs_event); ASSERT(r == 0); r = uv_fs_event_start(&fs_event, fs_event_fail, ".", 0); ASSERT(r == 0); uv_close((uv_handle_t*)&fs_event, close_cb); uv_close((uv_handle_t*)handle, close_cb); }
static void timer_cb(uv_timer_t* handle, int status) { int r; ASSERT(status == 0); r = uv_fs_event_init(handle->loop, &fs_event, ".", fs_event_fail, 0); ASSERT(r == 0); uv_close((uv_handle_t*)&fs_event, close_cb); uv_close((uv_handle_t*)handle, close_cb); }
/* _unix_dir_watch(): instantiate directory tracker. */ static void _unix_dir_watch(u2_udir* dir_u, u2_udir* par_u, c3_c* pax_c) { uv_fs_event_init(u2L, &dir_u->was_u, pax_c, _unix_fs_event_cb, 0); dir_u->yes = u2_yes; dir_u->dry = u2_no; dir_u->pax_c = pax_c; dir_u->par_u = par_u; dir_u->dis_u = 0; dir_u->fil_u = 0; dir_u->nex_u = 0; }
/* _unix_file_watch(): create file tracker (from filesystem) */ static void _unix_file_watch(u2_ufil* fil_u, u2_udir* dir_u, c3_c* pax_c, mpz_t mod_mp) { // (1) build data structure // fil_u->non = u2_no; fil_u->dry = u2_no; fil_u->pax_c = pax_c; { c3_c* dot_c = strrchr(pax_c, '.'); c3_c* fas_c = strrchr(pax_c, '/'); fil_u->dot_c = dot_c ? (fas_c ? ((dot_c > fas_c) ? dot_c : 0) : dot_c) : 0; } fil_u->par_u = dir_u; mpz_init_set(fil_u->mod_mp, mod_mp); fil_u->nex_u = 0; c3_assert(!fil_u->dot_c || (fil_u->dot_c > fil_u->pax_c)); // (2) stuff data structure into libuv // c3_w ret_w = uv_fs_event_init(u2L, // loop &fil_u->was_u // uv_fs_event_t ); if (0 != ret_w){ uL(fprintf(uH, "event init: %s\n", strerror(ret_w))); c3_assert(0); } // note that we're doing something tricky here; see comment in _unix_fs_event_cb // ret_w = uv_fs_event_start(&fil_u->was_u, // uv_fs_event_t _unix_fs_event_cb, // callback pax_c, // dir as strings 0); // flags if (0 != ret_w){ uL(fprintf(uH, "event start: %s\n", strerror(ret_w))); c3_assert(0); } }
int main(int argc, char **argv) { if (argc <= 2) { fprintf(stderr, "Usage: %s <command> <file1> [file2 ...]\n", argv[0]); return 1; } loop = uv_default_loop(); command = argv[1]; while (argc-- > 2) { fprintf(stderr, "Adding watch on %s\n", argv[argc]); uv_fs_event_t *fs_event_req = malloc(sizeof(uv_fs_event_t)); uv_fs_event_init(loop, fs_event_req); // The recursive flag watches subdirectories too. uv_fs_event_start(fs_event_req, run_command, argv[argc], UV_FS_EVENT_RECURSIVE); } return uv_run(loop, UV_RUN_DEFAULT); }
/* * Class: com_iwebpp_libuvpp_handles_FileEventHandle * Method: _start * Signature: (JJLjava/lang/String;Z)I */ extern "C" JNIEXPORT jint JNICALL Java_com_iwebpp_libuvpp_handles_FileEventHandle__1start (JNIEnv *env, jobject that, jlong loop_ptr, jlong fs_event_ptr, jstring path, jboolean persistent) { assert(loop_ptr); assert(fs_event_ptr); uv_loop_t* loop = reinterpret_cast<uv_loop_t*>(loop_ptr); uv_fs_event_t* handle = reinterpret_cast<uv_fs_event_t*>(fs_event_ptr); const char* cpath = env->GetStringUTFChars(path, 0); int r = uv_fs_event_init(loop, handle, cpath, on_event_cb, 0); if (r == 0 && !persistent) { uv_unref(reinterpret_cast<uv_handle_t*>(handle)); } if (r) { ThrowException(env, loop, "uv_fs_event_init"); } env->ReleaseStringUTFChars(path, cpath); return r; }
int main(int argc, char *argv[]) { if (argc <= 1) { fprintf(stderr, "Usage: %s <file1> [file2 ...]\n", argv[0]); return 1; } //const char *command = argv[1]; for (int i = 1; i < argc; ++i) { fprintf(stderr, "Adding watch on %s\n", argv[i]); uv_fs_event_t *fs_event = (uv_fs_event_t*)malloc(sizeof(uv_fs_event_t)); int r = uv_fs_event_init(uv_default_loop(), fs_event); assert(r == 0); r = uv_fs_event_start(fs_event, OnChange, argv[i], 0); assert(r == 0); } uv_run(uv_default_loop(), UV_RUN_DEFAULT); return 0; }
static void assert_watch_file_current_dir(uv_loop_t* const loop, int file_or_dir) { uv_timer_t timer; uv_fs_event_t fs_event; int r; /* Setup */ remove("watch_file"); create_file("watch_file"); r = uv_fs_event_init(loop, &fs_event); ASSERT(r == 0); /* watching a dir is the only way to get fsevents involved on apple platforms */ r = uv_fs_event_start(&fs_event, fs_event_cb_file_current_dir, file_or_dir == 1 ? "." : "watch_file", 0); ASSERT(r == 0); r = uv_timer_init(loop, &timer); ASSERT(r == 0); r = uv_timer_start(&timer, timer_cb_touch, 100, 0); ASSERT(r == 0); ASSERT(timer_cb_touch_called == 0); ASSERT(fs_event_cb_called == 0); uv_run(loop, UV_RUN_DEFAULT); ASSERT(timer_cb_touch_called == 1); ASSERT(fs_event_cb_called == 1); /* Cleanup */ remove("watch_file"); fs_event_cb_called = 0; timer_cb_touch_called = 0; uv_run(loop, UV_RUN_DEFAULT); /* flush pending closes */ }
void setup() { setupAntTweakBar(); checkCudaErrors(cuInit(0)); if (sketchArgc != 2) { printf("Usage: %s <cuda_toy.cu>\n", sketchArgv[0]); quit(); return; } if (RMT_ERROR_NONE != rmt_CreateGlobalInstance(&rmt)) { //return -1; } int r = uv_fs_event_init(uv_default_loop(), &fs_event); r = uv_fs_event_start(&fs_event, on_fs_event_cb, sketchArgv[1], 0); char title[256]; sprintf(title, "CUDA ShaderToy - %s", sketchArgv[1]); glfwSetWindowTitle(window, title); setupModuleResource(sketchArgv[1]); setupSizeResource(); rmtCUDABind bind; bind.context = cuContext; bind.CtxSetCurrent = &cuCtxSetCurrent; bind.CtxGetCurrent = &cuCtxGetCurrent; bind.EventCreate = &cuEventCreate; bind.EventDestroy = &cuEventDestroy; bind.EventRecord = &cuEventRecord; bind.EventQuery = &cuEventQuery; bind.EventElapsedTime = &cuEventElapsedTime; rmt_BindCUDA(&bind); rmt_BindOpenGL(); }
int DirWatcher::init(std::string dir, dirwatch_on_rename_callback cb, void* usr) { int r = 0; directory = dir; if (NULL != loop) { RX_ERROR("Looks like we're already initialize, call shutdown() first."); return -1; } if (false == rx_is_dir(directory)) { RX_ERROR("Not a directory: %s", directory.c_str()); return -2; } loop = uv_default_loop(); r = uv_fs_event_init(loop, &fs_event); if (0 != r) { RX_ERROR("Cannot initialize the fs event: %s", uv_strerror(r)); loop = NULL; return -3; } r = uv_fs_event_start(&fs_event, on_dir_change, directory.c_str(), 0); if (0 != r) { RX_ERROR("Cannot start the fs event: %s", uv_strerror(r)); loop = NULL; return -4; } fs_event.data = this; on_rename = cb; user = usr; return 0; }
DLLEXPORT int jl_fs_event_init(uv_loop_t* loop, uv_fs_event_t* handle, const char* filename, int flags) { return uv_fs_event_init(loop,handle,filename,&jl_fseventscb,flags); }
static int scan_dir(struct lws *wsi, struct per_session_data__tbl_dir *pss) { /* uuh travis... */ #if UV_VERSION_MAJOR > 0 uv_loop_t *loop = lws_uv_getloop(lws_get_context(wsi), 0); char *end = &(pss->strings[sizeof(pss->strings) - 1]); struct fobj *prev = &pss->base; char path[512], da[200]; const char *icon; uv_dirent_t dent; struct fobj *f; struct stat st; struct tm *tm; int ret = 0, n; uv_fs_t req; lws_protocol_dir_kill_monitor(pss); lws_snprintf(path, sizeof(path) - 1, "%s/%s", pss->dir, pss->reldir); //lwsl_notice("path = %s\n", path); pss->event_req = malloc(sizeof(*pss->event_req)); if (!pss->event_req) return 2; pss->wsi = wsi; pss->event_req->data = pss; uv_fs_event_init(lws_uv_getloop(lws_get_context(wsi), 0), pss->event_req); // The recursive flag watches subdirectories too. n = uv_fs_event_start(pss->event_req, mon_cb, path, UV_FS_EVENT_RECURSIVE); //lwsl_notice("monitoring %s (%d)\n", path, n); if (!uv_fs_scandir(loop, &req, path, 0, NULL)) { lwsl_err("Scandir on %s failed\n", path); return 2; } pss->p = pss->strings; while (uv_fs_scandir_next(&req, &dent) != UV_EOF) { lws_snprintf(path, sizeof(path) - 1, "%s/%s/%s", pss->dir, pss->reldir, dent.name); if (stat(path, &st)) { lwsl_info("unable to stat %s\n", path); continue; } f = malloc(sizeof(*f)); f->next = NULL; f->name = pss->p; n = lws_snprintf(pss->p, end - pss->p, "%s", dent.name); pss->p += n + 1; f->uri = NULL; if ((S_IFMT & st.st_mode) == S_IFDIR) { n = lws_snprintf(pss->p, end - pss->p, "=%s/%s", pss->reldir, dent.name); f->uri = pss->p; } if (lws_get_mimetype(dent.name, NULL)) { n = lws_snprintf(pss->p, end - pss->p, "./serve/%s/%s", pss->reldir, dent.name); f->uri = pss->p; } if (f->uri) pss->p += n + 1; if (end - pss->p < 100) { free(f); break; } icon = " "; if ((S_IFMT & st.st_mode) == S_IFDIR) icon = "📂"; f->icon = pss->p; n = lws_snprintf(pss->p, end - pss->p, "%s", icon); pss->p += n + 1; f->date = pss->p; tm = gmtime(&st.st_mtime); strftime(da, sizeof(da), "%Y-%b-%d %H:%M:%S %z", tm); n = lws_snprintf(pss->p, end - pss->p, "%s", da); pss->p += n + 1; f->size = st.st_size; f->m = st.st_mtime; prev->next = f; prev = f; } uv_fs_req_cleanup(&req); return ret; #else return 0; #endif }