Skip to content

Commit

Permalink
utils/file: Add fremove_recursive()
Browse files Browse the repository at this point in the history
    $ sudo ./src/tests/ulpatch_test -f File.ftouch_remove --lv=9 -v
    === ---------------------------
    ===   32/70   File.ftouch_remove
    16:13:48 [DEBUG][file.c __remove_recursive:113] Delete file a
    16:13:48 [DEBUG][file.c __remove_recursive:113] Delete file b
    16:13:48 [DEBUG][file.c __remove_recursive:113] Delete file c
    269us OK

Signed-off-by: Rong Tao <[email protected]>
  • Loading branch information
Rtoax committed May 8, 2024
1 parent fd9d4cd commit 8805281
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/tests/utils/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ TEST(File, ftouch_remove, 0)
ret |= fremove(files[i]);
}

for (i = 0; i < ARRAY_SIZE(files); i++) {
ret |= ftouch(files[i]);
ret |= fremove_recursive(files[i]);
}

return ret;
}

Expand Down
46 changes: 46 additions & 0 deletions src/utils/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <assert.h>
#include <libgen.h>
#include <stdlib.h>
#include <dirent.h>

#include <gelf.h>

Expand Down Expand Up @@ -89,6 +90,51 @@ int ftouch(const char *filepath)
return 0;
}

static int __remove_recursive(const char *path)
{
DIR *dirp;
struct dirent *dp;
struct stat dir_stat;
char dir_name[PATH_MAX];

/* path not exist */
if (!fexist(path)) {
return 0;
}

/* get attribution error */
if (stat(path, &dir_stat) == -1) {
lerror("get dir:%s stat error.\n", path);
return -1;
}

/* regular file, delete */
if (S_ISREG(dir_stat.st_mode)) {
ldebug("Delete file %s\n", path);
remove(path);
} else if (S_ISDIR(dir_stat.st_mode)) {
dirp = opendir(path);
while ((dp = readdir(dirp))!= NULL) {
if ((0 == strcmp(".", dp->d_name)) ||
(0 == strcmp("..", dp->d_name)))
continue;
sprintf(dir_name, "%s/%s", path, dp->d_name);
__remove_recursive(dir_name);
}
closedir(dirp);
ldebug("Delete dir %s\n", path);
rmdir(path);
} else {
lerror("unknow type: %s!\n", path);
}
return 0;
}

int fremove_recursive(const char *filepath)
{
return __remove_recursive(filepath);
}

static int _file_type_mem(struct mmap_struct *mem)
{
file_type type = FILE_UNKNOWN;
Expand Down
4 changes: 2 additions & 2 deletions src/utils/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,12 @@ struct mmap_struct {
size_t size;
};

/* there are some file mmap apis
*/
/* File and directory operations */
int fsize(const char *filepath);
bool fexist(const char *filepath);
int ftouch(const char *filepath);
int fremove(const char *filepath);
int fremove_recursive(const char *filepath);
file_type ftype(const char *filepath);
int fcopy(const char *srcpath, const char *dstpath);
char* fmktempfile(char *buf, int buf_len, char *seed);
Expand Down

0 comments on commit 8805281

Please sign in to comment.