15
15
#include " auth/src/desktop/secure/user_secure_fake_internal.h"
16
16
17
17
#if defined(_WIN32)
18
+ #include < direct.h>
18
19
#include < windows.h>
19
20
#else
20
21
#include < dirent.h>
21
22
#include < sys/stat.h>
22
23
#include < sys/types.h>
23
24
#endif // defined(_WIN32)
24
25
26
+ #include < errno.h>
27
+
25
28
#include < cstdio>
26
29
#include < fstream>
30
+ #include < string>
27
31
28
32
namespace firebase {
29
33
namespace auth {
30
34
namespace secure {
31
35
36
+ #if defined(_WIN32)
37
+ static const char kDirectorySeparator [] = " \\ " ;
38
+ #define unlink _unlink
39
+ #define mkdir (x, y ) _mkdir(x)
40
+ #define rmdir _rmdir
41
+ #else
42
+ static const char kDirectorySeparator [] = " /" ;
43
+ #endif // defined(_WIN32)
44
+
45
+ static const char kFileExtension [] = " .authbin" ;
46
+
32
47
UserSecureFakeInternal::UserSecureFakeInternal (const char * secure_path)
33
48
: secure_path_(secure_path) {}
34
49
@@ -41,11 +56,12 @@ std::string UserSecureFakeInternal::LoadUserData(const std::string& app_name) {
41
56
std::ifstream infile;
42
57
infile.open (filename, std::ios::binary);
43
58
if (infile.fail ()) {
59
+ LogDebug (" LoadUserData: Failed to read %s" , filename.c_str ());
44
60
return " " ;
45
61
}
46
62
47
63
infile.seekg (0 , std::ios::end);
48
- int64_t length = infile.tellg ();
64
+ size_t length = infile.tellg ();
49
65
infile.seekg (0 , std::ios::beg);
50
66
output.resize (length);
51
67
infile.read (&*output.begin (), length);
@@ -59,11 +75,13 @@ std::string UserSecureFakeInternal::LoadUserData(const std::string& app_name) {
59
75
void UserSecureFakeInternal::SaveUserData (const std::string& app_name,
60
76
const std::string& user_data) {
61
77
// 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
65
- mkdir (secure_path_.c_str (), 0700 );
66
- #endif
78
+ if (mkdir (secure_path_.c_str (), 0700 ) < 0 ) {
79
+ int error = errno;
80
+ if (error != 0 && error != EEXIST) {
81
+ LogWarning (" SaveUserData: Couldn't create directory %s: %d" ,
82
+ secure_path_.c_str (), error);
83
+ }
84
+ }
67
85
68
86
std::string filename = GetFilePath (app_name);
69
87
@@ -88,41 +106,68 @@ void UserSecureFakeInternal::DeleteUserData(const std::string& app_name) {
88
106
}
89
107
90
108
void UserSecureFakeInternal::DeleteAllData () {
109
+ std::vector<std::string> files_to_delete;
91
110
#if defined(_WIN32)
111
+ std::string file_spec =
112
+ secure_path_ + kDirectorySeparator + " *" + kFileExtension ;
92
113
WIN32_FIND_DATA file_data;
93
- HANDLE handle = FindFirstFile (secure_path_ .c_str (), &file_data);
114
+ HANDLE handle = FindFirstFile (file_spec .c_str (), &file_data);
94
115
if (INVALID_HANDLE_VALUE == handle) {
116
+ DWORD error = GetLastError ();
117
+ if (error != ERROR_FILE_NOT_FOUND) {
118
+ LogWarning (" DeleteAllData: Couldn't find file list matching %s: %d" ,
119
+ file_spec.c_str (), error);
120
+ }
95
121
return ;
96
122
}
97
- DeleteFile (file_data.cFileName );
98
- while (FindNextFile (handle, &file_data)) {
99
- DeleteFile (file_data.cFileName );
100
- }
123
+ do {
124
+ std::string file_path =
125
+ secure_path_ + kDirectorySeparator + file_data.cFileName ;
126
+ files_to_delete.push_back (file_path);
127
+ } while (FindNextFile (handle, &file_data));
101
128
FindClose (handle);
102
- RemoveDirectory (secure_path_.c_str ());
103
129
#else
104
130
// These are data types defined in the "dirent" header
105
- DIR* theFolder = opendir (secure_path_.c_str ());
106
- if (!theFolder ) {
131
+ DIR* the_folder = opendir (secure_path_.c_str ());
132
+ if (!the_folder ) {
107
133
return ;
108
134
}
109
135
struct dirent * next_file;
110
136
111
- while ((next_file = readdir (theFolder)) != nullptr ) {
112
- // build the path for each file in the folder
113
- std::string filepath = secure_path_ + " /" ;
114
- filepath.append (next_file->d_name );
115
- unlink (filepath.c_str ());
137
+ while ((next_file = readdir (the_folder)) != nullptr ) {
138
+ // Only delete files matching the file extension.
139
+ if (strcasestr (next_file->d_name , kFileExtension ) !=
140
+ next_file->d_name + strlen (next_file->d_name ) -
141
+ strlen (kFileExtension )) {
142
+ continue ;
143
+ }
144
+ // Build the path for each file in the folder
145
+ std::string file_path =
146
+ secure_path_ + kDirectorySeparator + next_file->d_name ;
147
+ files_to_delete.push_back (file_path);
116
148
}
117
- closedir (theFolder);
118
-
119
- // Remove the directory if it's empty, ignoring errors.
120
- rmdir (secure_path_.c_str ());
149
+ closedir (the_folder);
121
150
#endif
151
+ for (int i = 0 ; i < files_to_delete.size (); ++i) {
152
+ if (unlink (files_to_delete[i].c_str ()) == -1 ) {
153
+ int error = errno;
154
+ if (error != 0 ) {
155
+ LogWarning (" DeleteAllData: Couldn't remove file %s: %d" ,
156
+ files_to_delete[i].c_str (), error);
157
+ }
158
+ }
159
+ }
160
+ // Remove the directory if it's empty, ignoring errors.
161
+ if (rmdir (secure_path_.c_str ()) == -1 ) {
162
+ int error = errno;
163
+ LogDebug (" DeleteAllData: Couldn't remove directory %s: %d" ,
164
+ secure_path_.c_str (), error);
165
+ }
122
166
}
123
167
124
168
std::string UserSecureFakeInternal::GetFilePath (const std::string& app_name) {
125
- std::string filepath = secure_path_ + " /" + app_name + " _bin" ;
169
+ std::string filepath =
170
+ secure_path_ + kDirectorySeparator + app_name + kFileExtension ;
126
171
return filepath;
127
172
}
128
173
0 commit comments