@@ -2127,10 +2127,10 @@ BackupPageHeader2*
2127
2127
get_data_file_headers (HeaderMap * hdr_map , pgFile * file , uint32 backup_version )
2128
2128
{
2129
2129
size_t read_len = 0 ;
2130
- FILE * in = NULL ;
2131
2130
pg_crc32 hdr_crc ;
2132
2131
BackupPageHeader2 * headers = NULL ;
2133
2132
/* header decompression */
2133
+ int z_len = 0 ;
2134
2134
char * zheaders = NULL ;
2135
2135
const char * errormsg = NULL ;
2136
2136
@@ -2140,16 +2140,36 @@ get_data_file_headers(HeaderMap *hdr_map, pgFile *file, uint32 backup_version)
2140
2140
if (file -> n_headers <= 0 )
2141
2141
return NULL ;
2142
2142
2143
- in = fopen (hdr_map -> path , PG_BINARY_R );
2143
+ // in = fopen(hdr_map->path, PG_BINARY_R);
2144
+ //
2145
+ // if (!in)
2146
+ // elog(ERROR, "Cannot open header file \"%s\": %s", hdr_map->path, strerror(errno));
2144
2147
2145
- if (!in )
2146
- elog (ERROR , "Cannot open header file \"%s\": %s" , hdr_map -> path , strerror (errno ));
2148
+ if (!hdr_map -> r_fp )
2149
+ {
2150
+ pthread_lock (& (hdr_map -> mutex ));
2147
2151
2148
- /* disable buffering */
2149
- setvbuf (in , NULL , _IONBF , BUFSIZ );
2152
+ /* it is possible for another contender got here first, so double check */
2153
+ if (!hdr_map -> r_fp ) /* this file will be closed in restore.c and merge.c */
2154
+ {
2155
+ elog (LOG , "Opening page header map \"%s\"" , hdr_map -> path );
2156
+
2157
+ hdr_map -> r_fp = fopen (hdr_map -> path , PG_BINARY_R );
2158
+ if (hdr_map -> r_fp == NULL )
2159
+ elog (ERROR , "Cannot open header file \"%s\": %s" ,
2160
+ hdr_map -> path , strerror (errno ));
2150
2161
2151
- if (fseek (in , file -> hdr_off , SEEK_SET ))
2152
- elog (ERROR , "Cannot seek to position %lu in header map \"%s\": %s" ,
2162
+ /* enable buffering for header file */
2163
+ hdr_map -> r_buf = pgut_malloc (LARGE_CHUNK_SIZE );
2164
+ setvbuf (hdr_map -> r_fp , hdr_map -> r_buf , _IOFBF , LARGE_CHUNK_SIZE );
2165
+ }
2166
+
2167
+ /* End critical section */
2168
+ pthread_mutex_unlock (& (hdr_map -> mutex ));
2169
+ }
2170
+
2171
+ if (fseek (hdr_map -> r_fp , file -> hdr_off , SEEK_SET ))
2172
+ elog (ERROR , "Cannot seek to position %lu in page header map \"%s\": %s" ,
2153
2173
file -> hdr_off , hdr_map -> path , strerror (errno ));
2154
2174
2155
2175
/*
@@ -2164,21 +2184,22 @@ get_data_file_headers(HeaderMap *hdr_map, pgFile *file, uint32 backup_version)
2164
2184
zheaders = pgut_malloc (file -> hdr_size );
2165
2185
memset (zheaders , 0 , file -> hdr_size );
2166
2186
2167
- if (fread (zheaders , 1 , file -> hdr_size , in ) != file -> hdr_size )
2187
+ if (fread (zheaders , 1 , file -> hdr_size , hdr_map -> r_fp ) != file -> hdr_size )
2168
2188
elog (ERROR , "Cannot read header file at offset: %li len: %i \"%s\": %s" ,
2169
2189
file -> hdr_off , file -> hdr_size , hdr_map -> path , strerror (errno ));
2170
2190
2171
2191
// elog(INFO, "zsize: %i, size: %i", file->hdr_size, read_len);
2172
2192
2173
- if (do_decompress (headers , read_len , zheaders , file -> hdr_size ,
2174
- ZLIB_COMPRESS , & errormsg ) != read_len )
2193
+ z_len = do_decompress (headers , read_len , zheaders , file -> hdr_size ,
2194
+ ZLIB_COMPRESS , & errormsg );
2195
+ if (z_len <= 0 )
2175
2196
{
2176
2197
if (errormsg )
2177
2198
elog (ERROR , "An error occured during metadata decompression for file \"%s\": %s" ,
2178
2199
file -> rel_path , errormsg );
2179
2200
else
2180
- elog (ERROR , "An error occured during metadata decompression for file \"%s\"" ,
2181
- file -> rel_path );
2201
+ elog (ERROR , "An error occured during metadata decompression for file \"%s\": %i " ,
2202
+ file -> rel_path , z_len );
2182
2203
}
2183
2204
2184
2205
/* validate checksum */
@@ -2190,9 +2211,6 @@ get_data_file_headers(HeaderMap *hdr_map, pgFile *file, uint32 backup_version)
2190
2211
elog (ERROR , "Header map for file \"%s\" crc mismatch \"%s\" offset: %lu, len: %lu, current: %u, expected: %u" ,
2191
2212
file -> rel_path , hdr_map -> path , file -> hdr_off , read_len , hdr_crc , file -> hdr_crc );
2192
2213
2193
- if (fclose (in ))
2194
- elog (ERROR , "Cannot close header file \"%s\": %s" , hdr_map -> path , strerror (errno ));
2195
-
2196
2214
pg_free (zheaders );
2197
2215
2198
2216
return headers ;
@@ -2217,18 +2235,18 @@ write_page_headers(BackupPageHeader2 *headers, pgFile *file, HeaderMap *hdr_map,
2217
2235
/* writing to header map must be serialized */
2218
2236
pthread_lock (& (hdr_map -> mutex )); /* what if we crash while trying to obtain mutex? */
2219
2237
2220
- if (!hdr_map -> fp )
2238
+ if (!hdr_map -> w_fp )
2221
2239
{
2222
2240
elog (LOG , "Creating page header map \"%s\"" , map_path );
2223
2241
2224
- hdr_map -> fp = fopen (map_path , PG_BINARY_W );
2225
- if (hdr_map -> fp == NULL )
2242
+ hdr_map -> w_fp = fopen (map_path , PG_BINARY_W );
2243
+ if (hdr_map -> w_fp == NULL )
2226
2244
elog (ERROR , "Cannot open header file \"%s\": %s" ,
2227
2245
map_path , strerror (errno ));
2228
2246
2229
2247
/* enable buffering for header file */
2230
- hdr_map -> buf = pgut_malloc (STDIO_BUFSIZE );
2231
- setvbuf (hdr_map -> fp , hdr_map -> buf , _IOFBF , STDIO_BUFSIZE );
2248
+ hdr_map -> w_buf = pgut_malloc (LARGE_CHUNK_SIZE );
2249
+ setvbuf (hdr_map -> w_fp , hdr_map -> w_buf , _IOFBF , LARGE_CHUNK_SIZE );
2232
2250
2233
2251
/* update file permission */
2234
2252
if (chmod (map_path , FILE_PERMISSION ) == -1 )
@@ -2238,7 +2256,7 @@ write_page_headers(BackupPageHeader2 *headers, pgFile *file, HeaderMap *hdr_map,
2238
2256
file -> hdr_off = 0 ;
2239
2257
}
2240
2258
else
2241
- file -> hdr_off = ftell (hdr_map -> fp ); /* TODO: replace by counter */
2259
+ file -> hdr_off = ftell (hdr_map -> w_fp ); /* TODO: replace by counter */
2242
2260
2243
2261
read_len = (file -> n_headers + 1 ) * sizeof (BackupPageHeader2 );
2244
2262
@@ -2253,7 +2271,7 @@ write_page_headers(BackupPageHeader2 *headers, pgFile *file, HeaderMap *hdr_map,
2253
2271
z_len = do_compress (zheaders , read_len * 2 , headers ,
2254
2272
read_len , ZLIB_COMPRESS , 1 , & errormsg );
2255
2273
2256
- if (z_len < 0 )
2274
+ if (z_len <= 0 )
2257
2275
{
2258
2276
if (errormsg )
2259
2277
elog (ERROR , "An error occured during compressing metadata for file \"%s\": %s" ,
@@ -2263,13 +2281,13 @@ write_page_headers(BackupPageHeader2 *headers, pgFile *file, HeaderMap *hdr_map,
2263
2281
file -> rel_path , z_len );
2264
2282
}
2265
2283
2266
- if (fwrite (zheaders , 1 , z_len , hdr_map -> fp ) != z_len )
2284
+ if (fwrite (zheaders , 1 , z_len , hdr_map -> w_fp ) != z_len )
2267
2285
elog (ERROR , "Cannot write to file \"%s\": %s" , map_path , strerror (errno ));
2268
2286
2269
2287
elog (VERBOSE , "Writing header map for file \"%s\" offset: %li, len: %i, crc: %u" ,
2270
2288
file -> rel_path , file -> hdr_off , z_len , file -> hdr_crc );
2271
2289
2272
- // elog(INFO, "File: %s, Unzip: %i , zip: %i", file->rel_path, read_len, z_len);
2290
+ elog (INFO , "File: %s, Unzip: %li , zip: %i" , file -> rel_path , read_len , z_len );
2273
2291
2274
2292
file -> hdr_size = z_len ;
2275
2293
@@ -2278,3 +2296,38 @@ write_page_headers(BackupPageHeader2 *headers, pgFile *file, HeaderMap *hdr_map,
2278
2296
2279
2297
pg_free (zheaders );
2280
2298
}
2299
+
2300
+ void
2301
+ init_header_map (pgBackup * backup )
2302
+ {
2303
+ backup -> hdr_map .r_fp = NULL ;
2304
+ backup -> hdr_map .w_fp = NULL ;
2305
+ backup -> hdr_map .r_buf = NULL ;
2306
+ backup -> hdr_map .w_buf = NULL ;
2307
+ join_path_components (backup -> hdr_map .path , backup -> root_dir , HEADER_MAP );
2308
+ join_path_components (backup -> hdr_map .path_tmp , backup -> root_dir , HEADER_MAP_TMP );
2309
+ backup -> hdr_map .mutex = (pthread_mutex_t )PTHREAD_MUTEX_INITIALIZER ;
2310
+ }
2311
+
2312
+ void
2313
+ cleanup_header_map (HeaderMap * hdr_map )
2314
+ {
2315
+
2316
+ /* cleanup read descriptor */
2317
+ if (hdr_map -> r_fp && fclose (hdr_map -> r_fp ))
2318
+ elog (ERROR , "Cannot close file \"%s\"" , hdr_map -> path );
2319
+
2320
+ hdr_map -> r_fp = NULL ;
2321
+ pg_free (hdr_map -> r_buf );
2322
+ hdr_map -> r_buf = NULL ;
2323
+ hdr_map -> r_offset = 0 ;
2324
+
2325
+ /* cleanup write descriptor */
2326
+ if (hdr_map -> w_fp && fclose (hdr_map -> w_fp ))
2327
+ elog (ERROR , "Cannot close file \"%s\"" , hdr_map -> path );
2328
+
2329
+ hdr_map -> w_fp = NULL ;
2330
+ pg_free (hdr_map -> w_buf );
2331
+ hdr_map -> w_buf = NULL ;
2332
+ hdr_map -> w_offset = 0 ;
2333
+ }
0 commit comments