Esempio n. 1
0
/* Frees data associated with the handle. */
static void gc_free(MVMThreadContext *tc, MVMObject *h, void *d) {
    MVMIOFileData *data = (MVMIOFileData *)d;
    if (data) {
        if (data->ds)
            MVM_string_decodestream_destory(tc, data->ds);
        if (data->filename)
            MVM_free(data->filename);
        MVM_free(data);
    }
}
Esempio n. 2
0
static void do_close(MVMThreadContext *tc, MVMIOSyncSocketData *data) {
    if (data->ss.handle) {
         uv_close((uv_handle_t *)data->ss.handle, NULL);
         data->ss.handle = NULL;
    }
    if (data->ss.ds) {
        MVM_string_decodestream_destory(tc, data->ss.ds);
        data->ss.ds = NULL;
    }
}
Esempio n. 3
0
static void closefh(MVMThreadContext *tc, MVMOSHandle *h) {
    MVMIOSyncStreamData *data = (MVMIOSyncStreamData *)h->body.data;
    if (data->handle && not_std_handle(tc, (MVMObject *)h)) {
         uv_close((uv_handle_t *)data->handle, NULL);
         data->handle = NULL;
         if (data->ds) {
            MVM_string_decodestream_destory(tc, data->ds);
            data->ds = NULL;
        }
    }
}
Esempio n. 4
0
/* Closes the file. */
static MVMint64 closefh(MVMThreadContext *tc, MVMOSHandle *h) {
    MVMIOFileData *data = (MVMIOFileData *)h->body.data;
    uv_fs_t req;
    if (data->ds) {
        MVM_string_decodestream_destory(tc, data->ds);
        data->ds = NULL;
    }
    if (uv_fs_close(tc->loop, &req, data->fd, NULL) < 0) {
        data->fd = -1;
        MVM_exception_throw_adhoc(tc, "Failed to close filehandle: %s", uv_strerror(req.result));
    }
    data->fd = -1;
    return 0;
}
Esempio n. 5
0
/* Frees data associated with the handle, closing it if needed. */
static void gc_free(MVMThreadContext *tc, MVMObject *h, void *d) {
    MVMIOSyncStreamData *data = (MVMIOSyncStreamData *)d;
    if (data) {
        if (data->handle && not_std_handle(tc, h)) {
            uv_close((uv_handle_t *)data->handle, NULL);
            data->handle = NULL;
        }
        if (data->ds) {
            MVM_string_decodestream_destory(tc, data->ds);
            data->ds = NULL;
        }
        free(data);
    }
}
Esempio n. 6
0
/* Seek to the specified position in the file. */
static void seek(MVMThreadContext *tc, MVMOSHandle *h, MVMint64 offset, MVMint64 whence) {
    MVMIOFileData *data = (MVMIOFileData *)h->body.data;
    MVMint64 r;

    if (data->ds) {
        /* We'll start over from a new position. */
        MVM_string_decodestream_destory(tc, data->ds);
        data->ds = NULL;
    }

    /* Seek, then get absolute position for new decodestream. */
    if (MVM_platform_lseek(data->fd, offset, whence) == -1)
        MVM_exception_throw_adhoc(tc, "Failed to seek in filehandle: %d", errno);
    if ((r = MVM_platform_lseek(data->fd, 0, SEEK_CUR)) == -1)
        MVM_exception_throw_adhoc(tc, "Failed to seek in filehandle: %d", errno);
    data->ds = MVM_string_decodestream_create(tc, data->encoding, r);
}
Esempio n. 7
0
/* Closes the pipe. */
static MVMint64 do_close(MVMThreadContext *tc, MVMIOSyncPipeData *data) {
#ifdef _WIN32
    DWORD status = 0;
#else
    int status = 0;
#endif
    if (data->ss.handle == NULL || uv_is_closing((uv_handle_t*)data->ss.handle))
        return 0;
    /* closing the in-/output std filehandle will shutdown the child process. */
    uv_unref((uv_handle_t*)data->ss.handle);
    uv_close((uv_handle_t*)data->ss.handle, NULL);
    if (data->process) {
#ifdef _WIN32
        if (!uv_is_closing((uv_handle_t*)data->process))
            uv_process_close(tc->loop, data->process);
        GetExitCodeProcess(data->process->process_handle, &status);
        status = status << 8;
#else
        pid_t wpid;
        do
            wpid = waitpid(data->process->pid, &status, 0);
        while (wpid == -1 && errno == EINTR);
#endif
    }
    if (!status && data->process->data) {
        status = *(MVMint64*)data->process->data;
        MVM_free(data->process->data);
        data->process->data = NULL;
    }
    uv_unref((uv_handle_t *)data->process);
    uv_run(tc->loop, UV_RUN_DEFAULT);
    data->process   = NULL;
    data->ss.handle = NULL;
    if (data->ss.ds) {
        MVM_string_decodestream_destory(tc, data->ss.ds);
        data->ss.ds = NULL;
    }
    return (MVMint64)status;
}