@@ -2126,6 +2126,7 @@ send_pages(ConnectionArgs* conn_arg, const char *to_fullpath, const char *from_f
21262126BackupPageHeader2 *
21272127get_data_file_headers (HeaderMap * hdr_map ,pgFile * file ,uint32 backup_version )
21282128{
2129+ FILE * in = NULL ;
21292130size_t read_len = 0 ;
21302131pg_crc32 hdr_crc ;
21312132BackupPageHeader2 * headers = NULL ;
@@ -2140,35 +2141,15 @@ get_data_file_headers(HeaderMap *hdr_map, pgFile *file, uint32 backup_version)
21402141if (file -> n_headers <=0 )
21412142return NULL ;
21422143
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 );
21612146
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 );
21702151
2171- if (fseek (hdr_map -> r_fp ,file -> hdr_off ,SEEK_SET ))
2152+ if (fseek (in ,file -> hdr_off ,SEEK_SET ))
21722153elog (ERROR ,"Cannot seek to position %lu in page header map \"%s\": %s" ,
21732154file -> hdr_off ,hdr_map -> path ,strerror (errno ));
21742155
@@ -2184,7 +2165,7 @@ get_data_file_headers(HeaderMap *hdr_map, pgFile *file, uint32 backup_version)
21842165zheaders = pgut_malloc (file -> hdr_size );
21852166memset (zheaders ,0 ,file -> hdr_size );
21862167
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 )
21882169elog (ERROR ,"Cannot read header file at offset: %li len: %i \"%s\": %s" ,
21892170file -> hdr_off ,file -> hdr_size ,hdr_map -> path ,strerror (errno ));
21902171
@@ -2211,6 +2192,9 @@ get_data_file_headers(HeaderMap *hdr_map, pgFile *file, uint32 backup_version)
22112192elog (ERROR ,"Header map for file \"%s\" crc mismatch \"%s\" offset: %lu, len: %lu, current: %u, expected: %u" ,
22122193file -> rel_path ,hdr_map -> path ,file -> hdr_off ,read_len ,hdr_crc ,file -> hdr_crc );
22132194
2195+ if (fclose (in ))
2196+ elog (ERROR ,"Cannot close file \"%s\"" ,hdr_map -> path );
2197+
22142198pg_free (zheaders );
22152199
22162200return headers ;
@@ -2231,22 +2215,35 @@ write_page_headers(BackupPageHeader2 *headers, pgFile *file, HeaderMap *hdr_map,
22312215
22322216/* when running merge we must save headers into the temp map */
22332217map_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 );
22342231
22352232/* writing to header map must be serialized */
22362233pthread_lock (& (hdr_map -> mutex ));/* what if we crash while trying to obtain mutex? */
22372234
2238- if (!hdr_map -> w_fp )
2235+ if (!hdr_map -> fp )
22392236{
22402237elog (LOG ,"Creating page header map \"%s\"" ,map_path );
22412238
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 )
22442241elog (ERROR ,"Cannot open header file \"%s\": %s" ,
22452242map_path ,strerror (errno ));
22462243
22472244/* 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 );
22502247
22512248/* update file permission */
22522249if (chmod (map_path ,FILE_PERMISSION )== -1 )
@@ -2256,20 +2253,7 @@ write_page_headers(BackupPageHeader2 *headers, pgFile *file, HeaderMap *hdr_map,
22562253file -> hdr_off = 0 ;
22572254}
22582255else
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 ;
22732257
22742258if (z_len <=0 )
22752259{
@@ -2281,15 +2265,14 @@ write_page_headers(BackupPageHeader2 *headers, pgFile *file, HeaderMap *hdr_map,
22812265file -> rel_path ,z_len );
22822266}
22832267
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" ,
22882269file -> rel_path ,file -> hdr_off ,z_len ,file -> hdr_crc );
22892270
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 ));
22912273
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 */
22932276
22942277/* End critical section */
22952278pthread_mutex_unlock (& (hdr_map -> mutex ));
@@ -2300,10 +2283,8 @@ write_page_headers(BackupPageHeader2 *headers, pgFile *file, HeaderMap *hdr_map,
23002283void
23012284init_header_map (pgBackup * backup )
23022285{
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 ;
23072288join_path_components (backup -> hdr_map .path ,backup -> root_dir ,HEADER_MAP );
23082289join_path_components (backup -> hdr_map .path_tmp ,backup -> root_dir ,HEADER_MAP_TMP );
23092290backup -> hdr_map .mutex = (pthread_mutex_t )PTHREAD_MUTEX_INITIALIZER ;
@@ -2312,22 +2293,11 @@ init_header_map(pgBackup *backup)
23122293void
23132294cleanup_header_map (HeaderMap * hdr_map )
23142295{
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 ))
23182298elog (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 ;
23332303}