Ejemplo n.º 1
0
/* copy a file from one to another. */
void MVM_file_copy(MVMThreadContext *tc, MVMString *src, MVMString *dest) {
    uv_fs_t req;
    char *       const a = MVM_string_utf8_encode_C_string(tc, src);
    const uv_file  in_fd = uv_fs_open(tc->loop, &req, (const char *)a, O_RDONLY, 0, NULL);

    if (in_fd >= 0 && uv_fs_stat(tc->loop, &req, a, NULL) >= 0) {
        char *       const b = MVM_string_utf8_encode_C_string(tc, dest);
        const uv_file out_fd = uv_fs_open(tc->loop, &req, (const char *)b, O_WRONLY | O_CREAT | O_TRUNC, DEFAULT_MODE, NULL);
        MVM_free(a);

        if (out_fd >= 0
                && uv_fs_sendfile(tc->loop, &req, out_fd, in_fd, 0, req.statbuf.st_size, NULL) >= 0) {
            MVM_free(b);

            if (uv_fs_close(tc->loop, &req, in_fd, NULL) < 0) {
                uv_fs_close(tc->loop, &req, out_fd, NULL); /* should close out_fd before throw. */
                MVM_exception_throw_adhoc(tc, "Failed to close file: %s", uv_strerror(req.result));
            }

            if (uv_fs_close(tc->loop, &req, out_fd, NULL) < 0) {
                MVM_exception_throw_adhoc(tc, "Failed to close file: %s", uv_strerror(req.result));
            }

            return;
        }
        else
            MVM_free(b);
    }
    else
        MVM_free(a);

    MVM_exception_throw_adhoc(tc, "Failed to copy file: %s", uv_strerror(req.result));
}
Ejemplo n.º 2
0
// ## open path as file
// Create a file-open-request, associate the state and dispatch it on the loop. The
// [on_file_open](#on-file-open) is invoked once the file is opened.
static void open_path_as_file(req_res_t *rr) {
	int r;
	uv_fs_t* file_open_req = malloc(sizeof(uv_fs_t));
    if (file_open_req != NULL) {
        DEBUG("read_file\n");

        file_open_req->data = rr;
        rr->pending_requests++;
        r = uv_fs_open(loop, file_open_req, rr->path, O_ASYNC | O_RDONLY, S_IFREG, on_file_open);
        if (r != 0) {
            fprintf(stderr, "open_path_as_file error: %d\n", r);
            rr->pending_requests--;
            free(file_open_req);
            /* ToDo: To be moved to finalize? */
            req_res_free(rr);
		// return;
        }
        else {
            /* ToDo: No file opened. Shouldn't this be reported? */
        }
	}
    else {
        DEBUG("open_path_as_file error: memory leak, no handle for file_open_req.\n");
    }
}
Ejemplo n.º 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");
    }
}
Ejemplo n.º 4
0
int main(int argc, char *argv[])
{
    if (argc < 2) return -1;

    int r = uv_pipe_init(uv_default_loop(), &stdin_pipe, 0);
    assert(r==0);
    uv_pipe_open(&stdin_pipe, 0);

    uv_pipe_init(uv_default_loop(), &stdout_pipe, 0);
    uv_pipe_open(&stdin_pipe, 1);

  printf("%d\n", uv_guess_handle(0));

    uv_fs_t file_req;
    int fd = uv_fs_open(uv_default_loop(), &file_req, argv[1], O_WRONLY | O_CREAT, 0644, NULL);

    uv_pipe_init(uv_default_loop(), &file_pipe, 0);
    uv_pipe_open(&file_pipe, fd);

    printf("hhhhhhhhhh\n");
    uv_read_start((uv_stream_t*) &stdin_pipe, buf_alloc, OnRead);

    uv_run(uv_default_loop(), UV_RUN_DEFAULT);

    return 0;
}
Ejemplo n.º 5
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);
}
Ejemplo n.º 6
0
/**
 * open new file, filename: $ROOT_DIR/yyyyMMddhhmmss.log
 */
int css_logger_new_file()
{
	int r;
	uv_fs_t req;
	r = css_logger_rename_old_logs();
	if (r < 0)
		return r;
	css_logger.writer = (css_logger_writer_t*) malloc(sizeof(css_logger_writer_t));
	sprintf(css_logger.writer->path, "%s%d%s", css_logger.root_dir, 0, DEFAULT_LOG_FILE_FIX);
	css_logger.writer->loop = css_logger.loop;
	css_logger.writer_offset = 0;
	r = uv_fs_open(css_logger.loop, &req, css_logger.writer->path,
	O_RDWR | O_CREAT, S_IWUSR | S_IRUSR, NULL);

	if (r < 0) {
		printf("open logger %s faild.\n", css_logger.writer->path);
		uv_fs_req_cleanup(&req);
	} else {
		// set fd to r, start timer and reset r=0 when r >0
//		printf("open logger %s successful.\n", css_logger.writer->path);
		css_logger.writer->fd = (uv_file) req.result;
		uv_fs_req_cleanup(&req);
		r = 0;
	}
	return r;
}
Ejemplo n.º 7
0
/* Opens a file, returning a synchronous file handle. */
MVMObject * MVM_file_open_fh(MVMThreadContext *tc, MVMString *filename, MVMString *mode) {
    char          * const fname  = MVM_string_utf8_encode_C_string(tc, filename);
    char          * const fmode  = MVM_string_utf8_encode_C_string(tc, mode);
    MVMOSHandle   * const result = (MVMOSHandle *)MVM_repr_alloc_init(tc, tc->instance->boot_types.BOOTIO);
    MVMIOFileData * const data   = MVM_calloc(1, sizeof(MVMIOFileData));
    uv_fs_t req;
    uv_file fd;

    /* Resolve mode description to flags. */
    int flag;
    if (!resolve_open_mode(&flag, fmode)) {
        char *waste[] = { fmode, NULL };
        MVM_free(fname);
        MVM_exception_throw_adhoc_free(tc, waste, "Invalid open mode: %s", fmode);
    }
    MVM_free(fmode);

    /* Try to open the file. */
    if ((fd = uv_fs_open(tc->loop, &req, (const char *)fname, flag, DEFAULT_MODE, NULL)) < 0) {
        char *waste[] = { fname, NULL };
        MVM_exception_throw_adhoc_free(tc, waste, "Failed to open file %s: %s", fname, uv_strerror(req.result));
    }

    /* Set up handle. */
    data->fd          = fd;
    data->filename    = fname;
    data->encoding    = MVM_encoding_type_utf8;
    result->body.ops  = &op_table;
    result->body.data = data;

    return (MVMObject *)result;
}
Ejemplo n.º 8
0
bool xmrig::CommonConfig::save()
{
    if (m_fileName.isNull()) {
        return false;
    }

    uv_fs_t req;
    const int fd = uv_fs_open(uv_default_loop(), &req, m_fileName.data(), O_WRONLY | O_CREAT | O_TRUNC, 0644, nullptr);
    if (fd < 0) {
        return false;
    }

    uv_fs_req_cleanup(&req);

    rapidjson::Document doc;
    getJSON(doc);

    FILE *fp = fdopen(fd, "w");

    char buf[4096];
    rapidjson::FileWriteStream os(fp, buf, sizeof(buf));
    rapidjson::PrettyWriter<rapidjson::FileWriteStream> writer(os);
    doc.Accept(writer);

    fflush(fp);

    uv_fs_close(uv_default_loop(), &req, fd, nullptr);
    uv_fs_req_cleanup(&req);

    LOG_NOTICE("configuration saved to: \"%s\"", m_fileName.data());
    return true;
}
Ejemplo n.º 9
0
int main(int argc, char **argv) {
  if (argc < 2) {
    fprintf(stderr, "Please provide output file.\n");
    fprintf(stderr, "Example: echo \"hello world\" | ./main test.txt\n");
    exit(1);
  }

  const char *filename = argv[1];

  uv_loop_t *loop = uv_default_loop();

  uv_pipe_init(loop, &stdin_pipe, NOIPC);
  uv_pipe_open(&stdin_pipe, STDIN);

  uv_pipe_init(loop, &stdout_pipe, NOIPC);
  uv_pipe_open(&stdout_pipe, STDOUT);

  uv_fs_t file_req;
  int fd = uv_fs_open(loop, &file_req, filename, O_CREAT | O_RDWR, 0644, NULL);
  uv_pipe_init(loop, &file_pipe, 0);
  uv_pipe_open(&file_pipe, fd);

  // uv_pipe_t subclasses uv_stream_t
  uv_read_start((uv_stream_t*)&stdin_pipe, alloc_cb, read_cb);

  uv_run(loop, UV_RUN_DEFAULT);
  return 0;
}
Ejemplo n.º 10
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;
}
Ejemplo n.º 11
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);
}
Ejemplo n.º 12
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;
}
Ejemplo n.º 13
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);
}
Ejemplo n.º 14
0
void on_message_written(uv_fs_t* req)
{
    uv_fs_t open_req;

    uv_fs_req_cleanup(req);

    uv_fs_open(uv_default_loop(), &open_req, req->data, O_RDONLY, 0, on_open);
}
Ejemplo n.º 15
0
/// Opens or creates a file and returns a non-negative integer representing
/// the lowest-numbered unused file descriptor, for use in subsequent system
/// calls (read, write, lseek, fcntl, etc.). If the operation fails, `-errno`
/// is returned, and no file is created or modified.
///
/// @param flags Bitwise OR of flags defined in <fcntl.h>
/// @param mode Permissions for the newly-created file (IGNORED if 'flags' is
///        not `O_CREAT` or `O_TMPFILE`), subject to the current umask
/// @return file descriptor, or negative `errno` on failure
int os_open(const char* path, int flags, int mode)
{
  uv_fs_t open_req;
  int r = uv_fs_open(uv_default_loop(), &open_req, path, flags, mode, NULL);
  uv_fs_req_cleanup(&open_req);
  // r is the same as open_req.result (except for OOM: then only r is set).
  return r;
}
Ejemplo n.º 16
0
int main (int argc, char **argv) {
    uv_fs_open(uv_default_loop(), &open_req, argv[1], O_RDONLY, 0, on_open);
    uv_run(uv_default_loop(), UV_RUN_DEFAULT);
    uv_fs_req_cleanup(&open_req);
    uv_fs_req_cleanup(&read_req);
    uv_fs_req_cleanup(&write_req);
    return 0;
}
Ejemplo n.º 17
0
void fileOpen(WrenVM* vm)
{
  const char* path = wrenGetSlotString(vm, 1);
  uv_fs_t* request = createRequest(wrenGetSlotValue(vm, 2));

  // TODO: Allow controlling flags and modes.
  uv_fs_open(getLoop(), request, path, O_RDONLY, 0, fileOpenCallback);
}
Ejemplo n.º 18
0
Archivo: uvc.c Proyecto: whtc123/libuvc
uv_file uvc_fs_open(uvc_io *io,char *path,int flasgs){
	io->handle->data=io;
	uv_fs_open(uvc_loop_default(),(uv_fs_t *)io->handle,path,flasgs,0,uvc_fs_cb);
	io->cur =uvc_self();
	uvc_yield( );
	io->file = ((uv_fs_t *)(io->handle))->result;
	uv_fs_req_cleanup((uv_fs_t *)io->handle);
	return io->file;
}
Ejemplo n.º 19
0
/*
 * fs.open
 */
static int fs_open(lua_State* L) {
  FSR__SETUP
  const char *path = luaL_checkstring(L, 1);
  int flags = fs_checkflags(L, 2);
  const char *mode_str = luaL_optstring(L, 3, "0666");
  int mode = strtol(mode_str, (char**) NULL, 8);
  FSR__SET_OPT_CB(4, on_fs_callback)
  uv_fs_open(loop, req, path, flags, mode, cb);
  FSR__TEARDOWN
}
Ejemplo n.º 20
0
  void FileObject::open(gc<Fiber> fiber, gc<String> path)
  {
    FSTask* task = new FSTask(fiber);

    // TODO(bob): Make this configurable.
    int flags = O_RDONLY;
    // TODO(bob): Make this configurable when creating a file.
    int mode = 0;
    uv_fs_open(task->loop(), task->request(), path->cString(), flags, mode,
               openFileCallback);
  }
Ejemplo n.º 21
0
AccessLog::AccessLog(xmrig::Controller *controller) :
    m_file(-1)
{
    const char *fileName = controller->config()->accessLog();
    if (!fileName) {
        return;
    }

    uv_fs_t req;
    m_file = uv_fs_open(uv_default_loop(), &req, fileName, O_CREAT | O_APPEND | O_WRONLY, 0644, nullptr);
    uv_fs_req_cleanup(&req);
}
Ejemplo n.º 22
0
static void create_file(const char* name) {
  int r;
  uv_file file;
  uv_fs_t req;

  r = uv_fs_open(NULL, &req, name, O_WRONLY | O_CREAT, S_IWUSR | S_IRUSR, NULL);
  ASSERT(r >= 0);
  file = r;
  uv_fs_req_cleanup(&req);
  r = uv_fs_close(NULL, &req, file, NULL);
  ASSERT(r == 0);
  uv_fs_req_cleanup(&req);
}
Ejemplo n.º 23
0
int main() {
    loop = uv_default_loop();

    int r = uv_fs_open(loop, &open_req, path, O_RDONLY, S_IRUSR, open_cb);

    if (r) {
        fprintf(stderr, "Error at opening file: %s\n",
                uv_strerror(uv_last_error(loop)));
    }

    uv_run(loop, UV_RUN_DEFAULT);

    return 0;
}
Ejemplo n.º 24
0
void init(uv_loop_t *loop) {
  int r;

  uv_fs_t *open_req = malloc(sizeof(uv_fs_t));

  /* 1. Setup open request */
  context_t *context = malloc(sizeof(context_t));
  context->open_req  = open_req;
  open_req->data = context;

  /* 2. Open file */
  r = uv_fs_open(loop, open_req, filename, O_RDONLY, S_IRUSR, open_cb);
  if (r < 0) CHECK(r, "uv_fs_open");
}
Ejemplo n.º 25
0
static void create_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_WRONLY | O_CREAT,
                   S_IWRITE | S_IREAD, NULL);
    ASSERT(r != -1);
    file = r;
    uv_fs_req_cleanup(&req);
    r = uv_fs_close(loop, &req, file, NULL);
    ASSERT(r == 0);
    uv_fs_req_cleanup(&req);
}
static bool_t getPage(EFSRepoRef const repo, HTTPConnectionRef const conn, HTTPMethod const method, strarg_t const URI) {
	if(HTTP_GET != method) return false;
	size_t pathlen = prefix("/", URI);
	if(!pathlen) return false;
	if(!pathterm(URI, (size_t)pathlen)) return false;
	EFSSessionRef const session = auth(repo, conn, method, URI+pathlen);
	if(!session) {
		HTTPConnectionSendStatus(conn, 403);
		return true;
	}

	// TODO: Parse querystring `q` parameter
	EFSFilterRef const filter = EFSFilterCreate(EFSTypeFilter);
	EFSFilterAddStringArg(filter, "text/html; charset=utf-8");

	EFSFileInfo *const files = EFSSessionCreateFileInfoList(session, filter, RESULTS_MAX);

	HTTPConnectionWriteResponse(conn, 200, "OK");
	HTTPConnectionWriteHeader(conn, "Content-Type", "text/html; charset=utf-8");
	HTTPConnectionWriteHeader(conn, "Transfer-Encoding", "chunked");
	HTTPConnectionBeginBody(conn);

	// TODO: Page header

	uv_fs_t req = { .data = co_active(); }
	for(index_t i = 0; i < files->count; ++i) {
		uv_fs_open(loop, &req, path, O_RDONLY, 0400, async_fs_cb);
		co_switch(yield);
		uv_fs_req_cleanup(&req);
		uv_file const file = req.result;
		if(file < 0) continue;
		HTTPConnectionWriteChunkLength(conn, files->items[i].size);
		HTTPConnectionWriteFile(conn, file);
		uv_fs_close(loop, &req, file, async_fs_cb);
		co_switch(yield);
		uv_fs_req_cleanup(&req);
		HTTPConnectionWrite(conn, "\r\n", 2);
	}

	// TODO: Page trailer

	HTTPConnectionWriteChunkLength(conn, 0);
	HTTPConnectionWrite(conn, "\r\n", 2);
	HTTPConnectionEnd(conn);

	EFSFileInfoListFree(files);
	return true;
}
Ejemplo n.º 27
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);
}
Ejemplo n.º 28
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;
}
Ejemplo n.º 29
0
/**
 * reopen last log file.
 */
int css_logger_reopen_file(char *filenames, int len)
{
	int r;
	char *logger_name_t = NULL;
	char *filename = filenames; //readdir_req.ptr;
	uv_fs_t req;
	int i;
	for (i = 0; i < len; i++) {
		if (strstr(filename, DEFAULT_LOG_FILE_FIX) != NULL) {
			if (logger_name_t == NULL || strcmp(filename, logger_name_t) < 0) {
				logger_name_t = filename;
			}
		}
		filename += strlen(filename) + 1;
	}
	if (logger_name_t == NULL) {
		r = css_logger_new_file();
	} else {
		css_logger.writer = (css_logger_writer_t*) malloc(sizeof(css_logger_writer_t));
		sprintf(css_logger.writer->path, "%s%s", css_logger.root_dir, logger_name_t);
		css_logger.writer->loop = css_logger.loop;
		r = uv_fs_open(css_logger.loop, &req, css_logger.writer->path,
		O_RDWR | O_APPEND, S_IWUSR | S_IRUSR, NULL);
		if (r < 0 || req.result < 0) {
			printf("reopen logger %s faild.\n", css_logger.writer->path);
			uv_fs_req_cleanup(&req);
		} else {
			// set fd to r, start timer and reset r=0 when r >0
			css_logger.writer->fd = (uv_file) req.result;
			uv_fs_req_cleanup(&req);
			r = uv_fs_stat(css_logger.loop, &req, css_logger.writer->path,
			NULL);
			if (r == 0 && req.result == 0) {
				uv_stat_t s = req.statbuf;
				css_logger.writer_offset = s.st_size;
				printf("reopen logger %s,offset:%lld successful.\n", css_logger.writer->path, css_logger.writer_offset);

			}
			uv_fs_req_cleanup(&req);
		}
	}
	return r;
}
Ejemplo n.º 30
0
int main(int argc, char **argv) {
    loop = uv_default_loop();

    uv_pipe_init(loop, &stdin_pipe, 0);
    uv_pipe_open(&stdin_pipe, 0);

    uv_pipe_init(loop, &stdout_pipe, 0);
    uv_pipe_open(&stdout_pipe, 1);
    
    uv_fs_t file_req;
    int fd = uv_fs_open(loop, &file_req, argv[1], O_CREAT | O_RDWR, 0644, NULL);
    uv_pipe_init(loop, &file_pipe, 0);
    uv_pipe_open(&file_pipe, fd);

    uv_read_start((uv_stream_t*)&stdin_pipe, alloc_buffer, read_stdin);

    uv_run(loop, UV_RUN_DEFAULT);
    return 0;
}