Skip to content

Commit

Permalink
[HACK] try without fs::remove_all
Browse files Browse the repository at this point in the history
Signed-off-by: Julian Oes <[email protected]>
  • Loading branch information
julianoes committed Oct 21, 2023
1 parent 7ab04e7 commit abb1ffd
Showing 1 changed file with 82 additions and 2 deletions.
84 changes: 82 additions & 2 deletions src/system_tests/fs_helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,85 @@
#include <fstream>
#include <cstring>

#include <sys/types.h>
#include <dirent.h>

#include <stdio.h>
#include <string.h>

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

// POSIX dependencies
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>

void rmtree(const char path[])
{
size_t path_len;
char* full_path;
DIR* dir;
struct stat stat_path, stat_entry;
struct dirent* entry;

// stat for the path
stat(path, &stat_path);

// if path does not exists or is not dir - exit with status -1
if (S_ISDIR(stat_path.st_mode) == 0) {
fprintf(stderr, "%s: %s\n", "Is not directory", path);
exit(-1);
}

// if not possible to read the directory for this user
if ((dir = opendir(path)) == NULL) {
fprintf(stderr, "%s: %s\n", "Can`t open directory", path);
exit(-1);
}

// the length of the path
path_len = strlen(path);

// iteration through entries in the directory
while ((entry = readdir(dir)) != NULL) {
// skip entries "." and ".."
if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, ".."))
continue;

// determinate a full path of an entry
full_path = (char*)calloc(path_len + strlen(entry->d_name) + 2, sizeof(char));
strcpy(full_path, path);
strcat(full_path, "/");
strcat(full_path, entry->d_name);

// stat for the entry
stat(full_path, &stat_entry);

// recursively remove a nested directory
if (S_ISDIR(stat_entry.st_mode) != 0) {
rmtree(full_path);
continue;
}

// remove a file object
if (unlink(full_path) == 0)
printf("Removed a file: %s\n", full_path);
else
printf("Can`t remove a file: %s\n", full_path);
free(full_path);
}

// remove the devastated directory and close the object of it
if (rmdir(path) == 0)
printf("Removed a directory: %s\n", path);
else
printf("Can`t remove a directory: %s\n", path);

closedir(dir);
}

bool create_temp_file(const fs::path& path, size_t len, uint8_t start)
{
const auto parent_path = path.parent_path();
Expand All @@ -30,8 +109,9 @@ bool create_temp_file(const fs::path& path, size_t len, uint8_t start)

bool reset_directories(const fs::path& path)
{
std::error_code ec;
fs::remove_all(path, ec);
// std::error_code ec;
// fs::remove_all(path, ec);
rmtree(path.string().c_str());

return fs::create_directories(path);
}
Expand Down

0 comments on commit abb1ffd

Please sign in to comment.