aboutsummaryrefslogtreecommitdiff
path: root/contrib/pg_dumplo/lo_export.c
blob: 80d401902c1721b02126eb3c0342546488bd1596 (plain)
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
#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 <time.h>

#include <libpq-fe.h>
#include <libpq/libpq-fs.h>

#include "pg_dumplo.h"

extern int 	errno;        

#define LOAD_LOLIST_QUERY "\
	SELECT c.relname, a.attname \
	FROM pg_class c, pg_attribute a, pg_type t \
	WHERE a.attnum > 0 \
		AND a.attrelid = c.oid \
		AND a.atttypid = t.oid \
		AND t.typname = 'oid' \
		AND c.relname NOT LIKE 'pg_%'"


void 
load_lolist( LODumpMaster *pgLO ) 
{
	LOlist		*ll;
	int		i;
	int		n;

 	/* ----------
 	 * Now find any candidate tables who have columns of type oid (the
   	 * column oid is ignored, as it has attnum < 1)
 	 * ----------
 	 */	
	if (!(pgLO->res = PQexec(pgLO->conn, LOAD_LOLIST_QUERY))) {
		
		fprintf(stderr, "%s: Select from pg_class failed.\n", progname);
    		exit(RE_ERROR);
  	}
	
	if ((n = PQntuples(pgLO->res)) == 0) {
		
		fprintf(stderr, "%s: No large objects in the database.\n", progname);
		exit(RE_ERROR);
 	}
	
	pgLO->lolist = (LOlist *) malloc((n + 1) * sizeof(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++) {
		ll->lo_table = strdup(PQgetvalue(pgLO->res, i, 0));
		ll->lo_attr  = strdup(PQgetvalue(pgLO->res, i, 1));
	}
	
	PQclear(pgLO->res);
 	ll++;
 	ll->lo_table = ll->lo_attr = (char *) NULL;
}

void 
pglo_export(LODumpMaster *pgLO)
{
	LOlist		*ll;
	int		tuples;
	char		path[BUFSIZ],
			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));
		fprintf(pgLO->index, "#\tHost:     %s\n", pgLO->host);
		fprintf(pgLO->index, "#\tDatabase: %s\n", pgLO->db);
		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++) {
		
		/* ----------
		 * Query
		 * ----------
		 */
		sprintf(Qbuff, "SELECT x.%s FROM %s x, pg_class c WHERE x.%s = c.oid and c.relkind = 'l'", 
			ll->lo_attr, ll->lo_table, ll->lo_attr);
		
		/* puts(Qbuff); */
			
		pgLO->res = PQexec(pgLO->conn, Qbuff);
	
		if ((tuples = PQntuples(pgLO->res)) == 0) {
		
			if (!pgLO->quiet && pgLO->action == ACTION_EXPORT_ATTR)
				printf("%s: not large objets in '%s'\n", progname, ll->lo_table);	
			continue;
		
		} else if (check_res(pgLO)) {
		
			int	t;
			char	*val;
	
			/* ----------
			 * Create DIR/FILE
			 * ----------
			 */
			if (tuples && 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) {
						perror(path);
						exit(RE_ERROR);					
					}	
				}
				
				sprintf(path, "%s/%s", path, ll->lo_attr);          
				
				if (mkdir(path, DIR_UMASK) == -1) {
					if (errno != EEXIST) {
						perror(path);
						exit(RE_ERROR);					
					}	
				}
				
				if (!pgLO->quiet)
					printf("dump %s.%s (%d lagre obj)\n", 
						ll->lo_table, ll->lo_attr, tuples);
			}

			pgLO->counter += tuples;
			
			for(t=0; t<tuples; t++) {
				
				Oid lo = (Oid) 0;
				
				val = PQgetvalue(pgLO->res, t, 0);
				
				if (!val)
					continue;
				else
					lo = (Oid) atol(val);
				
				if (pgLO->action == ACTION_SHOW) {
					printf("%s.%s: %ld\n", ll->lo_table, 
						ll->lo_attr, (long) 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) 
					fprintf(stderr, "%s: %s\n", PQerrorMessage(pgLO->conn), progname);
					
				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);
			}
		}
	}	
 }