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-05-10 02:46:32 +02:00
|
|
|
|
* $Header: /cvsroot/pgsql/src/backend/libpq/be-fsstubs.c,v 1.32 1999/05/10 00:45:09 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
|
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
|
* These functions operate in the current portal variable context, which
|
|
|
|
|
* means the large object descriptors hang around between transactions and
|
|
|
|
|
* are not deallocated until explicitly closed, or until the portal is
|
|
|
|
|
* closed.
|
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>
|
1997-04-21 06:28:59 +02:00
|
|
|
|
#include <string.h>
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
1996-11-06 09:48:33 +01:00
|
|
|
|
#include <postgres.h>
|
|
|
|
|
|
|
|
|
|
#include <lib/dllist.h>
|
|
|
|
|
#include <libpq/libpq.h>
|
|
|
|
|
#include <libpq/libpq-fs.h>
|
|
|
|
|
#include <nodes/nodes.h>
|
|
|
|
|
#include <utils/memutils.h>
|
|
|
|
|
#include <lib/fstack.h>
|
|
|
|
|
#include <utils/mcxt.h>
|
1997-09-07 07:04:48 +02:00
|
|
|
|
#include <storage/fd.h> /* for O_ */
|
1996-11-06 09:48:33 +01:00
|
|
|
|
#include <storage/large_object.h>
|
|
|
|
|
#include <libpq/be-fsstubs.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
|
|
|
|
|
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-09 17:00:18 +02:00
|
|
|
|
MemoryContext currentContext;
|
|
|
|
|
int status;
|
|
|
|
|
|
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-05-09 17:00:18 +02:00
|
|
|
|
currentContext = MemoryContextSwitchTo((MemoryContext) fscxt);
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
return(status);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
lo_write(int fd, char *buf, int len)
|
|
|
|
|
{
|
1999-05-09 17:00:18 +02:00
|
|
|
|
MemoryContext currentContext;
|
|
|
|
|
int status;
|
|
|
|
|
|
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-05-09 17:00:18 +02:00
|
|
|
|
currentContext = MemoryContextSwitchTo((MemoryContext) fscxt);
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
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;
|
|
|
|
|
int ret;
|
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
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
|
currentContext = MemoryContextSwitchTo((MemoryContext) fscxt);
|
1997-03-18 22:30:41 +01:00
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
|
ret = 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
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
|
return ret;
|
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;
|
|
|
|
|
}
|
|
|
|
|
return inv_tell(cookies[fd]);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
lo_unlink(Oid lobjId)
|
|
|
|
|
{
|
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;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
1997-09-08 04:41:22 +02:00
|
|
|
|
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
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* open the file to be read in
|
|
|
|
|
*/
|
1997-10-25 03:10:58 +02:00
|
|
|
|
StrNCpy(fnamebuf, VARDATA(filename), VARSIZE(filename) - VARHDRSZ + 1);
|
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-05-09 02:54:30 +02:00
|
|
|
|
elog(ERROR, "be_lo_import: can't open unix file \"%s\"\n",
|
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;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
1997-09-08 04:41:22 +02:00
|
|
|
|
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-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
|
|
|
|
|
*/
|
1997-10-25 03:10:58 +02:00
|
|
|
|
StrNCpy(fnamebuf, VARDATA(filename), VARSIZE(filename) - VARHDRSZ + 1);
|
1999-05-09 02:54:30 +02:00
|
|
|
|
oumask = umask((mode_t) 0);
|
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-05-09 02:54:30 +02:00
|
|
|
|
elog(ERROR, "lo_export: can't open unix file \"%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
|
|
|
|
/*
|
|
|
|
|
* 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
|
1998-07-21 06:17:30 +02:00
|
|
|
|
_lo_commit(void)
|
|
|
|
|
{
|
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)
|
|
|
|
|
return;
|
1998-09-01 06:40:42 +02:00
|
|
|
|
|
1998-07-21 06:17:30 +02:00
|
|
|
|
currentContext = MemoryContextSwitchTo((MemoryContext) fscxt);
|
|
|
|
|
|
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)
|
|
|
|
|
inv_cleanindex(cookies[i]);
|
1998-07-22 07:48:59 +02:00
|
|
|
|
}
|
1998-07-21 06:17:30 +02:00
|
|
|
|
|
|
|
|
|
MemoryContextSwitchTo(currentContext);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
}
|