Skip to content

Commit b2d69f4

Browse files
cynthiajianga-maurice
authored andcommitted
[Auth] Fix fake internal for windows
PiperOrigin-RevId: 246243469
1 parent e5c790f commit b2d69f4

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

auth/src/desktop/secure/user_secure_fake_internal.cc

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@
1414

1515
#include "auth/src/desktop/secure/user_secure_fake_internal.h"
1616

17+
#if defined(_WIN32)
18+
#include <windows.h>
19+
#else
1720
#include <dirent.h>
1821
#include <sys/stat.h>
1922
#include <sys/types.h>
23+
#endif // defined(_WIN32)
2024

2125
#include <cstdio>
2226
#include <fstream>
@@ -55,7 +59,11 @@ std::string UserSecureFakeInternal::LoadUserData(const std::string& app_name) {
5559
void UserSecureFakeInternal::SaveUserData(const std::string& app_name,
5660
const std::string& user_data) {
5761
// Make the directory in case it doesn't already exist, ignoring errors.
62+
#if defined(_WIN32)
63+
CreateDirectory(secure_path_.c_str(), NULL);
64+
#else
5865
mkdir(secure_path_.c_str(), 0700);
66+
#endif
5967

6068
std::string filename = GetFilePath(app_name);
6169

@@ -72,10 +80,27 @@ void UserSecureFakeInternal::DeleteUserData(const std::string& app_name) {
7280
return;
7381
}
7482
infile.close();
75-
std::remove(filename.c_str());
83+
#if defined(_WIN32)
84+
DeleteFile(filename.c_str());
85+
#else
86+
unlink(filename.c_str());
87+
#endif // defined(_WIN32)
7688
}
7789

7890
void UserSecureFakeInternal::DeleteAllData() {
91+
#if defined(_WIN32)
92+
WIN32_FIND_DATA file_data;
93+
HANDLE handle = FindFirstFile(secure_path_.c_str(), &file_data);
94+
if (INVALID_HANDLE_VALUE == handle) {
95+
return;
96+
}
97+
DeleteFile(file_data.cFileName);
98+
while (FindNextFile(handle, &file_data)) {
99+
DeleteFile(file_data.cFileName);
100+
}
101+
FindClose(handle);
102+
RemoveDirectory(secure_path_.c_str());
103+
#else
79104
// These are data types defined in the "dirent" header
80105
DIR* theFolder = opendir(secure_path_.c_str());
81106
if (!theFolder) {
@@ -87,12 +112,13 @@ void UserSecureFakeInternal::DeleteAllData() {
87112
// build the path for each file in the folder
88113
std::string filepath = secure_path_ + "/";
89114
filepath.append(next_file->d_name);
90-
remove(filepath.c_str());
115+
unlink(filepath.c_str());
91116
}
92117
closedir(theFolder);
93118

94119
// Remove the directory if it's empty, ignoring errors.
95120
rmdir(secure_path_.c_str());
121+
#endif
96122
}
97123

98124
std::string UserSecureFakeInternal::GetFilePath(const std::string& app_name) {

0 commit comments

Comments
 (0)