Example #1
0
void on_write(uv_fs_t *req) {
    if (req->result < 0) {
        fprintf(stderr, "Write error: %s\n", uv_strerror((int)req->result));
    } else {
        uv_fs_read(uv_default_loop(), &read_req, open_req.result, &iov, 1, -1, on_read);
    }
}
Example #2
0
// Sync readfile using libuv APIs as an API function.
static duk_ret_t duv_loadfile(duk_context *ctx) {
  const char* path = duk_require_string(ctx, 0);
  uv_fs_t req;
  int fd = 0;
  uint64_t size;
  char* chunk;
  uv_buf_t buf;

  if (uv_fs_open(&loop, &req, path, O_RDONLY, 0644, NULL) < 0) goto fail;
  fd = req.result;
  if (uv_fs_fstat(&loop, &req, fd, NULL) < 0) goto fail;
  size = req.statbuf.st_size;
  chunk = duk_alloc(ctx, size);
  buf = uv_buf_init(chunk, size);
  if (uv_fs_read(&loop, &req, fd, &buf, 1, 0, NULL) < 0) goto fail;
  duk_push_lstring(ctx, chunk, size);
  duk_free(ctx, chunk);
  uv_fs_close(&loop, &req, fd, NULL);
  uv_fs_req_cleanup(&req);

  return 1;

  fail:
  if (fd) uv_fs_close(&loop, &req, fd, NULL);
  uv_fs_req_cleanup(&req);
  duk_error(ctx, DUK_ERR_ERROR, "%s: %s: %s", uv_err_name(req.result), uv_strerror(req.result), path);
}
Example #3
0
void init(uv_loop_t *loop) {
    int err = 0;

    /* No more globals, we need to malloc each request and pass it around for later cleanup */
    uv_fs_t *open_req = malloc(sizeof(uv_fs_t));
    uv_fs_t *read_req = malloc(sizeof(uv_fs_t));

    context_t *context = malloc(sizeof(context_t));
    context->open_req = open_req;

    /* 1. Open file */
    err = uv_fs_open(loop, open_req, filename, O_RDONLY, S_IRUSR, NULL);
    if (err < 0) {
        UV_CHECK(err, "uv_fs_open");
    }

    /* 2. Create buffer and initialize it to turn it into a a uv_buf_t */
    size_t buf_len = sizeof(char) * BUF_SIZE;
    char *buf = malloc(buf_len);
    uv_buf_t iov = uv_buf_init(buf, buf_len);

    /* allow us to access the context inside read_cb */
    read_req->data = context;

    /* 3. Read from the file into the buffer */
    err = uv_fs_read(loop, read_req, open_req->result, &iov, 1, 0, read_cb);
    if (err < 0) {
        UV_CHECK(err, "uv_fs_read");
    }
}
Example #4
0
static size_t lmz_file_read(void *pOpaque, mz_uint64 file_ofs, void *pBuf, size_t n) {
  lmz_file_t* zip = pOpaque;
  const uv_buf_t buf = uv_buf_init(pBuf, n);
  file_ofs += zip->archive.m_pState->m_file_archive_start_ofs;
  uv_fs_read(zip->loop, &(zip->req), zip->fd, &buf, 1, file_ofs, NULL);
  return zip->req.result;
}
Example #5
0
static bool adc_read_data(uint32_t pin, struct adc_msg_s* msg) {
  int32_t adc_number = ADC_GET_NUMBER(pin);
  char path[ADC_DEVICE_PATH_BUFFER_SIZE] = { 0 };
  adc_get_path(path, adc_number);

  const iotjs_environment_t* env = iotjs_environment_get();
  uv_loop_t* loop = iotjs_environment_loop(env);
  int result, close_result;

  // Open file
  uv_fs_t open_req;
  result = uv_fs_open(loop, &open_req, path, O_RDONLY, 0666, NULL);
  uv_fs_req_cleanup(&open_req);
  if (result < 0) {
    return false;
  }

  // Read value
  uv_fs_t read_req;
  uv_buf_t uvbuf = uv_buf_init((char*)msg, sizeof(*msg));
  result = uv_fs_read(loop, &read_req, open_req.result, &uvbuf, 1, 0, NULL);
  uv_fs_req_cleanup(&read_req);

  // Close file
  uv_fs_t close_req;
  close_result = uv_fs_close(loop, &close_req, open_req.result, NULL);
  uv_fs_req_cleanup(&close_req);
  if (result < 0 || close_result < 0) {
    return false;
  }

  DDDLOG("ADC Read - path: %s, value: %d", path, msg->am_data);

  return true;
}
Example #6
0
DLLEXPORT int jl_fs_read(int handle, char *buf, size_t len)
{
    uv_fs_t req;
    int ret = uv_fs_read(jl_io_loop, &req, handle, buf, len, -1, NULL);
    uv_fs_req_cleanup(&req);
    return ret;
}
Example #7
0
void on_open(uv_fs_t* req)
{
    uv_fs_t read_req;
    char* buff = NULL;
    read_write_op_t* read_write_op = malloc(sizeof(read_write_op_t));

    read_write_op->read_fd  = req->result;

    read_req.data = read_write_op;

    if (req->result != -1)
    {
        buff = malloc(BUFFER_LEN * sizeof(*buff));
        read_write_op->buff  = buff;
        uv_fs_read(uv_default_loop(),
                   &read_req,
                   req->result,
                   buff,
                   BUFFER_LEN,
                   -1,
                   on_read);
    }
    else
    {
        fprintf(stderr, "Error opening file: %d\n", req->errorno);
    }

    uv_fs_req_cleanup(req);
}
Example #8
0
// ## read file
// Create a file-read-request and dispatch it.
// [on_file_read](#on-file-read) is invoked once the buffer is read from the file.
static void read_file(req_res_t *rr) {
	int r;
	uv_fs_t* fs_req = malloc(sizeof(uv_fs_t));
    if (fs_req != NULL) {
        DEBUG("read_file\n");
        
        fs_req->data = rr;
        rr->pending_requests++;
        r = uv_fs_read(loop, fs_req, rr->file, rr->file_buf, 1, rr->file_pos, on_file_read);
        if  (r != 0) {
            fprintf(stderr, "read error: %d\n", r);
            rr->pending_requests--;
            free(fs_req);
            /* ToDo: To be moved to finalize? */
            req_res_free(rr);
        }
        else {
            /* Do nothing.
             ToDo: Should we report? */
        }
    }
    else {
        DEBUG("read_file error: memory leak, no handle for fs_req.\n");
    }
}
Example #9
0
int main() {
    int err = 0;
    uv_loop_t *loop = uv_default_loop();

    /* 1. Open file */
    err = uv_fs_open(loop, &open_req, filename, O_RDONLY, S_IRUSR, NULL);
    if (err < 0) {
      UV_CHECK(err, "uv_fs_open");
    }

    /* 2. Create buffer and initialize it to turn it into a a uv_buf_t */
    char buf[BUF_SIZE];
    uv_buf_t iov = uv_buf_init(buf, sizeof(buf));

    /* 3. Use the file descriptor (the .result of the open_req) to read from the file into the buffer */
    uv_fs_t read_req;
    err = uv_fs_read(loop, &read_req, open_req.result, &iov, 1, 0, read_cb);
    if (err < 0) {
        UV_CHECK(err, "uv_fs_read");
    }

    uv_run(loop, UV_RUN_DEFAULT);

    return 0;
}
void FileRequestBaton::file_stated(uv_fs_t *req) {
    FileRequestBaton *ptr = (FileRequestBaton *)req->data;
    assert(ptr->thread_id == std::this_thread::get_id());

    if (req->result != 0 || ptr->canceled || !ptr->request) {
        // Stating failed or was canceled. We already have an open file handle
        // though, which we'll have to close.
        notify_error(req);

        uv_fs_req_cleanup(req);
        uv_fs_close(req->loop, req, ptr->fd, file_closed);
    } else {
#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10
        const uv_statbuf_t *stat = static_cast<const uv_statbuf_t *>(req->ptr);
#else
        const uv_stat_t *stat = static_cast<const uv_stat_t *>(req->ptr);
#endif
        if (stat->st_size > std::numeric_limits<int>::max()) {
            // File is too large for us to open this way because uv_buf's only support unsigned
            // ints as maximum size.
            if (ptr->request) {
                ptr->request->response = util::make_unique<Response>();
                ptr->request->response->code = UV_EFBIG;
#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10
                ptr->request->response->message = uv_strerror(uv_err_t {UV_EFBIG, 0});
#else
                ptr->request->response->message = uv_strerror(UV_EFBIG);
#endif
                ptr->request->notify();
            }

            uv_fs_req_cleanup(req);
            uv_fs_close(req->loop, req, ptr->fd, file_closed);
        } else {
            const unsigned int size = (unsigned int)(stat->st_size);
            ptr->body.resize(size);
            ptr->buffer = uv_buf_init(const_cast<char *>(ptr->body.data()), size);
            uv_fs_req_cleanup(req);
#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10
            uv_fs_read(req->loop, req, ptr->fd, ptr->buffer.base, ptr->buffer.len, -1, file_read);
#else
            uv_fs_read(req->loop, req, ptr->fd, &ptr->buffer, 1, 0, file_read);
#endif
        }
    }
}
Example #11
0
void on_write(uv_fs_t *req) {
    uv_fs_req_cleanup(req);
    if (req->result < 0) {
        fprintf(stderr, "Write error: %s\n", uv_strerror(uv_last_error(uv_default_loop())));
    }
    else {
        uv_fs_read(uv_default_loop(), &read_req, open_req.result, buffer, sizeof(buffer), -1, on_read);
    }
}
Example #12
0
void on_open(uv_fs_t *req) {
    assert(req == &open_req);
    if (req->result >= 0) {
        iov = uv_buf_init(buffer, sizeof(buffer));
        uv_fs_read(uv_default_loop(), &read_req, req->result, &iov, 1, -1, on_read);
    } else {
        fprintf(stderr, "error opening file %s\n", uv_strerror((int)req->result));
    }
}
Example #13
0
DLLEXPORT int jl_fs_read_byte(int handle)
{
    uv_fs_t req;
    char buf;
    int ret = uv_fs_read(jl_io_loop, &req, handle, &buf, 1, -1, NULL);
    uv_fs_req_cleanup(&req);
    if (ret == -1)
        return ret;
    return (int)buf;
}
Example #14
0
void wait_for_next_input() {
  int r;
  // Note: the example in the uvbook contains a small mistake  IMO since it's reading from fd: 1 which is stdout,
  //       however it works either way
  r = uv_fs_read(loop, &stdin_watcher, STDIN, buffer, 1024, -1, stdin_fs_cb);
  if (r) ERROR("reading stdin", r);

  r = uv_idle_start(&idler, idle_cb);
  if (r) ERROR("starting idler", r);
}
Example #15
0
File: jl_uv.c Project: 0/julia
JL_DLLEXPORT int jl_fs_read(int handle, char *data, size_t len)
{
    uv_fs_t req;
    uv_buf_t buf[1];
    buf[0].base = data;
    buf[0].len = len;
    int ret = uv_fs_read(jl_io_loop, &req, handle, buf, 1, -1, NULL);
    uv_fs_req_cleanup(&req);
    return ret;
}
Example #16
0
void on_open(uv_fs_t *req) {
    if (req->result != -1) {
        uv_fs_read(uv_default_loop(), &read_req, req->result,
                   buffer, sizeof(buffer), -1, on_read);
    }
    else {
        fprintf(stderr, "error opening file: %d\n", req->errorno);
    }
    uv_fs_req_cleanup(req);
}
Example #17
0
File: uvc.c Project: whtc123/libuvc
int uvc_fs_read(uvc_io *io,void *data,ssize_t size){
	io->handle->data=io;
	uv_buf_t buf;
	buf.len=size;
	buf.base=data;
	uv_fs_read(uvc_loop_default(),(uv_fs_t *)io->handle,io->file,&buf,1,-1,uvc_fs_cb);
	io->cur =uvc_self();
	uvc_yield( );
	uv_fs_req_cleanup((uv_fs_t *)io->handle);
	return ((uv_fs_t *)(io->handle))->result;;
}
Example #18
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();
}
Example #19
0
File: jl_uv.c Project: jskDr/julia
DLLEXPORT int jl_fs_read_byte(int handle)
{
    uv_fs_t req;
    char c;
    uv_buf_t buf[1];
    buf[0].base = &c;
    buf[0].len = 1;
    int ret = uv_fs_read(jl_io_loop, &req, handle, buf, 1, -1, NULL);
    uv_fs_req_cleanup(&req);
    if (ret == -1)
        return ret;
    return (int)c;
}
Example #20
0
/* Read a bunch of bytes into the current decode stream. */
static MVMint32 read_to_buffer(MVMThreadContext *tc, MVMIOFileData *data, MVMint32 bytes) {
    char *buf         = MVM_malloc(bytes);
    uv_buf_t read_buf = uv_buf_init(buf, bytes);
    uv_fs_t req;
    MVMint32 read;
    if ((read = uv_fs_read(tc->loop, &req, data->fd, &read_buf, 1, -1, NULL)) < 0) {
        MVM_free(buf);
        MVM_exception_throw_adhoc(tc, "Reading from filehandle failed: %s",
            uv_strerror(req.result));
    }
    MVM_string_decodestream_add_bytes(tc, data->ds, buf, read);
    return read;
}
Example #21
0
/*
 * fs.read
 */
static int fs_read(lua_State* L) {
  FSR__SETUP
  int fd = luaL_checkint(L, 1);
  MemSlice *ms = lev_checkbuffer(L, 2);
  int64_t file_pos = luaL_optlong(L, 3, 0);
  file_pos--; /* Luaisms */
  size_t file_until = luaL_optlong(L, 4, 0);
  if (file_until > ms->until) {
    file_until = ms->until;
  }
  FSR__SET_OPT_CB(5, on_fs_callback)
  uv_fs_read(loop, req, fd, ms->slice, (file_until ? file_until : ms->until), file_pos, cb);
  FSR__TEARDOWN
}
Example #22
0
static void open_cb(uv_fs_t* req) {
    int r;
    ASSERT(req == &open_req1);
    ASSERT(req->fs_type == UV_FS_OPEN);
    if (req->result < 0) {
        /* TODO get error with uv_last_error() */
        fprintf(stderr, "async open error: %d\n", req->errorno);
        ASSERT(0);
    }
    open_cb_count++;
    uv_fs_req_cleanup(req);
    memset(buf, 0, sizeof(buf));
    r = uv_fs_read(&read_req, open_req1.result, buf, sizeof(buf), -1, read_cb);
}
Example #23
0
void fileReadBytes(WrenVM* vm)
{
  uv_fs_t* request = createRequest(wrenGetSlotValue(vm, 3));

  int fd = *(int*)wrenGetSlotForeign(vm, 0);
  // TODO: Assert fd != -1.

  FileRequestData* data = (FileRequestData*)request->data;
  size_t length = (size_t)wrenGetSlotDouble(vm, 1);
  size_t offset = (size_t)wrenGetSlotDouble(vm, 2);

  data->buffer.len = length;
  data->buffer.base = (char*)malloc(length);

  uv_fs_read(getLoop(), request, fd, &data->buffer, 1, offset,
             fileReadBytesCallback);
}
Example #24
0
// Called by the by the 'idle' handle to emulate a reading event
static void fread_idle_cb(uv_idle_t *handle)
{
    uv_fs_t req;
    RStream *rstream = handle->data;

    rstream->uvbuf.base = rstream->buffer + rstream->wpos;
    rstream->uvbuf.len = rstream->buffer_size - rstream->wpos;

    // the offset argument to uv_fs_read is int64_t, could someone really try
    // to read more than 9 quintillion (9e18) bytes?
    // upcast is meant to avoid tautological condition warning on 32 bits
    uintmax_t fpos_intmax = rstream->fpos;
    assert(fpos_intmax <= INT64_MAX);

    // Synchronous read
    uv_fs_read(
        uv_default_loop(),
        &req,
        rstream->fd,
        &rstream->uvbuf,
        1,
        (int64_t) rstream->fpos,
        NULL);

    uv_fs_req_cleanup(&req);

    if (req.result <= 0) {
        uv_idle_stop(rstream->fread_idle);
        emit_read_event(rstream, true);
        return;
    }

    // no errors (req.result (ssize_t) is positive), it's safe to cast.
    size_t nread = (size_t) req.result;

    rstream->wpos += nread;
    rstream->fpos += nread;

    if (rstream->wpos == rstream->buffer_size) {
        // The last read filled the buffer, stop reading for now
        rstream_stop(rstream);
    }

    emit_read_event(rstream, false);
}
Example #25
0
// Called by the by the 'idle' handle to emulate a reading event
static void fread_idle_cb(uv_idle_t *handle)
{
  uv_fs_t req;
  RStream *rstream = handle->data;

  rstream->uvbuf.base = rstream->buffer + rstream->wpos;
  rstream->uvbuf.len = rstream->buffer_size - rstream->wpos;

  // the offset argument to uv_fs_read is int64_t, could someone really try
  // to read more than 9 quintillion (9e18) bytes?
  // DISABLED TO FIX BROKEN BUILD ON 32bit
  // TODO(elmart): Review types to allow assertion
  // assert(rstream->fpos <= INT64_MAX);

  // Synchronous read
  uv_fs_read(
      uv_default_loop(),
      &req,
      rstream->fd,
      &rstream->uvbuf,
      1,
      (int64_t) rstream->fpos,
      NULL);

  uv_fs_req_cleanup(&req);

  if (req.result <= 0) {
    uv_idle_stop(rstream->fread_idle);
    emit_read_event(rstream, true);
    return;
  }

  // no errors (req.result (ssize_t) is positive), it's safe to cast.
  size_t nread = (size_t) req.result;

  rstream->wpos += nread;
  rstream->fpos += nread;

  if (rstream->wpos == rstream->buffer_size) {
    // The last read filled the buffer, stop reading for now
    rstream_stop(rstream);
  }

  emit_read_event(rstream, false);
}
Example #26
0
File: jl_uv.c Project: 0/julia
JL_DLLEXPORT int jl_fs_read_byte(int handle)
{
    uv_fs_t req;
    char c;
    uv_buf_t buf[1];
    buf[0].base = &c;
    buf[0].len = 1;
    int ret = uv_fs_read(jl_io_loop, &req, handle, buf, 1, -1, NULL);
    uv_fs_req_cleanup(&req);
    switch (ret) {
    case -1: return ret;
    case  0: jl_eof_error();
    case  1: return (int)c;
    default:
        assert(0 && "jl_fs_read_byte: Invalid return value from uv_fs_read");
        return -1;
    }
}
Example #27
0
static void open_cb(uv_fs_t* req) {
  int r;
  ASSERT(req == &open_req1);
  ASSERT(req->fs_type == UV_FS_OPEN);
  if (req->result < 0) {
    fprintf(stderr, "async open error: %d\n", (int) req->result);
    ASSERT(0);
  }
  open_cb_count++;
  ASSERT(req->path);
  ASSERT(memcmp(req->path, "test_file2\0", 11) == 0);
  uv_fs_req_cleanup(req);
  memset(buf, 0, sizeof(buf));
  iov = uv_buf_init(buf, sizeof(buf));
  r = uv_fs_read(loop, &read_req, open_req1.result, &iov, 1, -1,
      read_cb);
  ASSERT(r == 0);
}
Example #28
0
static void open_cb(uv_fs_t* req) {
  int r;
  TUV_ASSERT(req == &open_req1);
  TUV_ASSERT(req->fs_type == UV_FS_OPEN);
  if (req->result < 0) {
    TDLOG("async open error: %d\n", (int)req->result);
    TUV_ASSERT(0);
  }
  open_cb_count++;

  TUV_ASSERT(req->path);
  TUV_ASSERT(memcmp(req->path, filename2, strlen(filename2)) == 0);
  uv_fs_req_cleanup(req);
  memset(buf, 0, sizeof(buf));
  iov = uv_buf_init(buf, sizeof(buf));
  r = uv_fs_read(loop, &read_req, open_req1.result, &iov, 1, -1, read_cb);
  TUV_ASSERT(r == 0);
}
Example #29
0
void open_cb(uv_fs_t* open_req) {
  int r = 0;
  if (open_req->result < 0) CHECK(open_req->result, "uv_fs_open callback");

  context_t* context = open_req->data;

  /* 3. Create buffer and initialize it */
  size_t buf_len = sizeof(char) * BUF_SIZE;
  char *buf = malloc(buf_len);
  context->iov = uv_buf_init(buf, buf_len);

  /* 4. Setup read request */
  uv_fs_t *read_req = malloc(sizeof(uv_fs_t));
  context->read_req = read_req;
  read_req->data = context;

  /* 5. Read from the file into the buffer */
  r = uv_fs_read(uv_default_loop(), read_req, open_req->result, &context->iov, 1, 0, read_cb);
  if (r < 0) CHECK(r, "uv_fs_read");
}
Example #30
0
static void
on_write(uv_write_t* req, int status) {
  http_response* response = (http_response*) req->data;

  if (status != 0) {
    fprintf(stderr, "Write error: %s: %s\n", uv_err_name(status), uv_strerror(status));
    destroy_response(response, 1);
    return;
  }

  if (response->response_offset >= response->response_size) {
    destroy_response(response, !response->request->keep_alive);
    return;
  }

  int r = uv_fs_read(loop, &response->read_req, response->fd, &response->buf, 1, response->response_offset, on_fs_read);
  if (r) {
    fprintf(stderr, "File read error: %s: %s\n", uv_err_name(r), uv_strerror(r));
    response_error(response->handle, 500, "Internal Server Error", NULL);
    destroy_request(response->request, 1);
  }
}