Merge "otautil: Delete dirUnlinkHierarchy()." am: 031661d4a9
am: 993cec5cc9
Change-Id: Iec927cd9ff2f85e898fa034239641e733e119a49
This commit is contained in:
commit
633aeba594
3 changed files with 0 additions and 101 deletions
|
@ -160,59 +160,3 @@ dirCreateHierarchy(const char *path, int mode,
|
|||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
dirUnlinkHierarchy(const char *path)
|
||||
{
|
||||
struct stat st;
|
||||
DIR *dir;
|
||||
struct dirent *de;
|
||||
int fail = 0;
|
||||
|
||||
/* is it a file or directory? */
|
||||
if (lstat(path, &st) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* a file, so unlink it */
|
||||
if (!S_ISDIR(st.st_mode)) {
|
||||
return unlink(path);
|
||||
}
|
||||
|
||||
/* a directory, so open handle */
|
||||
dir = opendir(path);
|
||||
if (dir == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* recurse over components */
|
||||
errno = 0;
|
||||
while ((de = readdir(dir)) != NULL) {
|
||||
//TODO: don't blow the stack
|
||||
char dn[PATH_MAX];
|
||||
if (!strcmp(de->d_name, "..") || !strcmp(de->d_name, ".")) {
|
||||
continue;
|
||||
}
|
||||
snprintf(dn, sizeof(dn), "%s/%s", path, de->d_name);
|
||||
if (dirUnlinkHierarchy(dn) < 0) {
|
||||
fail = 1;
|
||||
break;
|
||||
}
|
||||
errno = 0;
|
||||
}
|
||||
/* in case readdir or unlink_recursive failed */
|
||||
if (fail || errno < 0) {
|
||||
int save = errno;
|
||||
closedir(dir);
|
||||
errno = save;
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* close directory handle */
|
||||
if (closedir(dir) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* delete target directory */
|
||||
return rmdir(path);
|
||||
}
|
||||
|
|
|
@ -17,13 +17,8 @@
|
|||
#ifndef MINZIP_DIRUTIL_H_
|
||||
#define MINZIP_DIRUTIL_H_
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <utime.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct selabel_handle;
|
||||
|
||||
/* Like "mkdir -p", try to guarantee that all directories
|
||||
|
@ -43,12 +38,4 @@ int dirCreateHierarchy(const char *path, int mode,
|
|||
const struct utimbuf *timestamp, bool stripFileName,
|
||||
struct selabel_handle* sehnd);
|
||||
|
||||
/* rm -rf <path>
|
||||
*/
|
||||
int dirUnlinkHierarchy(const char *path);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // MINZIP_DIRUTIL_H_
|
||||
|
|
|
@ -116,35 +116,3 @@ TEST(DirUtilTest, create_mode_and_timestamp) {
|
|||
ASSERT_EQ(0, rmdir((prefix + "/a/b").c_str()));
|
||||
ASSERT_EQ(0, rmdir((prefix + "/a").c_str()));
|
||||
}
|
||||
|
||||
TEST(DirUtilTest, unlink_invalid) {
|
||||
// File doesn't exist.
|
||||
ASSERT_EQ(-1, dirUnlinkHierarchy("doesntexist"));
|
||||
|
||||
// Nonexistent directory.
|
||||
TemporaryDir td;
|
||||
std::string path(td.path);
|
||||
ASSERT_EQ(-1, dirUnlinkHierarchy((path + "/a").c_str()));
|
||||
ASSERT_EQ(ENOENT, errno);
|
||||
}
|
||||
|
||||
TEST(DirUtilTest, unlink_smoke) {
|
||||
// Unlink a file.
|
||||
TemporaryFile tf;
|
||||
ASSERT_EQ(0, dirUnlinkHierarchy(tf.path));
|
||||
ASSERT_EQ(-1, access(tf.path, F_OK));
|
||||
|
||||
TemporaryDir td;
|
||||
std::string path(td.path);
|
||||
constexpr mode_t mode = 0700;
|
||||
ASSERT_EQ(0, mkdir((path + "/a").c_str(), mode));
|
||||
ASSERT_EQ(0, mkdir((path + "/a/b").c_str(), mode));
|
||||
ASSERT_EQ(0, mkdir((path + "/a/b/c").c_str(), mode));
|
||||
ASSERT_EQ(0, mkdir((path + "/a/d").c_str(), mode));
|
||||
|
||||
// Remove "../a" recursively.
|
||||
ASSERT_EQ(0, dirUnlinkHierarchy((path + "/a").c_str()));
|
||||
|
||||
// Verify it's gone.
|
||||
ASSERT_EQ(-1, access((path + "/a").c_str(), F_OK));
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue