- switch (entry->d_type) {
- case DT_DIR:
- unlinkat(trace_dir_fd, entry->d_name, AT_REMOVEDIR);
- break;
- case DT_REG:
- unlinkat(trace_dir_fd, entry->d_name, 0);
- break;
- default:
+
+ name_len = strlen(entry->d_name);
+ if (path_len + name_len + 2 > sizeof(filename)) {
+ ERR("Failed to remove file: path name too long (%s/%s)",
+ path, entry->d_name);
+ continue;
+ }
+
+ if (snprintf(filename, sizeof(filename), "%s/%s",
+ path, entry->d_name) < 0) {
+ ERR("Failed to format path.");
+ continue;
+ }
+
+ if (stat(filename, &st)) {
+ PERROR("stat");
+ continue;
+ }
+
+ if (S_ISDIR(st.st_mode)) {
+ char *subpath = zmalloc(PATH_MAX);
+
+ if (!subpath) {
+ PERROR("zmalloc path");
+ ret = -1;
+ goto end;
+ }
+ strncpy(subpath, path, PATH_MAX);
+ subpath[PATH_MAX - 1] = '\0';
+ strncat(subpath, "/",
+ PATH_MAX - strlen(subpath) - 1);
+ strncat(subpath, entry->d_name,
+ PATH_MAX - strlen(subpath) - 1);
+
+ ret = delete_dir_recursive(subpath);
+ free(subpath);
+ if (ret) {
+ /* Error occurred, abort traversal. */
+ goto end;
+ }
+ } else if (S_ISREG(st.st_mode)) {
+ ret = unlinkat(dir_fd, entry->d_name, 0);
+ if (ret) {
+ PERROR("Unlinking '%s'", entry->d_name);
+ goto end;
+ }
+ } else {