PostgreSQL Source Code git master
Loading...
Searching...
No Matches
testlo.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * testlo.c
4 * test using large objects with libpq
5 *
6 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
8 *
9 *
10 * IDENTIFICATION
11 * src/test/examples/testlo.c
12 *
13 *-------------------------------------------------------------------------
14 */
15#include <stdio.h>
16#include <stdlib.h>
17
18#include <sys/types.h>
19#include <sys/stat.h>
20#include <fcntl.h>
21#include <unistd.h>
22
23#include "libpq-fe.h"
24#include "libpq/libpq-fs.h"
25
26#define BUFSIZE 1024
27
28/*
29 * importFile -
30 * import file "in_filename" into database as large object "lobjOid"
31 *
32 */
33static Oid
35{
36 Oid lobjId;
37 int lobj_fd;
38 char buf[BUFSIZE];
39 ssize_t nbytes;
40 int tmp;
41 int fd;
42
43 /*
44 * open the file to be read in
45 */
46 fd = open(filename, O_RDONLY, 0666);
47 if (fd < 0)
48 { /* error */
49 fprintf(stderr, "cannot open unix file\"%s\"\n", filename);
50 }
51
52 /*
53 * create the large object
54 */
56 if (lobjId == 0)
57 fprintf(stderr, "cannot create large object");
58
60
61 /*
62 * read in from the Unix file and write to the inversion file
63 */
64 while ((nbytes = read(fd, buf, BUFSIZE)) > 0)
65 {
66 tmp = lo_write(conn, lobj_fd, buf, nbytes);
67 if (tmp < nbytes)
68 fprintf(stderr, "error while reading \"%s\"", filename);
69 }
70
71 close(fd);
73
74 return lobjId;
75}
76
77static void
79{
80 int lobj_fd;
81 char *buf;
82 ssize_t nbytes;
83 int nread;
84
86 if (lobj_fd < 0)
87 fprintf(stderr, "cannot open large object %u", lobjId);
88
90 buf = malloc(len + 1);
91
92 nread = 0;
93 while (len - nread > 0)
94 {
95 nbytes = lo_read(conn, lobj_fd, buf, len - nread);
96 buf[nbytes] = '\0';
97 fprintf(stderr, ">>> %s", buf);
98 nread += nbytes;
99 if (nbytes <= 0)
100 break; /* no more data? */
101 }
102 free(buf);
103 fprintf(stderr, "\n");
105}
106
107static void
109{
110 int lobj_fd;
111 char *buf;
112 int nbytes;
113 int nwritten;
114 int i;
115
117 if (lobj_fd < 0)
118 fprintf(stderr, "cannot open large object %u", lobjId);
119
121 buf = malloc(len + 1);
122
123 for (i = 0; i < len; i++)
124 buf[i] = 'X';
125 buf[i] = '\0';
126
127 nwritten = 0;
128 while (len - nwritten > 0)
129 {
130 nbytes = lo_write(conn, lobj_fd, buf + nwritten, len - nwritten);
131 nwritten += nbytes;
132 if (nbytes <= 0)
133 {
134 fprintf(stderr, "\nWRITE FAILED!\n");
135 break;
136 }
137 }
138 free(buf);
139 fprintf(stderr, "\n");
141}
142
143
144/*
145 * exportFile -
146 * export large object "lobjOid" to file "out_filename"
147 *
148 */
149static void
151{
152 int lobj_fd;
153 char buf[BUFSIZE];
154 int nbytes;
155 int fd;
156
157 /*
158 * open the large object
159 */
161 if (lobj_fd < 0)
162 fprintf(stderr, "cannot open large object %u", lobjId);
163
164 /*
165 * open the file to be written to
166 */
167 fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0666);
168 if (fd < 0)
169 { /* error */
170 fprintf(stderr, "cannot open unix file\"%s\"",
171 filename);
172 }
173
174 /*
175 * read in from the inversion file and write to the Unix file
176 */
177 while ((nbytes = lo_read(conn, lobj_fd, buf, BUFSIZE)) > 0)
178 {
179 ssize_t tmp;
180
181 tmp = write(fd, buf, nbytes);
182 if (tmp < nbytes)
183 {
184 fprintf(stderr, "error while writing \"%s\"",
185 filename);
186 }
187 }
188
190 close(fd);
191}
192
193static void
195{
196 PQfinish(conn);
197 exit(1);
198}
199
200int
201main(int argc, char **argv)
202{
203 char *in_filename,
205 char *database;
206 Oid lobjOid;
207 PGconn *conn;
208 PGresult *res;
209
210 if (argc != 4)
211 {
212 fprintf(stderr, "Usage: %s database_name in_filename out_filename\n",
213 argv[0]);
214 exit(1);
215 }
216
217 database = argv[1];
218 in_filename = argv[2];
219 out_filename = argv[3];
220
221 /*
222 * set up the connection
223 */
224 conn = PQsetdb(NULL, NULL, NULL, NULL, database);
225
226 /* check to see that the backend connection was successfully made */
228 {
231 }
232
233 /* Set always-secure search path, so malicious users can't take control. */
234 res = PQexec(conn,
235 "SELECT pg_catalog.set_config('search_path', '', false)");
237 {
238 fprintf(stderr, "SET failed: %s", PQerrorMessage(conn));
239 PQclear(res);
241 }
242 PQclear(res);
243
244 res = PQexec(conn, "begin");
245 PQclear(res);
246 printf("importing file \"%s\" ...\n", in_filename);
247/* lobjOid = importFile(conn, in_filename); */
249 if (lobjOid == 0)
251 else
252 {
253 printf("\tas large object %u.\n", lobjOid);
254
255 printf("picking out bytes 1000-2000 of the large object\n");
256 pickout(conn, lobjOid, 1000, 1000);
257
258 printf("overwriting bytes 1000-2000 of the large object with X's\n");
259 overwrite(conn, lobjOid, 1000, 1000);
260
261 printf("exporting large object to file \"%s\" ...\n", out_filename);
262/* exportFile(conn, lobjOid, out_filename); */
265 }
266
267 res = PQexec(conn, "end");
268 PQclear(res);
269 PQfinish(conn);
270 return 0;
271}
int lo_write(int fd, const char *buf, int len)
Definition be-fsstubs.c:182
int lo_read(int fd, char *buf, int len)
Definition be-fsstubs.c:154
int main(void)
#define fprintf(file, fmt, msg)
Definition cubescan.l:21
ConnStatusType PQstatus(const PGconn *conn)
void PQfinish(PGconn *conn)
char * PQerrorMessage(const PGconn *conn)
PGresult * PQexec(PGconn *conn, const char *query)
Definition fe-exec.c:2279
int lo_close(PGconn *conn, int fd)
Definition fe-lobj.c:96
Oid lo_creat(PGconn *conn, int mode)
Definition fe-lobj.c:438
int lo_lseek(PGconn *conn, int fd, int offset, int whence)
Definition fe-lobj.c:344
int lo_open(PGconn *conn, Oid lobjId, int mode)
Definition fe-lobj.c:57
Oid lo_import(PGconn *conn, const char *filename)
Definition fe-lobj.c:626
int lo_export(PGconn *conn, Oid lobjId, const char *filename)
Definition fe-lobj.c:748
return str start
#define close(a)
Definition win32.h:12
#define write(a, b, c)
Definition win32.h:14
#define read(a, b, c)
Definition win32.h:13
int i
Definition isn.c:77
#define PQclear
#define PQresultStatus
@ CONNECTION_OK
Definition libpq-fe.h:90
@ PGRES_TUPLES_OK
Definition libpq-fe.h:134
#define PQsetdb(M_PGHOST, M_PGPORT, M_PGOPT, M_PGTTY, M_DBNAME)
Definition libpq-fe.h:352
#define INV_READ
Definition libpq-fs.h:22
#define INV_WRITE
Definition libpq-fs.h:21
const void size_t len
static char * filename
Definition pg_dumpall.c:120
static char buf[DEFAULT_XLOG_SEG_SIZE]
#define printf(...)
Definition port.h:267
unsigned int Oid
static int fd(const char *x, int i)
static int fb(int x)
#define free(a)
#define malloc(a)
PGconn * conn
Definition streamutil.c:52
static void pickout(PGconn *conn, Oid lobjId, int start, int len)
Definition testlo.c:78
static void exit_nicely(PGconn *conn)
Definition testlo.c:194
static void overwrite(PGconn *conn, Oid lobjId, int start, int len)
Definition testlo.c:108
static void exportFile(PGconn *conn, Oid lobjId, char *filename)
Definition testlo.c:150
#define BUFSIZE
Definition testlo.c:26
static Oid importFile(PGconn *conn, char *filename)
Definition testlo.c:34