@@ -597,27 +597,39 @@ SELECT lo_export(image.raster, '/tmp/motd') FROM image
597597 <example id="lo-example">
598598 <title>Large Objects with <application>libpq</application> Example Program</title>
599599<programlisting><![CDATA[
600- /*--------------------------------------------------------------
600+ /*-------------------------------------------------------------------------
601601 *
602- * testlo.c--
602+ * testlo.c
603603 * test using large objects with libpq
604604 *
605- * Copyright (c) 1994, Regents of the University of California
605+ * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
606+ * Portions Copyright (c) 1994, Regents of the University of California
606607 *
607- *--------------------------------------------------------------
608+ *
609+ * IDENTIFICATION
610+ * src/test/examples/testlo.c
611+ *
612+ *-------------------------------------------------------------------------
608613 */
609614#include <stdio.h>
615+ #include <stdlib.h>
616+
617+ #include <sys/types.h>
618+ #include <sys/stat.h>
619+ #include <fcntl.h>
620+ #include <unistd.h>
621+
610622#include "libpq-fe.h"
611623#include "libpq/libpq-fs.h"
612624
613- #define BUFSIZE 1024
625+ #define BUFSIZE 1024
614626
615627/*
616- * importFile
628+ * importFile -
617629 * import file "in_filename" into database as large object "lobjOid"
618630 *
619631 */
620- Oid
632+ static Oid
621633importFile(PGconn *conn, char *filename)
622634{
623635 Oid lobjId;
@@ -633,15 +645,15 @@ importFile(PGconn *conn, char *filename)
633645 fd = open(filename, O_RDONLY, 0666);
634646 if (fd < 0)
635647 { /* error */
636- fprintf(stderr, "cannot open unix file %s \n", filename);
648+ fprintf(stderr, "cannot open unix file\"%s\" \n", filename);
637649 }
638650
639651 /*
640652 * create the large object
641653 */
642654 lobjId = lo_creat(conn, INV_READ | INV_WRITE);
643655 if (lobjId == 0)
644- fprintf(stderr, "cannot create large object\n ");
656+ fprintf(stderr, "cannot create large object");
645657
646658 lobj_fd = lo_open(conn, lobjId, INV_WRITE);
647659
@@ -652,16 +664,16 @@ importFile(PGconn *conn, char *filename)
652664 {
653665 tmp = lo_write(conn, lobj_fd, buf, nbytes);
654666 if (tmp < nbytes)
655- fprintf(stderr, "error while readinglarge object\n" );
667+ fprintf(stderr, "error while reading\"%s\"", filename );
656668 }
657669
658- (void) close(fd);
659- (void) lo_close(conn, lobj_fd);
670+ close(fd);
671+ lo_close(conn, lobj_fd);
660672
661673 return lobjId;
662674}
663675
664- void
676+ static void
665677pickout(PGconn *conn, Oid lobjId, int start, int len)
666678{
667679 int lobj_fd;
@@ -671,10 +683,7 @@ pickout(PGconn *conn, Oid lobjId, int start, int len)
671683
672684 lobj_fd = lo_open(conn, lobjId, INV_READ);
673685 if (lobj_fd < 0)
674- {
675- fprintf(stderr, "cannot open large object %d\n",
676- lobjId);
677- }
686+ fprintf(stderr, "cannot open large object %u", lobjId);
678687
679688 lo_lseek(conn, lobj_fd, start, SEEK_SET);
680689 buf = malloc(len + 1);
@@ -683,16 +692,18 @@ pickout(PGconn *conn, Oid lobjId, int start, int len)
683692 while (len - nread > 0)
684693 {
685694 nbytes = lo_read(conn, lobj_fd, buf, len - nread);
686- buf[nbytes] = ' ';
695+ buf[nbytes] = '\0 ';
687696 fprintf(stderr, ">>> %s", buf);
688697 nread += nbytes;
698+ if (nbytes <= 0)
699+ break; /* no more data? */
689700 }
690701 free(buf);
691702 fprintf(stderr, "\n");
692703 lo_close(conn, lobj_fd);
693704}
694705
695- void
706+ static void
696707overwrite(PGconn *conn, Oid lobjId, int start, int len)
697708{
698709 int lobj_fd;
@@ -703,35 +714,38 @@ overwrite(PGconn *conn, Oid lobjId, int start, int len)
703714
704715 lobj_fd = lo_open(conn, lobjId, INV_WRITE);
705716 if (lobj_fd < 0)
706- {
707- fprintf(stderr, "cannot open large object %d\n",
708- lobjId);
709- }
717+ fprintf(stderr, "cannot open large object %u", lobjId);
710718
711719 lo_lseek(conn, lobj_fd, start, SEEK_SET);
712720 buf = malloc(len + 1);
713721
714722 for (i = 0; i < len; i++)
715723 buf[i] = 'X';
716- buf[i] = ' ';
724+ buf[i] = '\0 ';
717725
718726 nwritten = 0;
719727 while (len - nwritten > 0)
720728 {
721729 nbytes = lo_write(conn, lobj_fd, buf + nwritten, len - nwritten);
722730 nwritten += nbytes;
731+ if (nbytes <= 0)
732+ {
733+ fprintf(stderr, "\nWRITE FAILED!\n");
734+ break;
735+ }
723736 }
724737 free(buf);
725738 fprintf(stderr, "\n");
726739 lo_close(conn, lobj_fd);
727740}
728741
742+
729743/*
730- * exportFile
744+ * exportFile -
731745 * export large object "lobjOid" to file "out_filename"
732746 *
733747 */
734- void
748+ static void
735749exportFile(PGconn *conn, Oid lobjId, char *filename)
736750{
737751 int lobj_fd;
@@ -745,18 +759,15 @@ exportFile(PGconn *conn, Oid lobjId, char *filename)
745759 */
746760 lobj_fd = lo_open(conn, lobjId, INV_READ);
747761 if (lobj_fd < 0)
748- {
749- fprintf(stderr, "cannot open large object %d\n",
750- lobjId);
751- }
762+ fprintf(stderr, "cannot open large object %u", lobjId);
752763
753764 /*
754765 * open the file to be written to
755766 */
756- fd = open(filename, O_CREAT | O_WRONLY, 0666);
767+ fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC , 0666);
757768 if (fd < 0)
758769 { /* error */
759- fprintf(stderr, "cannot open unix file %s\n ",
770+ fprintf(stderr, "cannot open unix file\" %s\" ",
760771 filename);
761772 }
762773
@@ -768,18 +779,18 @@ exportFile(PGconn *conn, Oid lobjId, char *filename)
768779 tmp = write(fd, buf, nbytes);
769780 if (tmp < nbytes)
770781 {
771- fprintf(stderr, "error while writing %s\n ",
782+ fprintf(stderr, "error while writing\" %s\" ",
772783 filename);
773784 }
774785 }
775786
776- (void) lo_close(conn, lobj_fd);
777- (void) close(fd);
787+ lo_close(conn, lobj_fd);
788+ close(fd);
778789
779790 return;
780791}
781792
782- void
793+ static void
783794exit_nicely(PGconn *conn)
784795{
785796 PQfinish(conn);
@@ -813,37 +824,40 @@ main(int argc, char **argv)
813824 conn = PQsetdb(NULL, NULL, NULL, NULL, database);
814825
815826 /* check to see that the backend connection was successfully made */
816- if (PQstatus(conn)== CONNECTION_BAD )
827+ if (PQstatus(conn)!= CONNECTION_OK )
817828 {
818- fprintf(stderr, "Connection to database'%s' failed.\n", database);
819- fprintf(stderr, "%s", PQerrorMessage(conn));
829+ fprintf(stderr, "Connection to databasefailed: %s",
830+ PQerrorMessage(conn));
820831 exit_nicely(conn);
821832 }
822833
823834 res = PQexec(conn, "begin");
824835 PQclear(res);
825-
826- printf("importing file %s\n", in_filename);
836+ printf("importing file \"%s\" ...\n", in_filename);
827837/* lobjOid = importFile(conn, in_filename); */
828838 lobjOid = lo_import(conn, in_filename);
829- /*
830- printf("as large object %d.\n", lobjOid);
839+ if (lobjOid == 0)
840+ fprintf(stderr, "%s\n", PQerrorMessage(conn));
841+ else
842+ {
843+ printf("\tas large object %u.\n", lobjOid);
831844
832- printf("picking out bytes 1000-2000 of the large object\n");
833- pickout(conn, lobjOid, 1000, 1000);
845+ printf("picking out bytes 1000-2000 of the large object\n");
846+ pickout(conn, lobjOid, 1000, 1000);
834847
835- printf("overwriting bytes 1000-2000 of the large object with X's\n");
836- overwrite(conn, lobjOid, 1000, 1000);
837- */
848+ printf("overwriting bytes 1000-2000 of the large object with X's\n");
849+ overwrite(conn, lobjOid, 1000, 1000);
838850
839- printf("exporting large object to file %s\n", out_filename);
840- /* exportFile(conn, lobjOid, out_filename); */
841- lo_export(conn, lobjOid, out_filename);
851+ printf("exporting large object to file \"%s\" ...\n", out_filename);
852+ /* exportFile(conn, lobjOid, out_filename); */
853+ if (lo_export(conn, lobjOid, out_filename) < 0)
854+ fprintf(stderr, "%s\n", PQerrorMessage(conn));
855+ }
842856
843857 res = PQexec(conn, "end");
844858 PQclear(res);
845859 PQfinish(conn);
846- exit(0) ;
860+ return 0 ;
847861}
848862]]>
849863</programlisting>