tree-wide: replace all readdir cycles with FOREACH_DIRENT{,_ALL} (#4853)
This commit is contained in:
committed by
Martin Pitt
parent
9258a1cae3
commit
8fb3f00997
+2
-4
@@ -24,6 +24,7 @@
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "dirent-util.h"
|
||||
#include "fd-util.h"
|
||||
#include "fs-util.h"
|
||||
#include "macro.h"
|
||||
@@ -234,12 +235,9 @@ int close_all_fds(const int except[], unsigned n_except) {
|
||||
return r;
|
||||
}
|
||||
|
||||
while ((de = readdir(d))) {
|
||||
FOREACH_DIRENT(de, d, return -errno) {
|
||||
int fd = -1;
|
||||
|
||||
if (hidden_or_backup_file(de->d_name))
|
||||
continue;
|
||||
|
||||
if (safe_atoi(de->d_name, &fd) < 0)
|
||||
/* Let's better ignore this, just in case */
|
||||
continue;
|
||||
|
||||
+2
-11
@@ -17,7 +17,6 @@
|
||||
along with systemd; If not, see <http://www.gnu.org/licenses/>.
|
||||
***/
|
||||
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
@@ -446,6 +445,7 @@ int mkfifo_atomic(const char *path, mode_t mode) {
|
||||
|
||||
int get_files_in_directory(const char *path, char ***list) {
|
||||
_cleanup_closedir_ DIR *d = NULL;
|
||||
struct dirent *de;
|
||||
size_t bufsize = 0, n = 0;
|
||||
_cleanup_strv_free_ char **l = NULL;
|
||||
|
||||
@@ -459,16 +459,7 @@ int get_files_in_directory(const char *path, char ***list) {
|
||||
if (!d)
|
||||
return -errno;
|
||||
|
||||
for (;;) {
|
||||
struct dirent *de;
|
||||
|
||||
errno = 0;
|
||||
de = readdir(d);
|
||||
if (!de && errno > 0)
|
||||
return -errno;
|
||||
if (!de)
|
||||
break;
|
||||
|
||||
FOREACH_DIRENT_ALL(de, d, return -errno) {
|
||||
dirent_ensure_type(d, de);
|
||||
|
||||
if (!dirent_is_file(de))
|
||||
|
||||
+4
-11
@@ -17,7 +17,6 @@
|
||||
along with systemd; If not, see <http://www.gnu.org/licenses/>.
|
||||
***/
|
||||
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdbool.h>
|
||||
@@ -28,6 +27,7 @@
|
||||
|
||||
#include "btrfs-util.h"
|
||||
#include "cgroup-util.h"
|
||||
#include "dirent-util.h"
|
||||
#include "fd-util.h"
|
||||
#include "log.h"
|
||||
#include "macro.h"
|
||||
@@ -43,6 +43,7 @@ static bool is_physical_fs(const struct statfs *sfs) {
|
||||
|
||||
int rm_rf_children(int fd, RemoveFlags flags, struct stat *root_dev) {
|
||||
_cleanup_closedir_ DIR *d = NULL;
|
||||
struct dirent *de;
|
||||
int ret = 0, r;
|
||||
struct statfs sfs;
|
||||
|
||||
@@ -78,19 +79,10 @@ int rm_rf_children(int fd, RemoveFlags flags, struct stat *root_dev) {
|
||||
return errno == ENOENT ? 0 : -errno;
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
struct dirent *de;
|
||||
FOREACH_DIRENT_ALL(de, d, return -errno) {
|
||||
bool is_dir;
|
||||
struct stat st;
|
||||
|
||||
errno = 0;
|
||||
de = readdir(d);
|
||||
if (!de) {
|
||||
if (errno > 0 && ret == 0)
|
||||
ret = -errno;
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (streq(de->d_name, ".") || streq(de->d_name, ".."))
|
||||
continue;
|
||||
|
||||
@@ -178,6 +170,7 @@ int rm_rf_children(int fd, RemoveFlags flags, struct stat *root_dev) {
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int rm_rf(const char *path, RemoveFlags flags) {
|
||||
|
||||
+2
-14
@@ -18,7 +18,6 @@
|
||||
***/
|
||||
|
||||
#include <alloca.h>
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <sched.h>
|
||||
@@ -508,28 +507,17 @@ void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size,
|
||||
int on_ac_power(void) {
|
||||
bool found_offline = false, found_online = false;
|
||||
_cleanup_closedir_ DIR *d = NULL;
|
||||
struct dirent *de;
|
||||
|
||||
d = opendir("/sys/class/power_supply");
|
||||
if (!d)
|
||||
return errno == ENOENT ? true : -errno;
|
||||
|
||||
for (;;) {
|
||||
struct dirent *de;
|
||||
FOREACH_DIRENT(de, d, return -errno) {
|
||||
_cleanup_close_ int fd = -1, device = -1;
|
||||
char contents[6];
|
||||
ssize_t n;
|
||||
|
||||
errno = 0;
|
||||
de = readdir(d);
|
||||
if (!de && errno > 0)
|
||||
return -errno;
|
||||
|
||||
if (!de)
|
||||
break;
|
||||
|
||||
if (hidden_or_backup_file(de->d_name))
|
||||
continue;
|
||||
|
||||
device = openat(dirfd(d), de->d_name, O_DIRECTORY|O_RDONLY|O_CLOEXEC|O_NOCTTY);
|
||||
if (device < 0) {
|
||||
if (errno == ENOENT || errno == ENOTDIR)
|
||||
|
||||
Reference in New Issue
Block a user