@@ -110,20 +110,38 @@ static void
110110transfer_single_new_db (pageCnvCtx * pageConverter ,
111111 FileNameMap * maps , int size )
112112{
113+ char old_dir [MAXPGPATH ];
114+ struct dirent * * namelist = NULL ;
115+ int numFiles = 0 ;
113116 int mapnum ;
117+ int fileno ;
118+
119+ old_dir [0 ] = '\0' ;
114120
115121 for (mapnum = 0 ; mapnum < size ; mapnum ++ )
116122 {
117123 char old_file [MAXPGPATH ];
118124 char new_file [MAXPGPATH ];
119- struct dirent * * namelist = NULL ;
120- int numFiles ;
125+
126+ /* Changed tablespaces? Need a new directory scan? */
127+ if (strcmp (maps [mapnum ].old_dir , old_dir ) != 0 )
128+ {
129+ if (numFiles > 0 )
130+ {
131+ for (fileno = 0 ; fileno < numFiles ; fileno ++ )
132+ pg_free (namelist [fileno ]);
133+ pg_free (namelist );
134+ }
135+
136+ snprintf (old_dir , sizeof (old_dir ), "%s" , maps [mapnum ].old_dir );
137+ numFiles = pg_scandir (old_dir , & namelist , NULL );
138+ }
121139
122140 /* Copying files might take some time, so give feedback. */
123141
124- snprintf (old_file , sizeof (old_file ), "%s/%u" , maps [mapnum ].old_file ,
142+ snprintf (old_file , sizeof (old_file ), "%s/%u" , maps [mapnum ].old_dir ,
125143 maps [mapnum ].old_relfilenode );
126- snprintf (new_file , sizeof (new_file ), "%s/%u" , maps [mapnum ].new_file ,
144+ snprintf (new_file , sizeof (new_file ), "%s/%u" , maps [mapnum ].new_dir ,
127145 maps [mapnum ].new_relfilenode );
128146 pg_log (PG_REPORT , OVERWRITE_MESSAGE , old_file );
129147
@@ -139,28 +157,27 @@ transfer_single_new_db(pageCnvCtx *pageConverter,
139157 if (GET_MAJOR_VERSION (old_cluster .major_version ) >= 804 )
140158 {
141159 /*
142- * Now copy /link any fsm and vm files, if they exist
160+ * Copy /link any fsm and vm files, if they exist
143161 */
144162 snprintf (scandir_file_pattern , sizeof (scandir_file_pattern ), "%u_" ,
145163 maps [mapnum ].old_relfilenode );
146- numFiles = pg_scandir (maps [mapnum ].old_file , & namelist , dir_matching_filenames );
147164
148- while ( numFiles -- )
165+ for ( fileno = 0 ; fileno < numFiles ; fileno ++ )
149166 {
150- snprintf (old_file , sizeof (old_file ), "%s/%s" , maps [mapnum ].old_file ,
151- namelist [numFiles ]-> d_name );
152- snprintf (new_file , sizeof (new_file ), "%s/%u%s" , maps [mapnum ].new_file ,
153- maps [mapnum ].new_relfilenode , strchr (namelist [numFiles ]-> d_name , '_' ));
154-
155- unlink (new_file );
156- transfer_relfile (pageConverter , old_file , new_file ,
157- maps [mapnum ].old_nspname , maps [mapnum ].old_relname ,
158- maps [mapnum ].new_nspname , maps [mapnum ].new_relname );
159-
160- pg_free (namelist [numFiles ]);
167+ if (strncmp (namelist [fileno ]-> d_name , scandir_file_pattern ,
168+ strlen (scandir_file_pattern )) == 0 )
169+ {
170+ snprintf (old_file , sizeof (old_file ), "%s/%s" , maps [mapnum ].old_dir ,
171+ namelist [fileno ]-> d_name );
172+ snprintf (new_file , sizeof (new_file ), "%s/%u%s" , maps [mapnum ].new_dir ,
173+ maps [mapnum ].new_relfilenode , strchr (namelist [fileno ]-> d_name , '_' ));
174+
175+ unlink (new_file );
176+ transfer_relfile (pageConverter , old_file , new_file ,
177+ maps [mapnum ].old_nspname , maps [mapnum ].old_relname ,
178+ maps [mapnum ].new_nspname , maps [mapnum ].new_relname );
179+ }
161180 }
162-
163- pg_free (namelist );
164181 }
165182
166183 /*
@@ -172,23 +189,30 @@ transfer_single_new_db(pageCnvCtx *pageConverter,
172189 */
173190 snprintf (scandir_file_pattern , sizeof (scandir_file_pattern ), "%u." ,
174191 maps [mapnum ].old_relfilenode );
175- numFiles = pg_scandir (maps [mapnum ].old_file , & namelist , dir_matching_filenames );
176192
177- while ( numFiles -- )
193+ for ( fileno = 0 ; fileno < numFiles ; fileno ++ )
178194 {
179- snprintf (old_file , sizeof (old_file ), "%s/%s" , maps [mapnum ].old_file ,
180- namelist [numFiles ]-> d_name );
181- snprintf (new_file , sizeof (new_file ), "%s/%u%s" , maps [mapnum ].new_file ,
182- maps [mapnum ].new_relfilenode , strchr (namelist [numFiles ]-> d_name , '.' ));
183-
184- unlink (new_file );
185- transfer_relfile (pageConverter , old_file , new_file ,
186- maps [mapnum ].old_nspname , maps [mapnum ].old_relname ,
187- maps [mapnum ].new_nspname , maps [mapnum ].new_relname );
195+ if (strncmp (namelist [fileno ]-> d_name , scandir_file_pattern ,
196+ strlen (scandir_file_pattern )) == 0 )
197+ {
198+ snprintf (old_file , sizeof (old_file ), "%s/%s" , maps [mapnum ].old_dir ,
199+ namelist [fileno ]-> d_name );
200+ snprintf (new_file , sizeof (new_file ), "%s/%u%s" , maps [mapnum ].new_dir ,
201+ maps [mapnum ].new_relfilenode , strchr (namelist [fileno ]-> d_name , '.' ));
188202
189- pg_free (namelist [numFiles ]);
203+ unlink (new_file );
204+ transfer_relfile (pageConverter , old_file , new_file ,
205+ maps [mapnum ].old_nspname , maps [mapnum ].old_relname ,
206+ maps [mapnum ].new_nspname , maps [mapnum ].new_relname );
207+ }
190208 }
209+ }
191210
211+
212+ if (numFiles > 0 )
213+ {
214+ for (fileno = 0 ; fileno < numFiles ; fileno ++ )
215+ pg_free (namelist [fileno ]);
192216 pg_free (namelist );
193217 }
194218}
0 commit comments