Exemple #1
0
static void
module_on_scandir(uv_fs_t *req)
{
    uv_dirent_t dirEntry;
    char currentPath[512];
    size_t size;

    size = sizeof(currentPath);

    uv_cwd(currentPath, &size);
    uv_chdir(req->path);

    while(uv_fs_scandir_next(req, &dirEntry) != UV_EOF)
    {
        const char *extension;

        if(dirEntry.type != UV_DIRENT_FILE)
        {
            continue;
        }

        extension = module_get_extension(dirEntry.name);
        if(extension == NULL ||
           strcmp(module_get_extension(dirEntry.name), "so") != 0)
        {
            continue;
        }

        module_load(dirEntry.name);
    }

    uv_chdir(currentPath);
}
Exemple #2
0
Fichier : fs.c Projet : kidaa/luv
static int luv_fs_scandir_next(lua_State* L) {
  uv_fs_t* req = luv_check_fs(L, 1);
  uv_dirent_t ent;
  int ret = uv_fs_scandir_next(req, &ent);
  const char* type;
  if (ret == UV_EOF) {
    luv_cleanup_req(L, req->data);
    req->data = NULL;
    uv_fs_req_cleanup(req);
    return 0;
  }
  if (ret < 0) return luv_error(L, ret);
  lua_createtable(L, 0, 2);
  lua_pushstring(L, ent.name);
  lua_setfield(L, -2, "name");
  switch (ent.type) {
    case UV_DIRENT_UNKNOWN: type = NULL; break;
    case UV_DIRENT_FILE: type = "file"; break;
    case UV_DIRENT_DIR: type = "directory"; break;
    case UV_DIRENT_LINK: type = "link"; break;
    case UV_DIRENT_FIFO: type = "fifo"; break;
    case UV_DIRENT_SOCKET: type = "socket"; break;
    case UV_DIRENT_CHAR: type = "char"; break;
    case UV_DIRENT_BLOCK: type = "block"; break;
  }
  if (type) {
    lua_pushstring(L, type);
    lua_setfield(L, -2, "type");
  }
  return 1;
}
Exemple #3
0
static int
lwsws_get_config_d(void *user, const char *d, const char * const *paths,
		   int count_paths, lejp_callback cb)
{
	uv_dirent_t dent;
	uv_fs_t req;
	char path[256];
	int ret = 0;
	uv_loop_t loop;

	uv_loop_init(&loop);

	if (!uv_fs_scandir(&loop, &req, d, 0, NULL)) {
		lwsl_err("Scandir on %s failed\n", d);
		return 2;
	}

	while (uv_fs_scandir_next(&req, &dent) != UV_EOF) {
		snprintf(path, sizeof(path) - 1, "%s/%s", d, dent.name);
		ret = lwsws_get_config(user, path, paths, count_paths, cb);
		if (ret)
			goto bail;
	}

bail:
	uv_fs_req_cleanup(&req);
	uv_loop_close(&loop);

	return ret;
}
Exemple #4
0
void remove_realm_files_from_directory(const std::string &dir_path)
{
    FileSystemRequest scandir_req;
    if (uv_fs_scandir(uv_default_loop(), &scandir_req, dir_path.c_str(), 0, nullptr) < 0) {
        throw UVException(static_cast<uv_errno_t>(scandir_req.result));
    }

    uv_dirent_t entry;
    while (uv_fs_scandir_next(&scandir_req, &entry) != UV_EOF) {
        std::string path(dir_path + '/' + entry.name);

        if (entry.type == UV_DIRENT_DIR) {
            static std::string realm_management_extension(".realm.management");
            if (ends_with(path, realm_management_extension)) {
                uv_dirent_t management_entry;
                FileSystemRequest management_scandir_req;
                if (uv_fs_scandir(uv_default_loop(), &management_scandir_req, path.c_str(), 0, nullptr) < 0) {
                    throw UVException(static_cast<uv_errno_t>(scandir_req.result));
                }

                while (uv_fs_scandir_next(&management_scandir_req, &management_entry) != UV_EOF) {
                    std::string management_entry_path = path + '/' + management_entry.name;
                    FileSystemRequest delete_req;
                    if (uv_fs_unlink(uv_default_loop(), &delete_req, management_entry_path.c_str(), nullptr) != 0) {
                        throw UVException(static_cast<uv_errno_t>(delete_req.result));
                    }
                }

                FileSystemRequest management_rmdir_req;
                if (uv_fs_rmdir(uv_default_loop(), &management_rmdir_req, path.c_str(), nullptr)) {
                    throw UVException(static_cast<uv_errno_t>(management_rmdir_req.result));
                }
            }
        } else {
            static std::string realm_extension(".realm");
            static std::string realm_note_extension(".realm.note");
            static std::string realm_lock_extension(".realm.lock");
            if (ends_with(path, realm_extension) || ends_with(path, realm_note_extension) || ends_with(path, realm_lock_extension)) {
                FileSystemRequest delete_req;
                if (uv_fs_unlink(uv_default_loop(), &delete_req, path.c_str(), nullptr) != 0) {
                    throw UVException(static_cast<uv_errno_t>(delete_req.result));
                }
            }
        }
    }
}
Exemple #5
0
static void AfterAsync(uv_fs_t* req) {
  const jerry_value_t cb = *IOTJS_UV_REQUEST_JSCALLBACK(req);
  IOTJS_ASSERT(jerry_value_is_function(cb));

  jerry_value_t jargs[2] = { 0 };
  size_t jargc = 0;
  if (req->result < 0) {
    jerry_value_t jerror = iotjs_create_uv_exception(req->result, "open");
    jargs[jargc++] = jerror;
  } else {
    jargs[jargc++] = jerry_create_null();
    switch (req->fs_type) {
      case UV_FS_CLOSE: {
        break;
      }
      case UV_FS_OPEN:
      case UV_FS_READ:
      case UV_FS_WRITE: {
        jargs[jargc++] = jerry_create_number((double)req->result);
        break;
      }
      case UV_FS_SCANDIR: {
        int r;
        uv_dirent_t ent;
        uint32_t idx = 0;
        jargs[jargc++] = jerry_create_array(0);
        while ((r = uv_fs_scandir_next(req, &ent)) != UV_EOF) {
          jerry_value_t name =
              jerry_create_string((const jerry_char_t*)ent.name);
          iotjs_jval_set_property_by_index(jargs[1], idx, name);
          jerry_release_value(name);
          idx++;
        }
        break;
      }
      case UV_FS_FSTAT:
      case UV_FS_STAT: {
        uv_stat_t s = (req->statbuf);
        jargs[jargc++] = MakeStatObject(&s);
        break;
      }
      default: { break; }
    }
  }

  iotjs_invoke_callback(cb, jerry_create_undefined(), jargs, jargc);

  jerry_release_value(jargs[0]);
  jerry_release_value(jargs[1]);
  uv_fs_req_cleanup(req);
  iotjs_uv_request_destroy((uv_req_t*)req);
}
Exemple #6
0
static jerry_value_t AfterSync(uv_fs_t* req, int err,
                               const char* syscall_name) {
  if (err < 0) {
    jerry_value_t jvalue = iotjs_create_uv_exception(err, syscall_name);
    jerry_value_t jerror = jerry_create_error_from_value(jvalue, true);
    return jerror;
  }

  switch (req->fs_type) {
    case UV_FS_CLOSE:
      break;
    case UV_FS_OPEN:
    case UV_FS_READ:
    case UV_FS_WRITE:
      return jerry_create_number(err);
    case UV_FS_FSTAT:
    case UV_FS_STAT: {
      uv_stat_t* s = &(req->statbuf);
      return MakeStatObject(s);
    }
    case UV_FS_MKDIR:
    case UV_FS_RMDIR:
    case UV_FS_UNLINK:
    case UV_FS_RENAME:
      return jerry_create_undefined();
    case UV_FS_SCANDIR: {
      int r;
      uv_dirent_t ent;
      uint32_t idx = 0;
      jerry_value_t ret = jerry_create_array(0);
      while ((r = uv_fs_scandir_next(req, &ent)) != UV_EOF) {
        jerry_value_t name = jerry_create_string((const jerry_char_t*)ent.name);
        iotjs_jval_set_property_by_index(ret, idx, name);
        jerry_release_value(name);
        idx++;
      }
      return ret;
    }
    default: {
      IOTJS_ASSERT(false);
      break;
    }
  }
  return jerry_create_undefined();
}
Exemple #7
0
static void directoryListCallback(uv_fs_t* request)
{
  if (handleRequestError(request)) return;

  uv_dirent_t entry;

  WrenVM* vm = getVM();
  wrenEnsureSlots(vm, 3);
  wrenSetSlotNewList(vm, 2);
  
  while (uv_fs_scandir_next(request, &entry) != UV_EOF)
  {
    wrenSetSlotString(vm, 1, entry.name);
    wrenInsertInList(vm, 2, -1, 1);
  }
  
  schedulerResume(freeRequest(request), true);
  schedulerFinishResume();
}
Exemple #8
0
int luv_process_fs_result(lua_State* L, uv_fs_t* req) {
  luv_fs_ref_t* ref = req->data;

  int argc = 0;
  if (req->result < 0) {
    luv_push_async_error(L, req->result, "after_fs", req->path);
  } else {
    lua_pushnil(L);
    switch (req->fs_type) {

      case UV_FS_CLOSE:
      case UV_FS_RENAME:
      case UV_FS_UNLINK:
      case UV_FS_RMDIR:
      case UV_FS_MKDIR:
      case UV_FS_FTRUNCATE:
      case UV_FS_FSYNC:
      case UV_FS_FDATASYNC:
      case UV_FS_LINK:
      case UV_FS_SYMLINK:
      case UV_FS_CHMOD:
      case UV_FS_FCHMOD:
      case UV_FS_CHOWN:
      case UV_FS_FCHOWN:
      case UV_FS_UTIME:
      case UV_FS_FUTIME:
        argc = 0;
        break;

      case UV_FS_OPEN:
      case UV_FS_SENDFILE:
      case UV_FS_WRITE:
        argc = 1;
        lua_pushinteger(L, req->result);
        break;

      case UV_FS_STAT:
      case UV_FS_LSTAT:
      case UV_FS_FSTAT:
        argc = 1;
        luv_push_stats_table(L, &req->statbuf);
        break;

      case UV_FS_READLINK:
        argc = 1;
        lua_pushstring(L, (char*)req->ptr);
        break;

      case UV_FS_READ:
        argc = 2;
        lua_pushlstring(L, ref->buf, req->result);
        lua_pushinteger(L, req->result);
        free(ref->buf);
        break;

      case UV_FS_SCANDIR:
        {
          uv_dirent_t ent;
          int err = uv_fs_scandir_next(req, &ent);

          argc = 1;
          lua_createtable(L, 0, 0);
          int i = 0;
          while(err != UV_EOF) {
            lua_createtable(L, 2, 0);
            lua_pushstring(L, "name");
            lua_pushstring(L, ent.name);
            lua_settable(L, -3);

            lua_pushstring(L, "type");
            switch(ent.type) {
              case UV_DIRENT_FILE:
                  lua_pushstring(L, "FILE");
                  break;
              case UV_DIRENT_DIR:
                  lua_pushstring(L, "DIR");
                  break;
              case UV_DIRENT_LINK:
                  lua_pushstring(L, "LINK");
                  break;
              case UV_DIRENT_FIFO:
                  lua_pushstring(L, "FIFO");
                  break;
              case UV_DIRENT_SOCKET:
                  lua_pushstring(L, "SOCKET");
                  break;
              case UV_DIRENT_CHAR:
                  lua_pushstring(L, "CHAR");
                  break;
              case UV_DIRENT_BLOCK:
                  lua_pushstring(L, "BLOCK");
                  break;
              default:
              case UV_DIRENT_UNKNOWN:
                  lua_pushstring(L, "UNKOWN");
                  break;
            }
            lua_settable(L, -3);

            lua_rawseti(L, -2, ++i);

            err = uv_fs_scandir_next(req, &ent);
          }
        }
        break;

      default:
        assert(0 && "Unhandled eio response");
    }

  }

  return argc;

}
Exemple #9
0
LWS_VISIBLE int
lws_plat_plugins_init(struct lws_context * context, const char * const *d)
{
	struct lws_plugin_capability lcaps;
	struct lws_plugin *plugin;
	lws_plugin_init_func initfunc;
	int m, ret = 0;
	void *v;
	uv_dirent_t dent;
	uv_fs_t req;
	char path[256];
	uv_loop_t loop;
	uv_lib_t lib;

	lib.errmsg = NULL;
	lib.handle = NULL;

	uv_loop_init(&loop);

	lwsl_notice("  Plugins:\n");

	while (d && *d) {

		lwsl_notice("  Scanning %s\n", *d);
		m =uv_fs_scandir(&loop, &req, *d, 0, NULL);
		if (m < 1) {
			lwsl_err("Scandir on %s failed\n", *d);
			return 1;
		}

		while (uv_fs_scandir_next(&req, &dent) != UV_EOF) {
			if (strlen(dent.name) < 7)
				continue;

			lwsl_notice("   %s\n", dent.name);

			lws_snprintf(path, sizeof(path) - 1, "%s/%s", *d, dent.name);
			if (uv_dlopen(path, &lib)) {
				uv_dlerror(&lib);
				lwsl_err("Error loading DSO: %s\n", lib.errmsg);
				goto bail;
			}
			/* we could open it, can we get his init function? */
#if !defined(WIN32)
			m = lws_snprintf(path, sizeof(path) - 1, "init_%s",
				     dent.name + 3 /* snip lib... */);
			path[m - 3] = '\0'; /* snip the .so */
#else
			m = lws_snprintf(path, sizeof(path) - 1, "init_%s",
				     dent.name);
			path[m - 4] = '\0'; /* snip the .dll */
#endif
			if (uv_dlsym(&lib, path, &v)) {
				uv_dlerror(&lib);
				lwsl_err("Failed to get init on %s: %s",
						dent.name, lib.errmsg);
				goto bail;
			}
			initfunc = (lws_plugin_init_func)v;
			lcaps.api_magic = LWS_PLUGIN_API_MAGIC;
			m = initfunc(context, &lcaps);
			if (m) {
				lwsl_err("Initializing %s failed %d\n", dent.name, m);
				goto skip;
			}

			plugin = lws_malloc(sizeof(*plugin));
			if (!plugin) {
				lwsl_err("OOM\n");
				goto bail;
			}
			plugin->list = context->plugin_list;
			context->plugin_list = plugin;
			strncpy(plugin->name, dent.name, sizeof(plugin->name) - 1);
			plugin->name[sizeof(plugin->name) - 1] = '\0';
			plugin->lib = lib;
			plugin->caps = lcaps;
			context->plugin_protocol_count += lcaps.count_protocols;
			context->plugin_extension_count += lcaps.count_extensions;

			continue;

skip:
			uv_dlclose(&lib);
		}
bail:
		uv_fs_req_cleanup(&req);
		d++;
	}

	uv_loop_close(&loop);

	return ret;

}
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
}
Exemple #11
0
static int lluv_push_fs_result(lua_State* L, lluv_fs_request_t* lreq) {
  uv_fs_t *req = &lreq->req;
  /*lluv_loop_t *loop = req->loop->data;*/

  switch (req->fs_type) {
    case UV_FS_RENAME:
    case UV_FS_UNLINK:
    case UV_FS_RMDIR:
    case UV_FS_MKDIR:
    case UV_FS_MKDTEMP:
    case UV_FS_UTIME:
    case UV_FS_CHMOD:
    case UV_FS_LINK:
    case UV_FS_SYMLINK:
    case UV_FS_CHOWN:
      lua_pushstring(L, req->path);
      return 1;

    case UV_FS_CLOSE:
    case UV_FS_FTRUNCATE:
    case UV_FS_FSYNC:
    case UV_FS_FDATASYNC:
    case UV_FS_FUTIME:
    case UV_FS_FCHMOD:
    case UV_FS_FCHOWN:
    case UV_FS_OPEN:
      if(req->path) lua_pushstring(L, req->path);
      else lua_pushboolean(L, 1);
      return 1;

    case UV_FS_SENDFILE:
      lutil_pushint64(L, req->result);
      return 1;

    case UV_FS_STAT:
    case UV_FS_LSTAT:
      lluv_push_stat(L, &req->statbuf);
      lua_pushstring(L, req->path);
      return 2;

    case UV_FS_FSTAT:
      lluv_push_stat(L, &req->statbuf);
      return 1;

    case UV_FS_READLINK:
#if LLUV_UV_VER_GE(1,8,0)
    case UV_FS_REALPATH:
#endif
      lua_pushstring(L, (char*)req->ptr);
      return 1;

    case UV_FS_WRITE:
    case UV_FS_READ:
      lua_rawgetp(L, LLUV_LUA_REGISTRY, req);
      lua_pushnil(L); lua_rawsetp(L, LLUV_LUA_REGISTRY, req);
      lutil_pushint64(L, req->result);
      return 2;

    case UV_FS_SCANDIR:{
      uv_dirent_t ent;
      int i = 0, err;
      lua_createtable(L, (int)req->result, 0);
      lua_createtable(L, (int)req->result, 0);
      while((err = uv_fs_scandir_next(req, &ent)) >= 0){
        lua_pushstring (L, ent.name); lua_rawseti(L, -3, ++i);
#define XX(C,S) case S: lua_pushliteral(L, C); lua_rawseti(L, -2, i); break;
          switch(ent.type){
            LLUV_DIRENT_MAP(XX)
            default: lua_pushstring(L, "unknown"); lua_rawseti(L, -2, i);
          }
#undef XX
      }
      lua_pushstring(L, req->path);
      lua_insert(L, -2);
      return 3;
    }

    case UV_FS_ACCESS:{
      lua_pushboolean(L, req->result == 0);
      lua_pushstring(L, req->path);
      return 2;
    }

#if LLUV_UV_VER_GE(1,14,0)
    case UV_FS_COPYFILE:
      lua_pushboolean(L, req->result == 0);
      return 1;
#endif

    default:
      fprintf(stderr, "UNKNOWN FS TYPE %d\n", req->fs_type);
      return 0;
  }
}
Exemple #12
0
bool hcp::Runtime::scanDir(const char* codecPath,const char** error) {
	uv_fs_t req;
	int numFiles = uv_fs_scandir(uv_default_loop(), &req, codecPath, UV_FS_SCANDIR, nullptr);

	if (numFiles < 0) {
		if (error) {
			*error = uv_strerror(numFiles);
		}

		return false;
	}

	for (int i = 0; i < numFiles; i++) {
		uv_dirent_t ent;
		hcp::tCodec codec;

		int r = uv_fs_scandir_next(&req, &ent);

		if (r == UV_EOF) {
			break;
		}

		hcp_Boolean found = HCP_FALSE;
		// skip codecs with duplicate name
		hcp_FindFirst(&_codecs.header, 0, (void*)ent.name, &found);

		if (found == HCP_TRUE) {
			continue;
		}

		if (hcp::Runtime::hasExtension(ent.name, ".DLL") ||
			hcp::Runtime::hasExtension(ent.name, ".SO") ||
        hcp::Runtime::hasExtension(ent.name, ".DYLIB")) {

			hcp_Size_t pathLen = hcp_szStrLen((hcp_szStr)codecPath);
			hcp_Size_t nameLen = hcp_szStrLen((hcp_szStr)ent.name);

			codec.path = (char*)hcp_Malloc(_state, pathLen + nameLen + 2);
			
			hcp_Memcpy(_state, codec.path, codecPath, pathLen);
			hcp_Memcpy(_state, codec.path + pathLen, "/", 1);
			hcp_Memcpy(_state, codec.path + pathLen + 1, ent.name, nameLen + 1);

			if (loadLibrary(codec.path, &codec, error)) {
				hcp_Size_t index;
				auto code = hcp_Push(&_codecs.header, &codec, &index);

				if (code != HCP_NOERROR) {
					// TODO: Logg load errors
					hcp_Free(_state, codec.path);
				}
			}
			else {
				// TODO: Logg load errors
				hcp_Free(_state, codec.path);
			}
		}
		else {
			continue;
		}
	}

	return true;
}
Exemple #13
0
static void AfterAsync(uv_fs_t* req) {
  iotjs_fs_reqwrap_t* req_wrap = (iotjs_fs_reqwrap_t*)(req->data);
  IOTJS_ASSERT(req_wrap != NULL);
  IOTJS_ASSERT(&req_wrap->req == req);

  const jerry_value_t cb = iotjs_reqwrap_jcallback(&req_wrap->reqwrap);
  IOTJS_ASSERT(jerry_value_is_function(cb));

  iotjs_jargs_t jarg = iotjs_jargs_create(2);
  if (req->result < 0) {
    jerry_value_t jerror = iotjs_create_uv_exception(req->result, "open");
    iotjs_jargs_append_jval(&jarg, jerror);
    jerry_release_value(jerror);
  } else {
    iotjs_jargs_append_null(&jarg);
    switch (req->fs_type) {
      case UV_FS_CLOSE: {
        break;
      }
      case UV_FS_OPEN:
      case UV_FS_READ:
      case UV_FS_WRITE: {
        iotjs_jargs_append_number(&jarg, (double)req->result);
        break;
      }
      case UV_FS_SCANDIR: {
        int r;
        uv_dirent_t ent;
        uint32_t idx = 0;
        jerry_value_t ret = jerry_create_array(0);
        while ((r = uv_fs_scandir_next(req, &ent)) != UV_EOF) {
          jerry_value_t name =
              jerry_create_string((const jerry_char_t*)ent.name);
          iotjs_jval_set_property_by_index(ret, idx, name);
          jerry_release_value(name);
          idx++;
        }
        iotjs_jargs_append_jval(&jarg, ret);
        jerry_release_value(ret);
        break;
      }
      case UV_FS_FSTAT:
      case UV_FS_STAT: {
        uv_stat_t s = (req->statbuf);
        jerry_value_t ret = MakeStatObject(&s);
        iotjs_jargs_append_jval(&jarg, ret);
        jerry_release_value(ret);
        break;
      }
      default: {
        iotjs_jargs_append_null(&jarg);
        break;
      }
    }
  }

  iotjs_make_callback(cb, jerry_create_undefined(), &jarg);

  iotjs_jargs_destroy(&jarg);
  iotjs_fs_reqwrap_destroy(req_wrap);
}
Exemple #14
0
Fichier : fs.c Projet : ifzz/LuaIO
static void LuaIO_fs_callback(uv_fs_t* req) {
  LuaIO_fs_req_t* fs_req = container_of(req, LuaIO_fs_req_t, req);
  lua_State* L = fs_req->current_thread;
  int result = req->result;

  switch (req->fs_type) {
    case UV_FS_ACCESS:
    case UV_FS_CLOSE:
    case UV_FS_RENAME:
    case UV_FS_UNLINK:
    case UV_FS_RMDIR:
    case UV_FS_MKDIR:
    case UV_FS_FTRUNCATE:
    case UV_FS_FSYNC:
    case UV_FS_FDATASYNC:
    case UV_FS_LINK:
    case UV_FS_SYMLINK:
    case UV_FS_CHMOD:
    case UV_FS_FCHMOD:
    case UV_FS_CHOWN:
    case UV_FS_FCHOWN:
    case UV_FS_UTIME:
    case UV_FS_FUTIME:
    case UV_FS_OPEN:
    case UV_FS_SENDFILE:
      lua_pushinteger(L, result);
      LuaIO_pfree(&LuaIO_fs_req_pool, fs_req);
      LuaIO_resume(L, 1);
      return;

    case UV_FS_READ:
      if (result > 0) {
        fs_req->read_buffer->write_pos += result;
      }

      lua_pushinteger(L, result);
      LuaIO_pfree(&LuaIO_fs_req_pool, fs_req);
      LuaIO_resume(L, 1);
      return;

    case UV_FS_WRITE:
      if (result < 0) {
        lua_pushinteger(L, result);
      } else {
        lua_pushinteger(L, fs_req->bytes);
      }

      luaL_unref(L, LUA_REGISTRYINDEX, fs_req->write_data_ref);
      LuaIO_pfree(&LuaIO_fs_req_pool, fs_req);
      LuaIO_resume(L, 1);
      return;

    case UV_FS_STAT:
    case UV_FS_LSTAT:
    case UV_FS_FSTAT:
      if (result < 0) {
        lua_pushnil(L);
        lua_pushinteger(L, result);
      } else {
        LuaIO_fs_create_stat(L, &req->statbuf);
        lua_pushinteger(L, 0);
      }

      LuaIO_pfree(&LuaIO_fs_req_pool, fs_req);
      LuaIO_resume(L, 2);
      return;

    case UV_FS_MKDTEMP:
      if (result < 0) {
        lua_pushnil(L);
        lua_pushinteger(L, result);
      } else {
        lua_pushstring(L, req->path);
        lua_pushinteger(L, 0);
      }

      LuaIO_pfree(&LuaIO_fs_req_pool, fs_req);
      LuaIO_resume(L, 2);
      return;

    case UV_FS_READLINK:
      if (result < 0) {
        lua_pushnil(L);
        lua_pushinteger(L, result);
      } else {
        lua_pushstring(L, (char*)req->ptr);
        lua_pushinteger(L, 0);
      }

      LuaIO_pfree(&LuaIO_fs_req_pool, fs_req);
      LuaIO_resume(L, 2);
      return;

    case UV_FS_SCANDIR:
      if (result < 0) {
        lua_pushnil(L);
        lua_pushinteger(L, result);
      } else {
        int ret;
        lua_createtable(L, 0, 0);

        for (int i = 1; ; i++) {
          uv_dirent_t ent;
          ret = uv_fs_scandir_next(req, &ent);
          if (ret) break;

          lua_pushstring(L, ent.name);
          lua_rawseti(L, -2, i);
        }

        if (ret && ret != UV_EOF) {
          lua_pop(L, 1);
          lua_pushnil(L);
          lua_pushinteger(L, ret);
        } else {
          lua_pushinteger(L, 0);
        }
      }

      LuaIO_pfree(&LuaIO_fs_req_pool, fs_req);
      LuaIO_resume(L, 2);
      return;

    default:
      luaL_error(L, "fs_native module error: unknown fs type(%d)", req->fs_type); 
  }
}