Skip to content

Commit e36c7db

Browse files
author
Tiago Alves
committed
WL#12209 REFACTOR FILE OPERATIONS FOR CODE REUSE
Refactor logic to check file existence, copy and delete file to their own methods for later reuse.
1 parent 22d8219 commit e36c7db

File tree

2 files changed

+40
-13
lines changed

2 files changed

+40
-13
lines changed

storage/ndb/test/run-test/atrt.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ bool sshx(atrt_config&, unsigned procmask);
142142
bool start(atrt_config&, unsigned procmask);
143143

144144
bool remove_dir(const char*, bool incl = true);
145+
bool exists_file(const char* path);
145146
bool connect_hosts(atrt_config&);
146147
bool connect_ndb_mgm(atrt_config&);
147148
bool wait_ndb(atrt_config&, int ndb_mgm_node_status);

storage/ndb/test/run-test/files.cpp

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
static bool generate_my_cnf(BaseString mycnf, atrt_config& config);
2323
static 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

2527
bool 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+
186219
bool 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

Comments
 (0)