diff options
author | Eric Biggers <ebiggers@google.com> | 2020-11-02 15:31:56 -0800 |
---|---|---|
committer | Eric Biggers <ebiggers@google.com> | 2020-11-02 15:47:24 -0800 |
commit | 7bcf4273696aa5b941add7669c7bc9ef82937fa4 (patch) | |
tree | b128ca64f07561158b99654af93c49b46a2632f9 | |
parent | 4a969dba6037d71f913334cfca6480d93f1f21a5 (diff) | |
download | vold-7bcf4273696aa5b941add7669c7bc9ef82937fa4.tar.gz |
Utils: add IsDotOrDotDot() and use it in the appropriate places
Change-Id: I704522b26acfb3e7c423d9a14d69ede513b50482
-rw-r--r-- | MoveStorage.cpp | 4 | ||||
-rw-r--r-- | Utils.cpp | 14 | ||||
-rw-r--r-- | Utils.h | 2 |
3 files changed, 9 insertions, 11 deletions
diff --git a/MoveStorage.cpp b/MoveStorage.cpp index 2447cce4..3f636a22 100644 --- a/MoveStorage.cpp +++ b/MoveStorage.cpp @@ -70,9 +70,7 @@ static bool pushBackContents(const std::string& path, std::vector<std::string>& bool found = false; struct dirent* ent; while ((ent = readdir(dirp.get())) != NULL) { - if ((!strcmp(ent->d_name, ".")) || (!strcmp(ent->d_name, ".."))) { - continue; - } + if (IsDotOrDotDot(*ent)) continue; auto subdir = path + "/" + ent->d_name; found |= pushBackContents(subdir, cmd, searchLevels - 1); } @@ -956,10 +956,7 @@ int64_t calculate_dir_size(int dfd) { int subfd; /* always skip "." and ".." */ - if (name[0] == '.') { - if (name[1] == 0) continue; - if ((name[1] == '.') && (name[2] == 0)) continue; - } + if (IsDotOrDotDot(*de)) continue; subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY | O_CLOEXEC); if (subfd >= 0) { @@ -1256,6 +1253,10 @@ status_t UnmountTree(const std::string& mountPoint) { return OK; } +bool IsDotOrDotDot(const struct dirent& ent) { + return strcmp(ent.d_name, ".") == 0 || strcmp(ent.d_name, "..") == 0; +} + static status_t delete_dir_contents(DIR* dir) { // Shamelessly borrowed from android::installd int dfd = dirfd(dir); @@ -1269,10 +1270,7 @@ static status_t delete_dir_contents(DIR* dir) { const char* name = de->d_name; if (de->d_type == DT_DIR) { /* always skip "." and ".." */ - if (name[0] == '.') { - if (name[1] == 0) continue; - if ((name[1] == '.') && (name[2] == 0)) continue; - } + if (IsDotOrDotDot(*de)) continue; android::base::unique_fd subfd( openat(dfd, name, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC)); @@ -168,6 +168,8 @@ bool IsVirtioBlkDevice(unsigned int major); status_t UnmountTreeWithPrefix(const std::string& prefix); status_t UnmountTree(const std::string& mountPoint); +bool IsDotOrDotDot(const struct dirent& ent); + status_t DeleteDirContentsAndDir(const std::string& pathname); status_t DeleteDirContents(const std::string& pathname); |