| 652 | } |
| 653 | |
| 654 | static Oid |
| 655 | lo_import_internal(PGconn *conn, const char *filename, Oid oid) |
| 656 | { |
| 657 | int fd; |
| 658 | int nbytes, |
| 659 | tmp; |
| 660 | char buf[LO_BUFSIZE]; |
| 661 | Oid lobjOid; |
| 662 | int lobj; |
| 663 | char sebuf[PG_STRERROR_R_BUFLEN]; |
| 664 | |
| 665 | if (conn == NULL) |
| 666 | return InvalidOid; |
| 667 | |
| 668 | /* Since this is the beginning of a query cycle, reset the error buffer */ |
| 669 | resetPQExpBuffer(&conn->errorMessage); |
| 670 | |
| 671 | /* |
| 672 | * open the file to be read in |
| 673 | */ |
| 674 | fd = open(filename, O_RDONLY | PG_BINARY, 0666); |
| 675 | if (fd < 0) |
| 676 | { /* error */ |
| 677 | appendPQExpBuffer(&conn->errorMessage, |
| 678 | libpq_gettext("could not open file \"%s\": %s\n"), |
| 679 | filename, strerror_r(errno, sebuf, sizeof(sebuf))); |
| 680 | return InvalidOid; |
| 681 | } |
| 682 | |
| 683 | /* |
| 684 | * create an inversion object |
| 685 | */ |
| 686 | if (oid == InvalidOid) |
| 687 | lobjOid = lo_creat(conn, INV_READ | INV_WRITE); |
| 688 | else |
| 689 | lobjOid = lo_create(conn, oid); |
| 690 | |
| 691 | if (lobjOid == InvalidOid) |
| 692 | { |
| 693 | /* we assume lo_create() already set a suitable error message */ |
| 694 | (void) close(fd); |
| 695 | return InvalidOid; |
| 696 | } |
| 697 | |
| 698 | lobj = lo_open(conn, lobjOid, INV_WRITE); |
| 699 | if (lobj == -1) |
| 700 | { |
| 701 | /* we assume lo_open() already set a suitable error message */ |
| 702 | (void) close(fd); |
| 703 | return InvalidOid; |
| 704 | } |
| 705 | |
| 706 | /* |
| 707 | * read in from the file and write to the large object |
| 708 | */ |
| 709 | while ((nbytes = read(fd, buf, LO_BUFSIZE)) > 0) |
| 710 | { |
| 711 | tmp = lo_write(conn, lobj, buf, nbytes); |
no test coverage detected