@@ -632,22 +632,96 @@ void
632632write_backup_filelist (pgBackup * backup , parray * files , const char * root ,
633633 const char * external_prefix , parray * external_list )
634634{
635- FILE * fp ;
635+ FILE * out ;
636636 char path [MAXPGPATH ];
637637 char path_temp [MAXPGPATH ];
638638 int errno_temp ;
639+ size_t i = 0 ;
640+ #define BUFFERSZ BLCKSZ*500
641+ char buf [BUFFERSZ ];
642+ size_t write_len = 0 ;
639643
640644 pgBackupGetPath (backup , path , lengthof (path ), DATABASE_FILE_LIST );
641645 snprintf (path_temp , sizeof (path_temp ), "%s.tmp" , path );
642646
643- fp = fio_fopen (path_temp , PG_BINARY_W , FIO_BACKUP_HOST );
644- if (fp == NULL )
647+ out = fio_fopen (path_temp , PG_BINARY_W , FIO_BACKUP_HOST );
648+ if (out == NULL )
645649 elog (ERROR , "Cannot open file list \"%s\": %s" , path_temp ,
646650 strerror (errno ));
647651
648- print_file_list (fp , files , root , external_prefix , external_list );
652+ /* print each file in the list */
653+ while (i < parray_num (files ))
654+ {
655+ pgFile * file = (pgFile * ) parray_get (files , i );
656+ char * path = file -> path ;
657+ char line [BLCKSZ ];
658+ int len = 0 ;
659+
660+ /* omit root directory portion */
661+ if (root && strstr (path , root ) == path )
662+ path = GetRelativePath (path , root );
663+ else if (file -> external_dir_num && !external_prefix )
664+ {
665+ Assert (external_list );
666+ path = GetRelativePath (path , parray_get (external_list ,
667+ file -> external_dir_num - 1 ));
668+ }
649669
650- if (fio_fflush (fp ) || fio_fclose (fp ))
670+ len = sprintf (line , "{\"path\":\"%s\", \"size\":\"" INT64_FORMAT "\", "
671+ "\"mode\":\"%u\", \"is_datafile\":\"%u\", "
672+ "\"is_cfs\":\"%u\", \"crc\":\"%u\", "
673+ "\"compress_alg\":\"%s\", \"external_dir_num\":\"%d\"" ,
674+ path , file -> write_size , file -> mode ,
675+ file -> is_datafile ? 1 : 0 ,
676+ file -> is_cfs ? 1 : 0 ,
677+ file -> crc ,
678+ deparse_compress_alg (file -> compress_alg ),
679+ file -> external_dir_num );
680+
681+ if (file -> is_datafile )
682+ len += sprintf (line + len , ",\"segno\":\"%d\"" , file -> segno );
683+
684+ if (file -> linked )
685+ len += sprintf (line + len , ",\"linked\":\"%s\"" , file -> linked );
686+
687+ if (file -> n_blocks != BLOCKNUM_INVALID )
688+ len += sprintf (line + len , ",\"n_blocks\":\"%i\"" , file -> n_blocks );
689+
690+ len += sprintf (line + len , "}\n" );
691+
692+ if (write_len + len <= BUFFERSZ )
693+ {
694+ memcpy (buf + write_len , line , len );
695+ write_len += len ;
696+ }
697+ else
698+ {
699+ /* write buffer to file */
700+ if (fio_fwrite (out , buf , write_len ) != write_len )
701+ {
702+ errno_temp = errno ;
703+ fio_unlink (path_temp , FIO_BACKUP_HOST );
704+ elog (ERROR , "Cannot write file list \"%s\": %s" ,
705+ path_temp , strerror (errno ));
706+ }
707+ /* reset write_len */
708+ write_len = 0 ;
709+ }
710+
711+ i ++ ;
712+ }
713+
714+ /* write what is left in the buffer to file */
715+ if (write_len > 0 )
716+ if (fio_fwrite (out , buf , write_len ) != write_len )
717+ {
718+ errno_temp = errno ;
719+ fio_unlink (path_temp , FIO_BACKUP_HOST );
720+ elog (ERROR , "Cannot write file list \"%s\": %s" ,
721+ path_temp , strerror (errno ));
722+ }
723+
724+ if (fio_fflush (out ) || fio_fclose (out ))
651725 {
652726 errno_temp = errno ;
653727 fio_unlink (path_temp , FIO_BACKUP_HOST );
0 commit comments