diff options
Diffstat (limited to 'contrib/pg_dumplo/lo_export.c')
-rw-r--r-- | contrib/pg_dumplo/lo_export.c | 172 |
1 files changed, 95 insertions, 77 deletions
diff --git a/contrib/pg_dumplo/lo_export.c b/contrib/pg_dumplo/lo_export.c index 5a2a1614a57..6a62c88bc6f 100644 --- a/contrib/pg_dumplo/lo_export.c +++ b/contrib/pg_dumplo/lo_export.c @@ -1,20 +1,20 @@ /* ------------------------------------------------------------------------- * pg_dumplo * - * $Header: /cvsroot/pgsql/contrib/pg_dumplo/Attic/lo_export.c,v 1.5 2001/01/24 19:42:44 momjian Exp $ + * $Header: /cvsroot/pgsql/contrib/pg_dumplo/Attic/lo_export.c,v 1.6 2001/03/22 03:59:10 momjian Exp $ * * Karel Zak 1999-2000 * ------------------------------------------------------------------------- */ -#include <stdio.h> +#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <string.h> #include <sys/stat.h> #include <sys/types.h> #include <fcntl.h> -#include <errno.h> +#include <errno.h> #include <time.h> #include <libpq-fe.h> @@ -22,17 +22,17 @@ #include "pg_dumplo.h" -extern int errno; +extern int errno; -void -load_lolist( LODumpMaster *pgLO ) +void +load_lolist(LODumpMaster * pgLO) { - LOlist *ll; - int i; - int n; + LOlist *ll; + int i; + int n; - /* ---------- + /* ---------- * Now find any candidate tables who have columns of type oid. * * NOTE: System tables including pg_largeobject will be ignored. @@ -40,8 +40,8 @@ load_lolist( LODumpMaster *pgLO ) * * NOTE: the system oid column is ignored, as it has attnum < 1. * This shouldn't matter for correctness, but it saves time. - * ---------- - */ + * ---------- + */ pgLO->res = PQexec(pgLO->conn, "SELECT c.relname, a.attname " "FROM pg_class c, pg_attribute a, pg_type t " @@ -52,43 +52,49 @@ load_lolist( LODumpMaster *pgLO ) " AND c.relkind = 'r' " " AND c.relname NOT LIKE 'pg_%'"); - if (PQresultStatus(pgLO->res) != PGRES_TUPLES_OK) { + if (PQresultStatus(pgLO->res) != PGRES_TUPLES_OK) + { fprintf(stderr, "%s: Failed to get LO OIDs:\n%s", progname, PQerrorMessage(pgLO->conn)); exit(RE_ERROR); } - if ((n = PQntuples(pgLO->res)) == 0) { + if ((n = PQntuples(pgLO->res)) == 0) + { fprintf(stderr, "%s: No OID columns in the database.\n", progname); exit(RE_ERROR); - } - + } + pgLO->lolist = (LOlist *) malloc((n + 1) * sizeof(LOlist)); - - if (!pgLO->lolist) { + + if (!pgLO->lolist) + { fprintf(stderr, "%s: can't allocate memory\n", progname); exit(RE_ERROR); - } - - for (i = 0, ll = pgLO->lolist; i < n; i++, ll++) { + } + + for (i = 0, ll = pgLO->lolist; i < n; i++, ll++) + { ll->lo_table = strdup(PQgetvalue(pgLO->res, i, 0)); - ll->lo_attr = strdup(PQgetvalue(pgLO->res, i, 1)); + ll->lo_attr = strdup(PQgetvalue(pgLO->res, i, 1)); } - ll->lo_table = ll->lo_attr = (char *) NULL; - + ll->lo_table = ll->lo_attr = (char *) NULL; + PQclear(pgLO->res); } -void -pglo_export(LODumpMaster *pgLO) +void +pglo_export(LODumpMaster * pgLO) { - LOlist *ll; - int tuples; + LOlist *ll; + int tuples; char path[BUFSIZ], - Qbuff[QUERY_BUFSIZ]; - - if (pgLO->action != ACTION_SHOW) { - time_t t; + Qbuff[QUERY_BUFSIZ]; + + if (pgLO->action != ACTION_SHOW) + { + time_t t; + time(&t); fprintf(pgLO->index, "#\n# This is the PostgreSQL large object dump index\n#\n"); fprintf(pgLO->index, "#\tDate: %s", ctime(&t)); @@ -97,90 +103,102 @@ pglo_export(LODumpMaster *pgLO) fprintf(pgLO->index, "#\tUser: %s\n", pgLO->user); fprintf(pgLO->index, "#\n# oid\ttable\tattribut\tinfile\n#\n"); } - + pgLO->counter = 0; - for(ll=pgLO->lolist; ll->lo_table != NULL; ll++) { - + for (ll = pgLO->lolist; ll->lo_table != NULL; ll++) + { + /* ---------- * Query: find the LOs referenced by this column * ---------- */ sprintf(Qbuff, "SELECT DISTINCT l.loid FROM \"%s\" x, pg_largeobject l WHERE x.\"%s\" = l.loid", ll->lo_table, ll->lo_attr); - + /* puts(Qbuff); */ - + pgLO->res = PQexec(pgLO->conn, Qbuff); - if (PQresultStatus(pgLO->res) != PGRES_TUPLES_OK) { + if (PQresultStatus(pgLO->res) != PGRES_TUPLES_OK) + { fprintf(stderr, "%s: Failed to get LO OIDs:\n%s", progname, PQerrorMessage(pgLO->conn)); - } - else if ((tuples = PQntuples(pgLO->res)) == 0) { + } + else if ((tuples = PQntuples(pgLO->res)) == 0) + { if (!pgLO->quiet && pgLO->action == ACTION_EXPORT_ATTR) printf("%s: no large objects in \"%s\".\"%s\"\n", - progname, ll->lo_table, ll->lo_attr); - } else { - - int t; - char *val; - + progname, ll->lo_table, ll->lo_attr); + } + else + { + + int t; + char *val; + /* ---------- * Create DIR/FILE * ---------- */ - if (pgLO->action != ACTION_SHOW) { - + if (pgLO->action != ACTION_SHOW) + { + sprintf(path, "%s/%s/%s", pgLO->space, pgLO->db, ll->lo_table); - if (mkdir(path, DIR_UMASK) == -1) { - if (errno != EEXIST) { + if (mkdir(path, DIR_UMASK) == -1) + { + if (errno != EEXIST) + { perror(path); - exit(RE_ERROR); - } + exit(RE_ERROR); + } } sprintf(path, "%s/%s/%s/%s", pgLO->space, pgLO->db, - ll->lo_table, ll->lo_attr); - - if (mkdir(path, DIR_UMASK) == -1) { - if (errno != EEXIST) { + ll->lo_table, ll->lo_attr); + + if (mkdir(path, DIR_UMASK) == -1) + { + if (errno != EEXIST) + { perror(path); - exit(RE_ERROR); - } + exit(RE_ERROR); + } } - + if (!pgLO->quiet) - printf("dump %s.%s (%d large obj)\n", - ll->lo_table, ll->lo_attr, tuples); + printf("dump %s.%s (%d large obj)\n", + ll->lo_table, ll->lo_attr, tuples); } pgLO->counter += tuples; - - for(t=0; t<tuples; t++) { - Oid lo; - + + for (t = 0; t < tuples; t++) + { + Oid lo; + val = PQgetvalue(pgLO->res, t, 0); - + lo = atooid(val); - - if (pgLO->action == ACTION_SHOW) { + + if (pgLO->action == ACTION_SHOW) + { printf("%s.%s: %u\n", ll->lo_table, ll->lo_attr, lo); continue; } - - sprintf(path, "%s/%s/%s/%s/%s", pgLO->space, - pgLO->db, ll->lo_table, ll->lo_attr, val); - - if (lo_export(pgLO->conn, lo, path) < 0) + + sprintf(path, "%s/%s/%s/%s/%s", pgLO->space, + pgLO->db, ll->lo_table, ll->lo_attr, val); + + if (lo_export(pgLO->conn, lo, path) < 0) fprintf(stderr, "%s: lo_export failed:\n%s", progname, PQerrorMessage(pgLO->conn)); - - else - fprintf(pgLO->index, "%s\t%s\t%s\t%s/%s/%s/%s\n", val, - ll->lo_table, ll->lo_attr, pgLO->db, ll->lo_table, ll->lo_attr, val); + + else + fprintf(pgLO->index, "%s\t%s\t%s\t%s/%s/%s/%s\n", val, + ll->lo_table, ll->lo_attr, pgLO->db, ll->lo_table, ll->lo_attr, val); } } |