Пример #1
0
/* _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);
    }
}
Пример #2
0
Файл: unix.c Проект: esaul/urbit
/* _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);
  }

}
Пример #3
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);
    }
}
Пример #4
0
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);
}
Пример #5
0
Файл: unix.c Проект: esaul/urbit
/* _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);
  }

}
Пример #6
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);
}
Пример #7
0
static int luv_fs_event_start(lua_State* L) {
  uv_fs_event_t* handle = luv_check_fs_event(L, 1);
  const char* path = luaL_checkstring(L, 2);
  int flags = 0, ret;
  luaL_checktype(L, 3, LUA_TTABLE);
  lua_getfield(L, 3, "watch_entry");
  if (lua_toboolean(L, -1)) flags |= UV_FS_EVENT_WATCH_ENTRY;
  lua_pop(L, 1);
  lua_getfield(L, 3, "stat");
  if (lua_toboolean(L, -1)) flags |= UV_FS_EVENT_STAT;
  lua_pop(L, 1);
  lua_getfield(L, 3, "recursive");
  if (lua_toboolean(L, -1)) flags |= UV_FS_EVENT_RECURSIVE;
  lua_pop(L, 1);
  luv_check_callback(L, handle->data, LUV_FS_EVENT, 4);
  ret = uv_fs_event_start(handle, luv_fs_event_cb, path, flags);
  if (ret < 0) return luv_error(L, ret);
  lua_pushinteger(L, ret);
  return 1;
}
Пример #8
0
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;
}
Пример #9
0
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();
}
Пример #10
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 */
}
Пример #11
0
  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;
  }
Пример #12
0
/* _unix_update_file(): update file, producing list of changes
 *
 * when scanning through files, if dry, do nothing.  otherwise, mark as
 * dry, then check if file exists.  if not, remove self from node list
 * and add path plus sig to %into event.  otherwise, read the file and
 * get a mug checksum.  if same as mug_w, move on.  otherwise, overwrite
 * mug_w with new mug and add path plus data to %into event.
*/
static u3_noun
_unix_update_file(u3_ufil* fil_u)
{
    c3_assert( c3n == fil_u->dir );

    if ( c3y == fil_u->dry ) {
        return u3_nul;
    }

    fil_u->dry = c3y;

    struct stat buf_u;
    c3_i  fid_i = open(fil_u->pax_c, O_RDONLY, 0644);
    c3_ws len_ws, red_ws;
    c3_y* dat_y;

    if ( fid_i < 0 || fstat(fid_i, &buf_u) < 0 ) {
        if ( ENOENT == errno ) {
            return u3nc(u3nc(_unix_string_to_path(fil_u->pax_c), u3_nul), u3_nul);
        }
        else {
            uL(fprintf(uH, "error opening file %s: %s\r\n",
                       fil_u->pax_c, strerror(errno)));
            return u3_nul;
        }
    }

    // So, if file gets deleted and then quickly re-added, like vim and
    // other editors do, we lose the notification.  This is a bad thing,
    // so we always stop and restart the notification.
    uv_fs_event_stop(&fil_u->was_u);
    c3_w ret_w = uv_fs_event_start(&fil_u->was_u,
                                   _unix_fs_event_cb,
                                   fil_u->pax_c,
                                   0);
    if ( 0 != ret_w ) {
        uL(fprintf(uH, "update file event start: %s\n", uv_strerror(ret_w)));
        c3_assert(0);
    }

    len_ws = buf_u.st_size;
    dat_y = c3_malloc(len_ws);

    red_ws = read(fid_i, dat_y, len_ws);

    if ( close(fid_i) < 0 ) {
        uL(fprintf(uH, "error closing file %s: %s\r\n",
                   fil_u->pax_c, strerror(errno)));
    }

    if ( len_ws != red_ws ) {
        if ( red_ws < 0 ) {
            uL(fprintf(uH, "error reading file %s: %s\r\n",
                       fil_u->pax_c, strerror(errno)));
        }
        else {
            uL(fprintf(uH, "wrong # of bytes read in file %s: %d %d\r\n",
                       fil_u->pax_c, len_ws, red_ws));
        }
        free(dat_y);
        return u3_nul;
    }
    else {
        c3_w mug_w = u3r_mug_bytes(dat_y, len_ws);
        if ( mug_w == fil_u->mug_w ) {
            free(dat_y);
            return u3_nul;
        }
        else if ( mug_w == fil_u->gum_w ) {
            fil_u->mug_w = mug_w;
            free(dat_y);
            return u3_nul;
        }
        else {
            fil_u->mug_w = mug_w;

            u3_noun pax = _unix_string_to_path(fil_u->pax_c);
            u3_noun mim = u3nt(c3__text, u3i_string("plain"), u3_nul);
            u3_noun dat = u3nt(mim, len_ws, u3i_bytes(len_ws, dat_y));

            free(dat_y);
            return u3nc(u3nt(pax, u3_nul, dat), u3_nul);
        }
    }
}
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 = "&#x1f4c2;";

		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
}
Пример #14
0
DLLEXPORT int jl_fs_event_init(uv_loop_t *loop, uv_fs_event_t *handle,
                               const char *filename, int flags)
{
    uv_fs_event_init(loop,handle);
    return uv_fs_event_start(handle,&jl_uv_fseventscb,filename,flags);
}
Пример #15
0
int bdg_add_file(void* handle, char* filename) {
	return uv_fs_event_start((uv_fs_event_t*) handle, on_change, filename, 0);
}