PostgreSQL Source Code  git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
testlo.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "libpq-fe.h"
#include "libpq/libpq-fs.h"
Include dependency graph for testlo.c:

Go to the source code of this file.

Macros

#define BUFSIZE   1024
 

Functions

static Oid importFile (PGconn *conn, char *filename)
 
static void pickout (PGconn *conn, Oid lobjId, int start, int len)
 
static void overwrite (PGconn *conn, Oid lobjId, int start, int len)
 
static void exportFile (PGconn *conn, Oid lobjId, char *filename)
 
static void exit_nicely (PGconn *conn)
 
int main (int argc, char **argv)
 

Macro Definition Documentation

◆ BUFSIZE

#define BUFSIZE   1024

Definition at line 26 of file testlo.c.

Function Documentation

◆ exit_nicely()

static void exit_nicely ( PGconn conn)
static

Definition at line 193 of file testlo.c.

194 {
195  PQfinish(conn);
196  exit(1);
197 }
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4879
exit(1)
PGconn * conn
Definition: streamutil.c:53

References conn, exit(), and PQfinish().

Referenced by main().

◆ exportFile()

static void exportFile ( PGconn conn,
Oid  lobjId,
char *  filename 
)
static

Definition at line 150 of file testlo.c.

151 {
152  int lobj_fd;
153  char buf[BUFSIZE];
154  int nbytes,
155  tmp;
156  int fd;
157 
158  /*
159  * open the large object
160  */
161  lobj_fd = lo_open(conn, lobjId, INV_READ);
162  if (lobj_fd < 0)
163  fprintf(stderr, "cannot open large object %u", lobjId);
164 
165  /*
166  * open the file to be written to
167  */
168  fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0666);
169  if (fd < 0)
170  { /* error */
171  fprintf(stderr, "cannot open unix file\"%s\"",
172  filename);
173  }
174 
175  /*
176  * read in from the inversion file and write to the Unix file
177  */
178  while ((nbytes = lo_read(conn, lobj_fd, buf, BUFSIZE)) > 0)
179  {
180  tmp = write(fd, buf, nbytes);
181  if (tmp < nbytes)
182  {
183  fprintf(stderr, "error while writing \"%s\"",
184  filename);
185  }
186  }
187 
188  lo_close(conn, lobj_fd);
189  close(fd);
190 }
int lo_read(int fd, char *buf, int len)
Definition: be-fsstubs.c:154
int lo_close(PGconn *conn, int fd)
Definition: fe-lobj.c:96
int lo_open(PGconn *conn, Oid lobjId, int mode)
Definition: fe-lobj.c:57
#define close(a)
Definition: win32.h:12
#define write(a, b, c)
Definition: win32.h:14
#define INV_READ
Definition: libpq-fs.h:22
static char * filename
Definition: pg_dumpall.c:119
static char * buf
Definition: pg_test_fsync.c:72
#define fprintf
Definition: port.h:242
static int fd(const char *x, int i)
Definition: preproc-init.c:105
#define BUFSIZE
Definition: testlo.c:26

References buf, BUFSIZE, close, conn, fd(), filename, fprintf, INV_READ, lo_close(), lo_open(), lo_read(), and write.

◆ importFile()

static Oid importFile ( PGconn conn,
char *  filename 
)
static

Definition at line 34 of file testlo.c.

35 {
36  Oid lobjId;
37  int lobj_fd;
38  char buf[BUFSIZE];
39  int nbytes,
40  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  */
55  lobjId = lo_creat(conn, INV_READ | INV_WRITE);
56  if (lobjId == 0)
57  fprintf(stderr, "cannot create large object");
58 
59  lobj_fd = lo_open(conn, lobjId, INV_WRITE);
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);
72  lo_close(conn, lobj_fd);
73 
74  return lobjId;
75 }
int lo_write(int fd, const char *buf, int len)
Definition: be-fsstubs.c:182
Oid lo_creat(PGconn *conn, int mode)
Definition: fe-lobj.c:438
#define read(a, b, c)
Definition: win32.h:13
#define INV_WRITE
Definition: libpq-fs.h:21
unsigned int Oid
Definition: postgres_ext.h:31

References buf, BUFSIZE, close, conn, fd(), filename, fprintf, INV_READ, INV_WRITE, lo_close(), lo_creat(), lo_open(), lo_write(), and read.

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 200 of file testlo.c.

201 {
202  char *in_filename,
203  *out_filename;
204  char *database;
205  Oid lobjOid;
206  PGconn *conn;
207  PGresult *res;
208 
209  if (argc != 4)
210  {
211  fprintf(stderr, "Usage: %s database_name in_filename out_filename\n",
212  argv[0]);
213  exit(1);
214  }
215 
216  database = argv[1];
217  in_filename = argv[2];
218  out_filename = argv[3];
219 
220  /*
221  * set up the connection
222  */
223  conn = PQsetdb(NULL, NULL, NULL, NULL, database);
224 
225  /* check to see that the backend connection was successfully made */
226  if (PQstatus(conn) != CONNECTION_OK)
227  {
228  fprintf(stderr, "%s", PQerrorMessage(conn));
229  exit_nicely(conn);
230  }
231 
232  /* Set always-secure search path, so malicious users can't take control. */
233  res = PQexec(conn,
234  "SELECT pg_catalog.set_config('search_path', '', false)");
236  {
237  fprintf(stderr, "SET failed: %s", PQerrorMessage(conn));
238  PQclear(res);
239  exit_nicely(conn);
240  }
241  PQclear(res);
242 
243  res = PQexec(conn, "begin");
244  PQclear(res);
245  printf("importing file \"%s\" ...\n", in_filename);
246 /* lobjOid = importFile(conn, in_filename); */
247  lobjOid = lo_import(conn, in_filename);
248  if (lobjOid == 0)
249  fprintf(stderr, "%s\n", PQerrorMessage(conn));
250  else
251  {
252  printf("\tas large object %u.\n", lobjOid);
253 
254  printf("picking out bytes 1000-2000 of the large object\n");
255  pickout(conn, lobjOid, 1000, 1000);
256 
257  printf("overwriting bytes 1000-2000 of the large object with X's\n");
258  overwrite(conn, lobjOid, 1000, 1000);
259 
260  printf("exporting large object to file \"%s\" ...\n", out_filename);
261 /* exportFile(conn, lobjOid, out_filename); */
262  if (lo_export(conn, lobjOid, out_filename) < 0)
263  fprintf(stderr, "%s\n", PQerrorMessage(conn));
264  }
265 
266  res = PQexec(conn, "end");
267  PQclear(res);
268  PQfinish(conn);
269  return 0;
270 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:7200
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:7137
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3411
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2262
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
@ CONNECTION_OK
Definition: libpq-fe.h:81
@ PGRES_TUPLES_OK
Definition: libpq-fe.h:123
#define PQsetdb(M_PGHOST, M_PGPORT, M_PGOPT, M_PGTTY, M_DBNAME)
Definition: libpq-fe.h:327
#define printf(...)
Definition: port.h:244
static void pickout(PGconn *conn, Oid lobjId, int start, int len)
Definition: testlo.c:78
static void exit_nicely(PGconn *conn)
Definition: testlo.c:193
static void overwrite(PGconn *conn, Oid lobjId, int start, int len)
Definition: testlo.c:108

References conn, CONNECTION_OK, exit(), exit_nicely(), fprintf, lo_export(), lo_import(), overwrite(), PGRES_TUPLES_OK, pickout(), PQclear(), PQerrorMessage(), PQexec(), PQfinish(), PQresultStatus(), PQsetdb, PQstatus(), printf, and res.

◆ overwrite()

static void overwrite ( PGconn conn,
Oid  lobjId,
int  start,
int  len 
)
static

Definition at line 108 of file testlo.c.

109 {
110  int lobj_fd;
111  char *buf;
112  int nbytes;
113  int nwritten;
114  int i;
115 
116  lobj_fd = lo_open(conn, lobjId, INV_WRITE);
117  if (lobj_fd < 0)
118  fprintf(stderr, "cannot open large object %u", lobjId);
119 
120  lo_lseek(conn, lobj_fd, start, SEEK_SET);
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");
140  lo_close(conn, lobj_fd);
141 }
int lo_lseek(PGconn *conn, int fd, int offset, int whence)
Definition: fe-lobj.c:344
return str start
#define free(a)
Definition: header.h:65
#define malloc(a)
Definition: header.h:50
int i
Definition: isn.c:72
const void size_t len

References buf, conn, fprintf, free, i, INV_WRITE, len, lo_close(), lo_lseek(), lo_open(), lo_write(), malloc, and start.

Referenced by main(), and pgwin32_setenv().

◆ pickout()

static void pickout ( PGconn conn,
Oid  lobjId,
int  start,
int  len 
)
static

Definition at line 78 of file testlo.c.

79 {
80  int lobj_fd;
81  char *buf;
82  int nbytes;
83  int nread;
84 
85  lobj_fd = lo_open(conn, lobjId, INV_READ);
86  if (lobj_fd < 0)
87  fprintf(stderr, "cannot open large object %u", lobjId);
88 
89  lo_lseek(conn, lobj_fd, start, SEEK_SET);
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");
104  lo_close(conn, lobj_fd);
105 }

References buf, conn, fprintf, free, INV_READ, len, lo_close(), lo_lseek(), lo_open(), lo_read(), malloc, and start.

Referenced by main().