@@ -597,27 +597,39 @@ SELECT lo_export(image.raster, '/tmp/motd') FROM image
597
597
<example id="lo-example">
598
598
<title>Large Objects with <application>libpq</application> Example Program</title>
599
599
<programlisting><![CDATA[
600
- /*--------------------------------------------------------------
600
+ /*-------------------------------------------------------------------------
601
601
*
602
- * testlo.c--
602
+ * testlo.c
603
603
* test using large objects with libpq
604
604
*
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
606
607
*
607
- *--------------------------------------------------------------
608
+ *
609
+ * IDENTIFICATION
610
+ * src/test/examples/testlo.c
611
+ *
612
+ *-------------------------------------------------------------------------
608
613
*/
609
614
#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
+
610
622
#include "libpq-fe.h"
611
623
#include "libpq/libpq-fs.h"
612
624
613
- #define BUFSIZE 1024
625
+ #define BUFSIZE 1024
614
626
615
627
/*
616
- * importFile
628
+ * importFile -
617
629
* import file "in_filename" into database as large object "lobjOid"
618
630
*
619
631
*/
620
- Oid
632
+ static Oid
621
633
importFile(PGconn *conn, char *filename)
622
634
{
623
635
Oid lobjId;
@@ -633,15 +645,15 @@ importFile(PGconn *conn, char *filename)
633
645
fd = open(filename, O_RDONLY, 0666);
634
646
if (fd < 0)
635
647
{ /* error */
636
- fprintf(stderr, "cannot open unix file %s \n", filename);
648
+ fprintf(stderr, "cannot open unix file\"%s\" \n", filename);
637
649
}
638
650
639
651
/*
640
652
* create the large object
641
653
*/
642
654
lobjId = lo_creat(conn, INV_READ | INV_WRITE);
643
655
if (lobjId == 0)
644
- fprintf(stderr, "cannot create large object\n ");
656
+ fprintf(stderr, "cannot create large object");
645
657
646
658
lobj_fd = lo_open(conn, lobjId, INV_WRITE);
647
659
@@ -652,16 +664,16 @@ importFile(PGconn *conn, char *filename)
652
664
{
653
665
tmp = lo_write(conn, lobj_fd, buf, nbytes);
654
666
if (tmp < nbytes)
655
- fprintf(stderr, "error while reading large object\n" );
667
+ fprintf(stderr, "error while reading \"%s\"", filename );
656
668
}
657
669
658
- (void) close(fd);
659
- (void) lo_close(conn, lobj_fd);
670
+ close(fd);
671
+ lo_close(conn, lobj_fd);
660
672
661
673
return lobjId;
662
674
}
663
675
664
- void
676
+ static void
665
677
pickout(PGconn *conn, Oid lobjId, int start, int len)
666
678
{
667
679
int lobj_fd;
@@ -671,10 +683,7 @@ pickout(PGconn *conn, Oid lobjId, int start, int len)
671
683
672
684
lobj_fd = lo_open(conn, lobjId, INV_READ);
673
685
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);
678
687
679
688
lo_lseek(conn, lobj_fd, start, SEEK_SET);
680
689
buf = malloc(len + 1);
@@ -683,16 +692,18 @@ pickout(PGconn *conn, Oid lobjId, int start, int len)
683
692
while (len - nread > 0)
684
693
{
685
694
nbytes = lo_read(conn, lobj_fd, buf, len - nread);
686
- buf[nbytes] = ' ';
695
+ buf[nbytes] = '\0 ';
687
696
fprintf(stderr, ">>> %s", buf);
688
697
nread += nbytes;
698
+ if (nbytes <= 0)
699
+ break; /* no more data? */
689
700
}
690
701
free(buf);
691
702
fprintf(stderr, "\n");
692
703
lo_close(conn, lobj_fd);
693
704
}
694
705
695
- void
706
+ static void
696
707
overwrite(PGconn *conn, Oid lobjId, int start, int len)
697
708
{
698
709
int lobj_fd;
@@ -703,35 +714,38 @@ overwrite(PGconn *conn, Oid lobjId, int start, int len)
703
714
704
715
lobj_fd = lo_open(conn, lobjId, INV_WRITE);
705
716
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);
710
718
711
719
lo_lseek(conn, lobj_fd, start, SEEK_SET);
712
720
buf = malloc(len + 1);
713
721
714
722
for (i = 0; i < len; i++)
715
723
buf[i] = 'X';
716
- buf[i] = ' ';
724
+ buf[i] = '\0 ';
717
725
718
726
nwritten = 0;
719
727
while (len - nwritten > 0)
720
728
{
721
729
nbytes = lo_write(conn, lobj_fd, buf + nwritten, len - nwritten);
722
730
nwritten += nbytes;
731
+ if (nbytes <= 0)
732
+ {
733
+ fprintf(stderr, "\nWRITE FAILED!\n");
734
+ break;
735
+ }
723
736
}
724
737
free(buf);
725
738
fprintf(stderr, "\n");
726
739
lo_close(conn, lobj_fd);
727
740
}
728
741
742
+
729
743
/*
730
- * exportFile
744
+ * exportFile -
731
745
* export large object "lobjOid" to file "out_filename"
732
746
*
733
747
*/
734
- void
748
+ static void
735
749
exportFile(PGconn *conn, Oid lobjId, char *filename)
736
750
{
737
751
int lobj_fd;
@@ -745,18 +759,15 @@ exportFile(PGconn *conn, Oid lobjId, char *filename)
745
759
*/
746
760
lobj_fd = lo_open(conn, lobjId, INV_READ);
747
761
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);
752
763
753
764
/*
754
765
* open the file to be written to
755
766
*/
756
- fd = open(filename, O_CREAT | O_WRONLY, 0666);
767
+ fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC , 0666);
757
768
if (fd < 0)
758
769
{ /* error */
759
- fprintf(stderr, "cannot open unix file %s\n ",
770
+ fprintf(stderr, "cannot open unix file\" %s\" ",
760
771
filename);
761
772
}
762
773
@@ -768,18 +779,18 @@ exportFile(PGconn *conn, Oid lobjId, char *filename)
768
779
tmp = write(fd, buf, nbytes);
769
780
if (tmp < nbytes)
770
781
{
771
- fprintf(stderr, "error while writing %s\n ",
782
+ fprintf(stderr, "error while writing \" %s\" ",
772
783
filename);
773
784
}
774
785
}
775
786
776
- (void) lo_close(conn, lobj_fd);
777
- (void) close(fd);
787
+ lo_close(conn, lobj_fd);
788
+ close(fd);
778
789
779
790
return;
780
791
}
781
792
782
- void
793
+ static void
783
794
exit_nicely(PGconn *conn)
784
795
{
785
796
PQfinish(conn);
@@ -813,37 +824,40 @@ main(int argc, char **argv)
813
824
conn = PQsetdb(NULL, NULL, NULL, NULL, database);
814
825
815
826
/* check to see that the backend connection was successfully made */
816
- if (PQstatus(conn) == CONNECTION_BAD )
827
+ if (PQstatus(conn) != CONNECTION_OK )
817
828
{
818
- fprintf(stderr, "Connection to database '%s' failed.\n", database);
819
- fprintf(stderr, "%s", PQerrorMessage(conn));
829
+ fprintf(stderr, "Connection to database failed: %s",
830
+ PQerrorMessage(conn));
820
831
exit_nicely(conn);
821
832
}
822
833
823
834
res = PQexec(conn, "begin");
824
835
PQclear(res);
825
-
826
- printf("importing file %s\n", in_filename);
836
+ printf("importing file \"%s\" ...\n", in_filename);
827
837
/* lobjOid = importFile(conn, in_filename); */
828
838
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);
831
844
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);
834
847
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);
838
850
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
+ }
842
856
843
857
res = PQexec(conn, "end");
844
858
PQclear(res);
845
859
PQfinish(conn);
846
- exit(0) ;
860
+ return 0 ;
847
861
}
848
862
]]>
849
863
</programlisting>
0 commit comments