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
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 1994, Regents of the University of California
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* IDENTIFICATION
|
1999-07-17 22:18:55 +02:00
|
|
|
|
* $Header: /cvsroot/pgsql/src/backend/libpq/be-fsstubs.c,v 1.41 1999/07/17 20:17:01 momjian 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
|
|
|
|
*
|
1999-06-01 00:53:59 +02:00
|
|
|
|
* These functions operate in a private GlobalMemoryContext, which means
|
|
|
|
|
* that large object descriptors hang around until we destroy the context.
|
|
|
|
|
* That happens in lo_commit(). It'd be possible to prolong the lifetime
|
|
|
|
|
* 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
|
|
|
|
|
* relations at all. For now, we'll stick with the existing documented
|
|
|
|
|
* semantics of LO FDs: they're only good within a transaction.
|
|
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
|
*/
|
|
|
|
|
|
1996-11-06 09:48:33 +01:00
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <sys/types.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 "postgres.h"
|
1996-11-06 09:48:33 +01:00
|
|
|
|
|
1999-07-16 05:14:30 +02:00
|
|
|
|
#include "catalog/pg_shadow.h"
|
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"
|
|
|
|
|
#include "storage/large_object.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*/
|
1999-05-03 21:10:48 +02:00
|
|
|
|
#define MAX_LOBJ_FDS 256
|
|
|
|
|
#define BUFSIZE 1024
|
|
|
|
|
#define FNAME_BUFSIZE 8192
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
|
static LargeObjectDesc *cookies[MAX_LOBJ_FDS];
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
|
|
static GlobalMemory fscxt = NULL;
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
lo_open(Oid lobjId, int mode)
|
|
|
|
|
{
|
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
|
1999-05-10 02:46:32 +02:00
|
|
|
|
elog(NOTICE, "LOopen(%u,%d)", lobjId, mode);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
#endif
|
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
|
if (fscxt == NULL)
|
|
|
|
|
fscxt = CreateGlobalMemory("Filesystem");
|
|
|
|
|
currentContext = MemoryContextSwitchTo((MemoryContext) 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
|
1999-05-10 02:46:32 +02:00
|
|
|
|
elog(NOTICE, "cannot open large object %u", lobjId);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
#endif
|
1997-09-07 07:04:48 +02:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fd = newLOfd(lobjDesc);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
|
/* switch context back to orig. */
|
|
|
|
|
MemoryContextSwitchTo(currentContext);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
1999-06-01 00:53:59 +02:00
|
|
|
|
#if FSDB
|
|
|
|
|
if (fd < 0) /* newLOfd couldn't find a slot */
|
|
|
|
|
elog(NOTICE, "Out of space for large object FDs");
|
|
|
|
|
#endif
|
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
|
return fd;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
lo_close(int fd)
|
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
|
MemoryContext currentContext;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
1999-05-09 02:54:30 +02:00
|
|
|
|
if (fd < 0 || fd >= MAX_LOBJ_FDS)
|
1997-09-07 07:04:48 +02:00
|
|
|
|
{
|
1998-01-07 22:07:04 +01:00
|
|
|
|
elog(ERROR, "lo_close: large obj descriptor (%d) out of range", fd);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
return -2;
|
|
|
|
|
}
|
|
|
|
|
if (cookies[fd] == NULL)
|
|
|
|
|
{
|
1998-01-07 22:07:04 +01:00
|
|
|
|
elog(ERROR, "lo_close: invalid large obj descriptor (%d)", fd);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
return -3;
|
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
#if FSDB
|
1997-09-07 07:04:48 +02:00
|
|
|
|
elog(NOTICE, "LOclose(%d)", fd);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
#endif
|
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
|
Assert(fscxt != NULL);
|
|
|
|
|
currentContext = MemoryContextSwitchTo((MemoryContext) 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
|
|
|
|
MemoryContextSwitchTo(currentContext);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
|
deleteLOfd(fd);
|
|
|
|
|
return 0;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
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.
|
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
|
|
|
|
|
1999-05-09 02:54:30 +02:00
|
|
|
|
if (fd < 0 || fd >= MAX_LOBJ_FDS)
|
|
|
|
|
{
|
|
|
|
|
elog(ERROR, "lo_read: large obj descriptor (%d) out of range", fd);
|
|
|
|
|
return -2;
|
|
|
|
|
}
|
|
|
|
|
if (cookies[fd] == NULL)
|
|
|
|
|
{
|
|
|
|
|
elog(ERROR, "lo_read: invalid large obj descriptor (%d)", fd);
|
|
|
|
|
return -3;
|
|
|
|
|
}
|
1999-06-01 00:53:59 +02:00
|
|
|
|
|
|
|
|
|
Assert(fscxt != NULL);
|
1999-05-25 18:15:34 +02:00
|
|
|
|
currentContext = MemoryContextSwitchTo((MemoryContext) 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);
|
1999-05-25 18:15:34 +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
|
|
|
|
|
1999-05-09 02:54:30 +02:00
|
|
|
|
if (fd < 0 || fd >= MAX_LOBJ_FDS)
|
|
|
|
|
{
|
|
|
|
|
elog(ERROR, "lo_write: large obj descriptor (%d) out of range", fd);
|
|
|
|
|
return -2;
|
|
|
|
|
}
|
|
|
|
|
if (cookies[fd] == NULL)
|
|
|
|
|
{
|
|
|
|
|
elog(ERROR, "lo_write: invalid large obj descriptor (%d)", fd);
|
|
|
|
|
return -3;
|
|
|
|
|
}
|
1999-06-01 00:53:59 +02:00
|
|
|
|
|
|
|
|
|
Assert(fscxt != NULL);
|
1999-05-25 18:15:34 +02:00
|
|
|
|
currentContext = MemoryContextSwitchTo((MemoryContext) 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);
|
1999-05-25 18:15:34 +02:00
|
|
|
|
return (status);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
lo_lseek(int fd, int offset, int whence)
|
|
|
|
|
{
|
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
|
|
|
|
|
1999-05-09 02:54:30 +02:00
|
|
|
|
if (fd < 0 || fd >= MAX_LOBJ_FDS)
|
1997-09-07 07:04:48 +02:00
|
|
|
|
{
|
1999-05-09 02:54:30 +02:00
|
|
|
|
elog(ERROR, "lo_lseek: large obj descriptor (%d) out of range", fd);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
return -2;
|
|
|
|
|
}
|
1999-05-09 02:54:30 +02:00
|
|
|
|
if (cookies[fd] == NULL)
|
|
|
|
|
{
|
|
|
|
|
elog(ERROR, "lo_lseek: invalid large obj descriptor (%d)", fd);
|
|
|
|
|
return -3;
|
|
|
|
|
}
|
1997-03-18 22:30:41 +01:00
|
|
|
|
|
1999-06-01 00:53:59 +02:00
|
|
|
|
Assert(fscxt != NULL);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
currentContext = MemoryContextSwitchTo((MemoryContext) 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
|
|
|
|
|
1999-06-01 00:53:59 +02:00
|
|
|
|
return status;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Oid
|
|
|
|
|
lo_creat(int mode)
|
|
|
|
|
{
|
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)
|
|
|
|
|
fscxt = CreateGlobalMemory("Filesystem");
|
|
|
|
|
|
|
|
|
|
currentContext = MemoryContextSwitchTo((MemoryContext) fscxt);
|
|
|
|
|
|
|
|
|
|
lobjDesc = inv_create(mode);
|
|
|
|
|
|
|
|
|
|
if (lobjDesc == NULL)
|
|
|
|
|
{
|
|
|
|
|
MemoryContextSwitchTo(currentContext);
|
|
|
|
|
return InvalidOid;
|
|
|
|
|
}
|
|
|
|
|
|
1998-08-19 04:04:17 +02:00
|
|
|
|
lobjId = RelationGetRelid(lobjDesc->heap_r);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
|
|
inv_close(lobjDesc);
|
|
|
|
|
|
|
|
|
|
/* switch context back to original memory context */
|
1996-07-09 08:22:35 +02:00
|
|
|
|
MemoryContextSwitchTo(currentContext);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
|
|
return lobjId;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
lo_tell(int fd)
|
|
|
|
|
{
|
1999-05-09 02:54:30 +02:00
|
|
|
|
if (fd < 0 || fd >= MAX_LOBJ_FDS)
|
1997-09-07 07:04:48 +02:00
|
|
|
|
{
|
1998-01-07 22:07:04 +01:00
|
|
|
|
elog(ERROR, "lo_tell: large object descriptor (%d) out of range", fd);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
return -2;
|
|
|
|
|
}
|
|
|
|
|
if (cookies[fd] == NULL)
|
|
|
|
|
{
|
1998-01-07 22:07:04 +01:00
|
|
|
|
elog(ERROR, "lo_tell: invalid large object descriptor (%d)", fd);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
return -3;
|
|
|
|
|
}
|
1999-06-01 00:53:59 +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...
|
|
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
|
return inv_tell(cookies[fd]);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
lo_unlink(Oid lobjId)
|
|
|
|
|
{
|
1999-06-01 00:53:59 +02:00
|
|
|
|
/*
|
|
|
|
|
* inv_destroy 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.)
|
|
|
|
|
*
|
|
|
|
|
* XXX there ought to be some code to clean up any open LOs that
|
|
|
|
|
* reference the specified relation... as is, they remain "open".
|
|
|
|
|
*/
|
1998-09-01 05:29:17 +02:00
|
|
|
|
return inv_destroy(lobjId);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************
|
1997-09-07 07:04:48 +02:00
|
|
|
|
* Read/Write using varlena
|
1996-07-09 08:22:35 +02:00
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
struct varlena *
|
1997-05-06 09:16:21 +02:00
|
|
|
|
loread(int fd, int len)
|
1996-07-09 08:22:35 +02:00
|
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
|
struct varlena *retval;
|
1997-09-08 04:41:22 +02:00
|
|
|
|
int totalread = 0;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
1997-12-08 05:42:48 +01:00
|
|
|
|
retval = (struct varlena *) palloc(VARHDRSZ + len);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
totalread = lo_read(fd, VARDATA(retval), len);
|
1997-12-08 05:42:48 +01:00
|
|
|
|
VARSIZE(retval) = totalread + VARHDRSZ;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
|
|
return retval;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
}
|
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
|
int
|
|
|
|
|
lowrite(int fd, struct varlena * wbuf)
|
1996-07-09 08:22:35 +02:00
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
|
int totalwritten;
|
|
|
|
|
int bytestowrite;
|
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);
|
|
|
|
|
return 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
|
|
|
|
*/
|
|
|
|
|
Oid
|
1997-09-08 23:56:23 +02:00
|
|
|
|
lo_import(text *filename)
|
1996-07-09 08:22:35 +02:00
|
|
|
|
{
|
1999-05-09 02:54:30 +02:00
|
|
|
|
File fd;
|
1997-09-08 04:41:22 +02:00
|
|
|
|
int nbytes,
|
|
|
|
|
tmp;
|
|
|
|
|
char buf[BUFSIZE];
|
1999-05-03 21:10:48 +02:00
|
|
|
|
char fnamebuf[FNAME_BUFSIZE];
|
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())
|
|
|
|
|
elog(ERROR, "You must have Postgres superuser privilege to use "
|
|
|
|
|
"server-side lo_import().\n\tAnyone 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
|
|
|
|
|
*/
|
1999-06-01 00:53:59 +02:00
|
|
|
|
nbytes = VARSIZE(filename) - VARHDRSZ + 1;
|
|
|
|
|
if (nbytes > FNAME_BUFSIZE)
|
|
|
|
|
nbytes = FNAME_BUFSIZE;
|
|
|
|
|
StrNCpy(fnamebuf, VARDATA(filename), nbytes);
|
1999-01-17 07:20:06 +01:00
|
|
|
|
#ifndef __CYGWIN32__
|
1999-05-09 02:54:30 +02:00
|
|
|
|
fd = PathNameOpenFile(fnamebuf, O_RDONLY, 0666);
|
1999-01-17 07:20:06 +01:00
|
|
|
|
#else
|
1999-05-09 02:54:30 +02:00
|
|
|
|
fd = PathNameOpenFile(fnamebuf, O_RDONLY | O_BINARY, 0666);
|
1999-01-17 07:20:06 +01:00
|
|
|
|
#endif
|
1997-09-07 07:04:48 +02:00
|
|
|
|
if (fd < 0)
|
|
|
|
|
{ /* error */
|
1999-06-01 00:53:59 +02:00
|
|
|
|
elog(ERROR, "lo_import: can't open unix file \"%s\": %m",
|
1997-09-07 07:04:48 +02:00
|
|
|
|
fnamebuf);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
}
|
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
|
/*
|
|
|
|
|
* create an inversion "object"
|
|
|
|
|
*/
|
|
|
|
|
lobj = inv_create(INV_READ | INV_WRITE);
|
|
|
|
|
if (lobj == NULL)
|
|
|
|
|
{
|
1998-01-07 22:07:04 +01:00
|
|
|
|
elog(ERROR, "lo_import: can't create inv object for \"%s\"",
|
1997-09-07 07:04:48 +02:00
|
|
|
|
fnamebuf);
|
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
|
/*
|
|
|
|
|
* the oid for the large object is just the oid of the relation
|
|
|
|
|
* XInv??? which contains the data.
|
|
|
|
|
*/
|
1998-08-19 04:04:17 +02:00
|
|
|
|
lobjOid = RelationGetRelid(lobj->heap_r);
|
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);
|
|
|
|
|
if (tmp < nbytes)
|
1998-01-07 22:07:04 +01:00
|
|
|
|
elog(ERROR, "lo_import: error while reading \"%s\"",
|
1997-09-07 07:04:48 +02:00
|
|
|
|
fnamebuf);
|
|
|
|
|
}
|
|
|
|
|
|
1999-05-09 02:54:30 +02:00
|
|
|
|
FileClose(fd);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
inv_close(lobj);
|
|
|
|
|
|
|
|
|
|
return 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
|
|
|
|
*/
|
|
|
|
|
int4
|
1997-09-08 23:56:23 +02:00
|
|
|
|
lo_export(Oid lobjId, text *filename)
|
1996-07-09 08:22:35 +02:00
|
|
|
|
{
|
1999-05-09 02:54:30 +02:00
|
|
|
|
File fd;
|
1997-09-08 04:41:22 +02:00
|
|
|
|
int nbytes,
|
|
|
|
|
tmp;
|
|
|
|
|
char buf[BUFSIZE];
|
1999-05-03 21:10:48 +02:00
|
|
|
|
char fnamebuf[FNAME_BUFSIZE];
|
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())
|
|
|
|
|
elog(ERROR, "You must have Postgres superuser privilege to use "
|
|
|
|
|
"server-side lo_export().\n\tAnyone 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
|
|
|
|
/*
|
1999-05-09 02:54:30 +02:00
|
|
|
|
* open the inversion "object"
|
1997-09-07 07:04:48 +02:00
|
|
|
|
*/
|
|
|
|
|
lobj = inv_open(lobjId, INV_READ);
|
|
|
|
|
if (lobj == NULL)
|
1999-05-10 02:46:32 +02:00
|
|
|
|
elog(ERROR, "lo_export: can't open inv object %u", lobjId);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* open the file to be written to
|
1999-06-01 00:53:59 +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
|
|
|
|
*/
|
1999-06-01 00:53:59 +02:00
|
|
|
|
nbytes = VARSIZE(filename) - VARHDRSZ + 1;
|
|
|
|
|
if (nbytes > FNAME_BUFSIZE)
|
|
|
|
|
nbytes = FNAME_BUFSIZE;
|
|
|
|
|
StrNCpy(fnamebuf, VARDATA(filename), nbytes);
|
|
|
|
|
oumask = umask((mode_t) 0022);
|
1999-01-17 07:20:06 +01:00
|
|
|
|
#ifndef __CYGWIN32__
|
1999-05-09 02:54:30 +02:00
|
|
|
|
fd = PathNameOpenFile(fnamebuf, O_CREAT | O_WRONLY | O_TRUNC, 0666);
|
1999-01-17 07:20:06 +01:00
|
|
|
|
#else
|
1999-05-09 02:54:30 +02:00
|
|
|
|
fd = PathNameOpenFile(fnamebuf, O_CREAT | O_WRONLY | O_TRUNC | O_BINARY, 0666);
|
1999-01-17 07:20:06 +01:00
|
|
|
|
#endif
|
1997-09-07 07:04:48 +02:00
|
|
|
|
umask(oumask);
|
|
|
|
|
if (fd < 0)
|
|
|
|
|
{ /* error */
|
1999-06-01 00:53:59 +02:00
|
|
|
|
elog(ERROR, "lo_export: can't open unix file \"%s\": %m",
|
1997-09-07 07:04:48 +02:00
|
|
|
|
fnamebuf);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
}
|
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
|
/*
|
|
|
|
|
* read in from the Unix file and write to the inversion file
|
|
|
|
|
*/
|
|
|
|
|
while ((nbytes = inv_read(lobj, buf, BUFSIZE)) > 0)
|
|
|
|
|
{
|
1999-05-09 02:54:30 +02:00
|
|
|
|
tmp = FileWrite(fd, buf, nbytes);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
if (tmp < nbytes)
|
1998-01-07 22:07:04 +01:00
|
|
|
|
elog(ERROR, "lo_export: error while writing \"%s\"",
|
1997-09-07 07:04:48 +02:00
|
|
|
|
fnamebuf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inv_close(lobj);
|
1999-05-09 02:54:30 +02:00
|
|
|
|
FileClose(fd);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
|
return 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
|
|
|
|
|
1998-07-21 06:17:30 +02:00
|
|
|
|
currentContext = MemoryContextSwitchTo((MemoryContext) fscxt);
|
|
|
|
|
|
1999-06-01 00:53:59 +02:00
|
|
|
|
/* Clean out still-open index scans (not necessary if aborting)
|
|
|
|
|
* and clear cookies array so that LO fds are no longer good.
|
|
|
|
|
*/
|
1998-07-22 07:48:59 +02:00
|
|
|
|
for (i = 0; i < MAX_LOBJ_FDS; i++)
|
|
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
|
if (cookies[i] != NULL)
|
1999-06-01 00:53:59 +02:00
|
|
|
|
{
|
|
|
|
|
if (isCommit)
|
|
|
|
|
inv_cleanindex(cookies[i]);
|
|
|
|
|
cookies[i] = NULL;
|
|
|
|
|
}
|
1998-07-22 07:48:59 +02:00
|
|
|
|
}
|
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. */
|
|
|
|
|
GlobalMemoryDestroy(fscxt);
|
|
|
|
|
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
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
|
int i;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
|
|
for (i = 0; i < MAX_LOBJ_FDS; i++)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if (cookies[i] == NULL)
|
|
|
|
|
{
|
|
|
|
|
cookies[i] = lobjCookie;
|
|
|
|
|
return i;
|
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
return -1;
|
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
|
|
|
|
}
|