/* * Synchronous readfile using libuv APIs. * TODO: Use for module loading. */ duk_ret_t mn_loadfile(duk_context *ctx) { const char *path = duk_require_string(ctx, 0); uv_fs_t req; int fd = 0; size_t size; char *chunk; uv_buf_t buf; if (uv_fs_open(mn_loop, &req, path, O_RDONLY, 0644, NULL) < 0) { goto sadplace; } uv_fs_req_cleanup(&req); fd = req.result; if (uv_fs_fstat(mn_loop, &req, fd, NULL) < 0) { goto sadplace; } uv_fs_req_cleanup(&req); size = req.statbuf.st_size; chunk = duk_alloc(ctx, size); buf = uv_buf_init(chunk, size); if (uv_fs_read(mn_loop, &req, fd, &buf, 1, 0, NULL) < 0) { duk_free(ctx, chunk); goto sadplace; } uv_fs_req_cleanup(&req); duk_push_lstring(ctx, chunk, size); duk_free(ctx, chunk); uv_fs_close(mn_loop, &req, fd, NULL); uv_fs_req_cleanup(&req); return 1; sadplace: uv_fs_req_cleanup(&req); if (fd) { uv_fs_close(mn_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 ); }
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++; ASSERT(req->path); ASSERT(memcmp(req->path, "test_file2\0", 11) == 0); uv_fs_req_cleanup(req); memset(buf, 0, sizeof(buf)); r = uv_fs_read(loop, &read_req, open_req1.result, buf, sizeof(buf), -1, read_cb); }
static void read_cb(uv_fs_t* req) { int r; ASSERT(req == &read_req); ASSERT(req->fs_type == UV_FS_READ); ASSERT(req->result >= 0); /* FIXME(bnoordhuis) Check if requested size? */ read_cb_count++; uv_fs_req_cleanup(req); if (read_cb_count == 1) { ASSERT(strcmp(buf, test_buf) == 0); r = uv_fs_ftruncate(loop, &ftruncate_req, open_req1.result, 7, ftruncate_cb); } else { ASSERT(strcmp(buf, "test-bu") == 0); r = uv_fs_close(loop, &close_req, open_req1.result, close_cb); } ASSERT(r == 0); }
static void on_fs_callback(uv_fs_t *req) { UNWRAP(req); int ret_n; if (LUA_NOREF == holder->threadref || holder->use_lcb) { push_callback_no_obj(L, holder, FSR__CBNAME); ret_n = push_results(L, req); lua_call(L, ret_n, 0); } else { /* coroutines */ ret_n = push_results(L, req); /*luv_lua_debug_stackdump(L, "RESUME");*/ lua_resume(L, ret_n); } uv_fs_req_cleanup(req); lev_handle_unref(L, (LevRefStruct_t *)holder); }
static void check_mkdtemp_result(uv_fs_t* req) { int r; TUV_ASSERT(req->fs_type == UV_FS_MKDTEMP); TUV_ASSERT(req->result == 0); TUV_ASSERT(req->path); TUV_ASSERT(strlen(req->path) == 15); TUV_ASSERT(memcmp(req->path, "test_dir_", 9) == 0); TUV_ASSERT(memcmp(req->path + 9, "XXXXXX", 6) != 0); check_permission(req->path, 0700); /* Check if req->path is actually a directory */ r = uv_fs_stat(uv_default_loop(), &stat_req, req->path, NULL); TUV_ASSERT(r == 0); TUV_ASSERT(((uv_stat_t*)stat_req.ptr)->st_mode & S_IFDIR); uv_fs_req_cleanup(&stat_req); }
/* * Return TRUE if "name" is an executable file, FALSE if not or it doesn't * exist. */ static int is_executable(const char_u *name) { uv_fs_t request; int result = uv_fs_stat(uv_default_loop(), &request, (const char*) name, NULL); uint64_t mode = request.statbuf.st_mode; uv_fs_req_cleanup(&request); if (result != 0) { return FALSE; } if (S_ISREG(mode) && (S_IEXEC & mode)) { return TRUE; } return FALSE; }
static void chown_root_cb(uv_fs_t* req) { TUV_ASSERT(req->fs_type == UV_FS_CHOWN); #if defined(_WIN32) || defined(__NUTTX__) /* On windows, chown is a no-op and always succeeds. */ TUV_ASSERT(req->result == 0); #else /* On unix, chown'ing the root directory is not allowed - * unless you're root, of course. */ if (geteuid() == 0) TUV_ASSERT(req->result == 0); else TUV_ASSERT(req->result == UV_EPERM); #endif chown_cb_count++; uv_fs_req_cleanup(req); }
// 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); }
// 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); }
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; } }
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); }
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); }
/* * return TRUE if "name" is a directory * return FALSE if "name" is not a directory * return FALSE for error */ int mch_isdir(const char_u *name) { uv_fs_t request; int result = uv_fs_stat(uv_default_loop(), &request, (const char*) name, NULL); uint64_t mode = request.statbuf.st_mode; uv_fs_req_cleanup(&request); if (0 != result) { return FALSE; } if (!S_ISDIR(mode)) { return FALSE; } return TRUE; }
static void After(uv_fs_t* req) { FsReqWrap* req_wrap = static_cast<FsReqWrap*>(req->data); IOTJS_ASSERT(req_wrap != NULL); IOTJS_ASSERT(req_wrap->data() == req); JObject cb = req_wrap->jcallback(); IOTJS_ASSERT(cb.IsFunction()); JArgList jarg(2); if (req->result < 0) { JObject jerror(CreateUVException(req->result, "open")); jarg.Add(jerror); } else { jarg.Add(JObject::Null()); switch (req->fs_type) { case UV_FS_CLOSE: { break; } case UV_FS_OPEN: case UV_FS_READ: case UV_FS_WRITE: { JObject arg1(static_cast<int>(req->result)); jarg.Add(arg1); break; } case UV_FS_STAT: { uv_stat_t s = (req->statbuf); JObject ret(MakeStatObject(&s)); jarg.Add(ret); break; } default: jarg.Add(JObject::Null()); } } JObject res = MakeCallback(cb, JObject::Null(), jarg); uv_fs_req_cleanup(req); delete req_wrap; }
// #### req res free file // Close the file descriptor. static void req_res_free_file(req_res_t* rr) { if (rr != NULL){ if (rr->file != 0) { uv_fs_t close_req; // `NULL` as callback function makes the call synchron. if (0 != uv_fs_close(loop, &close_req, rr->file, NULL)) { fprintf(stderr, "uv_fs_close error\n"); } uv_fs_req_cleanup(&close_req); } else { /* Do nothing yet. ToDo: DEBUG msg proposed for later server state.*/ } } else { DEBUG("req_res_free_file error: rr is NULL\n"); } }
static int squvco_open(sqlite3_vfs *const vfs, char const *const path, sqlite3_file *const file, int const sqflags, int *const outFlags) { int uvflags = 0; if(!path) return SQLITE_IOERR; if(sqflags & SQLITE_OPEN_EXCLUSIVE) uvflags |= O_EXCL; if(sqflags & SQLITE_OPEN_CREATE) uvflags |= O_CREAT; if(sqflags & SQLITE_OPEN_READWRITE) uvflags |= O_RDWR; if(sqflags & SQLITE_OPEN_READONLY) uvflags |= O_RDONLY; uv_fs_t req = { .data = co_active() }; uv_fs_open(vfs->pAppData->loop, &req, path, uvflags, 0600, fs_cb); co_switch(vfs->pAppData->yield); int const result = req.result; uv_fs_req_cleanup(&req); if(result < 0) return SQLITE_CANTOPEN; file->methods = &io_methods; file->file = result; file->thread = vfs->pAppData; file->lockLevel = SQLITE_LOCK_NONE; return SQLITE_OK; }
static int fs_exists(lua_State* L) { FSR__SETUP const char *path = luaL_checkstring(L, 1); FSR__SET_OPT_CB(2, on_exists_callback) uv_fs_stat(loop, req, path, cb); /* NOTE: remove "object" */ lua_remove(L, 1); if (LUA_NOREF != holder->threadref) { /* we're in coroutine land */ return lua_yield(L, 0); } else {/* no coroutines */ if (!cb) { uv_fs_req_cleanup(req); } lua_pushboolean(L, req->result != -1); return 1; } return 0; /* unreachable */ }
void FileRequestBaton::file_read(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); } else { // File was successfully read. if (ptr->request) { ptr->request->response = util::make_unique<Response>(); ptr->request->response->code = 200; ptr->request->response->data = std::move(ptr->body); ptr->request->notify(); } } uv_fs_req_cleanup(req); uv_fs_close(req->loop, req, ptr->fd, file_closed); }
// 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_get_rstream((uv_handle_t *)handle); rstream->uvbuf.len = rbuffer_available(rstream->buffer); rstream->uvbuf.base = rbuffer_write_ptr(rstream->buffer); // 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; if (fpos_intmax > INT64_MAX) { ELOG("stream offset overflow"); preserve_exit(); } // 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); return; } // no errors (req.result (ssize_t) is positive), it's safe to cast. size_t nread = (size_t) req.result; rbuffer_produced(rstream->buffer, nread); rstream->fpos += nread; }
static void check_permission(const char* filename, unsigned int mode) { int r; uv_fs_t req; uv_stat_t* s; r = uv_fs_stat(NULL, &req, filename, NULL); ASSERT(r == 0); ASSERT(req.result == 0); s = &req.statbuf; #ifdef _WIN32 /* * On Windows, chmod can only modify S_IWUSR (_S_IWRITE) bit, * so only testing for the specified flags. */ ASSERT((s->st_mode & 0777) & mode); #else ASSERT((s->st_mode & 0777) == mode); #endif uv_fs_req_cleanup(&req); }
void check_permission(const char* filename, int mode) { int r; uv_fs_t req; struct stat* s; r = uv_fs_stat(uv_default_loop(), &req, filename, NULL); ASSERT(r == 0); ASSERT(req.result == 0); s = req.ptr; #ifdef _WIN32 /* * On Windows, chmod can only modify S_IWUSR (_S_IWRITE) bit, * so only testing for the specified flags. */ ASSERT((s->st_mode & 0777) & mode); #else ASSERT((s->st_mode & 0777) == mode); #endif uv_fs_req_cleanup(&req); }
void after_write_logger_and_close_cb(uv_fs_t* req) { int r; uv_fs_t* close_req; css_logger_write_req_t *wreq = (css_logger_write_req_t*) req->data; css_logger_writer_t *writer = wreq->writer; if (req->result < 0) { printf("writer last data to log %s error:%s.\n", wreq->writer->path, uv_strerror((int) req->result)); } uv_fs_req_cleanup(req); FREE(wreq); FREE(req); close_req = (uv_fs_t*) malloc(sizeof(uv_fs_t)); close_req->data = writer; r = uv_fs_close(css_logger.loop, close_req, writer->fd, after_roll_logger_cb); if (r < 0) { printf("close file %s error:%s \n", writer->path, uv_strerror((int) req->result)); FREE(writer); FREE(close_req); } }
static void luv_fs_cb(uv_fs_t* req) { lua_State* L = luv_state(req->loop); int nargs = push_fs_result(L, req); if (nargs == 2 && lua_isnil(L, -nargs)) { // If it was an error, convert to (err, value) format. lua_remove(L, -nargs); nargs--; } else { // Otherwise insert a nil in front to convert to (err, value) format. lua_pushnil(L); lua_insert(L, -nargs - 1); nargs++; } luv_fulfill_req(L, req->data, nargs); if (req->fs_type != UV_FS_SCANDIR) { luv_cleanup_req(L, req->data); req->data = NULL; uv_fs_req_cleanup(req); } }
void on_read(uv_fs_t* req) { ssize_t size_read = req->result; read_write_op_t* read_write_op = req->data; uv_fs_req_cleanup(req); if (size_read < 0) { fprintf(stderr, "Error when reading: %s\n", uv_strerror(uv_last_error(uv_default_loop()))); } else if (size_read == 0) { uv_fs_t close_req; if (read_write_op && read_write_op->buff) { free(read_write_op->buff); read_write_op->buff = NULL; } uv_fs_close(uv_default_loop(), &close_req, read_write_op->read_fd, NULL); } else { uv_fs_t write_req; write_req.data = read_write_op; uv_fs_write(uv_default_loop(), &write_req, 1, read_write_op->buff, size_read, -1, on_write); } }
static int pipe_echo_start(char* pipeName) { int r; #ifndef _WIN32 { uv_fs_t req; uv_fs_unlink(uv_default_loop(), &req, pipeName, NULL); uv_fs_req_cleanup(&req); } #endif server = (uv_handle_t*)&pipeServer; serverType = PIPE; r = uv_pipe_init(loop, &pipeServer, 0); if (r) { fprintf(stderr, "uv_pipe_init: %s\n", uv_strerror(uv_last_error(loop))); return 1; } r = uv_pipe_bind(&pipeServer, pipeName); if (r) { fprintf(stderr, "uv_pipe_bind: %s\n", uv_strerror(uv_last_error(loop))); return 1; } r = uv_listen((uv_stream_t*)&pipeServer, SOMAXCONN, on_connection); if (r) { fprintf(stderr, "uv_pipe_listen: %s\n", uv_strerror(uv_last_error(loop))); return 1; } return 0; }
static void check_utime(const char* path, double atime, double mtime) { struct stat* s; uv_fs_t req; int r; r = uv_fs_stat(loop, &req, path, NULL); ASSERT(r == 0); ASSERT(req.result == 0); s = req.ptr; #if _WIN32 ASSERT(s->st_atime == atime); ASSERT(s->st_mtime == mtime); #elif !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE) ASSERT(s->st_atimespec.tv_sec == atime); ASSERT(s->st_mtimespec.tv_sec == mtime); #else ASSERT(s->st_atim.tv_sec == atime); ASSERT(s->st_mtim.tv_sec == mtime); #endif uv_fs_req_cleanup(&req); }
void FileRequestBaton::file_opened(uv_fs_t *req) { FileRequestBaton *ptr = (FileRequestBaton *)req->data; assert(ptr->thread_id == std::this_thread::get_id()); if (req->result < 0) { // Opening failed or was canceled. There isn't much left we can do. notify_error(req); cleanup(req); } else { const uv_file fd = uv_file(req->result); // We're going to reuse this handle, so we need to cleanup first. uv_fs_req_cleanup(req); if (ptr->canceled || !ptr->request) { // Either the FileRequest object has been destructed, or the // request was canceled. uv_fs_close(req->loop, req, fd, file_closed); } else { ptr->fd = fd; uv_fs_fstat(req->loop, req, fd, file_stated); } } }
void on_write(uv_fs_t* req) { read_write_op_t* read_write_op = req->data; uv_fs_req_cleanup(req); if (req->result < 0) { fprintf(stderr, "Error when writing: %s\n", uv_strerror(uv_last_error(uv_default_loop()))); } else { uv_fs_t read_req; read_req.data = read_write_op; uv_fs_read(uv_default_loop(), &read_req, read_write_op->read_fd, read_write_op->buff, BUFFER_LEN, -1, on_read); } }
static void on_fs_read(uv_fs_t *req) { http_response* response = (http_response*) req->data; int result = req->result; uv_fs_req_cleanup(req); if (result < 0) { fprintf(stderr, "File read error: %s: %s\n", uv_err_name(result), uv_strerror(result)); response_error(response->handle, 500, "Internal Server Error", NULL); destroy_response(response, 1); return; } else if (result == 0) { destroy_response(response, 1); return; } uv_buf_t buf = uv_buf_init(response->pbuf, result); int r = uv_write(&response->write_req, (uv_stream_t*) response->handle, &buf, 1, on_write); if (r) { destroy_response(response, 1); return; } response->response_offset += result; }
static void open_loop_cb(uv_fs_t* req) { TUV_ASSERT(req->fs_type == UV_FS_OPEN); TUV_ASSERT(req->result == UV_ELOOP); open_cb_count++; uv_fs_req_cleanup(req); }