/** * Checks if the specified directory is part of a SVN working copy. * * @returns true if it is, false if it isn't or we cannot tell. * @param pszDir The directory in question. */ bool ScmSvnIsDirInWorkingCopy(const char *pszDir) { #ifdef SCM_WITHOUT_LIBSVN scmSvnFindSvnBinary(NULL); if (g_enmSvnVersion < kScmSvnVersion_1_7) { /* * Hack: check if the .svn/ dir exists. */ char szPath[RTPATH_MAX]; int rc = RTPathJoin(szPath, sizeof(szPath), pszDir, ".svn"); if (RT_SUCCESS(rc)) return RTDirExists(szPath); } else { const char *apszArgs[] = { g_szSvnPath, "propget", "svn:no-such-property", pszDir, NULL }; char *pszValue; int rc = scmSvnRunAndGetOutput(NULL, apszArgs, true, &pszValue); if (RT_SUCCESS(rc)) { RTStrFree(pszValue); return true; } } #else NOREF(pState); #endif return false; }
/** * Constructs the extension pack directory path. * * A combination of RTPathJoin and VBoxExtPackMangleName. * * @returns IPRT status code like RTPathJoin. * @param pszExtPackDir Where to return the directory path. * @param cbExtPackDir The size of the return buffer. * @param pszParentDir The parent directory (".../Extensions"). * @param pszName The extension pack name, unmangled. */ int VBoxExtPackCalcDir(char *pszExtPackDir, size_t cbExtPackDir, const char *pszParentDir, const char *pszName) { AssertReturn(VBoxExtPackIsValidName(pszName), VERR_INTERNAL_ERROR_5); RTCString *pstrMangledName = VBoxExtPackMangleName(pszName); if (!pstrMangledName) return VERR_INTERNAL_ERROR_4; int vrc = RTPathJoin(pszExtPackDir, cbExtPackDir, pszParentDir, pstrMangledName->c_str()); delete pstrMangledName; return vrc; }
static RTEXITCODE rtZipUnzipCmdExtractCallback(PRTZIPUNZIPCMDOPS pOpts, RTVFSOBJ hVfsObj, const char *pszName, RTEXITCODE rcExit, PRTFOFF pcBytes) { if (pOpts->fVerbose) RTPrintf("%s\n", pszName); /* * Query all the information. */ RTFSOBJINFO UnixInfo; int rc = RTVfsObjQueryInfo(hVfsObj, &UnixInfo, RTFSOBJATTRADD_UNIX); if (RT_FAILURE(rc)) return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTVfsObjQueryInfo returned %Rrc on '%s'", rc, pszName); *pcBytes = UnixInfo.cbObject; char szDst[RTPATH_MAX]; rc = RTPathJoin(szDst, sizeof(szDst), pOpts->pszDirectory ? pOpts->pszDirectory : ".", pszName); if (RT_FAILURE(rc)) return RTMsgErrorExit(RTEXITCODE_FAILURE, "%s: Failed to construct destination path for: %Rrc", pszName, rc); /* * Extract according to the type. */ switch (UnixInfo.Attr.fMode & RTFS_TYPE_MASK) { case RTFS_TYPE_FILE: return rtZipUnzipCmdExtractFile(pOpts, hVfsObj, rcExit, szDst, &UnixInfo); case RTFS_TYPE_DIRECTORY: rc = RTDirCreateFullPath(szDst, UnixInfo.Attr.fMode & RTFS_UNIX_ALL_ACCESS_PERMS); if (RT_FAILURE(rc)) return RTMsgErrorExit(RTEXITCODE_FAILURE, "%s: Error creating directory: %Rrc", szDst, rc); break; default: return RTMsgErrorExit(RTEXITCODE_FAILURE, "%s: Unknown file type.", pszName); } if (!pOpts->fNoModTimeDirectories) { rc = RTPathSetTimesEx(szDst, NULL, &UnixInfo.ModificationTime, NULL, NULL, RTPATH_F_ON_LINK); if (RT_FAILURE(rc) && rc != VERR_NOT_SUPPORTED && rc != VERR_NS_SYMLINK_SET_TIME) rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "%s: Error changing modification time: %Rrc.", pszName, rc); } return rcExit; }
HRESULT VFSExplorer::i_deleteFS(TaskVFSExplorer *aTask) { LogFlowFuncEnter(); AutoCaller autoCaller(this); if (FAILED(autoCaller.rc())) return autoCaller.rc(); AutoWriteLock appLock(this COMMA_LOCKVAL_SRC_POS); HRESULT rc = S_OK; float fPercentStep = 100.0f / aTask->filenames.size(); try { char szPath[RTPATH_MAX]; std::list<Utf8Str>::const_iterator it; size_t i = 0; for (it = aTask->filenames.begin(); it != aTask->filenames.end(); ++it, ++i) { int vrc = RTPathJoin(szPath, sizeof(szPath), m->strPath.c_str(), (*it).c_str()); if (RT_FAILURE(vrc)) throw setError(E_FAIL, tr("Internal Error (%Rrc)"), vrc); vrc = RTFileDelete(szPath); if (RT_FAILURE(vrc)) throw setError(VBOX_E_FILE_ERROR, tr("Can't delete file '%s' (%Rrc)"), szPath, vrc); if (aTask->progress) aTask->progress->SetCurrentOperationProgress((ULONG)(fPercentStep * i)); } } catch(HRESULT aRC) { rc = aRC; } aTask->rc = rc; if (!aTask->progress.isNull()) aTask->progress->i_notifyComplete(rc); LogFlowFunc(("rc=%Rhrc\n", rc)); LogFlowFuncLeave(); return VINF_SUCCESS; }
/** * Reads the extension pack descriptor. * * @returns NULL on success, pointer to an error message on failure (caller * deletes it). * @param a_pszDir The directory containing the description file. * @param a_pExtPackDesc Where to store the extension pack descriptor. * @param a_pObjInfo Where to store the object info for the file (unix * attribs). Optional. */ RTCString *VBoxExtPackLoadDesc(const char *a_pszDir, PVBOXEXTPACKDESC a_pExtPackDesc, PRTFSOBJINFO a_pObjInfo) { vboxExtPackClearDesc(a_pExtPackDesc); /* * Validate, open and parse the XML file. */ char szFilePath[RTPATH_MAX]; int vrc = RTPathJoin(szFilePath, sizeof(szFilePath), a_pszDir, VBOX_EXTPACK_DESCRIPTION_NAME); if (RT_FAILURE(vrc)) return new RTCString("RTPathJoin failed with %Rrc", vrc); RTFSOBJINFO ObjInfo; vrc = RTPathQueryInfoEx(szFilePath, &ObjInfo, RTFSOBJATTRADD_UNIX, RTPATH_F_ON_LINK); if (RT_FAILURE(vrc)) return &(new RTCString())->printf("RTPathQueryInfoEx failed with %Rrc", vrc); if (a_pObjInfo) *a_pObjInfo = ObjInfo; if (!RTFS_IS_FILE(ObjInfo.Attr.fMode)) { if (RTFS_IS_SYMLINK(ObjInfo.Attr.fMode)) return new RTCString("The XML file is symlinked, that is not allowed"); return &(new RTCString)->printf("The XML file is not a file (fMode=%#x)", ObjInfo.Attr.fMode); } xml::Document Doc; { xml::XmlFileParser Parser; try { Parser.read(szFilePath, Doc); } catch (xml::XmlError Err) { return new RTCString(Err.what()); } } /* * Hand the xml doc over to the common code. */ return vboxExtPackLoadDescFromDoc(&Doc, a_pExtPackDesc); }
/** * Gets a re-formatted version string from the VS_FIXEDFILEINFO table. * * @returns VBox status code. The output buffer is always valid and the status * code can safely be ignored. * * @param pszPath The base path. * @param pszFilaname The filename. * @param pszVersion Where to return the version string. * @param cbVersion The size of the version string buffer. This MUST be * at least 2 bytes! */ int VBoxServiceGetFileVersionString(const char *pszPath, const char *pszFilename, char *pszVersion, size_t cbVersion) { /* * We will ALWAYS return with a valid output buffer. */ AssertReturn(cbVersion >= 2, VERR_BUFFER_OVERFLOW); pszVersion[0] = '-'; pszVersion[1] = '\0'; /* * Create the path and query the bits. */ char szFullPath[RTPATH_MAX]; int rc = RTPathJoin(szFullPath, sizeof(szFullPath), pszPath, pszFilename); if (RT_SUCCESS(rc)) { DWORD dwMajor, dwMinor, dwBuild, dwRev; rc = VBoxServiceGetFileVersion(szFullPath, &dwMajor, &dwMinor, &dwBuild, &dwRev); if (RT_SUCCESS(rc)) RTStrPrintf(pszVersion, cbVersion, "%u.%u.%ur%u", dwMajor, dwMinor, dwBuild, dwRev); } return rc; }
/** * Adds a file to the cache. * * @returns IPRT status code. * @param pszSrcPath Path to the source file. * @param pszDstName The name of the destionation file (no path stuff). * @param pszExtraSuff Optional extra suffix. Mach-O dSYM hack. * @param pszDstSubDir The subdirectory to file it under. This is the * stringification of a relatively unique identifier of * the file in question. * @param pAddToUuidMap Optional file UUID that is used to create a UUID map * entry. * @param pszUuidMapDir The UUID map subdirectory in the cache, if this is * wanted, otherwise NULL. * @param pCfg The configuration. */ static int rtDbgSymCacheAddOneFile(const char *pszSrcPath, const char *pszDstName, const char *pszExtraStuff, const char *pszDstSubDir, PRTUUID pAddToUuidMap, const char *pszUuidMapDir, PCRTDBGSYMCACHEADDCFG pCfg) { /* * Build and create the destination path, step by step. */ char szDstPath[RTPATH_MAX]; int rc = RTPathJoin(szDstPath, sizeof(szDstPath), pCfg->pszCache, pszDstName); if (RT_FAILURE(rc)) return RTMsgErrorRc(rc, "Error constructing cache path for '%s': %Rrc", pszSrcPath, rc); if (!RTDirExists(szDstPath)) { rc = RTDirCreate(szDstPath, 0755, RTDIRCREATE_FLAGS_NOT_CONTENT_INDEXED_NOT_CRITICAL); if (RT_FAILURE(rc)) return RTMsgErrorRc(rc, "Error creating '%s': %Rrc", szDstPath, rc); } rc = RTPathAppend(szDstPath, sizeof(szDstPath), pszDstSubDir); if (RT_FAILURE(rc)) return RTMsgErrorRc(rc, "Error constructing cache path for '%s': %Rrc", pszSrcPath, rc); if (!RTDirExists(szDstPath)) { rc = RTDirCreate(szDstPath, 0755, RTDIRCREATE_FLAGS_NOT_CONTENT_INDEXED_NOT_CRITICAL); if (RT_FAILURE(rc)) return RTMsgErrorRc(rc, "Error creating '%s': %Rrc", szDstPath, rc); } rc = RTPathAppend(szDstPath, sizeof(szDstPath), pszDstName); if (RT_FAILURE(rc)) return RTMsgErrorRc(rc, "Error constructing cache path for '%s': %Rrc", pszSrcPath, rc); if (pszExtraStuff) { rc = RTStrCat(szDstPath, sizeof(szDstPath), pszExtraStuff); if (RT_FAILURE(rc)) return RTMsgErrorRc(rc, "Error constructing cache path for '%s': %Rrc", pszSrcPath, rc); } /* * If the file exists, we compare the two and throws an error if the doesn't match. */ if (RTPathExists(szDstPath)) { rc = RTFileCompare(pszSrcPath, szDstPath); if (RT_SUCCESS(rc)) { RTMsgInfo("%s is already in the cache.", pszSrcPath); if (pAddToUuidMap && pszUuidMapDir) return rtDbgSymCacheAddCreateUuidMapping(szDstPath, pAddToUuidMap, pszUuidMapDir, pCfg); return VINF_SUCCESS; } if (rc == VERR_NOT_EQUAL) RTMsgInfo("Cache conflict with existing entry '%s' when inserting '%s'.", szDstPath, pszSrcPath); else RTMsgInfo("Error comparing '%s' with '%s': %Rrc", pszSrcPath, szDstPath, rc); if (!pCfg->fOverwriteOnConflict) return rc; } /* * The file doesn't exist or we should overwrite it, */ RTMsgInfo("Copying '%s' to '%s'...", pszSrcPath, szDstPath); rc = RTFileCopy(pszSrcPath, szDstPath); if (RT_FAILURE(rc)) return RTMsgErrorRc(rc, "Error copying '%s' to '%s': %Rrc", pszSrcPath, szDstPath, rc); if (pAddToUuidMap && pszUuidMapDir) return rtDbgSymCacheAddCreateUuidMapping(szDstPath, pAddToUuidMap, pszUuidMapDir, pCfg); return VINF_SUCCESS; }
/** * Creates a UUID mapping for the file. * * @returns IPRT status code. * @param pszCacheFile The path to the file in the cache. * @param pFileUuid The UUID of the file. * @param pszUuidMapDir The UUID map subdirectory in the cache, if this is * wanted, otherwise NULL. * @param pCfg The configuration. */ static int rtDbgSymCacheAddCreateUuidMapping(const char *pszCacheFile, PRTUUID pFileUuid, const char *pszUuidMapDir, PCRTDBGSYMCACHEADDCFG pCfg) { /* * Create the UUID map entry first, deep. */ char szMapPath[RTPATH_MAX]; int rc = RTPathJoin(szMapPath, sizeof(szMapPath) - sizeof("/xxxx/yyyy/xxxx/yyyy/xxxx/zzzzzzzzzzzz") + 1, pCfg->pszCache, pszUuidMapDir); if (RT_FAILURE(rc)) return RTMsgErrorRc(rc, "Error constructing UUID map path (RTPathJoin): %Rrc", rc); size_t cch = strlen(szMapPath); szMapPath[cch] = '-'; rc = RTUuidToStr(pFileUuid, &szMapPath[cch + 2], sizeof(szMapPath) - cch); if (RT_FAILURE(rc)) return RTMsgErrorRc(rc, "Error constructing UUID map path (RTUuidToStr): %Rrc", rc); /* Uppercase the whole lot. */ RTStrToUpper(&szMapPath[cch + 2]); /* Split the first dword in two. */ szMapPath[cch + 1] = szMapPath[cch + 2]; szMapPath[cch + 2] = szMapPath[cch + 3]; szMapPath[cch + 3] = szMapPath[cch + 4]; szMapPath[cch + 4] = szMapPath[cch + 5]; szMapPath[cch + 5] = '-'; /* * Create the directories in the path. */ char chSaved = RTPATH_SLASH; for (unsigned i = 0; i < 6; i++, cch += 5) { Assert(szMapPath[cch] == '-'); szMapPath[cch] = '\0'; if (!RTDirExists(szMapPath)) { rc = RTDirCreate(szMapPath, 0755, RTDIRCREATE_FLAGS_NOT_CONTENT_INDEXED_NOT_CRITICAL); if (RT_FAILURE(rc)) return RTMsgErrorRc(rc, "RTDirCreate failed on '%s' (UUID map path): %Rrc", szMapPath, rc); } szMapPath[cch] = RTPATH_SLASH; } cch -= 5; /* * Calculate a relative path from there to the actual file. */ char szLinkTarget[RTPATH_MAX]; //szMapPath[cch] = '\0'; rc = RTPathCalcRelative(szLinkTarget, sizeof(szLinkTarget), szMapPath, pszCacheFile); //szMapPath[cch] = RTPATH_SLASH; if (RT_FAILURE(rc)) return RTMsgErrorRc(rc, "Failed to calculate relative path from '%s' to '%s': %Rrc", szMapPath, pszCacheFile, rc); /* * If there is already a link there, check if it matches or whether * perhaps it's target doesn't exist. */ RTFSOBJINFO ObjInfo; rc = RTPathQueryInfoEx(szMapPath, &ObjInfo, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK); if (RT_SUCCESS(rc)) { if (RTFS_IS_SYMLINK(ObjInfo.Attr.fMode)) { rc = RTPathQueryInfoEx(szMapPath, &ObjInfo, RTFSOBJATTRADD_NOTHING, RTPATH_F_FOLLOW_LINK); if (RT_SUCCESS(rc)) { char *pszCurTarget = NULL; rc = RTSymlinkReadA(szMapPath, &pszCurTarget); if (RT_FAILURE(rc)) return RTMsgErrorRc(rc, "UUID map: failed to read existing symlink '%s': %Rrc", szMapPath, rc); if (RTPathCompare(pszCurTarget, szLinkTarget) == 0) RTMsgInfo("UUID map: existing link '%s' has the same target ('%s').", szMapPath, pszCurTarget); else { RTMsgError("UUID map: Existing mapping '%s' pointing to '%s' insted of '%s'", szMapPath, pszCurTarget, szLinkTarget); rc = VERR_ALREADY_EXISTS; } RTStrFree(pszCurTarget); return rc; } else RTMsgInfo("UUID map: replacing dangling link '%s'", szMapPath); RTSymlinkDelete(szMapPath, 0 /*fFlags*/); } else if (RTFS_IS_FILE(ObjInfo.Attr.fMode)) return RTMsgErrorRc(VERR_IS_A_FILE, "UUID map: found file at '%s', expect symbolic link or nothing.", szMapPath); else if (RTFS_IS_DIRECTORY(ObjInfo.Attr.fMode)) return RTMsgErrorRc(VERR_IS_A_DIRECTORY, "UUID map: found directory at '%s', expect symbolic link or nothing.", szMapPath); else return RTMsgErrorRc(VERR_NOT_SYMLINK, "UUID map: Expected symbolic link or nothing at '%s', found: fMode=%#x", szMapPath, ObjInfo.Attr.fMode); } /* * Create the symbolic link. */ rc = RTSymlinkCreate(szMapPath, szLinkTarget, RTSYMLINKTYPE_FILE, 0); if (RT_FAILURE(rc)) return RTMsgErrorRc(rc, "Failed to create UUID map symlink '%s' to '%s': %Rrc", szMapPath, szLinkTarget, rc); RTMsgInfo("UUID map: %s => %s", szMapPath, szLinkTarget); return VINF_SUCCESS; }
static void test1Worker(RTTEST hTest, const char *pszBaseDir, const char *pszTarget, RTSYMLINKTYPE enmType, bool fDangling) { char szPath1[RTPATH_MAX]; char szPath2[RTPATH_MAX]; size_t cchTarget = strlen(pszTarget); char szPath3[RTPATH_MAX]; RTStrCopy(szPath3, sizeof(szPath3), pszTarget); #ifdef RT_OS_WINDOWS /* see RTSymlinkCreate in symlink-win.cpp */ char c; char *psz = szPath3; while ((c = *psz) != '\0') { if (c == '/') *psz = '\\'; psz++; } #endif /* Create it.*/ RTTESTI_CHECK_RC_OK_RETV(RTPathJoin(szPath1, sizeof(szPath1), pszBaseDir, "tstRTSymlink-link-1")); RTSymlinkDelete(szPath1, 0); /* clean up previous run */ RTTESTI_CHECK_RC_RETV(RTSymlinkCreate(szPath1, pszTarget, RTSYMLINKTYPE_FILE, 0), VINF_SUCCESS); /* Check the predicate functions. */ RTTESTI_CHECK(RTSymlinkExists(szPath1)); RTTESTI_CHECK(RTSymlinkIsDangling(szPath1) == fDangling); /* Read it. */ memset(szPath2, 0xff, sizeof(szPath2)); szPath2[sizeof(szPath2) - 1] = '\0'; RTTESTI_CHECK_RC(RTSymlinkRead(szPath1, szPath2, sizeof(szPath2), 0), VINF_SUCCESS); RTTESTI_CHECK_MSG(strcmp(szPath2, szPath3) == 0, ("got=\"%s\" expected=\"%s\"", szPath2, szPath3)); memset(szPath2, 0xff, sizeof(szPath2)); szPath2[sizeof(szPath2) - 1] = '\0'; RTTESTI_CHECK_RC(RTSymlinkRead(szPath1, szPath2, cchTarget + 1, 0), VINF_SUCCESS); RTTESTI_CHECK_MSG(strcmp(szPath2, szPath3) == 0, ("got=\"%s\" expected=\"%s\"", szPath2, szPath3)); memset(szPath2, 0xff, sizeof(szPath2)); szPath2[sizeof(szPath2) - 1] = '\0'; RTTESTI_CHECK_RC(RTSymlinkRead(szPath1, szPath2, cchTarget, 0), VERR_BUFFER_OVERFLOW); RTTESTI_CHECK_MSG( strncmp(szPath2, szPath3, cchTarget - 1) == 0 && szPath2[cchTarget - 1] == '\0', ("got=\"%s\" expected=\"%.*s\"", szPath2, cchTarget - 1, szPath3)); /* Other APIs that have to handle symlinks carefully. */ int rc; RTFSOBJINFO ObjInfo; RTTESTI_CHECK_RC(rc = RTPathQueryInfo(szPath1, &ObjInfo, RTFSOBJATTRADD_NOTHING), VINF_SUCCESS); if (RT_SUCCESS(rc)) RTTESTI_CHECK(RTFS_IS_SYMLINK(ObjInfo.Attr.fMode)); RTTESTI_CHECK_RC(rc = RTPathQueryInfoEx(szPath1, &ObjInfo, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK), VINF_SUCCESS); if (RT_SUCCESS(rc)) RTTESTI_CHECK(RTFS_IS_SYMLINK(ObjInfo.Attr.fMode)); if (!fDangling) { RTTESTI_CHECK_RC(rc = RTPathQueryInfoEx(szPath1, &ObjInfo, RTFSOBJATTRADD_NOTHING, RTPATH_F_FOLLOW_LINK), VINF_SUCCESS); if (RT_SUCCESS(rc)) RTTESTI_CHECK(!RTFS_IS_SYMLINK(ObjInfo.Attr.fMode)); else RT_ZERO(ObjInfo); if (enmType == RTSYMLINKTYPE_DIR) { RTTESTI_CHECK(RTDirExists(szPath1)); RTTESTI_CHECK(RTFS_IS_DIRECTORY(ObjInfo.Attr.fMode)); } else if (enmType == RTSYMLINKTYPE_FILE) { RTTESTI_CHECK(RTFileExists(szPath1)); RTTESTI_CHECK(RTFS_IS_FILE(ObjInfo.Attr.fMode)); } /** @todo Check more APIs */ } /* Finally, the removal of the symlink. */ RTTESTI_CHECK_RC(RTSymlinkDelete(szPath1, 0), VINF_SUCCESS); RTTESTI_CHECK_RC(RTSymlinkDelete(szPath1, 0), VERR_FILE_NOT_FOUND); }
int main(int argc, char **argv) { RTEXITCODE rcExit = RTTestInitAndCreate("tstRTPrfIO", &g_hTest); if (rcExit != RTEXITCODE_SUCCESS) return rcExit; RTTestBanner(g_hTest); /* * Parse arguments */ static const RTGETOPTDEF s_aOptions[] = { { "--test-dir", 'd', RTGETOPT_REQ_STRING }, }; bool fFileOpenCloseTest = true; bool fFileWriteByteTest = true; bool fPathQueryInfoTest = true; //bool fFileTests = true; //bool fDirTests = true; int ch; RTGETOPTUNION ValueUnion; RTGETOPTSTATE GetState; RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, 0); while ((ch = RTGetOpt(&GetState, &ValueUnion))) { switch (ch) { case 'd': g_pszTestDir = ValueUnion.psz; break; case 'V': RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "$Revision$\n"); return RTTestSummaryAndDestroy(g_hTest); case 'h': RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "usage: testname [-d <testdir>]\n"); return RTTestSummaryAndDestroy(g_hTest); default: RTTestFailed(g_hTest, "invalid argument"); RTGetOptPrintError(ch, &ValueUnion); return RTTestSummaryAndDestroy(g_hTest); } } /* * Set up and check the prerequisites. */ RTTESTI_CHECK_RC(RTPathJoin(g_szTestFile1, sizeof(g_szTestFile1), g_pszTestDir, "tstRTPrfIO-TestFile1"), VINF_SUCCESS); RTTESTI_CHECK_RC(RTPathJoin(g_szTestDir1, sizeof(g_szTestDir1), g_pszTestDir, "tstRTPrfIO-TestDir1"), VINF_SUCCESS); RTTESTI_CHECK_RC(RTPathJoin(g_szNotExitingFile, sizeof(g_szNotExitingFile), g_pszTestDir, "tstRTPrfIO-nonexistent-file"), VINF_SUCCESS); RTTESTI_CHECK_RC(RTPathJoin(g_szNotExitingDir, sizeof(g_szNotExitingDir), g_pszTestDir, "tstRTPrfIO-nonexistent-dir"), VINF_SUCCESS); RTTESTI_CHECK_RC(RTPathJoin(g_szNotExitingDirFile, sizeof(g_szNotExitingDirFile), g_szNotExitingDir, "nonexistent-file"), VINF_SUCCESS); RTTESTI_CHECK(RTDirExists(g_pszTestDir)); if (RTPathExists(g_szTestDir1)) RTTestFailed(g_hTest, "The primary test directory (%s) already exist, please remove it", g_szTestDir1); if (RTPathExists(g_szTestFile1)) RTTestFailed(g_hTest, "The primary test file (%s) already exist, please remove it", g_szTestFile1); if (RTPathExists(g_szNotExitingFile)) RTTestFailed(g_hTest, "'%s' exists, remove it", g_szNotExitingFile); if (RTPathExists(g_szNotExitingDir)) RTTestFailed(g_hTest, "'%s' exists, remove it", g_szNotExitingDir); if (RTPathExists(g_szNotExitingDirFile)) RTTestFailed(g_hTest, "'%s' exists, remove it", g_szNotExitingDirFile); /* * Do the testing. */ if (RTTestIErrorCount() == 0) { #if 1 if (fPathQueryInfoTest) benchmarkPathQueryInfo(); if (fFileOpenCloseTest) benchmarkFileOpenClose(); #endif if (fFileWriteByteTest) benchmarkFileWriteByte(); //if (fFileTests) // benchmarkFile(); //if (fDirTests) // benchmarkDir(); /* * Cleanup. */ RTFileDelete(g_szTestFile1); RTDirRemoveRecursive(g_szTestDir1, 0); RTTESTI_CHECK(RTDirExists(g_pszTestDir)); RTTESTI_CHECK(!RTPathExists(g_szTestDir1)); RTTESTI_CHECK(!RTPathExists(g_szTestFile1)); } return RTTestSummaryAndDestroy(g_hTest); }
int main() { char szPath[RTPATH_MAX]; /* * Init RT+Test. */ RTTEST hTest; int rc = RTTestInitAndCreate("tstRTPath", &hTest); if (rc) return rc; RTTestBanner(hTest); /* * RTPathExecDir, RTPathUserHome and RTProcGetExecutablePath. */ RTTestSub(hTest, "RTPathExecDir"); RTTESTI_CHECK_RC(rc = RTPathExecDir(szPath, sizeof(szPath)), VINF_SUCCESS); if (RT_SUCCESS(rc)) RTTestIPrintf(RTTESTLVL_INFO, "ExecDir={%s}\n", szPath); RTTestSub(hTest, "RTProcGetExecutablePath"); if (RTProcGetExecutablePath(szPath, sizeof(szPath)) == szPath) RTTestIPrintf(RTTESTLVL_INFO, "ExecutableName={%s}\n", szPath); else RTTestIFailed("RTProcGetExecutablePath -> NULL"); RTTestSub(hTest, "RTPathUserHome"); RTTESTI_CHECK_RC(rc = RTPathUserHome(szPath, sizeof(szPath)), VINF_SUCCESS); if (RT_SUCCESS(rc)) RTTestIPrintf(RTTESTLVL_INFO, "UserHome={%s}\n", szPath); RTTestSub(hTest, "RTPathUserDocuments"); RTTESTI_CHECK_RC(rc = RTPathUserDocuments(szPath, sizeof(szPath)), VINF_SUCCESS); if (RT_SUCCESS(rc)) RTTestIPrintf(RTTESTLVL_INFO, "UserDocuments={%s}\n", szPath); RTTestSub(hTest, "RTPathTemp"); RTTESTI_CHECK_RC(rc = RTPathTemp(szPath, sizeof(szPath)), VINF_SUCCESS); if (RT_SUCCESS(rc)) RTTestIPrintf(RTTESTLVL_INFO, "PathTemp={%s}\n", szPath); size_t cch = strlen(szPath); RTTESTI_CHECK_RC(RTPathTemp(szPath, cch), VERR_BUFFER_OVERFLOW); RTTESTI_CHECK_RC(RTPathTemp(szPath, cch+1), VINF_SUCCESS); RTTESTI_CHECK_RC(RTPathTemp(szPath, cch+2), VINF_SUCCESS); /* * RTPathAbsEx */ RTTestSub(hTest, "RTPathAbsEx"); static const struct { const char *pcszInputBase; const char *pcszInputPath; int rc; const char *pcszOutput; } s_aRTPathAbsExTests[] = { #if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS) { NULL, "", VERR_INVALID_PARAMETER, NULL }, { NULL, ".", VINF_SUCCESS, "%p" }, { NULL, "\\", VINF_SUCCESS, "%d\\" }, { NULL, "\\..", VINF_SUCCESS, "%d\\" }, { NULL, "/absolute/..", VINF_SUCCESS, "%d\\" }, { NULL, "/absolute\\\\../..", VINF_SUCCESS, "%d\\" }, { NULL, "/absolute//../path\\", VINF_SUCCESS, "%d\\path" }, { NULL, "/absolute/../../path", VINF_SUCCESS, "%d\\path" }, { NULL, "relative/../dir\\.\\.\\.\\file.txt", VINF_SUCCESS, "%p\\dir\\file.txt" }, { NULL, "\\data\\", VINF_SUCCESS, "%d\\data" }, { "relative_base/dir\\", "\\from_root", VINF_SUCCESS, "%d\\from_root" }, { "relative_base/dir/", "relative_also", VINF_SUCCESS, "%p\\relative_base\\dir\\relative_also" }, #else { NULL, "", VERR_INVALID_PARAMETER, NULL }, { NULL, ".", VINF_SUCCESS, "%p" }, { NULL, "/", VINF_SUCCESS, "/" }, { NULL, "/..", VINF_SUCCESS, "/" }, { NULL, "/absolute/..", VINF_SUCCESS, "/" }, { NULL, "/absolute\\\\../..", VINF_SUCCESS, "/" }, { NULL, "/absolute//../path/", VINF_SUCCESS, "/path" }, { NULL, "/absolute/../../path", VINF_SUCCESS, "/path" }, { NULL, "relative/../dir/./././file.txt", VINF_SUCCESS, "%p/dir/file.txt" }, { NULL, "relative/../dir\\.\\.\\.\\file.txt", VINF_SUCCESS, "%p/dir\\.\\.\\.\\file.txt" }, /* linux-specific */ { NULL, "/data/", VINF_SUCCESS, "/data" }, { "relative_base/dir/", "/from_root", VINF_SUCCESS, "/from_root" }, { "relative_base/dir/", "relative_also", VINF_SUCCESS, "%p/relative_base/dir/relative_also" }, #endif #if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS) { NULL, "C:\\", VINF_SUCCESS, "C:\\" }, { "C:\\", "..", VINF_SUCCESS, "C:\\" }, { "C:\\temp", "..", VINF_SUCCESS, "C:\\" }, { "C:\\VirtualBox/Machines", "..\\VirtualBox.xml", VINF_SUCCESS, "C:\\VirtualBox\\VirtualBox.xml" }, { "C:\\MustDie", "\\from_root/dir/..", VINF_SUCCESS, "C:\\from_root" }, { "C:\\temp", "D:\\data", VINF_SUCCESS, "D:\\data" }, { NULL, "\\\\server\\..\\share", VINF_SUCCESS, "\\\\server\\..\\share" /* kind of strange */ }, { NULL, "\\\\server/", VINF_SUCCESS, "\\\\server" }, { NULL, "\\\\", VINF_SUCCESS, "\\\\" }, { NULL, "\\\\\\something", VINF_SUCCESS, "\\\\\\something" /* kind of strange */ }, { "\\\\server\\share_as_base", "/from_root", VINF_SUCCESS, "\\\\server\\from_root" }, { "\\\\just_server", "/from_root", VINF_SUCCESS, "\\\\just_server\\from_root" }, { "\\\\server\\share_as_base", "relative\\data", VINF_SUCCESS, "\\\\server\\share_as_base\\relative\\data" }, { "base", "\\\\?\\UNC\\relative/edwef/..", VINF_SUCCESS, "\\\\?\\UNC\\relative" }, { "\\\\?\\UNC\\base", "/from_root", VERR_INVALID_NAME, NULL }, #else { "/temp", "..", VINF_SUCCESS, "/" }, { "/VirtualBox/Machines", "../VirtualBox.xml", VINF_SUCCESS, "/VirtualBox/VirtualBox.xml" }, { "/MustDie", "/from_root/dir/..", VINF_SUCCESS, "/from_root" }, { "\\temp", "\\data", VINF_SUCCESS, "%p/\\temp/\\data" }, #endif }; for (unsigned i = 0; i < RT_ELEMENTS(s_aRTPathAbsExTests); ++ i) { rc = RTPathAbsEx(s_aRTPathAbsExTests[i].pcszInputBase, s_aRTPathAbsExTests[i].pcszInputPath, szPath, sizeof(szPath)); if (rc != s_aRTPathAbsExTests[i].rc) { RTTestIFailed("unexpected result code!\n" " input base: '%s'\n" " input path: '%s'\n" " output: '%s'\n" " rc: %Rrc\n" " expected rc: %Rrc", s_aRTPathAbsExTests[i].pcszInputBase, s_aRTPathAbsExTests[i].pcszInputPath, szPath, rc, s_aRTPathAbsExTests[i].rc); continue; } char szTmp[RTPATH_MAX]; char *pszExpected = NULL; if (s_aRTPathAbsExTests[i].pcszOutput != NULL) { if (s_aRTPathAbsExTests[i].pcszOutput[0] == '%') { RTTESTI_CHECK_RC(rc = RTPathGetCurrent(szTmp, sizeof(szTmp)), VINF_SUCCESS); if (RT_FAILURE(rc)) break; pszExpected = szTmp; if (s_aRTPathAbsExTests[i].pcszOutput[1] == 'p') { cch = strlen(szTmp); if (cch + strlen(s_aRTPathAbsExTests[i].pcszOutput) - 2 <= sizeof(szTmp)) strcpy(szTmp + cch, s_aRTPathAbsExTests[i].pcszOutput + 2); } #if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS) else if (s_aRTPathAbsExTests[i].pcszOutput[1] == 'd') { if (2 + strlen(s_aRTPathAbsExTests[i].pcszOutput) - 2 <= sizeof(szTmp)) strcpy(szTmp + 2, s_aRTPathAbsExTests[i].pcszOutput + 2); } #endif } else { strcpy(szTmp, s_aRTPathAbsExTests[i].pcszOutput); pszExpected = szTmp; } if (strcmp(szPath, pszExpected)) { RTTestIFailed("Unexpected result\n" " input base: '%s'\n" " input path: '%s'\n" " output: '%s'\n" " expected: '%s'", s_aRTPathAbsExTests[i].pcszInputBase, s_aRTPathAbsExTests[i].pcszInputPath, szPath, s_aRTPathAbsExTests[i].pcszOutput); } } } /* * RTPathStripFilename */ RTTestSub(hTest, "RTPathStripFilename"); static const char *s_apszStripFilenameTests[] = { "/usr/include///", "/usr/include//", "/usr/include/", "/usr/include", "/usr/include", "/usr", "/usr", "/", "usr", ".", #if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS) "c:/windows", "c:/", "c:/", "c:/", "D:", "D:", "C:\\OS2\\DLLS", "C:\\OS2", #endif }; for (unsigned i = 0; i < RT_ELEMENTS(s_apszStripFilenameTests); i += 2) { const char *pszInput = s_apszStripFilenameTests[i]; const char *pszExpect = s_apszStripFilenameTests[i + 1]; strcpy(szPath, pszInput); RTPathStripFilename(szPath); if (strcmp(szPath, pszExpect)) { RTTestIFailed("Unexpected result\n" " input: '%s'\n" " output: '%s'\n" "expected: '%s'", pszInput, szPath, pszExpect); } } /* * RTPathAppend. */ RTTestSub(hTest, "RTPathAppend"); static const char *s_apszAppendTests[] = { /* base append result */ "/", "", "/", "", "/", "/", "/", "/", "/", "/x", "", "/x", "/x", "/", "/x/", "/", "x", "/x", "dir", "file", "dir/file", "dir", "/file", "dir/file", "dir", "//file", "dir/file", "dir", "///file", "dir/file", "dir/", "/file", "dir/file", "dir/", "//file", "dir/file", "dir/", "///file", "dir/file", "dir//", "file", "dir/file", "dir//", "/file", "dir/file", "dir//", "//file", "dir/file", "dir///", "///file", "dir/file", "/bin/testcase", "foo.r0", "/bin/testcase/foo.r0", #if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS) "/", "\\", "/", "\\", "/", "\\", "\\\\srv\\shr", "dir//", "\\\\srv\\shr/dir//", "\\\\srv\\shr", "dir//file", "\\\\srv\\shr/dir//file", "\\\\srv\\shr", "//dir//", "\\\\srv\\shr/dir//", "\\\\srv\\shr", "/\\dir//", "\\\\srv\\shr\\dir//", "\\\\", "not-srv/not-shr/file", "\\not-srv/not-shr/file", "C:", "autoexec.bat", "C:autoexec.bat", "C:", "/autoexec.bat", "C:/autoexec.bat", "C:", "\\autoexec.bat", "C:\\autoexec.bat", "C:\\", "/autoexec.bat", "C:\\autoexec.bat", "C:\\\\", "autoexec.bat", "C:\\autoexec.bat", "E:\\bin\\testcase", "foo.r0", "E:\\bin\\testcase/foo.r0", #endif }; for (unsigned i = 0; i < RT_ELEMENTS(s_apszAppendTests); i += 3) { const char *pszInput = s_apszAppendTests[i]; const char *pszAppend = s_apszAppendTests[i + 1]; const char *pszExpect = s_apszAppendTests[i + 2]; strcpy(szPath, pszInput); RTTESTI_CHECK_RC(rc = RTPathAppend(szPath, sizeof(szPath), pszAppend), VINF_SUCCESS); if (RT_FAILURE(rc)) continue; if (strcmp(szPath, pszExpect)) { RTTestIFailed("Unexpected result\n" " input: '%s'\n" " append: '%s'\n" " output: '%s'\n" "expected: '%s'", pszInput, pszAppend, szPath, pszExpect); } else { size_t const cchResult = strlen(szPath); strcpy(szPath, pszInput); RTTESTI_CHECK_RC(rc = RTPathAppend(szPath, cchResult + 2, pszAppend), VINF_SUCCESS); RTTESTI_CHECK(RT_FAILURE(rc) || !strcmp(szPath, pszExpect)); strcpy(szPath, pszInput); RTTESTI_CHECK_RC(rc = RTPathAppend(szPath, cchResult + 1, pszAppend), VINF_SUCCESS); RTTESTI_CHECK(RT_FAILURE(rc) || !strcmp(szPath, pszExpect)); if (strlen(pszInput) < cchResult) { strcpy(szPath, pszInput); RTTESTI_CHECK_RC(RTPathAppend(szPath, cchResult, pszAppend), VERR_BUFFER_OVERFLOW); } } } /* * RTPathJoin - reuse the append tests. */ RTTestSub(hTest, "RTPathJoin"); for (unsigned i = 0; i < RT_ELEMENTS(s_apszAppendTests); i += 3) { const char *pszInput = s_apszAppendTests[i]; const char *pszAppend = s_apszAppendTests[i + 1]; const char *pszExpect = s_apszAppendTests[i + 2]; memset(szPath, 'a', sizeof(szPath)); szPath[sizeof(szPath) - 1] = '\0'; RTTESTI_CHECK_RC(rc = RTPathJoin(szPath, sizeof(szPath), pszInput, pszAppend), VINF_SUCCESS); if (RT_FAILURE(rc)) continue; if (strcmp(szPath, pszExpect)) { RTTestIFailed("Unexpected result\n" " input: '%s'\n" " append: '%s'\n" " output: '%s'\n" "expected: '%s'", pszInput, pszAppend, szPath, pszExpect); } else { size_t const cchResult = strlen(szPath); memset(szPath, 'a', sizeof(szPath)); szPath[sizeof(szPath) - 1] = '\0'; RTTESTI_CHECK_RC(rc = RTPathJoin(szPath, cchResult + 2, pszInput, pszAppend), VINF_SUCCESS); RTTESTI_CHECK(RT_FAILURE(rc) || !strcmp(szPath, pszExpect)); memset(szPath, 'a', sizeof(szPath)); szPath[sizeof(szPath) - 1] = '\0'; RTTESTI_CHECK_RC(rc = RTPathJoin(szPath, cchResult + 1, pszInput, pszAppend), VINF_SUCCESS); RTTESTI_CHECK(RT_FAILURE(rc) || !strcmp(szPath, pszExpect)); RTTESTI_CHECK_RC(rc = RTPathJoin(szPath, cchResult, pszInput, pszAppend), VERR_BUFFER_OVERFLOW); } } /* * RTPathJoinA - reuse the append tests. */ RTTestSub(hTest, "RTPathJoinA"); for (unsigned i = 0; i < RT_ELEMENTS(s_apszAppendTests); i += 3) { const char *pszInput = s_apszAppendTests[i]; const char *pszAppend = s_apszAppendTests[i + 1]; const char *pszExpect = s_apszAppendTests[i + 2]; char *pszPathDst; RTTESTI_CHECK(pszPathDst = RTPathJoinA(pszInput, pszAppend)); if (!pszPathDst) continue; if (strcmp(pszPathDst, pszExpect)) { RTTestIFailed("Unexpected result\n" " input: '%s'\n" " append: '%s'\n" " output: '%s'\n" "expected: '%s'", pszInput, pszAppend, pszPathDst, pszExpect); } RTStrFree(pszPathDst); } /* * RTPathStripTrailingSlash */ static const char *s_apszStripTrailingSlash[] = { /* input result */ "/", "/", "//", "/", "////////////////////", "/", "/tmp", "/tmp", "/tmp////////////////", "/tmp", "tmp", "tmp", "tmp////////////////", "tmp", "./", ".", #if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS) "////////////////////", "/", "D:", "D:", "D:/", "D:/", "D:\\", "D:\\", "D:\\/\\", "D:\\", "D:/\\/\\", "D:/", "C:/Temp", "D:/Temp", "C:/Temp/", "D:/Temp/", "C:/Temp\\/", "D:/Temp", #endif }; for (unsigned i = 0; i < RT_ELEMENTS(s_apszStripTrailingSlash); i += 2) { const char *pszInput = s_apszStripTrailingSlash[i]; const char *pszExpect = s_apszStripTrailingSlash[i + 1]; strcpy(szPath, pszInput); cch = RTPathStripTrailingSlash(szPath); if (strcmp(szPath, pszExpect)) RTTestIFailed("Unexpected result\n" " input: '%s'\n" " output: '%s'\n" "expected: '%s'", pszInput, szPath, pszExpect); else RTTESTI_CHECK(cch == strlen(szPath)); } /* * RTPathCountComponents */ RTTestSub(hTest, "RTPathCountComponents"); RTTESTI_CHECK(RTPathCountComponents("") == 0); RTTESTI_CHECK(RTPathCountComponents("/") == 1); RTTESTI_CHECK(RTPathCountComponents("//") == 1); RTTESTI_CHECK(RTPathCountComponents("//////////////") == 1); RTTESTI_CHECK(RTPathCountComponents("//////////////bin") == 2); RTTESTI_CHECK(RTPathCountComponents("//////////////bin/") == 2); RTTESTI_CHECK(RTPathCountComponents("//////////////bin/////") == 2); RTTESTI_CHECK(RTPathCountComponents("..") == 1); RTTESTI_CHECK(RTPathCountComponents("../") == 1); RTTESTI_CHECK(RTPathCountComponents("../..") == 2); RTTESTI_CHECK(RTPathCountComponents("../../") == 2); #if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS) RTTESTI_CHECK(RTPathCountComponents("d:") == 1); RTTESTI_CHECK(RTPathCountComponents("d:/") == 1); RTTESTI_CHECK(RTPathCountComponents("d:/\\") == 1); RTTESTI_CHECK(RTPathCountComponents("d:\\") == 1); RTTESTI_CHECK(RTPathCountComponents("c:\\config.sys") == 2); RTTESTI_CHECK(RTPathCountComponents("c:\\windows") == 2); RTTESTI_CHECK(RTPathCountComponents("c:\\windows\\") == 2); RTTESTI_CHECK(RTPathCountComponents("c:\\windows\\system32") == 3); RTTESTI_CHECK(RTPathCountComponents("//./C$") == 1); RTTESTI_CHECK(RTPathCountComponents("\\\\.\\C$") == 1); RTTESTI_CHECK(RTPathCountComponents("/\\.\\C$") == 1); RTTESTI_CHECK(RTPathCountComponents("//myserver") == 1); RTTESTI_CHECK(RTPathCountComponents("//myserver/") == 1); RTTESTI_CHECK(RTPathCountComponents("//myserver/share") == 1); RTTESTI_CHECK(RTPathCountComponents("//myserver/share/") == 1); RTTESTI_CHECK(RTPathCountComponents("//myserver/share\\") == 1); RTTESTI_CHECK(RTPathCountComponents("//myserver/share\\x") == 2); RTTESTI_CHECK(RTPathCountComponents("//myserver/share\\x\\y") == 3); RTTESTI_CHECK(RTPathCountComponents("//myserver/share\\x\\y\\") == 3); #endif /* * RTPathCopyComponents */ struct { const char *pszSrc; size_t cComponents; const char *pszResult; } s_aCopyComponents[] = { { "", 0, "" }, { "", 5, "" }, { "/", 0, "" }, { "/", 1, "/" }, { "/", 2, "/" }, { "/usr/bin/sed", 0, "" }, { "/usr/bin/sed", 1, "/" }, { "/usr/bin/sed", 2, "/usr/" }, { "/usr/bin/sed", 3, "/usr/bin/" }, { "/usr/bin/sed", 4, "/usr/bin/sed" }, { "/usr/bin/sed", 5, "/usr/bin/sed" }, { "/usr/bin/sed", 6, "/usr/bin/sed" }, { "/usr///bin/sed", 2, "/usr///" }, }; for (unsigned i = 0; i < RT_ELEMENTS(s_aCopyComponents); i++) { const char *pszInput = s_aCopyComponents[i].pszSrc; size_t cComponents = s_aCopyComponents[i].cComponents; const char *pszResult = s_aCopyComponents[i].pszResult; memset(szPath, 'a', sizeof(szPath)); rc = RTPathCopyComponents(szPath, sizeof(szPath), pszInput, cComponents); RTTESTI_CHECK_RC(rc, VINF_SUCCESS); if (RT_SUCCESS(rc) && strcmp(szPath, pszResult)) RTTestIFailed("Unexpected result\n" " input: '%s' cComponents=%u\n" " output: '%s'\n" "expected: '%s'", pszInput, cComponents, szPath, pszResult); else if (RT_SUCCESS(rc)) { RTTESTI_CHECK_RC(RTPathCopyComponents(szPath, strlen(pszResult) + 1, pszInput, cComponents), VINF_SUCCESS); RTTESTI_CHECK_RC(RTPathCopyComponents(szPath, strlen(pszResult), pszInput, cComponents), VERR_BUFFER_OVERFLOW); } } /* * RTPathStripExt */ RTTestSub(hTest, "RTPathStripExt"); struct { const char *pszSrc; const char *pszResult; } s_aStripExt[] = { { "filename.ext", "filename" }, { "filename.ext1.ext2.ext3", "filename.ext1.ext2" }, { "filename..ext", "filename." }, { "filename.ext.", "filename.ext" }, /** @todo This is a bit weird/wrong, but not half as weird as the way Windows+OS/2 deals with a trailing dots. */ }; for (unsigned i = 0; i < RT_ELEMENTS(s_aStripExt); i++) { const char *pszInput = s_aStripExt[i].pszSrc; const char *pszResult = s_aStripExt[i].pszResult; strcpy(szPath, pszInput); RTPathStripExt(szPath); if (strcmp(szPath, pszResult)) RTTestIFailed("Unexpected result\n" " input: '%s'\n" " output: '%s'\n" "expected: '%s'", pszInput, szPath, pszResult); } /* * RTPathCalcRelative */ RTTestSub(hTest, "RTPathCalcRelative"); struct { const char *pszFrom; const char *pszTo; int rc; const char *pszExpected; } s_aRelPath[] = { { "/home/test.ext", "/home/test2.ext", VINF_SUCCESS, "test2.ext"}, { "/dir/test.ext", "/dir/dir2/test2.ext", VINF_SUCCESS, "dir2/test2.ext"}, { "/dir/dir2/test.ext", "/dir/test2.ext", VINF_SUCCESS, "../test2.ext"}, { "/dir/dir2/test.ext", "/dir/dir3/test2.ext", VINF_SUCCESS, "../dir3/test2.ext"}, #if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS) { "\\\\server\\share\\test.ext", "\\\\server\\share2\\test2.ext", VERR_NOT_SUPPORTED, ""}, { "c:\\dir\\test.ext", "f:\\dir\\test.ext", VERR_NOT_SUPPORTED, ""} #endif }; for (unsigned i = 0; i < RT_ELEMENTS(s_aRelPath); i++) { const char *pszFrom = s_aRelPath[i].pszFrom; const char *pszTo = s_aRelPath[i].pszTo; rc = RTPathCalcRelative(szPath, sizeof(szPath), pszFrom, pszTo); if (rc != s_aRelPath[i].rc) RTTestIFailed("Unexpected return code\n" " got: %Rrc\n" "expected: %Rrc", rc, s_aRelPath[i].rc); else if ( RT_SUCCESS(rc) && strcmp(szPath, s_aRelPath[i].pszExpected)) RTTestIFailed("Unexpected result\n" " from: '%s'\n" " to: '%s'\n" " output: '%s'\n" "expected: '%s'", pszFrom, pszTo, szPath, s_aRelPath[i].pszExpected); } /* * Summary. */ return RTTestSummaryAndDestroy(hTest); }
static int VBoxServiceAutoMountProcessMappings(PVBGLR3SHAREDFOLDERMAPPING paMappings, uint32_t cMappings, const char *pszMountDir, const char *pszSharePrefix, uint32_t uClientID) { if (cMappings == 0) return VINF_SUCCESS; AssertPtrReturn(paMappings, VERR_INVALID_PARAMETER); AssertPtrReturn(pszMountDir, VERR_INVALID_PARAMETER); AssertPtrReturn(pszSharePrefix, VERR_INVALID_PARAMETER); AssertReturn(uClientID > 0, VERR_INVALID_PARAMETER); int rc = VINF_SUCCESS; for (uint32_t i = 0; i < cMappings && RT_SUCCESS(rc); i++) { char *pszShareName = NULL; rc = VbglR3SharedFolderGetName(uClientID, paMappings[i].u32Root, &pszShareName); if ( RT_SUCCESS(rc) && *pszShareName) { VBoxServiceVerbose(3, "VBoxServiceAutoMountWorker: Connecting share %u (%s) ...\n", i+1, pszShareName); char *pszShareNameFull = NULL; if (RTStrAPrintf(&pszShareNameFull, "%s%s", pszSharePrefix, pszShareName) > 0) { char szMountPoint[RTPATH_MAX]; rc = RTPathJoin(szMountPoint, sizeof(szMountPoint), pszMountDir, pszShareNameFull); if (RT_SUCCESS(rc)) { VBoxServiceVerbose(4, "VBoxServiceAutoMountWorker: Processing mount point \"%s\"\n", szMountPoint); struct group *grp_vboxsf = getgrnam("vboxsf"); if (grp_vboxsf) { struct vbsf_mount_opts mount_opts = { 0, /* uid */ (int)grp_vboxsf->gr_gid, /* gid */ 0, /* ttl */ 0770, /* dmode, owner and group "vboxsf" have full access */ 0770, /* fmode, owner and group "vboxsf" have full access */ 0, /* dmask */ 0, /* fmask */ 0, /* ronly */ 0, /* noexec */ 0, /* nodev */ 0, /* nosuid */ 0, /* remount */ "\0", /* nls_name */ NULL, /* convertcp */ }; rc = VBoxServiceAutoMountSharedFolder(pszShareName, szMountPoint, &mount_opts); } else VBoxServiceError("VBoxServiceAutoMountWorker: Group \"vboxsf\" does not exist\n"); } else VBoxServiceError("VBoxServiceAutoMountWorker: Unable to join mount point/prefix/shrae, rc = %Rrc\n", rc); RTStrFree(pszShareNameFull); } else VBoxServiceError("VBoxServiceAutoMountWorker: Unable to allocate full share name\n"); RTStrFree(pszShareName); } else VBoxServiceError("VBoxServiceAutoMountWorker: Error while getting the shared folder name for root node = %u, rc = %Rrc\n", paMappings[i].u32Root, rc); } /* for cMappings. */ return rc; }