int
lobject_seek(lobjectObject *self, int pos, int whence)
{
    PGresult *pgres = NULL;
    char *error = NULL;
    int where;

    Dprintf("lobject_seek: fd = %d, pos = %d, whence = %d",
            self->fd, pos, whence);

    Py_BEGIN_ALLOW_THREADS;
    pthread_mutex_lock(&(self->conn->lock));

    where = lo_lseek(self->conn->pgconn, self->fd, pos, whence);
    Dprintf("lobject_seek: where = %d", where);
    if (where < 0)
        collect_error(self->conn, &error);

    pthread_mutex_unlock(&(self->conn->lock));
    Py_END_ALLOW_THREADS;

    if (where < 0)
        pq_complete_error(self->conn, &pgres, &error);
    return where;
}
int
lobject_export(lobjectObject *self, const char *filename)
{
    PGresult *pgres = NULL;
    char *error = NULL;
    int retvalue;

    Py_BEGIN_ALLOW_THREADS;
    pthread_mutex_lock(&(self->conn->lock));

    retvalue = pq_begin_locked(self->conn, &pgres, &error);
    if (retvalue < 0)
        goto end;

    retvalue = lo_export(self->conn->pgconn, self->oid, filename);
    if (retvalue < 0)
        collect_error(self->conn, &error);

 end:
    pthread_mutex_unlock(&(self->conn->lock));
    Py_END_ALLOW_THREADS;

    if (retvalue < 0)
        pq_complete_error(self->conn, &pgres, &error);
    return retvalue;
}
Py_ssize_t
lobject_write(lobjectObject *self, const char *buf, size_t len)
{
    Py_ssize_t written;
    PGresult *pgres = NULL;
    char *error = NULL;

    Dprintf("lobject_writing: fd = %d, len = %d",
            self->fd, len);

    Py_BEGIN_ALLOW_THREADS;
    pthread_mutex_lock(&(self->conn->lock));

    PQsetnonblocking(self->conn->pgconn, 0);
    written = lo_write(self->conn->pgconn, self->fd, buf, len);
    if (written < 0)
        collect_error(self->conn, &error);
    PQsetnonblocking(self->conn->pgconn, 1);

    pthread_mutex_unlock(&(self->conn->lock));
    Py_END_ALLOW_THREADS;

    if (written < 0)
        pq_complete_error(self->conn, &pgres, &error);
    return written;
}
示例#4
0
RAISES_NEG int
lobject_truncate(lobjectObject *self, size_t len)
{
    int retvalue;
    PGresult *pgres = NULL;
    char *error = NULL;

    Dprintf("lobject_truncate: fd = %d, len = " FORMAT_CODE_SIZE_T,
            self->fd, len);

    Py_BEGIN_ALLOW_THREADS;
    pthread_mutex_lock(&(self->conn->lock));

#ifdef HAVE_LO64
    if (self->conn->server_version < 90300) {
        retvalue = lo_truncate(self->conn->pgconn, self->fd, len);
    } else {
        retvalue = lo_truncate64(self->conn->pgconn, self->fd, len);
    }
#else
    retvalue = lo_truncate(self->conn->pgconn, self->fd, len);
#endif
    Dprintf("lobject_truncate: result = %d", retvalue);
    if (retvalue < 0)
        collect_error(self->conn, &error);

    pthread_mutex_unlock(&(self->conn->lock));
    Py_END_ALLOW_THREADS;

    if (retvalue < 0)
        pq_complete_error(self->conn, &pgres, &error);
    return retvalue;

}
int
lobject_unlink(lobjectObject *self)
{
    PGresult *pgres = NULL;
    char *error = NULL;
    int retvalue = -1;

    Py_BEGIN_ALLOW_THREADS;
    pthread_mutex_lock(&(self->conn->lock));

    retvalue = pq_begin_locked(self->conn, &pgres, &error);
    if (retvalue < 0)
        goto end;

    /* first we make sure the lobject is closed and then we unlink */
    retvalue = lobject_close_locked(self, &error);
    if (retvalue < 0)
        goto end;

    retvalue = lo_unlink(self->conn->pgconn, self->oid);
    if (retvalue < 0)
        collect_error(self->conn, &error);

 end:
    pthread_mutex_unlock(&(self->conn->lock));
    Py_END_ALLOW_THREADS;

    if (retvalue < 0)
        pq_complete_error(self->conn, &pgres, &error);
    return retvalue;
}
示例#6
0
int
lobject_truncate(lobjectObject *self, size_t len)
{
    int retvalue;
    PGresult *pgres = NULL;
    char *error = NULL;

    Dprintf("lobject_truncate: fd = %d, len = " FORMAT_CODE_PY_SSIZE_T,
            self->fd, len);

    Py_BEGIN_ALLOW_THREADS;
    pthread_mutex_lock(&(self->conn->lock));

    retvalue = lo_truncate(self->conn->pgconn, self->fd, len);
    Dprintf("lobject_truncate: result = %d", retvalue);
    if (retvalue < 0)
        collect_error(self->conn, &error);

    pthread_mutex_unlock(&(self->conn->lock));
    Py_END_ALLOW_THREADS;

    if (retvalue < 0)
        pq_complete_error(self->conn, &pgres, &error);
    return retvalue;

}
示例#7
0
RAISES_NEG Py_ssize_t
lobject_tell(lobjectObject *self)
{
    PGresult *pgres = NULL;
    char *error = NULL;
    Py_ssize_t where;

    Dprintf("lobject_tell: fd = %d", self->fd);

    Py_BEGIN_ALLOW_THREADS;
    pthread_mutex_lock(&(self->conn->lock));

#ifdef HAVE_LO64
    if (self->conn->server_version < 90300) {
        where = (Py_ssize_t)lo_tell(self->conn->pgconn, self->fd);
    } else {
        where = (Py_ssize_t)lo_tell64(self->conn->pgconn, self->fd);
    }
#else
    where = (Py_ssize_t)lo_tell(self->conn->pgconn, self->fd);
#endif
    Dprintf("lobject_tell: where = " FORMAT_CODE_PY_SSIZE_T, where);
    if (where < 0)
        collect_error(self->conn, &error);

    pthread_mutex_unlock(&(self->conn->lock));
    Py_END_ALLOW_THREADS;

    if (where < 0)
        pq_complete_error(self->conn, &pgres, &error);
    return where;
}
示例#8
0
RAISES_NEG static int
lobject_close_locked(lobjectObject *self, char **error)
{
    int retvalue;

    Dprintf("lobject_close_locked: conn->closed %ld", self->conn->closed);
    switch (self->conn->closed) {
    case 0:
        /* Connection is open, go ahead */
        break;
    case 1:
        /* Connection is closed, return a success */
        return 0;
        break;
    default:
        *error = strdup("the connection is broken");
        return -1;
        break;
    }

    if (self->conn->autocommit ||
        self->conn->mark != self->mark ||
        self->fd == -1)
        return 0;

    retvalue = lo_close(self->conn->pgconn, self->fd);
    self->fd = -1;
    if (retvalue < 0)
        collect_error(self->conn, error);

    return retvalue;
}
示例#9
0
文件: sampler.c 项目: mojaves/vmon
static gint
list_domains_work(gpointer data)
{
    VmonRequest *req = data;
    virDomainPtr *domains;
    size_t i;
    int ret = -1;
    int err = 0;

    ret = virConnectListAllDomains(req->ctx->conn,
                                   &domains,
                                   req->ctx->flags);
    if (ret < 0) {
        collect_error(req, ret, FALSE);
        return ret;
    }

    for (i = 0; i < (size_t)ret; i++) {
        VmonRequest vreq;
        memcpy(&vreq, req, sizeof(vreq));
        vreq.dom = domains[i];

        err = executor_dispatch(vreq.ctx->executor,
                                sample_domain_work,
                                sampling_collect,
                                &vreq,
                                sizeof(vreq),
                                vreq.ctx->conf.timeout);
        if (err) {
            collect_error(&vreq, err, FALSE);
        }
    }

    free(domains);
    return (err) ?err :0;
}
示例#10
0
文件: sampler.c 项目: mojaves/vmon
static gint
sampling_collect(gpointer data, gint error, gboolean timeout)
{
    VmonRequest *req = data;
    gboolean ret;

    if (error || timeout) {
        ret = collect_error(req, error, timeout);
    } else {
        ret = collect_success(req);
    }
    if (req->dom) {
        virDomainFree(req->dom);
    }
    return ret;
}
示例#11
0
static int
lobject_close_locked(lobjectObject *self, char **error)
{
    int retvalue;

    if (self->conn->isolation_level == 0 ||
        self->conn->mark != self->mark ||
        self->fd == -1)
        return 0;

    retvalue = lo_close(self->conn->pgconn, self->fd);
    self->fd = -1;
    if (retvalue < 0)
        collect_error(self->conn, error);

    return retvalue;
}
示例#12
0
Py_ssize_t
lobject_read(lobjectObject *self, char *buf, size_t len)
{
    Py_ssize_t n_read;
    PGresult *pgres = NULL;
    char *error = NULL;

    Py_BEGIN_ALLOW_THREADS;
    pthread_mutex_lock(&(self->conn->lock));

    n_read = lo_read(self->conn->pgconn, self->fd, buf, len);
    if (n_read < 0)
        collect_error(self->conn, &error);

    pthread_mutex_unlock(&(self->conn->lock));
    Py_END_ALLOW_THREADS;

    if (n_read < 0)
        pq_complete_error(self->conn, &pgres, &error);
    return n_read;
}
示例#13
0
int
lobject_open(lobjectObject *self, connectionObject *conn,
              Oid oid, int mode, Oid new_oid, const char *new_file)
{
    int retvalue = -1;
    PGresult *pgres = NULL;
    char *error = NULL;

    Py_BEGIN_ALLOW_THREADS;
    pthread_mutex_lock(&(self->conn->lock));

    retvalue = pq_begin_locked(self->conn, &pgres, &error);
    if (retvalue < 0)
        goto end;

    /* if the oid is InvalidOid we create a new lob before opening it
       or we import a file from the FS, depending on the value of
       new_name */
    if (oid == InvalidOid) {
        if (new_file)
            self->oid = lo_import(self->conn->pgconn, new_file);
        else
            self->oid = lo_create(self->conn->pgconn, new_oid);

        Dprintf("lobject_open: large object created with oid = %d",
                self->oid);

        if (self->oid == InvalidOid) {
            collect_error(self->conn, &error);
            retvalue = -1;
            goto end;
        }

        mode = INV_WRITE;
    }
    else {
        self->oid = oid;
        if (mode == 0) mode = INV_READ;
    }

    /* if the oid is a real one we try to open with the given mode,
       unless the mode is -1, meaning "don't open!" */
    if (mode != -1) {
        self->fd = lo_open(self->conn->pgconn, self->oid, mode);
        Dprintf("lobject_open: large object opened with fd = %d",
            self->fd);

        if (self->fd == -1) {
            collect_error(self->conn, &error);
            retvalue = -1;
            goto end;
        }
    }
    /* set the mode for future reference */
    switch (mode) {
    case -1:
        self->smode = "n"; break;
    case INV_READ:
        self->smode = "r"; break;
    case INV_WRITE:
        self->smode = "w"; break;
    case INV_READ+INV_WRITE:
        self->smode = "rw"; break;
    }
    retvalue = 0;

 end:
    pthread_mutex_unlock(&(self->conn->lock));
    Py_END_ALLOW_THREADS;

    if (retvalue < 0)
        pq_complete_error(self->conn, &pgres, &error);
    return retvalue;
}
示例#14
0
RAISES_NEG int
lobject_open(lobjectObject *self, connectionObject *conn,
              Oid oid, const char *smode, Oid new_oid, const char *new_file)
{
    int retvalue = -1;
    PGresult *pgres = NULL;
    char *error = NULL;
    int pgmode = 0;
    int mode;

    if (0 > (mode = _lobject_parse_mode(smode))) {
        return -1;
    }

    Py_BEGIN_ALLOW_THREADS;
    pthread_mutex_lock(&(self->conn->lock));

    retvalue = pq_begin_locked(self->conn, &pgres, &error, &_save);
    if (retvalue < 0)
        goto end;

    /* if the oid is InvalidOid we create a new lob before opening it
       or we import a file from the FS, depending on the value of
       new_file */
    if (oid == InvalidOid) {
        if (new_file)
            self->oid = lo_import(self->conn->pgconn, new_file);
        else {
            /* Use lo_creat when possible to be more middleware-friendly.
               See ticket #88. */
            if (new_oid != InvalidOid)
                self->oid = lo_create(self->conn->pgconn, new_oid);
            else
                self->oid = lo_creat(self->conn->pgconn, INV_READ | INV_WRITE);
        }

        Dprintf("lobject_open: large object created with oid = %d",
                self->oid);

        if (self->oid == InvalidOid) {
            collect_error(self->conn, &error);
            retvalue = -1;
            goto end;
        }

        mode = (mode & ~LOBJECT_READ) | LOBJECT_WRITE;
    }
    else {
        self->oid = oid;
    }

    /* if the oid is a real one we try to open with the given mode */
    if (mode & LOBJECT_READ) { pgmode |= INV_READ; }
    if (mode & LOBJECT_WRITE) { pgmode |= INV_WRITE; }
    if (pgmode) {
        self->fd = lo_open(self->conn->pgconn, self->oid, pgmode);
        Dprintf("lobject_open: large object opened with mode = %i fd = %d",
            pgmode, self->fd);

        if (self->fd == -1) {
            collect_error(self->conn, &error);
            retvalue = -1;
            goto end;
        }
    }

    /* set the mode for future reference */
    self->mode = mode;
    Py_BLOCK_THREADS;
    self->smode = _lobject_unparse_mode(mode);
    Py_UNBLOCK_THREADS;
    if (NULL == self->smode) {
        retvalue = 1;  /* exception already set */
        goto end;
    }

    retvalue = 0;

 end:
    pthread_mutex_unlock(&(self->conn->lock));
    Py_END_ALLOW_THREADS;

    if (retvalue < 0)
        pq_complete_error(self->conn, &pgres, &error);
    /* if retvalue > 0, an exception is already set */

    return retvalue;
}