1 /*-------------------------------------------------------------------------
4 * support for filesystem operations on large objects
6 * Copyright (c) 1994, Regents of the University of California
10 * $Header: /cvsroot/pgsql/src/backend/libpq/be-fsstubs.c,v 1.41 1999/07/17 20:17:01 momjian Exp $
13 * This should be moved to a more appropriate place. It is here
14 * for lack of a better place.
16 * Builtin functions for open/close/read/write operations on large objects.
18 * These functions operate in a private GlobalMemoryContext, which means
19 * that large object descriptors hang around until we destroy the context.
20 * That happens in lo_commit(). It'd be possible to prolong the lifetime
21 * of the context so that LO FDs are good across transactions (for example,
22 * we could release the context only if we see that no FDs remain open).
23 * But we'd need additional state in order to do the right thing at the
24 * end of an aborted transaction. FDs opened during an aborted xact would
25 * still need to be closed, since they might not be pointing at valid
26 * relations at all. For now, we'll stick with the existing documented
27 * semantics of LO FDs: they're only good within a transaction.
29 *-------------------------------------------------------------------------
33 #include <sys/types.h>
39 #include "catalog/pg_shadow.h"
40 #include "libpq/be-fsstubs.h"
41 #include "libpq/libpq-fs.h"
42 #include "storage/large_object.h"
44 /* [PA] is Pascal André <andre@via.ecp.fr> */
47 #define MAX_LOBJ_FDS 256
49 #define FNAME_BUFSIZE 8192
51 static LargeObjectDesc *cookies[MAX_LOBJ_FDS];
53 static GlobalMemory fscxt = NULL;
56 static int newLOfd(LargeObjectDesc *lobjCookie);
57 static void deleteLOfd(int fd);
59 /*****************************************************************************
60 * File Interfaces for Large Objects
61 *****************************************************************************/
64 lo_open(Oid lobjId, int mode)
66 LargeObjectDesc *lobjDesc;
68 MemoryContext currentContext;
71 elog(NOTICE, "LOopen(%u,%d)", lobjId, mode);
75 fscxt = CreateGlobalMemory("Filesystem");
76 currentContext = MemoryContextSwitchTo((MemoryContext) fscxt);
78 lobjDesc = inv_open(lobjId, mode);
82 MemoryContextSwitchTo(currentContext);
84 elog(NOTICE, "cannot open large object %u", lobjId);
89 fd = newLOfd(lobjDesc);
91 /* switch context back to orig. */
92 MemoryContextSwitchTo(currentContext);
95 if (fd < 0) /* newLOfd couldn't find a slot */
96 elog(NOTICE, "Out of space for large object FDs");
105 MemoryContext currentContext;
107 if (fd < 0 || fd >= MAX_LOBJ_FDS)
109 elog(ERROR, "lo_close: large obj descriptor (%d) out of range", fd);
112 if (cookies[fd] == NULL)
114 elog(ERROR, "lo_close: invalid large obj descriptor (%d)", fd);
118 elog(NOTICE, "LOclose(%d)", fd);
121 Assert(fscxt != NULL);
122 currentContext = MemoryContextSwitchTo((MemoryContext) fscxt);
124 inv_close(cookies[fd]);
126 MemoryContextSwitchTo(currentContext);
133 * We assume the large object supports byte oriented reads and seeks so
134 * that our work is easier.
137 lo_read(int fd, char *buf, int len)
139 MemoryContext currentContext;
142 if (fd < 0 || fd >= MAX_LOBJ_FDS)
144 elog(ERROR, "lo_read: large obj descriptor (%d) out of range", fd);
147 if (cookies[fd] == NULL)
149 elog(ERROR, "lo_read: invalid large obj descriptor (%d)", fd);
153 Assert(fscxt != NULL);
154 currentContext = MemoryContextSwitchTo((MemoryContext) fscxt);
156 status = inv_read(cookies[fd], buf, len);
158 MemoryContextSwitchTo(currentContext);
163 lo_write(int fd, char *buf, int len)
165 MemoryContext currentContext;
168 if (fd < 0 || fd >= MAX_LOBJ_FDS)
170 elog(ERROR, "lo_write: large obj descriptor (%d) out of range", fd);
173 if (cookies[fd] == NULL)
175 elog(ERROR, "lo_write: invalid large obj descriptor (%d)", fd);
179 Assert(fscxt != NULL);
180 currentContext = MemoryContextSwitchTo((MemoryContext) fscxt);
182 status = inv_write(cookies[fd], buf, len);
184 MemoryContextSwitchTo(currentContext);
190 lo_lseek(int fd, int offset, int whence)
192 MemoryContext currentContext;
195 if (fd < 0 || fd >= MAX_LOBJ_FDS)
197 elog(ERROR, "lo_lseek: large obj descriptor (%d) out of range", fd);
200 if (cookies[fd] == NULL)
202 elog(ERROR, "lo_lseek: invalid large obj descriptor (%d)", fd);
206 Assert(fscxt != NULL);
207 currentContext = MemoryContextSwitchTo((MemoryContext) fscxt);
209 status = inv_seek(cookies[fd], offset, whence);
211 MemoryContextSwitchTo(currentContext);
219 LargeObjectDesc *lobjDesc;
220 MemoryContext currentContext;
224 fscxt = CreateGlobalMemory("Filesystem");
226 currentContext = MemoryContextSwitchTo((MemoryContext) fscxt);
228 lobjDesc = inv_create(mode);
230 if (lobjDesc == NULL)
232 MemoryContextSwitchTo(currentContext);
236 lobjId = RelationGetRelid(lobjDesc->heap_r);
240 /* switch context back to original memory context */
241 MemoryContextSwitchTo(currentContext);
249 if (fd < 0 || fd >= MAX_LOBJ_FDS)
251 elog(ERROR, "lo_tell: large object descriptor (%d) out of range", fd);
254 if (cookies[fd] == NULL)
256 elog(ERROR, "lo_tell: invalid large object descriptor (%d)", fd);
261 * We assume we do not need to switch contexts for inv_tell.
262 * That is true for now, but is probably more than this module
265 return inv_tell(cookies[fd]);
269 lo_unlink(Oid lobjId)
272 * inv_destroy does not need a context switch, indeed it doesn't
273 * touch any LO-specific data structures at all. (Again, that's
274 * probably more than this module ought to be assuming.)
276 * XXX there ought to be some code to clean up any open LOs that
277 * reference the specified relation... as is, they remain "open".
279 return inv_destroy(lobjId);
282 /*****************************************************************************
283 * Read/Write using varlena
284 *****************************************************************************/
287 loread(int fd, int len)
289 struct varlena *retval;
292 retval = (struct varlena *) palloc(VARHDRSZ + len);
293 totalread = lo_read(fd, VARDATA(retval), len);
294 VARSIZE(retval) = totalread + VARHDRSZ;
300 lowrite(int fd, struct varlena * wbuf)
305 bytestowrite = VARSIZE(wbuf) - VARHDRSZ;
306 totalwritten = lo_write(fd, VARDATA(wbuf), bytestowrite);
310 /*****************************************************************************
311 * Import/Export of Large Object
312 *****************************************************************************/
316 * imports a file as an (inversion) large object.
319 lo_import(text *filename)
325 char fnamebuf[FNAME_BUFSIZE];
326 LargeObjectDesc *lobj;
329 #ifndef ALLOW_DANGEROUS_LO_FUNCTIONS
331 elog(ERROR, "You must have Postgres superuser privilege to use "
332 "server-side lo_import().\n\tAnyone can use the "
333 "client-side lo_import() provided by libpq.");
337 * open the file to be read in
339 nbytes = VARSIZE(filename) - VARHDRSZ + 1;
340 if (nbytes > FNAME_BUFSIZE)
341 nbytes = FNAME_BUFSIZE;
342 StrNCpy(fnamebuf, VARDATA(filename), nbytes);
344 fd = PathNameOpenFile(fnamebuf, O_RDONLY, 0666);
346 fd = PathNameOpenFile(fnamebuf, O_RDONLY | O_BINARY, 0666);
350 elog(ERROR, "lo_import: can't open unix file \"%s\": %m",
355 * create an inversion "object"
357 lobj = inv_create(INV_READ | INV_WRITE);
360 elog(ERROR, "lo_import: can't create inv object for \"%s\"",
365 * the oid for the large object is just the oid of the relation
366 * XInv??? which contains the data.
368 lobjOid = RelationGetRelid(lobj->heap_r);
371 * read in from the Unix file and write to the inversion file
373 while ((nbytes = FileRead(fd, buf, BUFSIZE)) > 0)
375 tmp = inv_write(lobj, buf, nbytes);
377 elog(ERROR, "lo_import: error while reading \"%s\"",
389 * exports an (inversion) large object.
392 lo_export(Oid lobjId, text *filename)
398 char fnamebuf[FNAME_BUFSIZE];
399 LargeObjectDesc *lobj;
402 #ifndef ALLOW_DANGEROUS_LO_FUNCTIONS
404 elog(ERROR, "You must have Postgres superuser privilege to use "
405 "server-side lo_export().\n\tAnyone can use the "
406 "client-side lo_export() provided by libpq.");
410 * open the inversion "object"
412 lobj = inv_open(lobjId, INV_READ);
414 elog(ERROR, "lo_export: can't open inv object %u", lobjId);
417 * open the file to be written to
419 * Note: we reduce backend's normal 077 umask to the slightly
420 * friendlier 022. This code used to drop it all the way to 0,
421 * but creating world-writable export files doesn't seem wise.
423 nbytes = VARSIZE(filename) - VARHDRSZ + 1;
424 if (nbytes > FNAME_BUFSIZE)
425 nbytes = FNAME_BUFSIZE;
426 StrNCpy(fnamebuf, VARDATA(filename), nbytes);
427 oumask = umask((mode_t) 0022);
429 fd = PathNameOpenFile(fnamebuf, O_CREAT | O_WRONLY | O_TRUNC, 0666);
431 fd = PathNameOpenFile(fnamebuf, O_CREAT | O_WRONLY | O_TRUNC | O_BINARY, 0666);
436 elog(ERROR, "lo_export: can't open unix file \"%s\": %m",
441 * read in from the Unix file and write to the inversion file
443 while ((nbytes = inv_read(lobj, buf, BUFSIZE)) > 0)
445 tmp = FileWrite(fd, buf, nbytes);
447 elog(ERROR, "lo_export: error while writing \"%s\"",
459 * prepares large objects for transaction commit [PA, 7/17/98]
462 lo_commit(bool isCommit)
465 MemoryContext currentContext;
468 return; /* no LO operations in this xact */
470 currentContext = MemoryContextSwitchTo((MemoryContext) fscxt);
472 /* Clean out still-open index scans (not necessary if aborting)
473 * and clear cookies array so that LO fds are no longer good.
475 for (i = 0; i < MAX_LOBJ_FDS; i++)
477 if (cookies[i] != NULL)
480 inv_cleanindex(cookies[i]);
485 MemoryContextSwitchTo(currentContext);
487 /* Release the LO memory context to prevent permanent memory leaks. */
488 GlobalMemoryDestroy(fscxt);
493 /*****************************************************************************
494 * Support routines for this file
495 *****************************************************************************/
498 newLOfd(LargeObjectDesc *lobjCookie)
502 for (i = 0; i < MAX_LOBJ_FDS; i++)
505 if (cookies[i] == NULL)
507 cookies[i] = lobjCookie;