@@ -2126,6 +2126,7 @@ send_pages(ConnectionArgs* conn_arg, const char *to_fullpath, const char *from_f
2126
2126
BackupPageHeader2 *
2127
2127
get_data_file_headers (HeaderMap * hdr_map , pgFile * file , uint32 backup_version )
2128
2128
{
2129
+ FILE * in = NULL ;
2129
2130
size_t read_len = 0 ;
2130
2131
pg_crc32 hdr_crc ;
2131
2132
BackupPageHeader2 * headers = NULL ;
@@ -2140,35 +2141,15 @@ get_data_file_headers(HeaderMap *hdr_map, pgFile *file, uint32 backup_version)
2140
2141
if (file -> n_headers <= 0 )
2141
2142
return NULL ;
2142
2143
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));
2147
-
2148
- if (!hdr_map -> r_fp )
2149
- {
2150
- pthread_lock (& (hdr_map -> mutex ));
2151
-
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 ));
2144
+ /* TODO: consider to make this descriptor thread-specific */
2145
+ in = fopen (hdr_map -> path , PG_BINARY_R );
2161
2146
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
- }
2147
+ if (!in )
2148
+ elog (ERROR , "Cannot open header file \"%s\": %s" , hdr_map -> path , strerror (errno ));
2149
+ /* disable buffering for header file */
2150
+ setvbuf (in , NULL , _IONBF , BUFSIZ );
2170
2151
2171
- if (fseek (hdr_map -> r_fp , file -> hdr_off , SEEK_SET ))
2152
+ if (fseek (in , file -> hdr_off , SEEK_SET ))
2172
2153
elog (ERROR , "Cannot seek to position %lu in page header map \"%s\": %s" ,
2173
2154
file -> hdr_off , hdr_map -> path , strerror (errno ));
2174
2155
@@ -2184,7 +2165,7 @@ get_data_file_headers(HeaderMap *hdr_map, pgFile *file, uint32 backup_version)
2184
2165
zheaders = pgut_malloc (file -> hdr_size );
2185
2166
memset (zheaders , 0 , file -> hdr_size );
2186
2167
2187
- if (fread (zheaders , 1 , file -> hdr_size , hdr_map -> r_fp ) != file -> hdr_size )
2168
+ if (fread (zheaders , 1 , file -> hdr_size , in ) != file -> hdr_size )
2188
2169
elog (ERROR , "Cannot read header file at offset: %li len: %i \"%s\": %s" ,
2189
2170
file -> hdr_off , file -> hdr_size , hdr_map -> path , strerror (errno ));
2190
2171
@@ -2211,6 +2192,9 @@ get_data_file_headers(HeaderMap *hdr_map, pgFile *file, uint32 backup_version)
2211
2192
elog (ERROR , "Header map for file \"%s\" crc mismatch \"%s\" offset: %lu, len: %lu, current: %u, expected: %u" ,
2212
2193
file -> rel_path , hdr_map -> path , file -> hdr_off , read_len , hdr_crc , file -> hdr_crc );
2213
2194
2195
+ if (fclose (in ))
2196
+ elog (ERROR , "Cannot close file \"%s\"" , hdr_map -> path );
2197
+
2214
2198
pg_free (zheaders );
2215
2199
2216
2200
return headers ;
@@ -2231,22 +2215,35 @@ write_page_headers(BackupPageHeader2 *headers, pgFile *file, HeaderMap *hdr_map,
2231
2215
2232
2216
/* when running merge we must save headers into the temp map */
2233
2217
map_path = (is_merge ) ? hdr_map -> path_tmp : hdr_map -> path ;
2218
+ read_len = (file -> n_headers + 1 ) * sizeof (BackupPageHeader2 );
2219
+
2220
+ /* calculate checksums */
2221
+ INIT_FILE_CRC32 (true, file -> hdr_crc );
2222
+ COMP_FILE_CRC32 (true, file -> hdr_crc , headers , read_len );
2223
+ FIN_FILE_CRC32 (true, file -> hdr_crc );
2224
+
2225
+ zheaders = pgut_malloc (read_len * 2 );
2226
+ memset (zheaders , 0 , read_len * 2 );
2227
+
2228
+ /* compress headers */
2229
+ z_len = do_compress (zheaders , read_len * 2 , headers ,
2230
+ read_len , ZLIB_COMPRESS , 1 , & errormsg );
2234
2231
2235
2232
/* writing to header map must be serialized */
2236
2233
pthread_lock (& (hdr_map -> mutex )); /* what if we crash while trying to obtain mutex? */
2237
2234
2238
- if (!hdr_map -> w_fp )
2235
+ if (!hdr_map -> fp )
2239
2236
{
2240
2237
elog (LOG , "Creating page header map \"%s\"" , map_path );
2241
2238
2242
- hdr_map -> w_fp = fopen (map_path , PG_BINARY_W );
2243
- if (hdr_map -> w_fp == NULL )
2239
+ hdr_map -> fp = fopen (map_path , PG_BINARY_W );
2240
+ if (hdr_map -> fp == NULL )
2244
2241
elog (ERROR , "Cannot open header file \"%s\": %s" ,
2245
2242
map_path , strerror (errno ));
2246
2243
2247
2244
/* enable buffering for header file */
2248
- hdr_map -> w_buf = pgut_malloc (LARGE_CHUNK_SIZE );
2249
- setvbuf (hdr_map -> w_fp , hdr_map -> w_buf , _IOFBF , LARGE_CHUNK_SIZE );
2245
+ hdr_map -> buf = pgut_malloc (LARGE_CHUNK_SIZE );
2246
+ setvbuf (hdr_map -> fp , hdr_map -> buf , _IOFBF , LARGE_CHUNK_SIZE );
2250
2247
2251
2248
/* update file permission */
2252
2249
if (chmod (map_path , FILE_PERMISSION ) == -1 )
@@ -2256,20 +2253,7 @@ write_page_headers(BackupPageHeader2 *headers, pgFile *file, HeaderMap *hdr_map,
2256
2253
file -> hdr_off = 0 ;
2257
2254
}
2258
2255
else
2259
- file -> hdr_off = ftell (hdr_map -> w_fp ); /* TODO: replace by counter */
2260
-
2261
- read_len = (file -> n_headers + 1 ) * sizeof (BackupPageHeader2 );
2262
-
2263
- /* calculate checksums */
2264
- INIT_FILE_CRC32 (true, file -> hdr_crc );
2265
- COMP_FILE_CRC32 (true, file -> hdr_crc , headers , read_len );
2266
- FIN_FILE_CRC32 (true, file -> hdr_crc );
2267
-
2268
- zheaders = pgut_malloc (read_len * 2 );
2269
- memset (zheaders , 0 , read_len * 2 );
2270
-
2271
- z_len = do_compress (zheaders , read_len * 2 , headers ,
2272
- read_len , ZLIB_COMPRESS , 1 , & errormsg );
2256
+ file -> hdr_off = hdr_map -> offset ;
2273
2257
2274
2258
if (z_len <= 0 )
2275
2259
{
@@ -2281,15 +2265,14 @@ write_page_headers(BackupPageHeader2 *headers, pgFile *file, HeaderMap *hdr_map,
2281
2265
file -> rel_path , z_len );
2282
2266
}
2283
2267
2284
- if (fwrite (zheaders , 1 , z_len , hdr_map -> w_fp ) != z_len )
2285
- elog (ERROR , "Cannot write to file \"%s\": %s" , map_path , strerror (errno ));
2286
-
2287
- elog (VERBOSE , "Writing header map for file \"%s\" offset: %li, len: %i, crc: %u" ,
2268
+ elog (VERBOSE , "Writing headers for file \"%s\" offset: %li, len: %i, crc: %u" ,
2288
2269
file -> rel_path , file -> hdr_off , z_len , file -> hdr_crc );
2289
2270
2290
- elog (INFO , "File: %s, Unzip: %li, zip: %i" , file -> rel_path , read_len , z_len );
2271
+ if (fwrite (zheaders , 1 , z_len , hdr_map -> fp ) != z_len )
2272
+ elog (ERROR , "Cannot write to file \"%s\": %s" , map_path , strerror (errno ));
2291
2273
2292
- file -> hdr_size = z_len ;
2274
+ file -> hdr_size = z_len ; /* save the length of compressed headers */
2275
+ hdr_map -> offset += z_len ; /* update current offset in map */
2293
2276
2294
2277
/* End critical section */
2295
2278
pthread_mutex_unlock (& (hdr_map -> mutex ));
@@ -2300,10 +2283,8 @@ write_page_headers(BackupPageHeader2 *headers, pgFile *file, HeaderMap *hdr_map,
2300
2283
void
2301
2284
init_header_map (pgBackup * backup )
2302
2285
{
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 ;
2286
+ backup -> hdr_map .fp = NULL ;
2287
+ backup -> hdr_map .buf = NULL ;
2307
2288
join_path_components (backup -> hdr_map .path , backup -> root_dir , HEADER_MAP );
2308
2289
join_path_components (backup -> hdr_map .path_tmp , backup -> root_dir , HEADER_MAP_TMP );
2309
2290
backup -> hdr_map .mutex = (pthread_mutex_t )PTHREAD_MUTEX_INITIALIZER ;
@@ -2312,22 +2293,11 @@ init_header_map(pgBackup *backup)
2312
2293
void
2313
2294
cleanup_header_map (HeaderMap * hdr_map )
2314
2295
{
2315
-
2316
- /* cleanup read descriptor */
2317
- if (hdr_map -> r_fp && fclose (hdr_map -> r_fp ))
2296
+ /* cleanup descriptor */
2297
+ if (hdr_map -> fp && fclose (hdr_map -> fp ))
2318
2298
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 ;
2299
+ hdr_map -> fp = NULL ;
2300
+ hdr_map -> offset = 0 ;
2301
+ pg_free (hdr_map -> buf );
2302
+ hdr_map -> buf = NULL ;
2333
2303
}
0 commit comments