00001
00002
00003
00004
00005
00006
00007 #include <stdio.h>
00008 #include <stdlib.h>
00009 #include <libpq-fe.h>
00010 #include <qsqlquery.h>
00011 #include <qcstring.h>
00012 #include <qapplication.h>
00013 #include <kmdcodec.h>
00014 #include <qfile.h>
00015 #include "../global.h"
00016 #include "libpq/libpq-fs.h"
00017
00018
00019 void exit_nicely(PGconn *conn);
00020
00021 void writebuf(PGconn *conn, QByteArray b, int chunkno, char* fileno) {
00022 PGresult *ins;
00023 size_t len;
00024
00025 QCString b64=KCodecs::base64Encode(qCompress(b));
00026
00027 QFile f("/tmp/writebuf"+QString().setNum(chunkno));
00028 f.open(IO_WriteOnly);
00029 f.writeBlock(b);
00030 f.close();
00031
00032
00033 ins=PQexec(conn, "insert into file (chunk, partdata_no, chunk_no) values ('"+QCString(b64)+"', "+QCString(fileno)+", "+QCString().setNum(chunkno)+")");
00034 if (!ins || PQresultStatus(ins) != PGRES_COMMAND_OK) {
00035 fprintf(stderr, "res: command failed\n");
00036 PQclear(ins);
00037 exit_nicely(conn);
00038 }
00039 PQclear(ins);
00040 }
00041
00042 void
00043 exit_nicely(PGconn *conn)
00044 {
00045 PQfinish(conn);
00046 exit(1);
00047 }
00048
00049 main()
00050 {
00051 char *pghost,
00052 *pgport,
00053 *pgoptions,
00054 *pgtty;
00055 char *dbName;
00056 int nFields;
00057 int i,
00058 j;
00059
00060 PGconn *conn;
00061 PGresult *recs, *res;
00062
00063
00064 pghost = NULL;
00065 pgport = NULL;
00066 pgoptions = NULL;
00067
00068 pgtty = NULL;
00069 dbName = "kaspadev";
00070
00071
00072 conn = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName);
00073
00074
00075
00076
00077 if (PQstatus(conn) == CONNECTION_BAD)
00078 {
00079 fprintf(stderr, "Connection to database '%s' failed.\n", dbName);
00080 fprintf(stderr, "%s", PQerrorMessage(conn));
00081 exit_nicely(conn);
00082 }
00083
00084
00085
00086
00087
00088 recs = PQexec(conn, "select no, filename from partdata");
00089 if (!recs || PQresultStatus(recs) != PGRES_TUPLES_OK)
00090 {
00091 fprintf(stderr, "recs: command failed\n");
00092 PQclear(recs);
00093 exit_nicely(conn);
00094 }
00095
00096 for (i=0; i<PQntuples(recs); i++)
00097 {
00098 res=PQexec(conn, QCString("select file from partdata where no=")+PQgetvalue(recs, i, 0));
00099 if (!res || PQresultStatus(res) != PGRES_TUPLES_OK)
00100 {
00101 fprintf(stderr, "res: command failed\n");
00102 PQclear(res);
00103 exit_nicely(conn);
00104 }
00105 fprintf(stderr, "Processing file %i\n", i);
00106 QByteArray buf;
00107 KCodecs::base64Decode(QCString(PQgetvalue(res, 0, 0)), buf);
00108 PQclear(res);
00109
00110
00111 QFile f("/tmp/orig");
00112 f.open(IO_WriteOnly);
00113 f.writeBlock(buf);
00114 f.close();
00115
00116
00117 #define bufsize 1000000
00118 QByteArray chunk(bufsize);
00119 int j=0;
00120 while((j+1)*bufsize<buf.size()) {
00121 memcpy(chunk.data(), buf.data()+j*bufsize, bufsize);
00122 j++;
00123 fprintf(stderr, "Writing chunk %i\n", j);
00124 writebuf(conn, chunk, j, PQgetvalue(recs, i, 0));
00125 }
00126 chunk.resize(buf.size()-j*bufsize);
00127 memcpy(chunk.data(), buf.data()+j*bufsize, buf.size()-j*bufsize);
00128 writebuf(conn, chunk, j+1, PQgetvalue(recs, i, 0));
00129 }
00130
00131 PQfinish(conn);
00132
00133 return 0;
00134 }
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167