/** * Removes a directory by recursively removing sub-directory and files. * * @param[in] portLibrary The port library * @param[in] directory to clean up * * @return void */ void deleteControlDirectory(struct OMRPortLibrary *portLibrary, char *baseDir) { OMRPORT_ACCESS_FROM_OMRPORT(portLibrary); struct J9FileStat buf; omrfile_stat(baseDir, (uint32_t)0, &buf); if (buf.isDir != 1) { omrfile_unlink(baseDir); } else { char mybaseFilePath[EsMaxPath]; char resultBuffer[EsMaxPath]; uintptr_t rc, handle; omrstr_printf(mybaseFilePath, EsMaxPath, "%s", baseDir); rc = handle = omrfile_findfirst(mybaseFilePath, resultBuffer); while ((uintptr_t)-1 != rc) { char nextEntry[EsMaxPath]; /* skip current and parent dir */ if (resultBuffer[0] == '.') { rc = omrfile_findnext(handle, resultBuffer); continue; } omrstr_printf(nextEntry, EsMaxPath, "%s/%s", mybaseFilePath, resultBuffer); deleteControlDirectory(OMRPORTLIB, nextEntry); rc = omrfile_findnext(handle, resultBuffer); } if (handle != (uintptr_t)-1) { omrfile_findclose(handle); } omrfile_unlinkdir(mybaseFilePath); } }
int main(void) { int32_t totalFiles = 0; intptr_t rc = 0; char resultBuffer[128]; uintptr_t rcFile; uintptr_t handle; OMRPortLibrary portLibrary; rc = omrthread_attach_ex(NULL, J9THREAD_ATTR_DEFAULT); if (0 != rc) { fprintf(stderr, "omrthread_attach_ex(NULL, J9THREAD_ATTR_DEFAULT) failed, rc=%d\n", (int)rc); return -1; } rc = omrport_init_library(&portLibrary, sizeof(OMRPortLibrary)); if (0 != rc) { fprintf(stderr, "omrport_init_library(&portLibrary, sizeof(OMRPortLibrary)), rc=%d\n", (int)rc); return -1; } OMRPORT_ACCESS_FROM_OMRPORT(&portLibrary); rcFile = handle = omrfile_findfirst(SRC_DIR, resultBuffer); if(rcFile == (uintptr_t)-1) { fprintf(stderr, "omrfile_findfirst(SRC_DIR, resultBuffer), return code=%d\n", (int)rcFile); return -1; } while ((uintptr_t)-1 != rcFile) { if (strncmp(resultBuffer, VERBOSE_GC_FILE_PREFIX, strlen(VERBOSE_GC_FILE_PREFIX)) == 0) { analyze(resultBuffer, portLibrary); totalFiles++; /* Clean up verbose log file */ omrfile_unlink(resultBuffer); } rcFile = omrfile_findnext(handle, resultBuffer); } if (handle != (uintptr_t)-1) { omrfile_findclose(handle); } if(totalFiles < 1) { omrtty_printf("Failed to find any verbose GC file to process!\n\n"); } portLibrary.port_shutdown_library(&portLibrary); omrthread_detach(NULL); }
/* * Test scenario for CorruptedAgent: * - create an empty agent file * - call omr_agent_create() and OMR_Agent::createAgent by providing the empty agent file * - expect openLibrary() to fail */ TEST_F(RASAgentNegativeTest, CorruptedAgent) { intptr_t fileDescriptor; char fileName[256]; char agentName[256]; char hostname[128]; OMRPORT_ACCESS_FROM_OMRVM(&testVM.omrVM); ASSERT_EQ(0, gethostname(hostname, sizeof(hostname))); /* generate machine specific file name to prevent conflict between multiple tests running on shared drive */ omrstr_printf(agentName, sizeof(agentName), "corruptedAgent_%s", hostname); /* create fileName with platform-dependent shared library prefix & suffix. * for Windows, fileName = corruptedAgent_<hostname>.dll * for Unix, fileName = libcorruptedAgent_<hostname>.so * for OSX, fileName = libcorruptedAgent_<hostname>.dylib */ #if defined(WIN32) || defined(WIN64) omrstr_printf(fileName, sizeof(fileName), "%s.dll", agentName); #elif defined(OSX) omrstr_printf(fileName, sizeof(fileName), "lib%s.dylib", agentName); #else /* defined(OSX) */ omrstr_printf(fileName, sizeof(fileName), "lib%s.so", agentName); #endif /* defined(WIN32) || defined(WIN64) */ /* create the empty agent file with permission 751*/ fileDescriptor = omrfile_open(fileName, EsOpenCreate | EsOpenWrite, 0751); ASSERT_NE(-1, fileDescriptor) << "omrfile_open \"" << fileName << "\" failed"; omrfile_close(fileDescriptor); /* call omr_agent_create() by providing the empty agent file */ struct OMR_Agent *agentC = omr_agent_create(&testVM.omrVM, agentName); ASSERT_FALSE(NULL == agentC) << "testAgent: createAgent() " << agentName << " failed"; OMRTEST_ASSERT_ERROR(OMR_ERROR_ILLEGAL_ARGUMENT, omr_agent_openLibrary(agentC)); /* call OMR_Agent::createAgent() by providing the empty agent file */ OMR_Agent *agentCPP = OMR_Agent::createAgent(&testVM.omrVM, agentName); ASSERT_FALSE(NULL == agentCPP) << "testAgent: createAgent() failed"; OMRTEST_ASSERT_ERROR(OMR_ERROR_ILLEGAL_ARGUMENT, agentCPP->openLibrary()); omrfile_unlink(fileName); }
/* Clean up the test output when test case passes */ void testFileCleanUp(const char* filePrefix) { if (::testing::UnitTest::GetInstance()->current_test_case()->Passed()) { OMRPORT_ACCESS_FROM_OMRPORT(portTestEnv->getPortLibrary()); uintptr_t rc = -1; uintptr_t handle = -1; char resultBuffer[1024]; rc = handle = omrfile_findfirst("./", resultBuffer); while ((uintptr_t)-1 != rc) { if (0 == strncmp(resultBuffer, filePrefix, strlen(filePrefix))) { omrfile_unlink(resultBuffer); } rc = omrfile_findnext(handle, resultBuffer); } if ((uintptr_t)-1 != handle) { omrfile_findclose(handle); } } }
void GCConfigTest::TearDown() { OMRPORT_ACCESS_FROM_OMRPORT(gcTestEnv->portLib); /* free root table */ hashTableFree(exampleVM->rootTable); /* free object table */ objectTable.free(); /* close verboseManager and clean up verbose files */ verboseManager->closeStreams(env); verboseManager->disableVerboseGC(); verboseManager->kill(env); if (false == gcTestEnv->keepLog) { if (0 == numOfFiles) { J9FileStat buf; int32_t fileStatRC = -1; fileStatRC = omrfile_stat(verboseFile, 0, &buf); if (0 == fileStatRC) { if (1 == buf.isFile) { omrfile_unlink(verboseFile); } } } else { for (int32_t seq = 1; seq <= (int32_t)numOfFiles; seq++) { char verboseFileSeq[MAX_NAME_LENGTH]; omrstr_printf(verboseFileSeq, MAX_NAME_LENGTH, "%s.%03zu", verboseFile, seq); J9FileStat buf; int32_t fileStatRC = -1; fileStatRC = omrfile_stat(verboseFileSeq, 0, &buf); if (0 > fileStatRC) { if (1 != buf.isFile) { break; } } omrfile_unlink(verboseFileSeq); } } } omrmem_free_memory((void *)verboseFile); /* Shut down the dispatcher threads */ omr_error_t rc = OMR_GC_ShutdownDispatcherThreads(exampleVM->_omrVMThread); ASSERT_EQ(OMR_ERROR_NONE, rc) << "TearDown(): OMR_GC_ShutdownDispatcherThreads failed, rc=" << rc; /* Shut down collector */ rc = OMR_GC_ShutdownCollector(exampleVM->_omrVMThread); ASSERT_EQ(OMR_ERROR_NONE, rc) << "TearDown(): OMR_GC_ShutdownCollector failed, rc=" << rc; /* Detach from VM */ rc = OMR_Thread_Free(exampleVM->_omrVMThread); ASSERT_EQ(OMR_ERROR_NONE, rc) << "TearDown(): OMR_Thread_Free failed, rc=" << rc; /* Shut down heap */ rc = OMR_GC_ShutdownHeap(exampleVM->_omrVM); ASSERT_EQ(OMR_ERROR_NONE, rc) << "TearDown(): OMR_GC_ShutdownHeap failed, rc=" << rc; printMemUsed("TearDown()", gcTestEnv->portLib); }