int rm_main(int argc UNUSED_PARAM, char **argv) { int status = 0; int flags = 0; unsigned opt; opt_complementary = "f-i:i-f"; /* -v (verbose) is ignored */ opt = getopt32(argv, "fiRrv"); argv += optind; if (opt & 1) flags |= FILEUTILS_FORCE; if (opt & 2) flags |= FILEUTILS_INTERACTIVE; if (opt & (8|4)) flags |= FILEUTILS_RECUR; if (*argv != NULL) { do { const char *base = bb_get_last_path_component_strip(*argv); if (DOT_OR_DOTDOT(base)) { bb_error_msg("cannot remove '.' or '..'"); } else if (remove_file(*argv, flags) >= 0) { continue; } status = 1; } while (*++argv); } else if (!(flags & FILEUTILS_FORCE)) { bb_show_usage(); } return status; }
// Recursively delete contents of rootfs. static void delete_contents(const char *directory, dev_t rootdev) { DIR *dir; struct dirent *d; struct stat st; // Don't descend into other filesystems if (lstat(directory, &st) || st.st_dev != rootdev) return; // Recursively delete the contents of directories. if (S_ISDIR(st.st_mode)) { dir = opendir(directory); if (dir) { while ((d = readdir(dir))) { char *newdir = d->d_name; // Skip . and .. if (DOT_OR_DOTDOT(newdir)) continue; // Recurse to delete contents newdir = concat_path_file(directory, newdir); delete_contents(newdir, rootdev); free(newdir); } closedir(dir); // Directory should now be empty. Zap it. rmdir(directory); } // It wasn't a directory. Zap it. } else unlink(directory); }
/* stolen from coreutils (cp,mv) package */ static struct dirent *readdir_ignoring_dots(DIR *dirp) { struct dirent *dp = NULL; while(1) { dp = readdir(dirp); if(dp == NULL || !DOT_OR_DOTDOT(dp->d_name)) break; } return dp; }
bool dir_list(const char *dir, list<String> &lst, bool full_path, bool show_hidden, bool show_dots) { E_ASSERT(dir != NULL); DIR *dirp = opendir(dir); E_RETURN_VAL_IF_FAIL(dirp != NULL, false); /* make sure the list is empty */ lst.clear(); String dirstr, tmp; if(full_path) { /* resolve full name if given folder in form: './file' */ dirstr = (dir[0] == '.' && dir[1] == '\0') ? dir_current() : dir; if(!str_ends(dirstr.c_str(), E_DIR_SEPARATOR_STR)) dirstr += E_DIR_SEPARATOR_STR; } for(dirent *dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) { if(!show_hidden && dp->d_name[0] == '.' && !DOT_OR_DOTDOT(dp->d_name)) continue; if(!show_dots && DOT_OR_DOTDOT(dp->d_name)) continue; if(full_path) { tmp = dirstr; tmp += dp->d_name; lst.push_back(tmp); } else { lst.push_back(dp->d_name); } } lst.sort(); closedir(dirp); return true; }
void Desktop::read_desktop_folder(const char *dpath) { E_RETURN_IF_FAIL(dpath != NULL); String path; DIR *dir = opendir(dpath); E_RETURN_IF_FAIL(dir != NULL); DesktopConfig pos; pos.load(ICONS_POS_FILE); dirent *d; while((d = readdir(dir)) != NULL) { if(DOT_OR_DOTDOT(d->d_name)) continue; if(d->d_type > 0) { if(d->d_type != DT_REG && d->d_type != DT_LNK && d->d_type != DT_DIR) continue; path = dpath; path += E_DIR_SEPARATOR; path += d->d_name; } else { /* * If we got here, it means d_type isn't set and we must do it via file_test() which could be much slower. * By POSIX standard, only d_name must be set, but many modern *nixes set all dirent members correctly. Except Slackware ;) */ path = dpath; path += E_DIR_SEPARATOR; path += d->d_name; if(!(file_test(path.c_str(), FILE_TEST_IS_REGULAR) || file_test(path.c_str(), FILE_TEST_IS_DIR) || file_test(path.c_str(), FILE_TEST_IS_SYMLINK))) continue; } DesktopIcon *o = read_desktop_file(path.c_str(), (const char*)d->d_name, &pos); if(o) add(o); } closedir(dir); }
static char *find_block_device_in_dir(struct arena *ap) { DIR *dir; struct dirent *entry; char *retpath = NULL; int len, rem; len = strlen(ap->devpath); rem = DEVNAME_MAX-2 - len; if (rem <= 0) return NULL; dir = opendir(ap->devpath); if (!dir) return NULL; ap->devpath[len++] = '/'; while ((entry = readdir(dir)) != NULL) { safe_strncpy(ap->devpath + len, entry->d_name, rem); /* lstat: do not follow links */ if (lstat(ap->devpath, &ap->st) != 0) continue; if (S_ISBLK(ap->st.st_mode) && ap->st.st_rdev == ap->dev) { retpath = xstrdup(ap->devpath); break; } if (S_ISDIR(ap->st.st_mode)) { /* Do not recurse for '.' and '..' */ if (DOT_OR_DOTDOT(entry->d_name)) continue; retpath = find_block_device_in_dir(ap); if (retpath) break; } } closedir(dir); return retpath; }
static char *concat_subpath_file(const char *path, const char *f) { if (f && DOT_OR_DOTDOT(f)) return NULL; return concat_path_file(path, f); }