1996-07-09 08:22:35 +02:00
|
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
|
* be-fsstubs.c
|
1997-09-07 07:04:48 +02:00
|
|
|
|
* support for filesystem operations on large objects
|
1996-07-09 08:22:35 +02:00
|
|
|
|
*
|
2002-06-20 22:29:54 +02:00
|
|
|
|
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
|
2000-01-26 06:58:53 +01:00
|
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-07-09 08:22:35 +02:00
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* IDENTIFICATION
|
2003-07-28 02:09:16 +02:00
|
|
|
|
* $Header: /cvsroot/pgsql/src/backend/libpq/be-fsstubs.c,v 1.66 2003/07/28 00:09:15 tgl Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
|
*
|
|
|
|
|
* NOTES
|
1997-09-07 07:04:48 +02:00
|
|
|
|
* This should be moved to a more appropriate place. It is here
|
|
|
|
|
* for lack of a better place.
|
1996-07-09 08:22:35 +02:00
|
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
|
* Builtin functions for open/close/read/write operations on large objects.
|
1996-07-09 08:22:35 +02:00
|
|
|
|
*
|
2000-06-28 05:33:33 +02:00
|
|
|
|
* These functions operate in a private MemoryContext, which means
|
1999-06-01 00:53:59 +02:00
|
|
|
|
* that large object descriptors hang around until we destroy the context.
|
2000-04-12 19:17:23 +02:00
|
|
|
|
* That happens in lo_commit(). It'd be possible to prolong the lifetime
|
1999-06-01 00:53:59 +02:00
|
|
|
|
* of the context so that LO FDs are good across transactions (for example,
|
|
|
|
|
* we could release the context only if we see that no FDs remain open).
|
|
|
|
|
* But we'd need additional state in order to do the right thing at the
|
|
|
|
|
* end of an aborted transaction. FDs opened during an aborted xact would
|
|
|
|
|
* still need to be closed, since they might not be pointing at valid
|
2000-06-28 05:33:33 +02:00
|
|
|
|
* relations at all. Locking semantics are also an interesting problem
|
|
|
|
|
* if LOs stay open across transactions. For now, we'll stick with the
|
|
|
|
|
* existing documented semantics of LO FDs: they're only good within a
|
|
|
|
|
* transaction.
|
1999-06-01 00:53:59 +02:00
|
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
|
*/
|
|
|
|
|
|
2000-10-24 03:38:44 +02:00
|
|
|
|
#include "postgres.h"
|
|
|
|
|
|
1996-11-06 09:48:33 +01:00
|
|
|
|
#include <fcntl.h>
|
1996-11-15 19:38:20 +01:00
|
|
|
|
#include <sys/stat.h>
|
1996-11-06 09:48:33 +01:00
|
|
|
|
#include <unistd.h>
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
1999-07-16 01:04:24 +02:00
|
|
|
|
#include "libpq/be-fsstubs.h"
|
1999-07-16 07:00:38 +02:00
|
|
|
|
#include "libpq/libpq-fs.h"
|
2001-06-13 23:44:41 +02:00
|
|
|
|
#include "miscadmin.h"
|
1999-07-16 07:00:38 +02:00
|
|
|
|
#include "storage/large_object.h"
|
2000-07-17 05:05:41 +02:00
|
|
|
|
#include "utils/memutils.h"
|
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
1998-07-21 06:17:30 +02:00
|
|
|
|
/* [PA] is Pascal Andr<64> <andre@via.ecp.fr> */
|
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
|
/*#define FSDB 1*/
|
2000-10-24 03:38:44 +02:00
|
|
|
|
#define BUFSIZE 8192
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
|
/*
|
2000-10-24 05:14:08 +02:00
|
|
|
|
* LO "FD"s are indexes into the cookies array.
|
|
|
|
|
*
|
2000-06-09 03:11:16 +02:00
|
|
|
|
* A non-null entry is a pointer to a LargeObjectDesc allocated in the
|
2000-10-24 05:14:08 +02:00
|
|
|
|
* LO private memory context. The cookies array itself is also dynamically
|
|
|
|
|
* allocated in that context. Its current allocated size is cookies_len
|
|
|
|
|
* entries, of which any unused entries will be NULL.
|
2000-06-09 03:11:16 +02:00
|
|
|
|
*/
|
2000-10-24 05:14:08 +02:00
|
|
|
|
static LargeObjectDesc **cookies = NULL;
|
2001-03-22 05:01:46 +01:00
|
|
|
|
static int cookies_size = 0;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
2000-06-28 05:33:33 +02:00
|
|
|
|
static MemoryContext fscxt = NULL;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
|
|
|
1997-09-08 23:56:23 +02:00
|
|
|
|
static int newLOfd(LargeObjectDesc *lobjCookie);
|
1997-09-08 04:41:22 +02:00
|
|
|
|
static void deleteLOfd(int fd);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
|
|
/*****************************************************************************
|
1997-09-07 07:04:48 +02:00
|
|
|
|
* File Interfaces for Large Objects
|
1996-07-09 08:22:35 +02:00
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
|
Datum
|
|
|
|
|
lo_open(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
|
Oid lobjId = PG_GETARG_OID(0);
|
|
|
|
|
int32 mode = PG_GETARG_INT32(1);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
LargeObjectDesc *lobjDesc;
|
1997-09-08 04:41:22 +02:00
|
|
|
|
int fd;
|
|
|
|
|
MemoryContext currentContext;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
|
#if FSDB
|
2003-05-27 19:49:47 +02:00
|
|
|
|
elog(DEBUG4, "lo_open(%u,%d)", lobjId, mode);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
#endif
|
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
|
if (fscxt == NULL)
|
2000-06-28 05:33:33 +02:00
|
|
|
|
fscxt = AllocSetContextCreate(TopMemoryContext,
|
|
|
|
|
"Filesystem",
|
|
|
|
|
ALLOCSET_DEFAULT_MINSIZE,
|
|
|
|
|
ALLOCSET_DEFAULT_INITSIZE,
|
|
|
|
|
ALLOCSET_DEFAULT_MAXSIZE);
|
|
|
|
|
|
|
|
|
|
currentContext = MemoryContextSwitchTo(fscxt);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
|
lobjDesc = inv_open(lobjId, mode);
|
|
|
|
|
|
|
|
|
|
if (lobjDesc == NULL)
|
|
|
|
|
{ /* lookup failed */
|
|
|
|
|
MemoryContextSwitchTo(currentContext);
|
|
|
|
|
#if FSDB
|
2003-07-28 02:09:16 +02:00
|
|
|
|
elog(DEBUG4, "could not open large object %u", lobjId);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
#endif
|
2000-06-09 03:11:16 +02:00
|
|
|
|
PG_RETURN_INT32(-1);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fd = newLOfd(lobjDesc);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
|
MemoryContextSwitchTo(currentContext);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
|
PG_RETURN_INT32(fd);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
}
|
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
|
Datum
|
|
|
|
|
lo_close(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
|
int32 fd = PG_GETARG_INT32(0);
|
1997-09-08 04:41:22 +02:00
|
|
|
|
MemoryContext currentContext;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
2000-10-24 05:14:08 +02:00
|
|
|
|
if (fd < 0 || fd >= cookies_size || cookies[fd] == NULL)
|
1997-09-07 07:04:48 +02:00
|
|
|
|
{
|
2003-07-22 21:00:12 +02:00
|
|
|
|
ereport(ERROR,
|
|
|
|
|
(errcode(ERRCODE_UNDEFINED_OBJECT),
|
|
|
|
|
errmsg("invalid large-object descriptor: %d", fd)));
|
2000-10-24 05:14:08 +02:00
|
|
|
|
PG_RETURN_INT32(-1);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
#if FSDB
|
2003-05-27 19:49:47 +02:00
|
|
|
|
elog(DEBUG4, "lo_close(%d)", fd);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
#endif
|
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
|
Assert(fscxt != NULL);
|
2000-06-28 05:33:33 +02:00
|
|
|
|
currentContext = MemoryContextSwitchTo(fscxt);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
|
inv_close(cookies[fd]);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
|
deleteLOfd(fd);
|
2000-06-09 03:11:16 +02:00
|
|
|
|
|
2000-10-24 03:38:44 +02:00
|
|
|
|
MemoryContextSwitchTo(currentContext);
|
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
|
PG_RETURN_INT32(0);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
}
|
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
|
* Bare Read/Write operations --- these are not fmgr-callable!
|
|
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
|
* We assume the large object supports byte oriented reads and seeks so
|
|
|
|
|
* that our work is easier.
|
2000-06-09 03:11:16 +02:00
|
|
|
|
*
|
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
|
int
|
|
|
|
|
lo_read(int fd, char *buf, int len)
|
|
|
|
|
{
|
1999-05-25 18:15:34 +02:00
|
|
|
|
MemoryContext currentContext;
|
|
|
|
|
int status;
|
1999-05-09 17:00:18 +02:00
|
|
|
|
|
2000-10-24 05:14:08 +02:00
|
|
|
|
if (fd < 0 || fd >= cookies_size || cookies[fd] == NULL)
|
1999-05-09 02:54:30 +02:00
|
|
|
|
{
|
2003-07-22 21:00:12 +02:00
|
|
|
|
ereport(ERROR,
|
|
|
|
|
(errcode(ERRCODE_UNDEFINED_OBJECT),
|
|
|
|
|
errmsg("invalid large-object descriptor: %d", fd)));
|
2000-10-24 05:14:08 +02:00
|
|
|
|
return -1;
|
1999-05-09 02:54:30 +02:00
|
|
|
|
}
|
1999-06-01 00:53:59 +02:00
|
|
|
|
|
|
|
|
|
Assert(fscxt != NULL);
|
2000-06-28 05:33:33 +02:00
|
|
|
|
currentContext = MemoryContextSwitchTo(fscxt);
|
1999-05-09 17:00:18 +02:00
|
|
|
|
|
|
|
|
|
status = inv_read(cookies[fd], buf, len);
|
1999-05-09 02:54:30 +02:00
|
|
|
|
|
1999-05-09 17:00:18 +02:00
|
|
|
|
MemoryContextSwitchTo(currentContext);
|
2000-06-09 03:11:16 +02:00
|
|
|
|
|
|
|
|
|
return status;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
lo_write(int fd, char *buf, int len)
|
|
|
|
|
{
|
1999-05-25 18:15:34 +02:00
|
|
|
|
MemoryContext currentContext;
|
|
|
|
|
int status;
|
1999-05-09 17:00:18 +02:00
|
|
|
|
|
2000-10-24 05:14:08 +02:00
|
|
|
|
if (fd < 0 || fd >= cookies_size || cookies[fd] == NULL)
|
1999-05-09 02:54:30 +02:00
|
|
|
|
{
|
2003-07-22 21:00:12 +02:00
|
|
|
|
ereport(ERROR,
|
|
|
|
|
(errcode(ERRCODE_UNDEFINED_OBJECT),
|
|
|
|
|
errmsg("invalid large-object descriptor: %d", fd)));
|
2000-10-24 05:14:08 +02:00
|
|
|
|
return -1;
|
1999-05-09 02:54:30 +02:00
|
|
|
|
}
|
1999-06-01 00:53:59 +02:00
|
|
|
|
|
|
|
|
|
Assert(fscxt != NULL);
|
2000-06-28 05:33:33 +02:00
|
|
|
|
currentContext = MemoryContextSwitchTo(fscxt);
|
1999-05-09 17:00:18 +02:00
|
|
|
|
|
|
|
|
|
status = inv_write(cookies[fd], buf, len);
|
1999-05-09 02:54:30 +02:00
|
|
|
|
|
1999-05-09 17:00:18 +02:00
|
|
|
|
MemoryContextSwitchTo(currentContext);
|
2000-06-09 03:11:16 +02:00
|
|
|
|
|
|
|
|
|
return status;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
|
Datum
|
|
|
|
|
lo_lseek(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
|
int32 fd = PG_GETARG_INT32(0);
|
|
|
|
|
int32 offset = PG_GETARG_INT32(1);
|
|
|
|
|
int32 whence = PG_GETARG_INT32(2);
|
1997-09-08 04:41:22 +02:00
|
|
|
|
MemoryContext currentContext;
|
1999-06-01 00:53:59 +02:00
|
|
|
|
int status;
|
1997-03-18 22:30:41 +01:00
|
|
|
|
|
2000-10-24 05:14:08 +02:00
|
|
|
|
if (fd < 0 || fd >= cookies_size || cookies[fd] == NULL)
|
1999-05-09 02:54:30 +02:00
|
|
|
|
{
|
2003-07-22 21:00:12 +02:00
|
|
|
|
ereport(ERROR,
|
|
|
|
|
(errcode(ERRCODE_UNDEFINED_OBJECT),
|
|
|
|
|
errmsg("invalid large-object descriptor: %d", fd)));
|
2000-10-24 05:14:08 +02:00
|
|
|
|
PG_RETURN_INT32(-1);
|
1999-05-09 02:54:30 +02:00
|
|
|
|
}
|
1997-03-18 22:30:41 +01:00
|
|
|
|
|
1999-06-01 00:53:59 +02:00
|
|
|
|
Assert(fscxt != NULL);
|
2000-06-28 05:33:33 +02:00
|
|
|
|
currentContext = MemoryContextSwitchTo(fscxt);
|
1997-03-18 22:30:41 +01:00
|
|
|
|
|
1999-06-01 00:53:59 +02:00
|
|
|
|
status = inv_seek(cookies[fd], offset, whence);
|
1997-03-18 22:30:41 +01:00
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
|
MemoryContextSwitchTo(currentContext);
|
1997-03-18 22:30:41 +01:00
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
|
PG_RETURN_INT32(status);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
}
|
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
|
Datum
|
|
|
|
|
lo_creat(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
|
int32 mode = PG_GETARG_INT32(0);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
LargeObjectDesc *lobjDesc;
|
1997-09-08 04:41:22 +02:00
|
|
|
|
MemoryContext currentContext;
|
|
|
|
|
Oid lobjId;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
|
|
if (fscxt == NULL)
|
2000-06-28 05:33:33 +02:00
|
|
|
|
fscxt = AllocSetContextCreate(TopMemoryContext,
|
|
|
|
|
"Filesystem",
|
|
|
|
|
ALLOCSET_DEFAULT_MINSIZE,
|
|
|
|
|
ALLOCSET_DEFAULT_INITSIZE,
|
|
|
|
|
ALLOCSET_DEFAULT_MAXSIZE);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
2000-06-28 05:33:33 +02:00
|
|
|
|
currentContext = MemoryContextSwitchTo(fscxt);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
|
|
lobjDesc = inv_create(mode);
|
|
|
|
|
|
|
|
|
|
if (lobjDesc == NULL)
|
|
|
|
|
{
|
|
|
|
|
MemoryContextSwitchTo(currentContext);
|
2000-06-09 03:11:16 +02:00
|
|
|
|
PG_RETURN_OID(InvalidOid);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2000-10-24 03:38:44 +02:00
|
|
|
|
lobjId = lobjDesc->id;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
|
|
inv_close(lobjDesc);
|
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
|
MemoryContextSwitchTo(currentContext);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
|
PG_RETURN_OID(lobjId);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
}
|
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
|
Datum
|
|
|
|
|
lo_tell(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
|
int32 fd = PG_GETARG_INT32(0);
|
|
|
|
|
|
2000-10-24 05:14:08 +02:00
|
|
|
|
if (fd < 0 || fd >= cookies_size || cookies[fd] == NULL)
|
1997-09-07 07:04:48 +02:00
|
|
|
|
{
|
2003-07-22 21:00:12 +02:00
|
|
|
|
ereport(ERROR,
|
|
|
|
|
(errcode(ERRCODE_UNDEFINED_OBJECT),
|
|
|
|
|
errmsg("invalid large-object descriptor: %d", fd)));
|
2000-10-24 05:14:08 +02:00
|
|
|
|
PG_RETURN_INT32(-1);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
}
|
1999-06-01 00:53:59 +02:00
|
|
|
|
|
|
|
|
|
/*
|
2000-04-12 19:17:23 +02:00
|
|
|
|
* We assume we do not need to switch contexts for inv_tell. That is
|
|
|
|
|
* true for now, but is probably more than this module ought to
|
|
|
|
|
* assume...
|
1999-06-01 00:53:59 +02:00
|
|
|
|
*/
|
2000-06-09 03:11:16 +02:00
|
|
|
|
PG_RETURN_INT32(inv_tell(cookies[fd]));
|
1996-07-09 08:22:35 +02:00
|
|
|
|
}
|
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
|
Datum
|
|
|
|
|
lo_unlink(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
|
Oid lobjId = PG_GETARG_OID(0);
|
2000-04-12 19:17:23 +02:00
|
|
|
|
|
2000-10-24 05:14:08 +02:00
|
|
|
|
/*
|
|
|
|
|
* If there are any open LO FDs referencing that ID, close 'em.
|
|
|
|
|
*/
|
|
|
|
|
if (fscxt != NULL)
|
|
|
|
|
{
|
|
|
|
|
MemoryContext currentContext;
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
currentContext = MemoryContextSwitchTo(fscxt);
|
|
|
|
|
for (i = 0; i < cookies_size; i++)
|
|
|
|
|
{
|
|
|
|
|
if (cookies[i] != NULL && cookies[i]->id == lobjId)
|
|
|
|
|
{
|
|
|
|
|
inv_close(cookies[i]);
|
|
|
|
|
deleteLOfd(i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
MemoryContextSwitchTo(currentContext);
|
|
|
|
|
}
|
|
|
|
|
|
1999-06-01 00:53:59 +02:00
|
|
|
|
/*
|
2000-04-12 19:17:23 +02:00
|
|
|
|
* inv_drop does not need a context switch, indeed it doesn't touch
|
|
|
|
|
* any LO-specific data structures at all. (Again, that's probably
|
|
|
|
|
* more than this module ought to be assuming.)
|
1999-06-01 00:53:59 +02:00
|
|
|
|
*/
|
2000-06-09 03:11:16 +02:00
|
|
|
|
PG_RETURN_INT32(inv_drop(lobjId));
|
1996-07-09 08:22:35 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************
|
2000-06-09 03:11:16 +02:00
|
|
|
|
* Read/Write using bytea
|
1996-07-09 08:22:35 +02:00
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
|
Datum
|
|
|
|
|
loread(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
|
int32 fd = PG_GETARG_INT32(0);
|
|
|
|
|
int32 len = PG_GETARG_INT32(1);
|
2002-08-25 19:20:01 +02:00
|
|
|
|
bytea *retval;
|
2000-06-09 03:11:16 +02:00
|
|
|
|
int totalread;
|
|
|
|
|
|
|
|
|
|
if (len < 0)
|
|
|
|
|
len = 0;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
2002-08-25 19:20:01 +02:00
|
|
|
|
retval = (bytea *) palloc(VARHDRSZ + len);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
totalread = lo_read(fd, VARDATA(retval), len);
|
2000-07-04 01:10:14 +02:00
|
|
|
|
VARATT_SIZEP(retval) = totalread + VARHDRSZ;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
2002-08-25 19:20:01 +02:00
|
|
|
|
PG_RETURN_BYTEA_P(retval);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
}
|
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
|
Datum
|
|
|
|
|
lowrite(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
|
int32 fd = PG_GETARG_INT32(0);
|
2002-08-25 19:20:01 +02:00
|
|
|
|
bytea *wbuf = PG_GETARG_BYTEA_P(1);
|
2001-03-22 05:01:46 +01:00
|
|
|
|
int bytestowrite;
|
|
|
|
|
int totalwritten;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
1997-12-08 05:42:48 +01:00
|
|
|
|
bytestowrite = VARSIZE(wbuf) - VARHDRSZ;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
totalwritten = lo_write(fd, VARDATA(wbuf), bytestowrite);
|
2000-06-09 03:11:16 +02:00
|
|
|
|
PG_RETURN_INT32(totalwritten);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************
|
1997-09-07 07:04:48 +02:00
|
|
|
|
* Import/Export of Large Object
|
1996-07-09 08:22:35 +02:00
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* lo_import -
|
1997-09-07 07:04:48 +02:00
|
|
|
|
* imports a file as an (inversion) large object.
|
1996-07-09 08:22:35 +02:00
|
|
|
|
*/
|
2000-06-09 03:11:16 +02:00
|
|
|
|
Datum
|
|
|
|
|
lo_import(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
|
text *filename = PG_GETARG_TEXT_P(0);
|
1999-05-09 02:54:30 +02:00
|
|
|
|
File fd;
|
1997-09-08 04:41:22 +02:00
|
|
|
|
int nbytes,
|
|
|
|
|
tmp;
|
|
|
|
|
char buf[BUFSIZE];
|
2000-10-24 03:38:44 +02:00
|
|
|
|
char fnamebuf[MAXPGPATH];
|
1997-09-07 07:04:48 +02:00
|
|
|
|
LargeObjectDesc *lobj;
|
1997-09-08 04:41:22 +02:00
|
|
|
|
Oid lobjOid;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
1999-06-04 23:13:38 +02:00
|
|
|
|
#ifndef ALLOW_DANGEROUS_LO_FUNCTIONS
|
1999-06-01 00:53:59 +02:00
|
|
|
|
if (!superuser())
|
2003-07-22 21:00:12 +02:00
|
|
|
|
ereport(ERROR,
|
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
|
|
|
|
errmsg("must be superuser to use server-side lo_import()"),
|
|
|
|
|
errhint("Anyone can use the client-side lo_import() provided by libpq.")));
|
1999-06-04 23:13:38 +02:00
|
|
|
|
#endif
|
1999-06-01 00:53:59 +02:00
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
|
/*
|
|
|
|
|
* open the file to be read in
|
|
|
|
|
*/
|
2000-07-07 23:12:53 +02:00
|
|
|
|
nbytes = VARSIZE(filename) - VARHDRSZ;
|
2000-10-24 03:38:44 +02:00
|
|
|
|
if (nbytes >= MAXPGPATH)
|
2001-03-22 05:01:46 +01:00
|
|
|
|
nbytes = MAXPGPATH - 1;
|
2000-07-07 23:12:53 +02:00
|
|
|
|
memcpy(fnamebuf, VARDATA(filename), nbytes);
|
|
|
|
|
fnamebuf[nbytes] = '\0';
|
2000-06-02 17:57:44 +02:00
|
|
|
|
fd = PathNameOpenFile(fnamebuf, O_RDONLY | PG_BINARY, 0666);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
if (fd < 0)
|
2003-07-22 21:00:12 +02:00
|
|
|
|
ereport(ERROR,
|
|
|
|
|
(errcode_for_file_access(),
|
|
|
|
|
errmsg("could not open server file \"%s\": %m",
|
|
|
|
|
fnamebuf)));
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
|
/*
|
2003-07-22 21:00:12 +02:00
|
|
|
|
* create an inversion object
|
1997-09-07 07:04:48 +02:00
|
|
|
|
*/
|
|
|
|
|
lobj = inv_create(INV_READ | INV_WRITE);
|
2000-10-24 03:38:44 +02:00
|
|
|
|
lobjOid = lobj->id;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* read in from the Unix file and write to the inversion file
|
|
|
|
|
*/
|
1999-05-09 02:54:30 +02:00
|
|
|
|
while ((nbytes = FileRead(fd, buf, BUFSIZE)) > 0)
|
1997-09-07 07:04:48 +02:00
|
|
|
|
{
|
|
|
|
|
tmp = inv_write(lobj, buf, nbytes);
|
2003-07-22 21:00:12 +02:00
|
|
|
|
Assert(tmp == nbytes);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2003-07-22 21:00:12 +02:00
|
|
|
|
if (nbytes < 0)
|
|
|
|
|
ereport(ERROR,
|
|
|
|
|
(errcode_for_file_access(),
|
|
|
|
|
errmsg("could not read server file \"%s\": %m",
|
|
|
|
|
fnamebuf)));
|
|
|
|
|
|
1999-05-09 02:54:30 +02:00
|
|
|
|
FileClose(fd);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
inv_close(lobj);
|
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
|
PG_RETURN_OID(lobjOid);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* lo_export -
|
1997-09-07 07:04:48 +02:00
|
|
|
|
* exports an (inversion) large object.
|
1996-07-09 08:22:35 +02:00
|
|
|
|
*/
|
2000-06-09 03:11:16 +02:00
|
|
|
|
Datum
|
|
|
|
|
lo_export(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
|
{
|
2000-06-09 03:11:16 +02:00
|
|
|
|
Oid lobjId = PG_GETARG_OID(0);
|
|
|
|
|
text *filename = PG_GETARG_TEXT_P(1);
|
1999-05-09 02:54:30 +02:00
|
|
|
|
File fd;
|
1997-09-08 04:41:22 +02:00
|
|
|
|
int nbytes,
|
|
|
|
|
tmp;
|
|
|
|
|
char buf[BUFSIZE];
|
2000-10-24 03:38:44 +02:00
|
|
|
|
char fnamebuf[MAXPGPATH];
|
1997-09-07 07:04:48 +02:00
|
|
|
|
LargeObjectDesc *lobj;
|
1997-09-08 04:41:22 +02:00
|
|
|
|
mode_t oumask;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
1999-06-04 23:13:38 +02:00
|
|
|
|
#ifndef ALLOW_DANGEROUS_LO_FUNCTIONS
|
1999-06-01 00:53:59 +02:00
|
|
|
|
if (!superuser())
|
2003-07-22 21:00:12 +02:00
|
|
|
|
ereport(ERROR,
|
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
|
|
|
|
errmsg("must be superuser to use server-side lo_export()"),
|
|
|
|
|
errhint("Anyone can use the client-side lo_export() provided by libpq.")));
|
1999-06-04 23:13:38 +02:00
|
|
|
|
#endif
|
1999-06-01 00:53:59 +02:00
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
|
/*
|
2003-07-22 21:00:12 +02:00
|
|
|
|
* open the inversion object (no need to test for failure)
|
1997-09-07 07:04:48 +02:00
|
|
|
|
*/
|
|
|
|
|
lobj = inv_open(lobjId, INV_READ);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* open the file to be written to
|
1999-06-01 00:53:59 +02:00
|
|
|
|
*
|
2000-04-12 19:17:23 +02:00
|
|
|
|
* Note: we reduce backend's normal 077 umask to the slightly friendlier
|
|
|
|
|
* 022. This code used to drop it all the way to 0, but creating
|
|
|
|
|
* world-writable export files doesn't seem wise.
|
1997-09-07 07:04:48 +02:00
|
|
|
|
*/
|
2000-07-07 23:12:53 +02:00
|
|
|
|
nbytes = VARSIZE(filename) - VARHDRSZ;
|
2000-10-24 03:38:44 +02:00
|
|
|
|
if (nbytes >= MAXPGPATH)
|
2001-03-22 05:01:46 +01:00
|
|
|
|
nbytes = MAXPGPATH - 1;
|
2000-07-07 23:12:53 +02:00
|
|
|
|
memcpy(fnamebuf, VARDATA(filename), nbytes);
|
|
|
|
|
fnamebuf[nbytes] = '\0';
|
1999-06-01 00:53:59 +02:00
|
|
|
|
oumask = umask((mode_t) 0022);
|
2000-06-02 17:57:44 +02:00
|
|
|
|
fd = PathNameOpenFile(fnamebuf, O_CREAT | O_WRONLY | O_TRUNC | PG_BINARY, 0666);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
umask(oumask);
|
|
|
|
|
if (fd < 0)
|
2003-07-22 21:00:12 +02:00
|
|
|
|
ereport(ERROR,
|
|
|
|
|
(errcode_for_file_access(),
|
|
|
|
|
errmsg("could not create server file \"%s\": %m",
|
|
|
|
|
fnamebuf)));
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
|
/*
|
2000-10-24 03:38:44 +02:00
|
|
|
|
* read in from the inversion file and write to the Unix file
|
1997-09-07 07:04:48 +02:00
|
|
|
|
*/
|
|
|
|
|
while ((nbytes = inv_read(lobj, buf, BUFSIZE)) > 0)
|
|
|
|
|
{
|
1999-05-09 02:54:30 +02:00
|
|
|
|
tmp = FileWrite(fd, buf, nbytes);
|
2000-10-24 03:38:44 +02:00
|
|
|
|
if (tmp != nbytes)
|
2003-07-22 21:00:12 +02:00
|
|
|
|
ereport(ERROR,
|
|
|
|
|
(errcode_for_file_access(),
|
|
|
|
|
errmsg("could not write server file \"%s\": %m",
|
|
|
|
|
fnamebuf)));
|
1997-09-07 07:04:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
1999-05-09 02:54:30 +02:00
|
|
|
|
FileClose(fd);
|
2003-07-22 21:00:12 +02:00
|
|
|
|
inv_close(lobj);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
2000-06-09 03:11:16 +02:00
|
|
|
|
PG_RETURN_INT32(1);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
}
|
|
|
|
|
|
1998-07-21 06:17:30 +02:00
|
|
|
|
/*
|
|
|
|
|
* lo_commit -
|
1998-09-01 06:40:42 +02:00
|
|
|
|
* prepares large objects for transaction commit [PA, 7/17/98]
|
1998-07-21 06:17:30 +02:00
|
|
|
|
*/
|
1998-09-01 06:40:42 +02:00
|
|
|
|
void
|
1999-06-01 00:53:59 +02:00
|
|
|
|
lo_commit(bool isCommit)
|
1998-07-21 06:17:30 +02:00
|
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
|
int i;
|
1998-07-21 06:17:30 +02:00
|
|
|
|
MemoryContext currentContext;
|
|
|
|
|
|
1998-07-22 07:48:59 +02:00
|
|
|
|
if (fscxt == NULL)
|
1999-06-01 00:53:59 +02:00
|
|
|
|
return; /* no LO operations in this xact */
|
1998-09-01 06:40:42 +02:00
|
|
|
|
|
2000-06-28 05:33:33 +02:00
|
|
|
|
currentContext = MemoryContextSwitchTo(fscxt);
|
1998-07-21 06:17:30 +02:00
|
|
|
|
|
2000-04-12 19:17:23 +02:00
|
|
|
|
/*
|
|
|
|
|
* Clean out still-open index scans (not necessary if aborting) and
|
|
|
|
|
* clear cookies array so that LO fds are no longer good.
|
1999-06-01 00:53:59 +02:00
|
|
|
|
*/
|
2000-10-24 05:14:08 +02:00
|
|
|
|
for (i = 0; i < cookies_size; i++)
|
1998-07-22 07:48:59 +02:00
|
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
|
if (cookies[i] != NULL)
|
1999-06-01 00:53:59 +02:00
|
|
|
|
{
|
|
|
|
|
if (isCommit)
|
2000-10-24 03:38:44 +02:00
|
|
|
|
inv_close(cookies[i]);
|
1999-06-01 00:53:59 +02:00
|
|
|
|
cookies[i] = NULL;
|
|
|
|
|
}
|
1998-07-22 07:48:59 +02:00
|
|
|
|
}
|
1998-07-21 06:17:30 +02:00
|
|
|
|
|
2000-10-24 05:14:08 +02:00
|
|
|
|
/* Needn't actually pfree since we're about to zap context */
|
|
|
|
|
cookies = NULL;
|
|
|
|
|
cookies_size = 0;
|
|
|
|
|
|
1998-07-21 06:17:30 +02:00
|
|
|
|
MemoryContextSwitchTo(currentContext);
|
|
|
|
|
|
1999-06-01 00:53:59 +02:00
|
|
|
|
/* Release the LO memory context to prevent permanent memory leaks. */
|
2000-06-28 05:33:33 +02:00
|
|
|
|
MemoryContextDelete(fscxt);
|
1999-06-01 00:53:59 +02:00
|
|
|
|
fscxt = NULL;
|
1998-07-21 06:17:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
|
|
/*****************************************************************************
|
1997-09-07 07:04:48 +02:00
|
|
|
|
* Support routines for this file
|
1996-07-09 08:22:35 +02:00
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
static int
|
1997-09-08 23:56:23 +02:00
|
|
|
|
newLOfd(LargeObjectDesc *lobjCookie)
|
1996-07-09 08:22:35 +02:00
|
|
|
|
{
|
2000-10-24 05:14:08 +02:00
|
|
|
|
int i,
|
|
|
|
|
newsize;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
2000-10-24 05:14:08 +02:00
|
|
|
|
/* Try to find a free slot */
|
|
|
|
|
for (i = 0; i < cookies_size; i++)
|
1997-09-07 07:04:48 +02:00
|
|
|
|
{
|
|
|
|
|
if (cookies[i] == NULL)
|
|
|
|
|
{
|
|
|
|
|
cookies[i] = lobjCookie;
|
|
|
|
|
return i;
|
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
}
|
2000-10-24 05:14:08 +02:00
|
|
|
|
|
|
|
|
|
/* No free slot, so make the array bigger */
|
|
|
|
|
if (cookies_size <= 0)
|
|
|
|
|
{
|
|
|
|
|
/* First time through, arbitrarily make 64-element array */
|
|
|
|
|
i = 0;
|
|
|
|
|
newsize = 64;
|
|
|
|
|
cookies = (LargeObjectDesc **)
|
|
|
|
|
palloc(newsize * sizeof(LargeObjectDesc *));
|
|
|
|
|
MemSet(cookies, 0, newsize * sizeof(LargeObjectDesc *));
|
|
|
|
|
cookies_size = newsize;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/* Double size of array */
|
|
|
|
|
i = cookies_size;
|
|
|
|
|
newsize = cookies_size * 2;
|
|
|
|
|
cookies = (LargeObjectDesc **)
|
|
|
|
|
repalloc(cookies, newsize * sizeof(LargeObjectDesc *));
|
|
|
|
|
MemSet(cookies + cookies_size, 0,
|
|
|
|
|
(newsize - cookies_size) * sizeof(LargeObjectDesc *));
|
|
|
|
|
cookies_size = newsize;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Assert(cookies[i] == NULL);
|
|
|
|
|
cookies[i] = lobjCookie;
|
|
|
|
|
return i;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
}
|
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
|
static void
|
1996-07-09 08:22:35 +02:00
|
|
|
|
deleteLOfd(int fd)
|
|
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
|
cookies[fd] = NULL;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
}
|