Skip to content

Commit ee9e916

Browse files
committed
clar: remove files internally instead of /bin/rm
Similar to how clar has used `/bin/cp` to copy files, it's used `/bin/rm` to remove them. This has similar deficiencies; meaning that leaks is noisy and it's slow. Move it to an internal function.
1 parent d03fd33 commit ee9e916

File tree

1 file changed

+37
-10
lines changed

1 file changed

+37
-10
lines changed

tests/clar/fs.h

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -455,19 +455,46 @@ fs_copy(const char *source, const char *_dest)
455455
}
456456

457457
static void
458-
fs_rm(const char *source)
458+
fs_rmdir_helper(const char *path)
459459
{
460-
char *argv[4];
460+
DIR *dir;
461+
struct dirent *d;
462+
463+
cl_assert_(dir = opendir(path), "Could not open dir");
464+
while ((d = (errno = 0, readdir(dir))) != NULL) {
465+
char *child;
461466

462-
argv[0] = "/bin/rm";
463-
argv[1] = "-Rf";
464-
argv[2] = (char *)source;
465-
argv[3] = NULL;
467+
if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
468+
continue;
466469

467-
cl_must_pass_(
468-
shell_out(argv),
469-
"Failed to cleanup the sandbox"
470-
);
470+
child = joinpath(path, d->d_name, -1);
471+
fs_rm(child);
472+
free(child);
473+
}
474+
475+
cl_assert_(errno == 0, "Failed to iterate source dir");
476+
closedir(dir);
477+
478+
cl_must_pass_(rmdir(path), "Could not remove directory");
479+
}
480+
481+
static void
482+
fs_rm(const char *path)
483+
{
484+
struct stat st;
485+
486+
if (lstat(path, &st)) {
487+
if (errno == ENOENT)
488+
return;
489+
490+
cl_fail("Cannot copy; cannot stat destination");
491+
}
492+
493+
if (S_ISDIR(st.st_mode)) {
494+
fs_rmdir_helper(path);
495+
} else {
496+
cl_must_pass(unlink(path));
497+
}
471498
}
472499

473500
void

0 commit comments

Comments
 (0)