RAISES_NEG Py_ssize_t lobject_seek(lobjectObject *self, Py_ssize_t pos, int whence) { PGresult *pgres = NULL; char *error = NULL; Py_ssize_t where; Dprintf("lobject_seek: fd = %d, pos = " FORMAT_CODE_PY_SSIZE_T ", whence = %d", self->fd, pos, whence); Py_BEGIN_ALLOW_THREADS; pthread_mutex_lock(&(self->conn->lock)); #ifdef HAVE_LO64 if (self->conn->server_version < 90300) { where = (Py_ssize_t)lo_lseek(self->conn->pgconn, self->fd, (int)pos, whence); } else { where = (Py_ssize_t)lo_lseek64(self->conn->pgconn, self->fd, pos, whence); } #else where = (Py_ssize_t)lo_lseek(self->conn->pgconn, self->fd, (int)pos, whence); #endif Dprintf("lobject_seek: 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; }
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; }
static void pickout(PGconn *conn, Oid lobjId, int start, int len) { int lobj_fd; char *buf; int nbytes; int nread; lobj_fd = lo_open(conn, lobjId, INV_READ); if (lobj_fd < 0) fprintf(stderr, "cannot open large object %u", lobjId); lo_lseek(conn, lobj_fd, start, SEEK_SET); buf = malloc(len + 1); nread = 0; while (len - nread > 0) { nbytes = lo_read(conn, lobj_fd, buf, len - nread); buf[nbytes] = '\0'; fprintf(stderr, ">>> %s", buf); nread += nbytes; if (nbytes <= 0) break; /* no more data? */ } free(buf); fprintf(stderr, "\n"); lo_close(conn, lobj_fd); }
static void overwrite(PGconn *conn, Oid lobjId, int start, int len) { int lobj_fd; char *buf; int nbytes; int nwritten; int i; lobj_fd = lo_open(conn, lobjId, INV_WRITE); if (lobj_fd < 0) fprintf(stderr, "cannot open large object %u", lobjId); lo_lseek(conn, lobj_fd, start, SEEK_SET); buf = malloc(len + 1); for (i = 0; i < len; i++) buf[i] = 'X'; buf[i] = '\0'; nwritten = 0; while (len - nwritten > 0) { nbytes = lo_write(conn, lobj_fd, buf + nwritten, len - nwritten); nwritten += nbytes; if (nbytes <= 0) { fprintf(stderr, "\nWRITE FAILED!\n"); break; } } free(buf); fprintf(stderr, "\n"); lo_close(conn, lobj_fd); }
/* ************************************************************************* */ int pg_lo_seek(ClipMachine* mp, SQLCONN* c, int oid_fd, int offset, int whence){ PG_CONN *conn = (PG_CONN*)c; int rt; if(!conn->at){ _clip_trap_err(mp,0,0,0,subsys,ER_START,er_start); return 1; } switch(whence){ case SQLBLOB_SEEKTOP: whence = SEEK_SET; break; case SQLBLOB_SEEKCURRENT: whence = SEEK_CUR; break; case SQLBLOB_SEEKBOTTOM: whence = SEEK_END; break; default: _clip_trap_err(mp,0,0,0,subsys,ER_START,er_blob_seek); return 1; } rt = lo_lseek(conn->conn, oid_fd, offset, whence); if (rt < 0){ _clip_trap_err(mp,0,0,0,subsys,ER_START,er_blob_seek); return 1; } _clip_retni(mp,(unsigned int) rt); return 0; }
static int pgsql_lob_seek(php_stream *stream, zend_off_t offset, int whence, zend_off_t *newoffset) { struct pdo_pgsql_lob_self *self = (struct pdo_pgsql_lob_self*)stream->abstract; int pos = lo_lseek(self->conn, self->lfd, offset, whence); *newoffset = pos; return pos >= 0 ? 0 : -1; }
static int pgsql_lob_seek(php_stream *stream, zend_off_t offset, int whence, zend_off_t *newoffset) { struct pdo_pgsql_lob_self *self = (struct pdo_pgsql_lob_self*)stream->abstract; #if HAVE_PG_LO64 && ZEND_ENABLE_ZVAL_LONG64 zend_off_t pos = lo_lseek64(self->conn, self->lfd, offset, whence); #else zend_off_t pos = lo_lseek(self->conn, self->lfd, offset, whence); #endif *newoffset = pos; return pos >= 0 ? 0 : -1; }
/*------------------------------------------------------------------------ * _ReadChunkArray1El -- * returns one element of the chunked array as specified by the index "st" * the chunked file descriptor is "fp" *------------------------------------------------------------------------- */ struct varlena * _ReadChunkArray1El(int st[], int bsize, int fp, ArrayType *array, bool *isNull) { int i, j, n, temp, srcOff; int chunk_st[MAXDIM]; int *C, csize, *dim, *lb; int PCHUNK[MAXDIM], PC[MAXDIM]; CHUNK_INFO *A = (CHUNK_INFO *) ARR_DATA_PTR(array); n = ARR_NDIM(array); lb = ARR_LBOUND(array); C = A->C; dim = ARR_DIMS(array); csize = C[n-1]; PC[n-1] = 1; temp = dim[n - 1]/C[n-1]; for (i = n-2; i >= 0; i--){ PC[i] = PC[i+1] * temp; temp = dim[i] / C[i]; csize *= C[i]; } for (i = 0; i < n; st[i] -= lb[i], i++); mda_get_prod(n, C, PCHUNK); array2chunk_coord(n, C, st, chunk_st); for (i = j = 0; i < n; i++) j+= chunk_st[i]*PC[i]; srcOff = j * csize; for(i = 0; i < n; i++) srcOff += (st[i]-chunk_st[i]*C[i])*PCHUNK[i]; srcOff *= bsize; if (lo_lseek(fp, srcOff, SEEK_SET) < 0) RETURN_NULL; #ifdef LOARRAY return (struct varlena *) LOread(fp, bsize); #endif return (struct varlena *) 0; }
/*-------------------------------------------------------------------------- * seek_and_read() * seeks to the asked location in the input file and reads the * appropriate number of blocks * Called By: read_chunk() *-------------------------------------------------------------------------- */ static int seek_and_read(int pos, int size, char buff[], int fp, int from) { struct varlena *v; /* Assuming only one file */ if ( lo_lseek(fp, pos, from ) < 0) elog(WARN, "File seek error"); #ifdef LOARRAY v = (struct varlena *) LOread(fp, size); #endif if (VARSIZE(v) - 4 < size) elog(WARN, "File read error"); memmove(buff, VARDATA(v), size); pfree(v); return(1); }
CAMLprim value lo_lseek_stub( value v_conn, value v_fd, value v_pos, value v_whence) { CAMLparam1(v_conn); PGconn *conn = get_conn(v_conn); value v_res; int whence; caml_enter_blocking_section(); switch (Int_val(v_whence)) { case 0 : whence = SEEK_SET; break; case 1 : whence = SEEK_CUR; break; default : whence = SEEK_END; break; } v_res = Val_int(lo_lseek(conn, Int_val(v_fd), Long_val(v_pos), whence)); caml_leave_blocking_section(); CAMLreturn(v_res); }
/*---------------------------------------------------------------------------- * _ReadChunkArray -- * returns the subarray specified bu the range indices "st" and "endp" * from the chunked array stored in file "fp" *--------------------------------------------------------------------------- */ int _ReadChunkArray(int st[], int endp[], int bsize, int fp, char *destfp, ArrayType *array, int isDestLO, bool *isNull) { int i,j,jj; int n, temp, words_read; int chunk_span[MAXDIM], chunk_off[MAXDIM]; int chunk_st[MAXDIM], chunk_end[MAXDIM]; int block_seek; int bptr, *C, csize, *dim, *lb; int range_st[MAXDIM], range_end[MAXDIM], range[MAXDIM], array_span[MAXDIM]; int PA[MAXDIM], PCHUNK[MAXDIM], PC[MAXDIM]; int to_read; int cdist[MAXDIM], adist[MAXDIM]; int dist[MAXDIM], temp_seek; int srcOff; /* Needed since LO don't understand SEEK_CUR*/ char *baseDestFp = (char *)destfp; CHUNK_INFO *A = (CHUNK_INFO *) ARR_DATA_PTR(array); n = ARR_NDIM(array); dim = ARR_DIMS(array); lb = ARR_LBOUND(array); C = A->C; csize = C[n-1]; PC[n-1] = 1; temp = dim[n - 1]/C[n-1]; for (i = n-2; i >= 0; i--){ PC[i] = PC[i+1] * temp; temp = dim[i] / C[i]; csize *= C[i]; } for (i = 0; i < n; st[i] -= lb[i], endp[i] -= lb[i], i++) ; mda_get_prod(n, C, PCHUNK); mda_get_range(n, array_span, st, endp); mda_get_prod(n, array_span, PA); array2chunk_coord(n, C, st, chunk_st); array2chunk_coord(n, C, endp, chunk_end); mda_get_range(n, chunk_span, chunk_st, chunk_end); mda_get_offset_values(n, dist, PC, chunk_span); for (i = 0; i < n; i++) { range_st[i] = st[i]; range_end[i] = min(chunk_st[i]*C[i]+C[i]-1, endp[i]); } for (i = j = 0; i < n; i++) j+= chunk_st[i]*PC[i]; temp_seek = srcOff = j * csize * bsize; if (lo_lseek(fp, srcOff, SEEK_SET) < 0) RETURN_NULL; jj = n-1; for (i = 0; i < n; chunk_off[i++] = 0) ; words_read = 0; temp_seek = 0; do { /* Write chunk (chunk_st) to output buffer */ mda_get_range(n, array_span, range_st, range_end); mda_get_offset_values(n, adist, PA, array_span); mda_get_offset_values(n, cdist, PCHUNK, array_span); for (i=0; i < n; range[i] = range_st[i]-st[i], i++); bptr = tuple2linear(n, range, PA); for (i = 0; i < n; range[i++] = 0); j = n-1; bptr *= bsize; if (isDestLO) { if (lo_lseek(destfp, bptr, SEEK_SET) < 0) RETURN_NULL; } else destfp = baseDestFp + bptr; for(i = 0, block_seek = 0; i < n; i++) block_seek += (range_st[i]-(chunk_st[i] + chunk_off[i]) *C[i])*PCHUNK[i]; if (dist[jj] + block_seek + temp_seek) { temp = (dist[jj]*csize+block_seek+temp_seek)*bsize; srcOff += temp; if (lo_lseek(fp, srcOff, SEEK_SET) < 0) RETURN_NULL; } for (i = n-1, to_read = bsize; i >= 0; to_read *= min(C[i], array_span[i]), i--) if (cdist[i] || adist[i]) break; do { if (cdist[j]) { srcOff += (cdist[j]*bsize); if (lo_lseek(fp, srcOff, SEEK_SET) < 0) RETURN_NULL; } block_seek += cdist[j]; bptr += adist[j]*bsize; if (isDestLO) { if (lo_lseek(destfp, bptr, SEEK_SET) < 0) RETURN_NULL; } else destfp = baseDestFp + bptr; temp = _LOtransfer ((char**)&destfp, to_read, 1, (char**)&fp, 1, isDestLO); if (temp < to_read) RETURN_NULL; srcOff += to_read; words_read+=to_read; bptr += to_read; block_seek += (to_read/bsize); /* * compute next tuple in range[] */ { int x; if (!(i+1)) j = -1; else { range[i] = (range[i]+1)%array_span[i]; for (x = i; x*(!range[x]); x--) range[x-1] = (range[x-1]+1)%array_span[x-1]; if (x) j = x; else { if (range[0]) j = 0; else j = -1; } } } /* * end of compute next tuple -- * j is set to -1 if tuple generation is over */ } while (j != -1); block_seek = csize - block_seek; temp_seek = block_seek; jj = next_tuple(n, chunk_off, chunk_span); if (jj == -1) break; range_st[jj] = (chunk_st[jj]+chunk_off[jj])*C[jj]; range_end[jj] = min(range_st[jj] + C[jj]-1, endp[jj]); for (i = jj+1; i < n; i++) { range_st[i] = st[i]; range_end[i] = min((chunk_st[i]+chunk_off[i])*C[i]+C[i]-1, endp[i]); } } while (jj != -1); return(words_read); }