Пример #1
0
static int lluv_file_readb(lua_State* L) {
  const char  *path = NULL;
  lluv_file_t *f    = lluv_check_file(L, 1, LLUV_FLAG_OPEN);
  lluv_loop_t *loop = f->loop;

  char *base; size_t capacity;
  lluv_fixed_buffer_t *buffer;

  int64_t   position = 0; /* position in file default: 0*/ 
  int64_t   offset   = 0; /* offset in buffer default: 0*/
  size_t    length   = 0; /* number or bytes  default: buffer->capacity - offset*/

  int         argc = 2;

  if(lua_islightuserdata(L, argc)){
    base     = lua_touserdata(L, argc);
    capacity = (size_t)lutil_checkint64(L, ++argc);
  }
  else{
    buffer   = lluv_check_fbuf(L, argc);
    base     = buffer->data;
    capacity = buffer->capacity;
  }

  if(lluv_arg_exists(L, argc+1)){      /* position        */
    position = lutil_checkint64(L, ++argc);
  }
  if(lluv_arg_exists(L, argc+2)){      /* offset + length */
    offset = lutil_checkint64(L, ++argc);
    length = (size_t)lutil_checkint64(L, ++argc);
  }
  else if(lluv_arg_exists(L, argc+1)){ /* offset          */
    offset = lutil_checkint64(L, ++argc);
    length = capacity - offset;
  }
  else{
    length = capacity;
  }

  luaL_argcheck (L, capacity > (size_t)offset, 4, LLUV_PREFIX" offset out of index"); 
  luaL_argcheck (L, capacity >= ((size_t)offset + length), 5, LLUV_PREFIX" length out of index");

  LLUV_PRE_FILE();
  {
    uv_buf_t ubuf = lluv_buf_init(&base[offset], length);

    lua_pushvalue(L, 2);
    lua_rawsetp(L, LLUV_LUA_REGISTRY, &req->req);
    lua_pushvalue(L, 1);
    req->file_ref = luaL_ref(L, LLUV_LUA_REGISTRY);
    err = uv_fs_read(loop->handle, &req->req, f->handle, &ubuf, 1, position, cb);
  }
  LLUV_POST_FILE();
}
Пример #2
0
static int lluv_file_write(lua_State* L) {
  // if you provide string then function does not copy this string
  // write(buffer | string, [position, [ [offset,] [length,] ] ] [callback])

  const char  *path           = NULL;
  lluv_file_t *f              = lluv_check_file(L, 1, LLUV_FLAG_OPEN);
  lluv_loop_t *loop           = f->loop;
  lluv_fixed_buffer_t *buffer = NULL;
  const char *str             = NULL;
  size_t    capacity;
  int64_t   position          = 0; /* position in file default: 0*/ 
  int64_t   offset            = 0; /* offset in buffer default: 0*/
  size_t    length            = 0; /* number or bytes  default: buffer->capacity - offset*/
  
  int         argc = 2;

  if(NULL == (str = lua_tolstring(L, 2, &capacity))){
    buffer   = lluv_check_fbuf(L, 2);
    capacity = buffer->capacity;
    str      = buffer->data;
  }

  if(lluv_arg_exists(L, 3)){      /* position        */
    position = lutil_checkint64(L, ++argc);
  }
  if(lluv_arg_exists(L, 5)){      /* offset + length */
    offset = lutil_checkint64(L, ++argc);
    length = (size_t)lutil_checkint64(L, ++argc);
  }
  else if(lluv_arg_exists(L, 4)){ /* offset          */
    offset = lutil_checkint64(L, ++argc);
    length = capacity - offset;
  }
  else{
    length = capacity;
  }

  luaL_argcheck (L, capacity > (size_t)offset, 4, LLUV_PREFIX" offset out of index"); 
  luaL_argcheck (L, capacity >= ((size_t)offset + length), 5, LLUV_PREFIX" length out of index");

  LLUV_PRE_FILE();
  {
    uv_buf_t ubuf = lluv_buf_init((char*)&str[offset], length);
    
    lua_pushvalue(L, 2); /*string or buffer*/
    lua_rawsetp(L, LLUV_LUA_REGISTRY, &req->req);
    lua_pushvalue(L, 1);
    req->file_ref = luaL_ref(L, LLUV_LUA_REGISTRY);
    err = uv_fs_write(loop->handle, &req->req, f->handle, &ubuf, 1, position, cb);
  }
  LLUV_POST_FILE();
}
Пример #3
0
static int lluv_file_chown(lua_State* L) {
  const char  *path = NULL;
  lluv_file_t *f    = lluv_check_file(L, 1, LLUV_FLAG_OPEN);
  lluv_loop_t *loop = f->loop;
  uv_uid_t     uid  = (uv_uid_t)lutil_checkint64(L, 2);
  uv_gid_t     gid  = (uv_gid_t)lutil_checkint64(L, 3);
  int         argc  = 3;

  LLUV_PRE_FILE();
  lua_pushvalue(L, 1);
  req->file_ref = luaL_ref(L, LLUV_LUA_REGISTRY);
  err = uv_fs_fchown(loop->handle, &req->req, f->handle, uid, gid, cb);
  LLUV_POST_FILE();
}
Пример #4
0
static int lluv_udp_open(lua_State *L){
  lluv_handle_t  *handle = lluv_check_udp(L, 1, LLUV_FLAG_OPEN);
  uv_os_sock_t sock = (uv_os_sock_t)lutil_checkint64(L, 2);
  int err = uv_udp_open(LLUV_H(handle, uv_udp_t), sock);
  if(err < 0){
    return lluv_fail(L, handle->flags, LLUV_ERR_UV, err, NULL);
  }

  lua_settop(L, 1);
  return 1;
}
Пример #5
0
static int lluv_file_read(lua_State* L) {
  // if buffer_length provided then function allocate buffer with this size
  // read(buffer | buffer_length, [position, [ [offset,] [length,] ] ] [callback])

  if(lua_isnumber(L, 2)){
    int64_t len = lutil_checkint64(L, 2);
    lluv_fbuf_alloc(L, (size_t)len);
    lua_remove(L, 2); // replace length
    lua_insert(L, 2); // with buffer
  }
  return lluv_file_readb(L);
}
Пример #6
0
static int lluv_file_truncate(lua_State *L){
  const char  *path = NULL;
  lluv_file_t *f    = lluv_check_file(L, 1, LLUV_FLAG_OPEN);
  lluv_loop_t *loop = f->loop;
  int64_t      len  = lutil_checkint64(L, 2);
  int         argc  = 2;

  LLUV_PRE_FILE();
  lua_pushvalue(L, 1);
  req->file_ref = luaL_ref(L, LLUV_LUA_REGISTRY);
  err = uv_fs_ftruncate(loop->handle, &req->req, f->handle, len, cb);
  LLUV_POST_FILE();
}
Пример #7
0
static int64_t opt_get_int64(lua_State *L, int idx, const char *name, int req, const char *err){
  int64_t value;
  idx = lua_absindex(L, idx);
  rawgets(L, idx, name);

  if(lua_isnil(L, -1)){
    lua_pop(L, 1);
    if(req){
      lua_pushstring(L, err);
      return lua_error(L);
    }
    return 0;
  }

  if(!lua_isnumber(L, -1)){
    lua_pop(L, 1);
    lua_pushstring(L, err);
    return lua_error(L);
  }

  value = lutil_checkint64(L, -1);
  lua_pop(L, 1);
  return value;
}
Пример #8
0
static void opt_get_stdio(lua_State *L, int idx, uv_process_options_t *opt){
  static const lluv_uv_const_t FLAGS[] = {
    { UV_IGNORE,         "ignore"         },
    { UV_CREATE_PIPE,    "create_pipe"    },
    { UV_INHERIT_FD,     "inherit_fd"     },
    { UV_INHERIT_STREAM, "inherit_stream" },
    { UV_READABLE_PIPE,  "readable_pipe"  },
    { UV_WRITABLE_PIPE,  "writable_pipe"  },

    { 0, NULL }
  };

  size_t i, n;

  rawgets(L, idx, "stdio");
  if(lua_isnil(L, -1)){
    lua_settop(L, 1);
    return;
  }

  if(!lua_istable(L, -1)){
    lua_pop(L, 1);
    lua_pushstring(L, "stdio option must be an array");
    lua_error(L);
    return;
  }

  n = lua_objlen(L, -1);

  if(n == 0){
    lua_pop(L, 1);
    return;
  }

  opt->stdio = lluv_alloc(L, n * sizeof(*opt->stdio));
  opt->stdio_count = n;

  for(i = 0; i < n; ++i){
    lua_rawgeti(L, -1, i + 1);

    if(lua_istable(L, -1)){
      uv_stdio_flags flags = 0;

      if(opt_exists(L, -1, "fd")){
        opt->stdio[i].data.fd = (int)opt_get_int64 (L, -1, "fd",  0, "stdio.fd option must be a number" );
        flags = UV_INHERIT_FD;
      }
      else if(opt_exists(L, -1, "stream")){
        lluv_handle_t *handle;
        rawgets(L, -1, "stream");
        handle = lluv_check_stream(L, -1, LLUV_FLAG_OPEN);
        lua_pop(L, 1);
        opt->stdio[i].data.stream = LLUV_H(handle, uv_stream_t);
        flags = UV_INHERIT_STREAM;
      }
      else{
        opt->stdio[i].data.fd = 0;
        flags = UV_IGNORE;
      }

      if(opt_exists(L, -1, "flags")){
        rawgets(L, -1, "flags");
        flags = lluv_opt_flags_ui(L, -1, 0, FLAGS);
        lua_pop(L, 1);
        // flags = opt_get_int64 (L, -1, "flags",  0, "stdio.flags option must be a number" );
      }

      opt->stdio[i].flags = flags;
    }
    else if(lua_isnumber(L, -1)){
      opt->stdio[i].data.fd = (int)lutil_checkint64(L, -1);
      opt->stdio[i].flags = UV_INHERIT_FD;
    }
    else if(lua_isuserdata(L, -1)){
      lluv_handle_t *handle = lluv_check_stream(L, -1, LLUV_FLAG_OPEN);
      opt->stdio[i].data.stream = LLUV_H(handle, uv_stream_t);
      opt->stdio[i].flags = UV_INHERIT_STREAM;
    }
    else{
      lua_pushstring(L, "stdio element must be table, stream or number");
      lua_error(L);
      return;
    }

    lua_pop(L, 1);
  }
  lua_pop(L, 1);
}