-
Notifications
You must be signed in to change notification settings - Fork 0
/
copytester.c
251 lines (212 loc) · 5.01 KB
/
copytester.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
* Test COPY FROM using files in corpus.
*
* For each file in the input directory (corpus), this calls
* "COPY copytest FROM STDIN" and sends the file to the server.
* If the command throws an error, like many of the input files do,
* it is printed out. After processing each file, the contents
* of the table are SELECTed and printed out.
*
* This is useful for testing a patch for regressions:
* Run copytester against a patched and unpatched server, and compare
* that the results are the same (except for acceptable differences
* in error messages).
*/
#include "c.h"
#include <dirent.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "libpq-fe.h"
/*
* How many bytes to send to the server in one message?
*
* We intentionally read and send the the file one byte at a time to the
* server. That's highly inefficient, of course, but it gives a better
* chance of uncovering bugs in look-ahead around buffer boundaries.
* But can make READSIZE larger if you don't want that.
*/
#define READSIZE 1
static void sendCopyFile(char *path, PGconn *conn);
static int
cmp_string(const void *a, const void *b)
{
const char *astr = *(const char **) a;
const char *bstr = *(const char **) b;
return strcmp(astr, bstr);
}
int
main(int argc, char **argv)
{
char *path;
char *conninfo;
PGconn *conn;
DIR *dir;
struct dirent *de;
PGresult *res;
char **filenames;
int nallocated;
int nfiles;
if (argc < 3)
{
fprintf(stderr, "usage: copytester <input file or dir> <connstring>\n");
exit(1);
}
path = argv[1];
conninfo = argv[2];
dir = opendir(path);
if (dir)
{
nallocated=100;
filenames = malloc(nallocated * sizeof(char *));
if (!filenames)
{
fprintf(stderr, "out of memory\n");
exit(1);
}
nfiles = 0;
while ((de = readdir(dir)) != NULL)
{
char fn[1000];
if (strcmp(de->d_name, ".") == 0 ||
strcmp(de->d_name, "..") == 0)
continue;
if (nfiles == nallocated)
{
nallocated *= 2;
filenames = realloc(filenames, nallocated * sizeof(char *));
if (!filenames)
{
fprintf(stderr, "out of memory\n");
exit(1);
}
}
snprintf(fn, sizeof(fn), "%s/%s", path, de->d_name);
filenames[nfiles++] = strdup(fn);
}
closedir(dir);
}
else if (errno == ENOTDIR)
{
filenames = &path;
nfiles = 1;
}
else
{
fprintf(stderr, "could not open directory \"%s\": %s",
path, strerror(errno));
exit(1);
}
/*
* Sort the filenames, so that you get deterministic output that you
* can easily compare between two runs.
*/
qsort(filenames, nfiles, sizeof(char *), cmp_string);
conn = PQconnectdb(conninfo);
if (PQstatus(conn) != CONNECTION_OK)
{
fprintf(stderr, "Connection to database failed: %s",
PQerrorMessage(conn));
exit(1);
}
printf("connected!\n");
res = PQexec(conn, "CREATE TEMP TABLE copytest (t text)");
if (PQresultStatus(res) != PGRES_COMMAND_OK)
{
printf("CREATE TABLE failed: %s\n", PQerrorMessage(conn));
exit(1);
}
PQclear(res);
for (int i = 0; i < nfiles; i++)
{
printf("file: %s\n", filenames[i]);
sendCopyFile(filenames[i], conn);
}
res = PQexec(conn, "select * from copytest");
if (PQresultStatus(res) != PGRES_TUPLES_OK)
{
printf("SELECT failed: %s\n", PQerrorMessage(conn));
exit(1);
}
printf("\ntable contents:\n");
/* next, print out the rows */
for (int i = 0; i < PQntuples(res); i++)
{
printf("%s\n", PQgetvalue(res, i, 0));
}
PQclear(res);
}
static void
sendCopyFile(char *path, PGconn *conn)
{
FILE *fp;
char buf[100];
int n;
bool OK = true;
PGresult *res;
res = PQexec(conn, "COPY copytest FROM STDIN");
if (PQresultStatus(res) != PGRES_COPY_IN)
{
fprintf(stderr, "COPY command failed: %s\n", PQerrorMessage(conn));
exit(1);
}
PQclear(res);
/*
* Send the file to the server.
*/
fp = fopen(path, "rb");
if (!fp)
{
fprintf(stderr, "could not open file \"%s\"\n", path);
exit(1);
}
for (;;)
{
n = fread(buf, READSIZE, 1, fp);
if (n == 0)
{
if (feof(fp))
break;
fprintf(stderr, "error reading file \"%s\"\n", path);
exit(1);
}
if (PQputCopyData(conn, buf, n) <= 0)
{
OK = false;
break;
}
}
fclose(fp);
if (PQputCopyEnd(conn, OK ? NULL : "aborted because of read failure") <= 0)
{
OK = false;
}
/*
* Check command status and return to normal libpq state.
*/
while (res = PQgetResult(conn), PQresultStatus(res) == PGRES_COPY_IN)
{
OK = false;
PQclear(res);
/* We can't send an error message if we're using protocol version 2 */
PQputCopyEnd(conn, "trying to exit copy mode");
}
if (PQresultStatus(res) != PGRES_COMMAND_OK)
{
char *f;
f = PQresultErrorField(res, PG_DIAG_MESSAGE_PRIMARY);
printf("COPY failed: %s\n", f ? f : "<no message>");
f = PQresultErrorField(res, PG_DIAG_MESSAGE_DETAIL);
if (f)
printf("DETAIL: %s\n", f);
f = PQresultErrorField(res, PG_DIAG_MESSAGE_HINT);
if (f)
printf("HINT: %s\n", f);
f = PQresultErrorField(res, PG_DIAG_CONTEXT);
if (f)
printf("CONTEXT: %s\n", f);
OK = false;
}
PQclear(res);
}