Example #1
0
/* Writes the specified string to the file handle, maybe with a newline. */
static MVMint64 write_str(MVMThreadContext *tc, MVMOSHandle *h, MVMString *str, MVMint64 newline) {
    MVMIOFileData *data = (MVMIOFileData *)h->body.data;
    MVMuint64 output_size;
    MVMint64 bytes_written;
    char *output = MVM_string_encode(tc, str, 0, -1, &output_size, data->encoding, NULL,
        MVM_TRANSLATE_NEWLINE_OUTPUT);
    uv_buf_t write_buf  = uv_buf_init(output, output_size);
    uv_fs_t req;

    bytes_written = uv_fs_write(tc->loop, &req, data->fd, &write_buf, 1, -1, NULL);
    if (bytes_written < 0) {
        MVM_free(output);
        MVM_exception_throw_adhoc(tc, "Failed to write bytes to filehandle: %s", uv_strerror(req.result));
    }
    MVM_free(output);

    if (newline) {
        uv_buf_t nl = uv_buf_init("\n", 1);
        if (uv_fs_write(tc->loop, &req, data->fd, &nl, 1, -1, NULL) < 0)
            MVM_exception_throw_adhoc(tc, "Failed to write newline to filehandle: %s", uv_strerror(req.result));
        bytes_written++;
    }

    return bytes_written;
}
Example #2
0
void AccessLog::write(const char *fmt, ...)
{
    va_list args;
    va_start(args, fmt);

    time_t now = time(nullptr);
    tm stime;

#   ifdef _WIN32
    localtime_s(&stime, &now);
#   else
    localtime_r(&now, &stime);
#   endif

    char *buf = new char[1024];
    int size = snprintf(buf, 23, "[%d-%02d-%02d %02d:%02d:%02d] ",
                        stime.tm_year + 1900,
                        stime.tm_mon + 1,
                        stime.tm_mday,
                        stime.tm_hour,
                        stime.tm_min,
                        stime.tm_sec);

    size = vsnprintf(buf + size, 1024 - size - 1, fmt, args) + size;
    buf[size] = '\n';

    uv_buf_t ubuf = uv_buf_init(buf, (unsigned int) size + 1);
    uv_fs_t *req = new uv_fs_t;
    req->data = ubuf.base;

    uv_fs_write(uv_default_loop(), req, m_file, &ubuf, 1, 0, AccessLog::onWrite);

    va_end(args);
}
Example #3
0
DLLEXPORT int jl_fs_write(int handle, char *buf, size_t len, size_t offset)
{
    uv_fs_t req;
    int ret = uv_fs_write(jl_io_loop, &req, handle, buf, len, offset, NULL);
    uv_fs_req_cleanup(&req);
    return ret;
}
Example #4
0
void saveBlock(block_s * block)
{
	// Get region path
	char * regionPath;
	int regionPathLength = setRegionPath(&regionPath, 0, 0);

	// Make the path
	makeDir(regionPath);

	// Get chunk path
	char * chunkPath;
	setChunkPath(&chunkPath, regionPath, regionPathLength, block->cx, block->cy);

	uv_fs_t open_req;
	uv_fs_t write_req;
	uv_fs_t close_req;

	// store type in buffer
	unsigned char typeBuffer[2];
	shortToBuffer(4, 0, typeBuffer);

	// Open
	uv_fs_open(uv_default_loop(), &open_req, chunkPath,  O_WRONLY | O_CREAT, 0700, NULL);
	uv_fs_req_cleanup(&open_req);

	uv_fs_write(uv_default_loop(), &write_req, open_req.result, typeBuffer, 2,(block->z * 512 + ((block->x  * 2) + block->y * 16 * 2)), NULL);
	uv_fs_req_cleanup(&write_req);

	// Close
	uv_fs_close(uv_default_loop(), &close_req, open_req.result, NULL);
	uv_fs_req_cleanup(&close_req);

	free(regionPath);
	free(chunkPath);
}
Example #5
0
File: fs.c Project: ifzz/LuaIO
/* local ret = fs.write(fd, data, pos)
 * ret < 0 => errno
 * ret >= 0 => write bytes
 */
static int LuaIO_fs_write(lua_State* L) {
  int fd = luaL_checkinteger(L, 1);
  /*common.h*/
  LuaIO_check_data(L, 2, fs_native:write(fd, data, pos));
  int pos = luaL_checkinteger(L, 3);

  CREATE_REQ1();
  req->bytes = bytes;
  int err = uv_fs_write(uv_default_loop(), 
                        &req->req,
                        fd,
                        bufs,
                        count,
                        pos,
                        LuaIO_fs_callback);
  if (err) {
    if(tmp != NULL) {
      LuaIO_pmemory_free(tmp);
    }

    LuaIO_pfree(&LuaIO_fs_req_pool, req);
    lua_pushinteger(L, err);
    return 1;
  }

  lua_pushvalue(L, 2);
  req->write_data_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  if(tmp != NULL) {
    LuaIO_pmemory_free(tmp);
  }

  return lua_yield(L, 0);
}
Example #6
0
File: jl_uv.c Project: jskDr/julia
DLLEXPORT int jl_putc(char c, uv_stream_t *stream)
{
    int err;
    if (stream!=0) {
        if (stream->type<UV_HANDLE_TYPE_MAX) { //is uv handle
            if (stream->type == UV_FILE) {
                JL_SIGATOMIC_BEGIN();
                jl_uv_file_t *file = (jl_uv_file_t *)stream;
                // Do a blocking write for now
                uv_fs_t req;
                uv_buf_t buf[1];
                buf[0].base = &c;
                buf[0].len = 1;
                err = uv_fs_write(file->loop, &req, file->file, buf, 1, -1, NULL);
                JL_SIGATOMIC_END();
                return err ? 0 : 1;
            }
            else {
                uv_write_t *uvw = (uv_write_t*)malloc(sizeof(uv_write_t)+1);
                err = jl_write_copy(stream,(char*)&c,1,uvw, (void*)&jl_uv_writecb);
                if (err < 0) {
                    free(uvw);
                    return 0;
                }
                return 1;
            }
        }
        else {
            ios_t *handle = (ios_t*)stream;
            return ios_putc(c,handle);
        }
    }
    return 0;
}
Example #7
0
File: jl_uv.c Project: jskDr/julia
DLLEXPORT size_t jl_write(uv_stream_t *stream, const char *str, size_t n)
{
    int err;
    //TODO: BAD!! Needed because Julia can't yet detect null stdio
    if (stream == 0)
        return 0;
    if (stream->type<UV_HANDLE_TYPE_MAX) { //is uv handle
        if (stream->type == UV_FILE) {
            JL_SIGATOMIC_BEGIN();
            jl_uv_file_t *file = (jl_uv_file_t *)stream;
            // Do a blocking write for now
            uv_fs_t req;
            uv_buf_t buf[1];
            buf[0].base = (char*)str;
            buf[0].len = n;
            err = uv_fs_write(file->loop, &req, file->file, buf, 1, -1, NULL);
            JL_SIGATOMIC_END();
            return err ? 0 : n;
        }
        else {
            uv_write_t *uvw = (uv_write_t*)malloc(sizeof(uv_write_t)+n);
            err = jl_write_copy(stream,str,n,uvw, (void*)&jl_uv_writecb);
            if (err < 0) {
                free(uvw);
                return 0;
            }
            return n;
        }
    }
    else {
        ios_t *handle = (ios_t*)stream;
        return ios_write(handle,str,n);
    }
}
Example #8
0
static void touch_file(const char* name, unsigned int size) {
  uv_file file;
  uv_fs_t req;
  uv_buf_t buf;
  int r;
  unsigned int i;

  r = uv_fs_open(NULL, &req, name, O_WRONLY | O_CREAT | O_TRUNC,
                 S_IWUSR | S_IRUSR, NULL);
  uv_fs_req_cleanup(&req);
  ASSERT(r >= 0);
  file = r;

  buf = uv_buf_init("a", 1);

  /* Inefficient but simple. */
  for (i = 0; i < size; i++) {
    r = uv_fs_write(NULL, &req, file, &buf, 1, i, NULL);
    uv_fs_req_cleanup(&req);
    ASSERT(r >= 0);
  }

  r = uv_fs_close(NULL, &req, file, NULL);
  uv_fs_req_cleanup(&req);
  ASSERT(r == 0);
}
Example #9
0
DLLEXPORT int jl_putc(unsigned char c, uv_stream_t *stream)
{
    if (stream!=0) {
        if (stream->type<UV_HANDLE_TYPE_MAX) { //is uv handle
            if (stream->type == UV_FILE) {
                JL_SIGATOMIC_BEGIN();
                jl_uv_file_t *file = (jl_uv_file_t *)stream;
                // Do a blocking write for now
                uv_fs_t req;
                int err = uv_fs_write(file->loop, &req, file->file, &c, 1, -1, NULL);
                JL_SIGATOMIC_END();
                return err ? 0 : 1;
            }
            else {
                JL_SIGATOMIC_BEGIN();
                uv_write_t *uvw = malloc(sizeof(uv_write_t) + 1);
                char *data = (char*)(uvw+1);
                *data = c;
                uv_buf_t buf[]  = {{.base = data,.len=1}};
                uvw->data = data;
                int err = uv_write(uvw,stream,buf,1,&jl_free_buffer);
                JL_SIGATOMIC_END();
                return err ? 0 : 1;
            }
        }
        else {
Example #10
0
DLLEXPORT int jl_fs_write_byte(int handle, char c)
{
    uv_fs_t req;
    int ret = uv_fs_write(jl_io_loop, &req, handle, &c, 1, -1, NULL);
    uv_fs_req_cleanup(&req);
    return ret;
}
Example #11
0
/* Writes the specified bytes to the file handle. */
static MVMint64 write_bytes(MVMThreadContext *tc, MVMOSHandle *h, char *buf, MVMint64 bytes) {
    MVMIOFileData *data = (MVMIOFileData *)h->body.data;
    uv_buf_t write_buf  = uv_buf_init(buf, bytes);
    uv_fs_t  req;
    MVMint64 bytes_written;
    bytes_written = uv_fs_write(tc->loop, &req, data->fd, &write_buf, 1, -1, NULL);
    if (bytes_written < 0)
        MVM_exception_throw_adhoc(tc, "Failed to write bytes to filehandle: %s", uv_strerror(req.result));
    return bytes_written;
}
Example #12
0
File: uvc.c Project: whtc123/libuvc
int uvc_fs_write(uvc_io *io,void *data,ssize_t size){
    uv_buf_t buf;
	io->handle->data=io;
    buf.len=size;
    buf.base=data;
	uv_fs_write(uvc_loop_default() ,(uv_fs_t *)io->handle,io->file,&buf,1,-1,uvc_fs_cb);
	io->cur =uvc_self();
	uvc_yield( );
	return ((uv_fs_t *)(io->handle))->result;;
}
Example #13
0
static void create_cb(uv_fs_t* req) {
  int r;
  ASSERT(req == &open_req1);
  ASSERT(req->fs_type == UV_FS_OPEN);
  ASSERT(req->result != -1);
  create_cb_count++;
  uv_fs_req_cleanup(req);
  r = uv_fs_write(loop, &write_req, req->result, test_buf, sizeof(test_buf),
      -1, write_cb);
}
Example #14
0
File: jl_uv.c Project: jskDr/julia
DLLEXPORT int jl_fs_write_byte(int handle, char c)
{
    uv_fs_t req;
    uv_buf_t buf[1];
    buf[0].base = &c;
    buf[0].len = 1;
    int ret = uv_fs_write(jl_io_loop, &req, handle, buf, 1, -1, NULL);
    uv_fs_req_cleanup(&req);
    return ret;
}
Example #15
0
File: jl_uv.c Project: jskDr/julia
DLLEXPORT int jl_fs_write(int handle, char *data, size_t len, size_t offset)
{
    uv_fs_t req;
    uv_buf_t buf[1];
    buf[0].base = data;
    buf[0].len = len;
    int ret = uv_fs_write(jl_io_loop, &req, handle, buf, 1, offset, NULL);
    uv_fs_req_cleanup(&req);
    return ret;
}
Example #16
0
void on_read(uv_fs_t *req) {
    if (req->result < 0) {
        fprintf(stderr, "Read error: %s\n", uv_strerror(req->result));
    } else if(req->result == 0) {
        uv_fs_t close_req;
        uv_fs_close(uv_default_loop(), &close_req, open_req.result, NULL);
    } else if(req->result > 0) {
        iov.len = req->result;
        uv_fs_write(uv_default_loop(), &write_req, 1, &iov, 1, -1, on_write);
    }
}
Example #17
0
static void create_cb(uv_fs_t* req) {
  int r;
  TUV_ASSERT(req == &open_req1);
  TUV_ASSERT(req->fs_type == UV_FS_OPEN);
  TUV_ASSERT(req->result >= 0);
  create_cb_count++;
  uv_fs_req_cleanup(req);
  iov = uv_buf_init(test_buf, sizeof(test_buf));

  r = uv_fs_write(loop, &write_req, req->result, &iov, 1, -1, write_cb);
  TUV_ASSERT(r == 0);
}
Example #18
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();
}
Example #19
0
File: jl_uv.c Project: 0/julia
JL_DLLEXPORT int jl_fs_write(int handle, const char *data, size_t len,
                             int64_t offset)
{
    if (jl_safe_restore)
        return write(handle, data, len);
    uv_fs_t req;
    uv_buf_t buf[1];
    buf[0].base = (char*)data;
    buf[0].len = len;
    int ret = uv_fs_write(jl_io_loop, &req, handle, buf, 1, offset, NULL);
    uv_fs_req_cleanup(&req);
    return ret;
}
Example #20
0
void on_read(uv_fs_t *req) {
    uv_fs_req_cleanup(req);
    if (req->result < 0) {
        fprintf(stderr, "Read error: %s\n", uv_strerror(uv_last_error(uv_default_loop())));
    }
    else if (req->result == 0) {
        uv_fs_t close_req;
        // synchronous
        uv_fs_close(uv_default_loop(), &close_req, open_req.result, NULL);
    }
    else {
        uv_fs_write(uv_default_loop(), &write_req, 1, buffer, req->result, -1, on_write);
    }
}
Example #21
0
/*
 * fs.write
 */
static int fs_write(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;
  }
  /* TODO: Check fs_callback index is really the same as file_until. */
  FSR__SET_OPT_CB(4, on_fs_callback)
  uv_fs_write(loop, req, fd, ms->slice, (file_until ? file_until : ms->until), file_pos, cb);
  FSR__TEARDOWN
}
Example #22
0
int main(int argc, char* argv[])
{
    char* format_string = NULL;
    uv_fs_t write_req;

    format_string = jstr_format("Reading file [%s]...\n", argv[1]);
    write_req.data = argv[1];

    uv_fs_write(uv_default_loop(), &write_req, 1, format_string, strlen(format_string), -1, on_message_written);

    uv_run(uv_default_loop(), UV_RUN_DEFAULT);

    uv_fs_req_cleanup(&write_req);

    return 0;
}
Example #23
0
static void touch_file(uv_loop_t* loop, const char* name) {
    int r;
    uv_file file;
    uv_fs_t req;

    r = uv_fs_open(loop, &req, name, O_RDWR, 0, NULL);
    ASSERT(r != -1);
    file = r;
    uv_fs_req_cleanup(&req);

    r = uv_fs_write(loop, &req, file, "foo", 4, -1, NULL);
    ASSERT(r != -1);
    uv_fs_req_cleanup(&req);

    r = uv_fs_close(loop, &req, file, NULL);
    ASSERT(r != -1);
    uv_fs_req_cleanup(&req);
}
Example #24
0
static duk_int_t myload_code(duk_context *ctx, const char *code)
{
  static int reqid = 0;
  char buf[1024];
  uv_fs_t open_req1;
  sprintf(buf, "file%d.js", reqid++);

  unlink(buf);

  duk_push_thread_stash(ctx, ctx);
  duk_bool_t rc = duk_get_prop_string(ctx, -1, "__duk_loop");
  assert(rc);
  uv_loop_t *dukLoop = (uv_loop_t *)duk_get_pointer(ctx, -1);
  duk_pop(ctx);
  duk_pop(ctx);

  int r;
  r = uv_fs_open(dukLoop, &open_req1, buf, O_WRONLY | O_CREAT,
    S_IWUSR | S_IRUSR, NULL);
  assert(r >= 0);
  assert(open_req1.result >= 0);
  uv_fs_req_cleanup(&open_req1);

  uv_buf_t iov = uv_buf_init((char *)code, strlen(code));
  uv_fs_t write_req;
  r = uv_fs_write(NULL, &write_req, open_req1.result, &iov, 1, -1, NULL);
  assert(r >= 0);
  assert(write_req.result >= 0);
  uv_fs_req_cleanup(&write_req);

  uv_fs_t close_req;
  uv_fs_close(dukLoop, &close_req, open_req1.result, NULL);

  duk_push_c_function(ctx, duv_main, 1);
  duk_push_string(ctx, buf);
  if (duk_pcall(ctx, 1)) {
    duv_dump_error(ctx, -1);
    //uv_loop_close(dukLoop);
    duk_destroy_heap(ctx);
    return 1;
  }

  return 0;
}
Example #25
0
static void touch_file(uv_loop_t* loop, const char* name) {
  int r;
  uv_file file;
  uv_fs_t req;
  uv_buf_t buf;

  r = uv_fs_open(loop, &req, name, O_RDWR, 0, NULL);
  ASSERT(r >= 0);
  file = r;
  uv_fs_req_cleanup(&req);

  buf = uv_buf_init("foo", 4);
  r = uv_fs_write(loop, &req, file, &buf, 1, -1, NULL);
  ASSERT(r >= 0);
  uv_fs_req_cleanup(&req);

  r = uv_fs_close(loop, &req, file, NULL);
  ASSERT(r == 0);
  uv_fs_req_cleanup(&req);
}
Example #26
0
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);
    }
}
Example #27
0
void css_logger_dump_to_file(int is_lock, int should_close)
{
	char *tbuffer;
	int64_t len = offset;
	uv_fs_t *req;
	css_logger_write_req_t *wreq;
	int ret;
	int should_roll_log = 0;
	if (!css_logger.writer) {
		printf("writer is null.\n");
		offset = 0;
		return;
	}
	if (!is_lock)
		LOGGER_UV_MUTEX_LOCK(&css_logger_mutex);
	if (offset == 0) {
		if (should_close) {
			req = (uv_fs_t*) malloc(sizeof(uv_fs_t));
			req->data = css_logger.writer;
			ret = uv_fs_close(css_logger.loop, req, css_logger.writer->fd, after_roll_logger_cb);
			if (ret < 0) {
				printf("close file:%s error by reason:%s\n", css_logger.writer->path, uv_strerror(ret));
				FREE(req);
				FREE(css_logger.writer);
			}
		}
		if (!is_lock)
			LOGGER_UV_MUTEX_UNLOCK(&css_logger_mutex);
		return;
	}
	tbuffer = buffer;
	if (bindex > (MAX_LOGGER_BUFFER_LEN - 1))
		bindex = 0;

	buffer = buffers[bindex];
	bindex++;
	offset = 0;
	*buffer = '\0';
	css_logger.writer_offset += len;

	wreq = (css_logger_write_req_t*) malloc(sizeof(css_logger_write_req_t));
	wreq->buf.base = tbuffer;
	wreq->buf.len = len;
	wreq->writer = css_logger.writer;
	req = (uv_fs_t*) malloc(sizeof(uv_fs_t));
	req->data = wreq;
//	printf("dump %lld.%lldb to file:%s.\n", css_logger.writer_offset / 1024,
//			css_logger.writer_offset % 1024, css_logger.writer->path);
	should_roll_log = css_logger.writer_offset > CSS_LOGGER_MAX_FILE_SIZE ? 1 : 0;
	if (should_roll_log)
		should_close = 1;
	if (should_close) {
		// close log.
		ret = uv_fs_write(css_logger.loop, req, css_logger.writer->fd, &wreq->buf, 1, -1,
				after_write_logger_and_close_cb);
		if (ret < 0) {
			req->data = wreq->writer;
			ret = uv_fs_close(css_logger.loop, req, wreq->writer->fd, after_roll_logger_cb);
			FREE(wreq);
		}
		css_logger.writer = NULL;
		css_logger.writer_offset = 0;
		if (should_roll_log) {
			css_logger_new_file();
		}
	} else {
		ret = uv_fs_write(css_logger.loop, req, css_logger.writer->fd, &wreq->buf, 1, -1, after_write_logger_cb);
	}
	if (!is_lock)
		LOGGER_UV_MUTEX_UNLOCK(&css_logger_mutex);
}
Example #28
0
extern "C" int
rust_uv_fs_write(uv_loop_t* loop, uv_fs_t* req, uv_file fd, void* buf,
                 size_t len, int64_t offset, uv_fs_cb cb) {
  return uv_fs_write(loop, req, fd, buf, len, offset, cb);
}