@@ -2127,10 +2127,10 @@ BackupPageHeader2*
21272127get_data_file_headers (HeaderMap * hdr_map , pgFile * file , uint32 backup_version )
21282128{
21292129 size_t read_len = 0 ;
2130- FILE * in = NULL ;
21312130 pg_crc32 hdr_crc ;
21322131 BackupPageHeader2 * headers = NULL ;
21332132 /* header decompression */
2133+ int z_len = 0 ;
21342134 char * zheaders = NULL ;
21352135 const char * errormsg = NULL ;
21362136
@@ -2140,16 +2140,36 @@ get_data_file_headers(HeaderMap *hdr_map, pgFile *file, uint32 backup_version)
21402140 if (file -> n_headers <= 0 )
21412141 return NULL ;
21422142
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));
21442147
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 ));
21472151
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 ));
21502161
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" ,
21532173 file -> hdr_off , hdr_map -> path , strerror (errno ));
21542174
21552175 /*
@@ -2164,21 +2184,22 @@ get_data_file_headers(HeaderMap *hdr_map, pgFile *file, uint32 backup_version)
21642184 zheaders = pgut_malloc (file -> hdr_size );
21652185 memset (zheaders , 0 , file -> hdr_size );
21662186
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 )
21682188 elog (ERROR , "Cannot read header file at offset: %li len: %i \"%s\": %s" ,
21692189 file -> hdr_off , file -> hdr_size , hdr_map -> path , strerror (errno ));
21702190
21712191// elog(INFO, "zsize: %i, size: %i", file->hdr_size, read_len);
21722192
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 )
21752196 {
21762197 if (errormsg )
21772198 elog (ERROR , "An error occured during metadata decompression for file \"%s\": %s" ,
21782199 file -> rel_path , errormsg );
21792200 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 );
21822203 }
21832204
21842205 /* validate checksum */
@@ -2190,9 +2211,6 @@ get_data_file_headers(HeaderMap *hdr_map, pgFile *file, uint32 backup_version)
21902211 elog (ERROR , "Header map for file \"%s\" crc mismatch \"%s\" offset: %lu, len: %lu, current: %u, expected: %u" ,
21912212 file -> rel_path , hdr_map -> path , file -> hdr_off , read_len , hdr_crc , file -> hdr_crc );
21922213
2193- if (fclose (in ))
2194- elog (ERROR , "Cannot close header file \"%s\": %s" , hdr_map -> path , strerror (errno ));
2195-
21962214 pg_free (zheaders );
21972215
21982216 return headers ;
@@ -2217,18 +2235,18 @@ write_page_headers(BackupPageHeader2 *headers, pgFile *file, HeaderMap *hdr_map,
22172235 /* writing to header map must be serialized */
22182236 pthread_lock (& (hdr_map -> mutex )); /* what if we crash while trying to obtain mutex? */
22192237
2220- if (!hdr_map -> fp )
2238+ if (!hdr_map -> w_fp )
22212239 {
22222240 elog (LOG , "Creating page header map \"%s\"" , map_path );
22232241
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 )
22262244 elog (ERROR , "Cannot open header file \"%s\": %s" ,
22272245 map_path , strerror (errno ));
22282246
22292247 /* 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 );
22322250
22332251 /* update file permission */
22342252 if (chmod (map_path , FILE_PERMISSION ) == -1 )
@@ -2238,7 +2256,7 @@ write_page_headers(BackupPageHeader2 *headers, pgFile *file, HeaderMap *hdr_map,
22382256 file -> hdr_off = 0 ;
22392257 }
22402258 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 */
22422260
22432261 read_len = (file -> n_headers + 1 ) * sizeof (BackupPageHeader2 );
22442262
@@ -2253,7 +2271,7 @@ write_page_headers(BackupPageHeader2 *headers, pgFile *file, HeaderMap *hdr_map,
22532271 z_len = do_compress (zheaders , read_len * 2 , headers ,
22542272 read_len , ZLIB_COMPRESS , 1 , & errormsg );
22552273
2256- if (z_len < 0 )
2274+ if (z_len <= 0 )
22572275 {
22582276 if (errormsg )
22592277 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,
22632281 file -> rel_path , z_len );
22642282 }
22652283
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 )
22672285 elog (ERROR , "Cannot write to file \"%s\": %s" , map_path , strerror (errno ));
22682286
22692287 elog (VERBOSE , "Writing header map for file \"%s\" offset: %li, len: %i, crc: %u" ,
22702288 file -> rel_path , file -> hdr_off , z_len , file -> hdr_crc );
22712289
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 );
22732291
22742292 file -> hdr_size = z_len ;
22752293
@@ -2278,3 +2296,38 @@ write_page_headers(BackupPageHeader2 *headers, pgFile *file, HeaderMap *hdr_map,
22782296
22792297 pg_free (zheaders );
22802298}
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