diff --git a/trunk/ConsoleDataMigration/ConsoleBackup/Exporter.cpp b/trunk/ConsoleDataMigration/ConsoleBackup/Exporter.cpp index de4bc64..2e9f4cf 100644 --- a/trunk/ConsoleDataMigration/ConsoleBackup/Exporter.cpp +++ b/trunk/ConsoleDataMigration/ConsoleBackup/Exporter.cpp @@ -353,6 +353,7 @@ void WriteTwlData(enum common::TWL_PATH_INDEX path) std::strlen(common::TWL_ARCHIVE_NAME_TABLE[path]) + 1); std::wstring archiveString(archiveName); common::CopyDirectory( + NULL, (archiveString + ::std::wstring(L"/")).c_str(), (common::SDMC_ROOT_DIRECTORY_PATH + ::std::wstring(common::SD_TWL_ROOTNAME_TABLE[path])).c_str(), buf, bufSize, true, &list, &s_FileListContext); @@ -456,10 +457,11 @@ void ExportThreadFunc() if (result.IsSuccess()) { common::CopyDirectory( - (::std::wstring(common::NAND_DATA_ROOT_PATHNAME_WITH_SLASH) + ::std::wstring(s_RootName) - + ::std::wstring(L"/")).c_str(), - (common::SDMC_ROOT_DIRECTORY_PATH + ::std::wstring(common::SD_SAVEDATA_ROOT_NAME) + ::std::wstring( - s_RootName) + ::std::wstring(L"/")).c_str(), buf, bufSize, true, &list, &s_FileListContext); + NULL, + ::std::wstring(common::NAND_DATA_ROOT_PATHNAME_WITH_SLASH).c_str(), + (std::wstring(common::SDMC_ROOT_DIRECTORY_PATH) + ::std::wstring( + common::SD_SAVEDATA_ROOT_NAME)).c_str(), buf, bufSize, true, &list, + &s_FileListContext); AddCmac(&list, &s_FileListContext); list.TryFlush(); @@ -503,8 +505,6 @@ nn::Result WriteSaveData() // コピー用ディレクトリ作成 s_SdWriter.CreateDirectory((::std::wstring(common::SDMC_ROOT_DIRECTORY_PATH) + ::std::wstring( common::SD_SAVEDATA_ROOT_NAME)).c_str()); - s_SdWriter.CreateDirectory((::std::wstring(common::SDMC_ROOT_DIRECTORY_PATH) + ::std::wstring( - common::SD_SAVEDATA_ROOT_NAME) + ::std::wstring(s_RootName) + ::std::wstring(L"/")).c_str()); COMMON_LOGGER("Export NAND Data Start...\n"); diff --git a/trunk/ConsoleDataMigration/ConsoleRestore/Controller.cpp b/trunk/ConsoleDataMigration/ConsoleRestore/Controller.cpp index b456d5c..d8a6432 100644 --- a/trunk/ConsoleDataMigration/ConsoleRestore/Controller.cpp +++ b/trunk/ConsoleDataMigration/ConsoleRestore/Controller.cpp @@ -46,6 +46,7 @@ typedef enum RestoreState UPDATE_DONE, // アップデート完了 NUP_ONLY_WAIT_SD_EJECT, // NUP_ONLYモードでSDカード抜き待ち NUP_ONLY_WAIT_NEXT, // NUP_ONLYモードでキー入力待ち + RESTORE_READ_FILELIST, // ファイル一覧の読み込み RESTORE_TWL_SOUND, // TWLサウンドの書き込み中 RESTORE_TWL_PHOTO, // TWL写真の書き込み中 RESTORE_IN_PROGRESS, // 書き込み中 @@ -451,7 +452,7 @@ void ControlState(::std::vector& operationMessage, bool& nextStep, } else { - s_RestoreState = RESTORE_TWL_SOUND; + s_RestoreState = RESTORE_READ_FILELIST; } } } @@ -473,7 +474,7 @@ void ControlState(::std::vector& operationMessage, bool& nextStep, if (nextStep) { - s_RestoreState = RESTORE_TWL_SOUND; + s_RestoreState = RESTORE_READ_FILELIST; } } break; @@ -572,6 +573,22 @@ void ControlState(::std::vector& operationMessage, bool& nextStep, } break; + case RESTORE_READ_FILELIST: + { + result = SetupFileList(); + COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); + + if(result.IsSuccess()) + { + s_RestoreState = RESTORE_TWL_SOUND; + } + else + { + s_RestoreState = FAIL; + } + } + break; + // TWLサウンド領域の書き込み中 case RESTORE_TWL_SOUND: diff --git a/trunk/ConsoleDataMigration/ConsoleRestore/Importer.cpp b/trunk/ConsoleDataMigration/ConsoleRestore/Importer.cpp index 3ea970d..35062b4 100644 --- a/trunk/ConsoleDataMigration/ConsoleRestore/Importer.cpp +++ b/trunk/ConsoleDataMigration/ConsoleRestore/Importer.cpp @@ -71,6 +71,16 @@ bool s_CheckedEqualsDeviceIdFileandDeviceId = false; bool s_CheckedEqualsRegionDataandRegion = false; bool s_ReadSerialNumber = false; +struct SdFileSize +{ + s64 totalFileSize; + s64 twlSoundFileSize; + s64 twlPhotoFileSize; + s64 ctrFileSize; +}; + +SdFileSize s_SdFileSize; + // シリアルナンバー u8 s_SerialNo[nn::cfg::CTR::CFG_SECURE_INFO_SERIAL_NO_LEN]; @@ -84,15 +94,14 @@ nn::Result ImportNorData(); // SDカードに保存してあるバージョン情報 common::VerDef s_SDVersionData; -struct ImportDataEntry -{ - std::string fileName; - NN_PADDING3; - bool isDirectory; -}; +// SDカードから読み込んだファイル一覧 +common::ImportDataList s_FileLists; } +// SDからファイル一覧を読み込む +nn::Result ReadFileList(SdFileSize* sdFileSize, common::ImportDataList* fileList); + CheckedNetworkSetting s_CurrentNetowrkSetting1; void ConvertTimeZoneString(const char* str) @@ -676,6 +685,7 @@ void ImportThreadFunc() { common::CopyDirectory( + &s_FileLists, (::std::wstring(common::SDMC_ROOT_DIRECTORY_PATH) + ::std::wstring(common::SD_SAVEDATA_ROOT_NAME)).c_str(), common::NAND_DATA_ROOT_PATHNAME_WITH_SLASH, buf, bufSize, false, NULL, NULL); @@ -697,19 +707,8 @@ void ImportSaveData() // SDカードからNANDに読み込み nn::Result result; - // SDカードにあるセーブデータサイズを計算 - u32 fileNum = 0; - s64 fileSize = 0; - - common::SdMountManager::Mount(); - common::CalculateFileNum(::std::wstring(common::SDMC_ROOT_DIRECTORY_PATH) + ::std::wstring(common::SD_SAVEDATA_ROOT_NAME), fileNum, fileSize); - common::SdMountManager::Unmount(); - // ファイルサイズ設定 - common::InitializeTransferProgress(fileSize); - - NN_LOG("File Number = %d\n", fileNum); - NN_LOG("File Size = %d\n", fileSize); + common::InitializeTransferProgress(s_SdFileSize.ctrFileSize); // SDカードにあるセーブデータディレクトリ以下のデータをNANDにコピー COMMON_LOGGER("Import NAND Data Start...\n"); @@ -1296,7 +1295,7 @@ CheckedNetworkSetting* GetTempNetworkSetting() return &s_CurrentNetowrkSetting1; } -void ImportTwlData(enum common::TWL_PATH_INDEX path) +void ImportTwlData(enum common::TWL_PATH_INDEX path, s64 fileSize) { NN_ASSERT(path < common::TWL_PATHNAME_MAX); @@ -1310,26 +1309,17 @@ void ImportTwlData(enum common::TWL_PATH_INDEX path) size_t bufSize = common::HeapManager::GetHeap()->GetAllocatableSize(); NN_LOG("AllocatableSize = %d\n", bufSize); - - u32 fileNum = 0; - s64 fileSize = 0; - - common::CalculateFileNum(::std::wstring(common::SDMC_ROOT_DIRECTORY_PATH) + ::std::wstring( - common::SD_TWL_ROOTNAME_TABLE[path]), fileNum, fileSize); // ファイルサイズ設定 common::InitializeTransferProgress(fileSize); - NN_LOG("File Number = %d\n", fileNum); - NN_LOG("File Size = %d\n", fileSize); - - void* buf = common::HeapManager::GetHeap()->Allocate(bufSize, AES_BLOCK_SIZE); if (buf != NULL) { - wchar_t archiveName[256]; + wchar_t archiveName[nn::fs::MAX_FILE_PATH_LENGTH]; ::std::mbstowcs(archiveName, common::TWL_ARCHIVE_NAME_TABLE[path], std::strlen(common::TWL_ARCHIVE_NAME_TABLE[path]) + 1); std::wstring archiveString(archiveName); common::CopyDirectory( + &s_FileLists, (::std::wstring(common::SDMC_ROOT_DIRECTORY_PATH) + ::std::wstring(common::SD_TWL_ROOTNAME_TABLE[path])).c_str(), (archiveString + ::std::wstring(L"/")).c_str(), buf, bufSize, false, NULL, NULL); @@ -1345,13 +1335,13 @@ void ImportTwlData(enum common::TWL_PATH_INDEX path) void ImportTwlPhotoDataFunc() { COMMON_LOGGER("Import Twl Photo Data.\n"); - ImportTwlData(common::TWL_PHOTO); + ImportTwlData(common::TWL_PHOTO, s_SdFileSize.twlPhotoFileSize); } void ImportTwlSoundDataFunc() { COMMON_LOGGER("Import Twl Sound Data.\n"); - ImportTwlData(common::TWL_SOUND); + ImportTwlData(common::TWL_SOUND, s_SdFileSize.twlSoundFileSize); } void ImportTwlPhotoData() @@ -1525,7 +1515,18 @@ nn::Result InitializeHardwareDependentSetting() return result; } -nn::Result ReadFileList(s64* totalFileSize, std::vector* fileList) +nn::Result SetupFileList() +{ + std::memset(&s_SdFileSize, 0, sizeof(s_SdFileSize)); + + // ファイル一覧を読み込む + nn::Result result = ReadFileList(&s_SdFileSize, &s_FileLists); + NN_UTIL_RETURN_IF_FAILED(result); + + return result; +} + +nn::Result ReadFileList(SdFileSize* sdFiles, common::ImportDataList* fileList) { nn::Result result = nn::ResultSuccess(); @@ -1562,7 +1563,7 @@ nn::Result ReadFileList(s64* totalFileSize, std::vector* fileLi char *token = NULL; token = std::strtok(reinterpret_cast(buf), comma); bool parseFileName = false; - ImportDataEntry entry; + common::ImportDataEntry entry; entry.fileName = std::string(token); while( token != NULL) @@ -1583,8 +1584,26 @@ nn::Result ReadFileList(s64* totalFileSize, std::vector* fileLi s64 size = std::atoll(token); if(size != -1) { - *totalFileSize += size; entry.isDirectory = false; + sdFiles->totalFileSize += size; + + wchar_t wcToken[nn::fs::MAX_FILE_PATH_LENGTH]; + if(std::mbstowcs(wcToken, entry.fileName.c_str(), entry.fileName.size()) != entry.fileName.size() - 1) + { + if(std::wcsstr(wcToken, common::SD_SAVEDATA_ROOT_NAME) != NULL) + { + sdFiles->ctrFileSize += size; + } + else if(std::wcsstr(wcToken, common::SD_SAVEDATA_TWL_PHOTO_ROOT_NAME) != NULL) + { + sdFiles->twlPhotoFileSize += size; + } + else if(std::wcsstr(wcToken, common::SD_SAVEDATA_TWL_SOUND_ROOT_NAME) != NULL) + { + sdFiles->twlSoundFileSize += size; + } + + } } else { @@ -1621,12 +1640,6 @@ nn::Result ImportData() // NANDのごみを削除する Cleanup(); - // ファイル一覧を読み込む - s64 totalFileSize = 0; - std::vector fileList; - result = ReadFileList(&totalFileSize, &fileList); - NN_UTIL_RETURN_IF_FAILED(result); - // SDカードのIVSファイルを書き込む result = ImportIvs(); NN_UTIL_RETURN_IF_FAILED(result); diff --git a/trunk/ConsoleDataMigration/ConsoleRestore/Importer.h b/trunk/ConsoleDataMigration/ConsoleRestore/Importer.h index 8c31c75..855130e 100644 --- a/trunk/ConsoleDataMigration/ConsoleRestore/Importer.h +++ b/trunk/ConsoleDataMigration/ConsoleRestore/Importer.h @@ -26,6 +26,7 @@ namespace ConsoleRestore nn::Result EqualsDeviceIdFileandDeviceId(); nn::Result EqualsRegionDataandRegion(); nn::Result ReadSerialNumber(u8* serial); +nn::Result SetupFileList(); void FinalizeImportThread(); bool IsImportFinished(); diff --git a/trunk/ConsoleDataMigration/common/FileTransfer.cpp b/trunk/ConsoleDataMigration/common/FileTransfer.cpp index eb80e54..beb2021 100644 --- a/trunk/ConsoleDataMigration/common/FileTransfer.cpp +++ b/trunk/ConsoleDataMigration/common/FileTransfer.cpp @@ -14,6 +14,7 @@ *---------------------------------------------------------------------------*/ #include +#include #include #include @@ -48,7 +49,7 @@ void AddPathNameAndUpdateContext(nn::fs::FileOutputStream* file, const wchar_t * const char* GetCharStr(const wchar_t* path) { - static char filename[256]; + static char filename[nn::fs::MAX_FILE_PATH_LENGTH]; std::memset(filename, 0, sizeof(filename)); std::wcstombs(filename, path, sizeof(filename)); filename[sizeof(filename) - 1] = '\0'; @@ -108,14 +109,39 @@ nn::Result CalculateFileNum(std::wstring currentDirectory, u32& fileNum, s64& fi } +bool ExistsInList(ImportDataList* fileList, const wchar_t* path, bool isDirectory) +{ + std::wstring sdPath(path); + if(isDirectory) + { + sdPath += std::wstring(L"/"); + } + + char str[nn::fs::MAX_FILE_PATH_LENGTH]; + std::strlcpy(str, GetCharStr(sdPath.c_str()), sizeof(str)); + + bool returnValue = false; + for(ImportDataList::iterator it = fileList->begin(); it != fileList->end(); it++) + { + if(std::strcmp(str, it->fileName.c_str()) == 0) + { + returnValue = true; + NN_LOG("%s exists in FileList.txt\n", str); + break; + } + } + + return returnValue; +} + // ディレクトリ間のコピー // アーカイブ越しのコピーが可能 // アーカイブにマウントした状態で呼び出す必要あり // 書き込み先のディレクトリはあらかじめ消去しておくこと。 // 引数はスラッシュ付き // TODO:分割して短くする -bool CopyDirectory(const wchar_t * from_path, const wchar_t * to_path, void* buf, const size_t bufSize, bool encode, - nn::fs::FileOutputStream* list, nn::crypto::Sha256Context* listContext) +bool CopyDirectory(ImportDataList* fileList, const wchar_t * from_path, const wchar_t * to_path, void* buf, + const size_t bufSize, bool encode, nn::fs::FileOutputStream* list, nn::crypto::Sha256Context* listContext) { nn::fs::Directory from_dir; nn::fs::DirectoryEntry entry; @@ -153,6 +179,16 @@ bool CopyDirectory(const wchar_t * from_path, const wchar_t * to_path, void* buf target_to.clear(std::stringstream::goodbit); target_to << to_path << entry.entryName; + // NAND書き込みの場合はリストに存在するかチェックする + if (!encode) + { + if (!ExistsInList(fileList, target_from.str().c_str(), entry.attributes.isDirectory)) + { + NN_LOG("============No such file %ls in FileList.txt. Skip=============\n", target_from.str().c_str()); + continue; + } + } + // ディレクトリの場合 if (entry.attributes.isDirectory) { @@ -175,7 +211,7 @@ bool CopyDirectory(const wchar_t * from_path, const wchar_t * to_path, void* buf // 再帰処理 - if (!CopyDirectory(target_from.str().c_str(), target_to.str().c_str(), buf, bufSize, encode, list, listContext)) + if (!CopyDirectory(fileList, target_from.str().c_str(), target_to.str().c_str(), buf, bufSize, encode, list, listContext)) { ret_value = false; } @@ -585,7 +621,7 @@ bool ConfirmFile(nn::fs::FileInputStream* from_file, nn::fs::FileStream* to_file if (!VerifyMac(from_file, to_file, sdFileSize, nandFileSize, buf, bufSize)) { // 検証に失敗したので削除する - COMMON_LOGGER("Verification Failed %s, Delete\n", GetCharStr(sdPath)); + COMMON_LOGGER("**********Verification Failed %s, Delete**********\n", GetCharStr(sdPath)); result = nn::fs::TryDeleteFile(tmpPath); COMMON_LOGGER_RESULT_IF_FAILED_WITH_LINE(result); ret_value = false; diff --git a/trunk/ConsoleDataMigration/common/FileTransfer.h b/trunk/ConsoleDataMigration/common/FileTransfer.h index 1147bf8..15f7fd8 100644 --- a/trunk/ConsoleDataMigration/common/FileTransfer.h +++ b/trunk/ConsoleDataMigration/common/FileTransfer.h @@ -19,13 +19,14 @@ #include #include #include +#include "common_Types.h" namespace common { nn::Result CalculateFileNum(std::wstring currentDirectory, u32& fileNum, s64& fileSize); -bool CopyDirectory(const wchar_t * from_path, const wchar_t * to_path, void* buf, const size_t bufSize, bool encode, - nn::fs::FileOutputStream* list, nn::crypto::Sha256Context* listContext); +bool CopyDirectory(ImportDataList* fileList, const wchar_t * from_path, const wchar_t * to_path, void* buf, + const size_t bufSize, bool encode, nn::fs::FileOutputStream* list, nn::crypto::Sha256Context* listContext); u32 GetProgress(); void InitializeTransferProgress(u64 totalSize); diff --git a/trunk/ConsoleDataMigration/common/common_Types.h b/trunk/ConsoleDataMigration/common/common_Types.h index 2a354a3..4f54f13 100644 --- a/trunk/ConsoleDataMigration/common/common_Types.h +++ b/trunk/ConsoleDataMigration/common/common_Types.h @@ -23,6 +23,9 @@ #include #include +#include +#include + namespace common { @@ -87,6 +90,14 @@ struct BackupDataHeader NN_PADDING4; }; +struct ImportDataEntry +{ + std::string fileName; + NN_PADDING3; + bool isDirectory; +}; + +typedef std::vector ImportDataList; } #endif /* COMMON_TYPES_H_ */