w_string realPath(const char *path) { auto options = OpenFileHandleOptions::queryFileInfo(); // Follow symlinks, because that's really the point of this function options.followSymlinks = 1; options.strictNameChecks = 0; #ifdef _WIN32 // Special cases for cwd w_string_piece pathPiece(path); // On Windows, "" is used to refer to the CWD. // We also allow using "." for parity with unix, even though that // doesn't generally work for that purpose on windows. // This allows `watchman watch-project .` to succeeed on windows. if (pathPiece.size() == 0 || pathPiece == ".") { std::wstring wchar; wchar.resize(WATCHMAN_NAME_MAX); auto len = GetCurrentDirectoryW(wchar.size(), &wchar[0]); auto err = GetLastError(); if (len == 0) { throw std::system_error(err, std::system_category(), "GetCurrentDirectoryW"); } // Assumption: that the OS maintains the CWD in canonical form return w_string(wchar.data(), len); } #endif auto handle = openFileHandle(path, options); return handle.getOpenedPath(); }
/// /// Initialization with the file name to use. /// void TraceUtil::initialize(const char* fileNameToUse) { impl_->fileName_ = fileNameToUse; // Open the file, deleting its old contents openFileHandle(); }
bool PeParser::readPeSectionsFromFile() { bool retValue = true; DWORD readOffset = 0; listPeSection.reserve(getNumberOfSections()); if (openFileHandle()) { for (WORD i = 0; i < getNumberOfSections(); i++) { readOffset = listPeSection[i].sectionHeader.PointerToRawData; listPeSection[i].normalSize = listPeSection[i].sectionHeader.SizeOfRawData; if (!readSectionFromFile(readOffset, listPeSection[i])) { retValue = false; } } closeFileHandle(); } else { retValue = false; } return retValue; }
bool PeParser::getFileOverlay() { DWORD numberOfBytesRead; bool retValue = false; if (!hasOverlayData()) { return false; } if (openFileHandle()) { DWORD overlayOffset = getSectionHeaderBasedFileSize(); DWORD fileSize = (DWORD)ProcessAccessHelp::getFileSize(hFile); overlaySize = fileSize - overlayOffset; overlayData = new BYTE[overlaySize]; SetFilePointer(hFile, overlayOffset, 0, FILE_BEGIN); if (ReadFile(hFile, overlayData, overlaySize, &numberOfBytesRead, 0)) { retValue = true; } closeFileHandle(); } return retValue; }
bool PeParser::readPeHeaderFromFile(bool readSectionHeaders) { bool retValue = false; DWORD correctSize = 0; DWORD numberOfBytesRead = 0; DWORD readSize = getInitialHeaderReadSize(readSectionHeaders); headerMemory = new BYTE[readSize]; if (openFileHandle()) { fileSize = (DWORD)ProcessAccessHelp::getFileSize(hFile); if (ReadFile(hFile, headerMemory, readSize, &numberOfBytesRead, 0)) { retValue = true; getDosAndNtHeader(headerMemory, (LONG)readSize); if (isValidPeFile()) { correctSize = calcCorrectPeHeaderSize(readSectionHeaders); if (readSize < correctSize) { readSize = correctSize; if (fileSize > 0) { if (fileSize < correctSize) { readSize = fileSize; } } delete [] headerMemory; headerMemory = new BYTE[readSize]; SetFilePointer(hFile, 0, 0, FILE_BEGIN); if (ReadFile(hFile, headerMemory, readSize, &numberOfBytesRead, 0)) { getDosAndNtHeader(headerMemory, (LONG)readSize); } } } } closeFileHandle(); } return retValue; }
FileInformation getFileInformation(const char *path, CaseSensitivity caseSensitive) { auto options = OpenFileHandleOptions::queryFileInfo(); options.caseSensitive = caseSensitive; #if defined(_WIN32) || defined(O_PATH) // These operating systems allow opening symlink nodes and querying them // for stat information auto handle = openFileHandle(path, options); auto info = handle.getInfo(); return info; #else // Since the leaf of the path may be a symlink, and this system doesn't // allow opening symlinks for stat purposes, we have to resort to performing // a relative fstatat() from the parent dir. w_string_piece pathPiece(path); auto parent = pathPiece.dirName().asWString(); auto handle = openFileHandle(parent.c_str(), options); struct stat st; if (fstatat( handle.fd(), pathPiece.baseName().data(), &st, AT_SYMLINK_NOFOLLOW)) { throw std::system_error(errno, std::generic_category(), "fstatat"); } if (caseSensitive == CaseSensitivity::Unknown) { caseSensitive = getCaseSensitivityForPath(path); } if (caseSensitive == CaseSensitivity::CaseInSensitive) { // We need to perform one extra check for case-insensitive // paths to make sure that we didn't accidentally open // the wrong case name. checkCanonicalBaseName(path); } return FileInformation(st); #endif }
void TraceUtil::setFileName(const char* fileNameToUse) { std::string temporaryString(fileNameToUse); if (temporaryString.length() <= 0) { return; } // Close the existing file handle if one is already opened. closeFileHandle(); // Open or reopen the file handle. impl_->fileName_ = temporaryString; openFileHandle(); }
w_string readSymbolicLink(const char* path) { #ifndef _WIN32 std::string result; // Speculatively assume that this is large enough to read the // symlink text. This helps to avoid an extra lstat call. result.resize(256); for (int retry = 0; retry < 2; ++retry) { auto len = readlink(path, &result[0], result.size()); if (len < 0) { throw std::system_error( errno, std::generic_category(), "readlink for readSymbolicLink"); } if (size_t(len) < result.size()) { return w_string(result.data(), len); } // Truncated read; we need to figure out the right size to use struct stat st; if (lstat(path, &st)) { throw std::system_error( errno, std::generic_category(), "lstat for readSymbolicLink"); } result.resize(st.st_size + 1, 0); } throw std::system_error( E2BIG, std::generic_category(), "readlink for readSymbolicLink: symlink changed while reading it"); #else return openFileHandle(path, OpenFileHandleOptions::queryFileInfo()) .readSymbolicLink(); #endif }
void ModulatorSamplerSound::openFileHandle() { FOR_EVERY_SOUND(openFileHandle()); }
TitleList *getUpdateCIAs() { Handle dir, CIAFile; FS_DirectoryEntry ent; int ret; int ciafiles; TitleList *updateCIAs; char ciaPath[9 + 16 + 4 + 1]; // /updates/ + 16 hex digits + .cia + \0 LOG_INFO("getUpdateCIAs"); // Run through first and count .cia files. dir = openDirectory(CIAS_PATH); if(dir == 0) { LOG_ERROR("getUpdateCIAs: Failed to open SDMC:" CIAS_PATH ".\n"); printf("Failed to open SDMC:" CIAS_PATH ".\n"); goto error0; } ciafiles = 0; for(;;) { ret = getNextFile(dir, &ent); if(ret < 1) { if(ret == -1) { LOG_ERROR("getUpdateCIAs: Error reading directory."); printf("Error reading directory.\n"); goto error2; } break; } simpleUTF16toASCII((char *)(ent.name)); if(isCIAName((char *)(ent.name)) == 1) { ciafiles++; } } closeDirectory(dir); LOG_VERBOSE("Found %d files.", ciafiles); updateCIAs = initTitleList(ciafiles, 0); // Run through a second time and add CIA info. dir = openDirectory(CIAS_PATH); if(dir == 0) { LOG_ERROR("getUpdateCIAs: Failed to open SDMC:" CIAS_PATH ".\n"); printf("Failed to open SDMC:" CIAS_PATH ".\n"); goto error1; } ciafiles = 0; for(;;) { ret = getNextFile(dir, &ent); if(ret < 1) { if(ret == -1) { LOG_ERROR("getUpdateCIAs: Error reading directory."); printf("Error reading directory.\n"); goto error2; } break; } simpleUTF16toASCII((char *)(ent.name)); if(isCIAName((char *)(ent.name)) == 1) { snprintf(ciaPath, 9 + 16 + 4 + 1, CIAS_PATH "%s", ent.name); CIAFile = openFileHandle(ciaPath, FS_OPEN_READ); if(CIAFile == 0) { LOG_ERROR("getUpdateCIAs: Failed to open %s for read.\n", ciaPath); printf("Failed to open %s for read.\n", ciaPath); goto error2; } if(R_FAILED(AM_GetCiaFileInfo(MEDIATYPE_NAND, updateCIAs->title[ciafiles], CIAFile))) { LOG_ERROR("getUpdateCIAs: Failed to get information on %s.\n", ciaPath); printf("Failed to get information on %s.\n", ciaPath); goto error3; } closeFileHandle(CIAFile); ciafiles++; LOG_VERBOSE("getUpdateCIAs: Added %s.", (char *)(ent.name)); } } closeDirectory(dir); LOG_INFO("getUpdateCIAs: Got CIAs from SD card."); return(updateCIAs); error3: closeFileHandle(CIAFile); error2: closeDirectory(dir); error1: freeTitleList(updateCIAs); error0: LOG_ERROR("getUpdateCIAs: Failed to get CIAs from SD card."); return(NULL); }
int installTitleFromCIA(const char *path, PrintConsole *con) { Handle CIAIn, CIAOut; u32 bytesread; u32 byteswritten; u64 totalread; int animpos; LOG_INFO("installTitleFromCIA: Installing CIA from %s.", path); CIAIn = openFileHandle(path, FS_OPEN_READ); if(CIAIn == 0) { LOG_ERROR("installTitleFromCIA: Failed to open %s.", path); printf("Failed to open file %s.\n", path); goto error0; } #ifdef ARMED if(R_FAILED(AM_StartCiaInstall(MEDIATYPE_NAND, &CIAOut))) { LOG_ERROR("installTitleFromCIA: AM_StartCiaInstall failed."); printf("Failed to start CIA install.\n"); goto error1; } #endif bytesread = BUFFERSIZE; totalread = 0; while(bytesread == BUFFERSIZE) { printf("%c", anim[animpos]); stepFrame(); con->cursorX--; animpos = (animpos + 1) % 4; if(R_FAILED(FSFILE_Read(CIAIn, &bytesread, totalread, buffer, BUFFERSIZE))) { printf("\nFailed to read %s around %llu!\n", path, totalread); stepFrame(); goto error2; } #ifdef ARMED if(R_FAILED(FSFILE_Write(CIAOut, &byteswritten, totalread, buffer, bytesread, FS_WRITE_FLUSH))) { printf("\nFailed to write %s around %llu!\n", path, totalread); stepFrame(); goto error2; } if(byteswritten < bytesread) { LOG_ERROR("installTitleFromCIA: Incomplete write around %llu.", totalread); printf("\nIncompelete write around %llu!\n", totalread); stepFrame(); goto error2; } #endif totalread += bytesread; } #ifdef ARMED if(R_FAILED(AM_FinishCiaInstall(MEDIATYPE_NAND, &CIAOut))) { LOG_ERROR("installTitleFromCIA: AM_FinishCiaInstall failed."); printf("Failed to finalize CIA install.\n"); goto error2; } #endif closeFileHandle(CIAIn); LOG_INFO("Successfully installed %s.", path); return(0); error2: #ifdef ARMED if(R_FAILED(AM_CancelCIAInstall(&CIAOut))) { printf("Couldn't cancel unsuccessful CIA install.\n"); } #endif error1: closeFileHandle(CIAIn); error0: LOG_ERROR("Failed installing %s.", path); return(-1); }
int checkUpdates(const UpdateInfo *info, TitleList *updateCIAs, PrintConsole *con) { Handle CIAHandle; char filename[UPDATE_FILENAME_LEN]; md5_state_t md5; u32 bytesread; u64 totalread; char digest[16]; char strdigest[33]; // 32 hex digits + \0 TitleList *extraTitles; int i, j; int animpos; extraTitles = initTitleList(updateCIAs->nTitles, 1); if(extraTitles == NULL) { printf("Couldn't create title list.\n"); goto error0; } memcpy(extraTitles->title, updateCIAs->title, sizeof(AM_TitleEntry *) * updateCIAs->nTitles); if(extraTitles->nTitles == 0) { printf("No titles found to check.\nThere is nothing to do.\n"); goto error1; } for(i = 0; i < info->nUpdates; i++) { snprintf(filename, UPDATE_FILENAME_LEN, CIAS_PATH "%016llX.cia", info->updates[i].titleID); printf("Checking %s... ", filename); stepFrame(); CIAHandle = openFileHandle(filename, FS_OPEN_READ); if(CIAHandle == 0) { if(info->info.region == CFG_REGION_JPN && info->updates[i].titleID == 0x000400102002CA00) { printf("Bad update missing, this is good.\n"); } else { printf("\nOpen Failed!\n", filename); goto error1; } } md5_init(&md5); bytesread = BUFFERSIZE; totalread = 0; animpos = 0; while(bytesread == BUFFERSIZE) { printf("%c", anim[animpos]); con->cursorX--; animpos = (animpos + 1) % 4; if(R_FAILED(FSFILE_Read(CIAHandle, &bytesread, totalread, buffer, BUFFERSIZE))) { printf("\nFailed to read %s around %s!\n", filename, totalread); stepFrame(); freeTitleList(extraTitles); goto error2; } md5_append(&md5, buffer, bytesread); totalread += bytesread; } closeFileHandle(CIAHandle); md5_finish(&md5, digest); printf(" \n"); // Cheezy bin2hex snprintf(strdigest, 33, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x \n", digest[0], digest[1], digest[2], digest[3], digest[4], digest[5], digest[6], digest[7], digest[8], digest[9], digest[10], digest[11], digest[12], digest[13], digest[14], digest[15]); printf("%s ", strdigest); stepFrame(); if(strncmp(info->updates[i].md5, strdigest, 33) != 0) { printf("!= \n%s FAIL\n", info->updates[i].md5); goto error1; } if(info->info.region == CFG_REGION_JPN && info->updates[i].titleID == 0x000400102002CA00) { printf("Firmware file known to brick Japanese 3DS\n" \ "present! It is recommended you delete the\n" \ "file named " CIAS_PATH "000400102002CA00.cia\n" \ "before installing the updates!\n" \ "Press any key to continue . . .\n"); stepFrame(); waitKey(); } else { printf("OK\n"); stepFrame(); for(j = 0; j < extraTitles->nTitles; j++) { if(extraTitles->title[j] == NULL) { continue; } if(extraTitles->title[j]->titleID == info->updates[i].titleID) { extraTitles->title[j] = NULL; break; } } } } rearrangeTitles(extraTitles); for(i = 0; i < extraTitles->nTitles; i++) { if(extraTitles->title[i] == NULL) { extraTitles->nTitles = i; break; } } if(extraTitles->nTitles > 0) { printf("These are the titles that were found but weren't\n" \ "in the internal hash tables, meaning they aren't\n" \ "part of the official update. They will be\n" \ "installed if you choose to install now. Look\n" \ "over them to see if they are what you want before\n" \ "choosing to install."); waitKey(); printTitlesMore(extraTitles, con); printf("Press any key to continue . . ."); waitKey(); printf("\n"); } else { printf("No excess titles.\n"); } freeTitleList(extraTitles); return(0); error2: closeFileHandle(CIAHandle); error1: freeTitleList(extraTitles); error0: return(-1); }