A PGLIB lo_export function for Win32

Started by Scott Holmesalmost 25 years ago2 messagesgeneral
Jump to latest
#1Scott Holmes
sholmes@pacificnet.net

Well, I'm making progress in my attempt to incorporate large objects in my NT
application. At this point I can export an object but the resulting file is
corrupt. The object within the database is not corrupt. I have checked by
using the normal lo_export function. Following is the section of code used to
create the file. I have removed the error checks for readability here. The
normal C function calls open(), write(), and close() as well as the single
call to lo_export() produce the corrupted file, the commented out Win32
calls cause a memory fault.

res = PQexec(conn, "begin");
PQclear(res);

lobj_fd = lo_open(conn, lobjId, INV_READ);

/* lo_export(conn, lobjId, filename); */

fd = open(filename, OF_CREATE | OF_WRITE, 0666);

/* fd = CreateFile(filename, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_DELETE
| FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL, NULL); */

while ((nbytes = lo_read(conn, lobj_fd, buf, BUFSIZE)) > 0)
{
tmp = write(fd, buf, nbytes);

/* tmp =WriteFile(fd, buf, nbytes, BUFSIZE, NULL); */

}

/* (void) lo_close(conn, lobj_fd); */

(void) close(fd);
(void) CloseHandle(fd);

res = PQexec(conn, "end");
PQclear(res);
PQfinish(conn);

#2Eric G. Miller
egm2@jps.net
In reply to: Scott Holmes (#1)
Re: A PGLIB lo_export function for Win32

On Thu, Jul 05, 2001 at 02:20:18PM -0700, Scott Holmes wrote:

Well, I'm making progress in my attempt to incorporate large objects in my NT
application. At this point I can export an object but the resulting file is
corrupt. The object within the database is not corrupt. I have checked by
using the normal lo_export function. Following is the section of code used to
create the file. I have removed the error checks for readability here. The
normal C function calls open(), write(), and close() as well as the single
call to lo_export() produce the corrupted file, the commented out Win32
calls cause a memory fault.

open(), write() and close() are not standard C. Maybe you'd prefer to
use fopen(), fwrite() and fclose()?

res = PQexec(conn, "begin");
PQclear(res);

lobj_fd = lo_open(conn, lobjId, INV_READ);

/* lo_export(conn, lobjId, filename); */

fd = open(filename, OF_CREATE | OF_WRITE, 0666);

^^^^^^^^^^^^^^^^^^^^
What are these symbols? Are they windows'isms?
O_CREAT | O_TRUNC | O_WRONLY

But maybe, 'fopen (filename, "wb");' would be more portable? Make
sure to use "wb" on Windows which does sh*t behind your back if you
don't and you really want a binary file.

/* fd = CreateFile(filename, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_DELETE
| FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL, NULL); */

while ((nbytes = lo_read(conn, lobj_fd, buf, BUFSIZE)) > 0)
{
tmp = write(fd, buf, nbytes);

Generally, this shouldn't fail for regular files, but why capture
the bytes written if you aren't going to do something with it?

/* tmp =WriteFile(fd, buf, nbytes, BUFSIZE, NULL); */

}

/* (void) lo_close(conn, lobj_fd); */

(void) close(fd);
(void) CloseHandle(fd);

What are these (void) casts for? And what's the difference between
close(fd) and CloseHandle(fd) ? Looks like you're closing the same
file twice. That should generate an error.

res = PQexec(conn, "end");
PQclear(res);
PQfinish(conn);

--
Eric G. Miller <egm2@jps.net>