forked from crazy4linux/SP_Flash_Tool_src_v3.1113.0.189
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFileUtils.cpp
477 lines (400 loc) · 13 KB
/
FileUtils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/*
* FileUtils.cpp
*
* Created on: Dec 30, 2010
* Author: MTK81019
*/
#include "FileUtils.h"
#include <windows.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fstream>
#include <iomanip>
#include <io.h>
#include "Utility.h"
//Log files
#include "Logger.h"
#include "brom.h"
#include "eboot\interface.h"
#include "meta.h"
#include "WMMETA_Wrapper.h"
#ifdef __BORLANDC__
#pragma warn -8004
#endif
namespace FileUtils{
using std::string;
const std::string kBackupFolder("BackupData");
}
bool FileUtils::CreateFiles(std::string fileName)
{
FILE *fpOut=fopen(fileName.c_str(), "wb+");
if(fpOut == NULL) return false;
fclose(fpOut);
return true;
}
std::string FileUtils::GetTimestamp(){
char infoTimestamp[_MAX_FNAME] = { 0 };
SYSTEMTIME timestamp;
GetLocalTime(×tamp);
sprintf(infoTimestamp,
"%02d-%02d-%02d-%02d-%02d-%02d",
timestamp.wMonth, timestamp.wDay, timestamp.wYear, timestamp.wHour,
timestamp.wMinute, timestamp.wSecond);
return string(infoTimestamp);
}
E_CHECK_DIR_STATUS FileUtils::CheckDirectory(const std::string &dir, bool createIfNotExist) {
if(FileUtils::IsDirectoryExist(dir)){
LOG("%s(): the dir(%s) already exists.", __FUNC__, dir);
return DIR_EXIST;
}
if(!createIfNotExist){
LOG("%s(): the dir(%s) does not exist.", __FUNC__, dir);
return DIR_NOT_EXIST;
}
WCHAR wszAndroidPath[256];
MultiByteToWideChar(CP_ACP, 0, dir.c_str(), -1, wszAndroidPath,
sizeof(wszAndroidPath) / sizeof(wszAndroidPath[0]));
if (::CreateDirectoryW(wszAndroidPath, NULL) == 0) {
LOG("%s(): create the dir(%s) failed.", __FUNC__, dir);
return CREATE_DIR_FAIL; //create directory fail
} else {
LOG("%s(): create the dir(%s) succeed.", __FUNC__, dir);
return CREATE_DIR_SUCCESS; //create directory successfully
}
}
/**
* Check whether the directory exists
*
* @param directory to be checked
*
* @return whether the directory exists
*/
bool FileUtils::IsDirectoryExist(const string & dir) {
struct stat fileInfo;
int ret = stat(dir.c_str(), &fileInfo);
return ( ret == 0 && S_ISDIR(fileInfo.st_mode));
}
/**
* Check whether the file exists
*
* @param file to be checked
*
* @return whether the file exists
*/
bool FileUtils::IsFileExist(const string & file) {
struct stat fileInfo;
int ret = stat(file.c_str(), &fileInfo);
return ( ret == 0 && S_ISREG(fileInfo.st_mode));
}
/**
* Delete a directory
*
* @param directory to be deleted
*
* @return whether the directory is deleted
*/
bool FileUtils::DeleteDirectory(const std::string &dir) {
std::vector<std::string> files;
bool isFindFiles = FileUtils::SearchFile(dir.c_str(), files, "");
string tmpFile;
if(isFindFiles) {
for(std::vector<std::string>::const_iterator it = files.begin();
it != files.end(); ++it) {
tmpFile = dir + "\\" + *it;
DeleteFile(tmpFile.c_str());
}
}
return RemoveDirectory(dir.c_str());
}
bool FileUtils::DeleteDummyFile(const std::string &file_name)
{
return DeleteFile(file_name.c_str());
}
std::string FileUtils::GetAppDirectory() {
char buf[MAX_PATH];
GetModuleFileName(NULL, buf, MAX_PATH);
std::string currentPath(buf);
return currentPath.substr(0, currentPath.rfind('\\'));
}
#if 0 //deprecated function
E_CHECK_DIR_STATUS FileUtils::CheckDirByCOMPort(int port, std::string &dir,
bool createIfNotExist){
string parentDir = FileUtils::GetAppDirectory()+"\\BackupNvram";
E_CHECK_DIR_STATUS status = FileUtils::CheckDirectory(parentDir,createIfNotExist);
if(status == DIR_NOT_EXIST || status == CREATE_DIR_FAIL){
return status;
}
dir = parentDir + "\\" + NumberToString(port);
return FileUtils::CheckDirectory(dir,createIfNotExist);
}
E_CHECK_DIR_STATUS FileUtils::CheckDirByChipID(unsigned char* chipId, unsigned int length,
std::string &dir, bool createIfNotExist){
std::ostringstream oss;
oss<<std::hex;
for(unsigned int i=0;i<length;++i){
oss<<std::setfill('0')<<std::setw(2)<<static_cast<unsigned int>(chipId[i]);
}
string chipName = oss.str();
string parentDir = FileUtils::GetAppDirectory()+"\\BackupNvram";
E_CHECK_DIR_STATUS status = FileUtils::CheckDirectory(parentDir, createIfNotExist);
if(status == DIR_NOT_EXIST || status == CREATE_DIR_FAIL){
return status;
}
dir = parentDir + "\\" + chipName;
return FileUtils::CheckDirectory(dir, createIfNotExist);
}
#endif
bool FileUtils::MakeParentBackupDir(const string& folder)
{
E_CHECK_DIR_STATUS backup_dir_status =
FileUtils::CheckDirectory(FileUtils::GetAppDirectory()+"\\" + folder, true);
return ( DIR_EXIST == backup_dir_status ||
CREATE_DIR_SUCCESS == backup_dir_status )
? true : false;
}
std::string FileUtils::ComposeDirNameByTimeStamp(const string &parentDir, const string &prefix)
{
std::string dirName = parentDir + "\\" +prefix + GetTimestamp();
return dirName;
}
std::string FileUtils::ComposeDirNameByChipID(const unsigned char* chipId, const unsigned int length, const string& folder)
{
std::ostringstream oss;
oss<<std::hex;
for(unsigned int i=0;i<length;++i){
oss<<std::setfill('0')<<std::setw(2)<<static_cast<unsigned int>(chipId[i]);
}
string chipName = oss.str();
string dirName = FileUtils::GetAppDirectory()+ "\\" + folder + "\\" +chipName;
LOG("%s(): folder name: %s.", __FUNC__, dirName.c_str());
return dirName;
}
std::string FileUtils::ComposeDirNameByCOMPort(const int port, const string& folder)
{
string dirName = FileUtils::GetAppDirectory()+ "\\" + folder + "\\" +NumberToString(port);
LOG("%s(): folder name: %s.", __FUNC__, dirName.c_str());
return dirName;
}
bool FileUtils::SearchFile(std::string folder, std::vector<std::string> &files,
std::string pattern, COMPARE_METHOD compare_m, bool recursive) {
WIN32_FIND_DATA find_data;
HANDLE hFind;
bool bIsDirectory;
string file_name, file_path, search_path;
search_path = folder + "\\*.*";
hFind = FindFirstFile(search_path.c_str(), &find_data);
if (hFind == INVALID_HANDLE_VALUE) {
return false;
}
do {
bIsDirectory = ((find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
!= 0);
file_name = find_data.cFileName;
if (!bIsDirectory) {
if (compare_m == NULL) {
if (pattern == "") {
//add all files if no pattern defined
files.push_back(file_name);
} else if (pattern == file_name) {
files.push_back(file_name);
}
} else {
if (compare_m(file_name, pattern)) {
files.push_back(file_name);
}
}
} else if (recursive) {
if ((".." != file_name) && ("." != file_name)) {
file_path = folder + "\\";
file_path += find_data.cFileName;
SearchFile(file_path, files, pattern, compare_m, recursive);
}
}
} while (FindNextFile(hFind, &find_data));
FindClose(hFind);
return true;
}
bool FileUtils::ListFiles(std::string folder, std::vector<std::string> &files){
return FileUtils::SearchFile(folder, files, "");
}
bool FileUtils::FindFileByPrefix(std::vector<std::string> &files, const std::string prefix, std::vector<std::string> &satisified_files)
{
for(std::vector<std::string>::const_iterator it = files.begin();
it != files.end(); ++it)
{
if((*it).find(prefix) == 0)
{
LOGD("Find object file(%s), prefix(%s)", (*it).c_str(), prefix.c_str());
satisified_files.push_back((*it));
}
}
return (satisified_files.size() > 0);
}
/**
* Get the size of the given file.
*
* @param filename a filename
*
* @return size of the given file
*/
unsigned int FileUtils::GetFileSize(const std::string &filename)
{
//
// FIXME: Should use some filesystem API instead
//
unsigned int size;
std::ifstream is(filename.c_str(), std::ios::in | std::ios::binary);
is.seekg(0, std::ios::end);
size = is.tellg();
is.close();
return size;
}
/**
* Get the content of the given file. The caller is responsible for memory
* deallocation.
*
* @param filename a filename
*
* @return content of the given file
*/
unsigned char* FileUtils::GetFileContent(const std::string &filename)
{
std::ifstream is(filename.c_str(), std::ios::in | std::ios::binary);
is.seekg(0, std::ios::end);
const unsigned int size = is.tellg();
unsigned char *content = new unsigned char[size];
is.seekg(0, std::ios::beg);
is.read(content, size);
is.close();
return content;
}
/**
* Set debug log file on
*
* @param NULL
*
* @return NULL
*/
void FileUtils::DebugLogsOn( void ) {
std::string dll_log;
Logger::SetSPFlashToolDumpFileFolder();
Logger::DebugOff();
Logger::DebugOn();
dll_log = Logger::GetSPFlashToolDumpFileFolder() + "\\EBOOT_DLL.log";
Eboot_DebugChangePath(dll_log.c_str());
dll_log = Logger::GetSPFlashToolDumpFileFolder() + "\\BROM_DLL_V5.log";
Brom_Debug_SetLogFilename(dll_log.c_str());
Brom_DebugOn();
dll_log = Logger::GetSPFlashToolDumpFileFolder() + "\\META_DLL.log";
META_DebugOn_With_FilePath(dll_log.c_str());
dll_log = Logger::GetSPFlashToolDumpFileFolder() + "\\SP_META_DLL.log";
SP_META_DebugOnThePath_Wrapper(dll_log.c_str());
}
/**
* Set debug log file on
*
* @param NULL
*
* @return NULL
*/
void FileUtils::DebugLogsOff( void ) {
Logger::DebugOff();
Eboot_DebugOff();
Brom_DebugOff();
META_DebugOff();
WM_META_DebugOff_Wrapper();
}
bool FileUtils::ComputeCloneCheckSum(const std::string &src_file, S_CHKSUM_Arg &chksum_arg)
{
unsigned char buffer[1024];
FILE *file = fopen ( src_file.c_str() , "rb" );
if (file==NULL) {
LOG("%s(): file open failed: %s.", __FUNC__, src_file.c_str());
return false;
}
chksum_arg.m_cb_rom_mem_checksum_init(chksum_arg.m_cb_rom_mem_checksum_init_arg,
src_file.c_str());
chksum_arg.chksum_value= 0;
unsigned int read_size = 0;
static unsigned char lastPercentage = 0;
unsigned char percentage = 0;
unsigned int file_len = 0;
unsigned int position = 0;
file_len = filelength(fileno(file));
while(!feof(file)){
read_size = fread(buffer, 1, 1024, file);
position = ftell(file);
percentage = static_cast<unsigned char>((static_cast<float> (position)/file_len)*100);
if (lastPercentage != percentage)
{
lastPercentage = percentage;
chksum_arg.m_cb_rom_mem_checksum(percentage,
position, read_size, chksum_arg.m_cb_rom_mem_checksum_arg);
}
for(unsigned int i=0;i<read_size;i++){
if(BOOT_STOP == *(chksum_arg.stopflag))
{
LOG("%s: user press stop button.", __FUNC__);
return false;
}
chksum_arg.chksum_value += buffer[i];
}
}
chksum_arg.m_cb_rom_mem_checksum(100, file_len, file_len, chksum_arg.m_cb_rom_mem_checksum_arg);
LOG("%s(): check sum result(%u).", __FUNC__, chksum_arg.chksum_value);
fclose (file);
return true;
}
bool FileUtils::ComputeCheckSum(const std::string &src_file, unsigned char &chksum)
{
unsigned char buffer[1024];
FILE *file = fopen ( src_file.c_str() , "rb" );
if (file==NULL) {
LOG("%s(): file open failed: %s.", __FUNC__, src_file.c_str());
return false;
}
chksum = 0;
unsigned int read_size = 0;
while(!feof(file)){
read_size = fread(buffer, 1, 1024, file);
for(unsigned int i=0;i<read_size;i++){
chksum += buffer[i];
}
}
LOG("%s(): check sum result(%u).", __FUNC__, chksum);
fclose (file);
return true;
}
bool FileUtils::LoadCheckSum(const std::string &chksum_file, unsigned char &chksum)
{
unsigned char buffer[2];
FILE *file = fopen ( chksum_file.c_str() , "rb" );
if (file==NULL) {
LOG("%s(): file open failed: %s.", __FUNC__, chksum_file.c_str());
return false;
}
unsigned int read_size = 0;
read_size = fread(buffer, 1, 1024, file);
if(read_size <= 0){
LOG("%s(): read file error: %s", __FUNC__, chksum_file.c_str());
return false;
}
chksum = buffer[0];
LOG("%s(): check sum result(%u).", __FUNC__, chksum);
fclose (file);
return true;
}
bool FileUtils::SaveCheckSum(const std::string &chksum_file, const unsigned char chksum)
{
unsigned char buffer[2] = {0};
buffer[0] = chksum;
FILE *file = fopen ( chksum_file.c_str() , "wb" );
if (file==NULL) {
LOG("%s(): file create/open failed: %s.", __FUNC__, chksum_file.c_str());
return false;
}
fwrite (buffer , 1 , sizeof(buffer) , file );
LOG("%s(): check sum result(%u).", __FUNC__, chksum);
fclose (file);
return true;
}