2121
2222static bool generate_my_cnf (BaseString mycnf, atrt_config& config);
2323static bool create_directory (const char * path);
24+ static bool delete_file_if_exists (const char * path);
25+ static bool copy_file (const char * src, const char * dst);
2426
2527bool setup_directories (atrt_config& config, int setup) {
2628 /* *
@@ -183,6 +185,37 @@ static bool generate_my_cnf(BaseString mycnf, atrt_config& config) {
183185 return true ;
184186}
185187
188+ bool exists_file (const char * path) {
189+ struct stat sbuf;
190+ int ret = lstat (path, &sbuf);
191+ return ret == 0 ;
192+ }
193+
194+ static bool delete_file_if_exists (const char * path) {
195+ if (!exists_file (path)) {
196+ return true ;
197+ }
198+
199+ if (unlink (path) != 0 ) {
200+ g_logger.error (" Failed to remove %s" , path);
201+ return false ;
202+ }
203+
204+ return true ;
205+ }
206+
207+ static bool copy_file (const char * src, const char * dst) {
208+ BaseString cp;
209+ cp.assfmt (" cp %s %s" , src, dst);
210+ to_fwd_slashes (cp);
211+ if (sh (cp.c_str ()) != 0 ) {
212+ g_logger.error (" Failed to '%s'" , cp.c_str ());
213+ return false ;
214+ }
215+
216+ return true ;
217+ }
218+
186219bool setup_files (atrt_config& config, int setup, int sshx) {
187220 /* *
188221 * 0 = validate
@@ -191,27 +224,20 @@ bool setup_files(atrt_config& config, int setup, int sshx) {
191224 */
192225 BaseString mycnf;
193226 mycnf.assfmt (" %s/my.cnf" , g_basedir);
227+ to_native (mycnf);
194228
195229 if (!create_directory (g_basedir)) {
196230 return false ;
197231 }
198232
199233 if (mycnf != g_my_cnf) {
200- struct stat sbuf;
201- int ret = lstat (to_native (mycnf).c_str (), &sbuf);
202-
203- if (ret == 0 ) {
204- if (unlink (to_native (mycnf).c_str ()) != 0 ) {
205- g_logger.error (" Failed to remove %s" , mycnf.c_str ());
206- return false ;
207- }
234+ if (!delete_file_if_exists (mycnf.c_str ())) {
235+ return false ;
208236 }
209237
210- BaseString cp;
211- cp.assfmt (" cp %s %s" , g_my_cnf, mycnf.c_str ());
212- to_fwd_slashes (cp);
213- if (sh (cp.c_str ()) != 0 ) {
214- g_logger.error (" Failed to '%s'" , cp.c_str ());
238+ BaseString aux (g_my_cnf);
239+ to_native (aux);
240+ if (!copy_file (aux.c_str (), mycnf.c_str ())) {
215241 return false ;
216242 }
217243 }
0 commit comments