FilePath getDefaultWorkingDirectory() { // calculate using user settings FilePath defaultWorkingDir = userSettings().initialWorkingDirectory(); FilePath sessionDefaultWorkingDir = FilePath(session::options().defaultWorkingDir()); // return it if it exists, otherwise use the // session specified value if it exists // otherwise, use the default user home path if (defaultWorkingDir.exists() && defaultWorkingDir.isDirectory()) return defaultWorkingDir; else if (sessionDefaultWorkingDir.exists() && sessionDefaultWorkingDir.isDirectory()) return sessionDefaultWorkingDir; else return session::options().userHomePath(); }
void onFileChanged(FilePath sourceFilePath) { // ignore file changes while Packrat is running if (s_runningPackratAction != PACKRAT_ACTION_NONE) return; // we only care about mutations to files in the Packrat library directory // (and packrat.lock) FilePath libraryPath = projects::projectContext().directory().complete(kPackratLibPath); if (sourceFilePath.filename() == kPackratLockfile) { PACKRAT_TRACE("detected change to lockfile " << sourceFilePath); checkHashes(HASH_TYPE_LOCKFILE, HASH_STATE_OBSERVED, onLockfileUpdate); } else if (sourceFilePath.isWithin(libraryPath) && (sourceFilePath.isDirectory() || sourceFilePath.filename() == "DESCRIPTION")) { // ignore changes in the RStudio-managed manipulate and rstudio // directories and the files within them if (sourceFilePath.filename() == "manipulate" || sourceFilePath.filename() == "rstudio" || sourceFilePath.parent().filename() == "manipulate" || sourceFilePath.parent().filename() == "rstudio") { return; } PACKRAT_TRACE("detected change to library file " << sourceFilePath); checkHashes(HASH_TYPE_LIBRARY, HASH_STATE_OBSERVED, onLibraryUpdate); } }
bool RVersionsScanner::detectSystemRVersion(core::r_util::RVersion* pVersion, std::string* pErrMsg) { // return cached version if we have it if (!systemVersion_.empty()) { *pVersion = systemVersion_; return true; } // check for which R override FilePath rWhichRPath; if (!whichROverride_.empty()) rWhichRPath = FilePath(whichROverride_); // if it's a directory then see if we can find the script if (rWhichRPath.isDirectory()) { FilePath rScriptPath = rWhichRPath.childPath("bin/R"); if (rScriptPath.exists()) rWhichRPath = rScriptPath; } // attempt to detect R version bool result = detectRVersion(rWhichRPath, pVersion, pErrMsg); // if we detected it then cache it if (result) systemVersion_ = *pVersion; // return result return result; }
FilePath getInitialWorkingDirectory() { // check for a project if (projects::projectContext().hasProject()) { return projects::projectContext().directory(); } // check for working dir in project none else if (options().sessionScope().isProjectNone()) { return getActiveSessionInitialWorkingDirectory(); } // see if there is an override from the environment (perhaps based // on a folder drag and drop or other file association) FilePath workingDirPath = session::options().initialWorkingDirOverride(); if (workingDirPath.exists() && workingDirPath.isDirectory()) { return workingDirPath; } else { return getActiveSessionInitialWorkingDirectory(); } }
// IN: Array<String> paths, String targetPath Error moveFiles(const ::core::json::JsonRpcRequest& request, json::JsonRpcResponse* pResponse) { json::Array files; std::string targetPath; Error error = json::readParams(request.params, &files, &targetPath); if (error) return error ; // extract vector of FilePath std::vector<FilePath> filePaths ; Error extractError = extractFilePaths(files, &filePaths) ; if (extractError) return extractError ; // create FilePath for target directory FilePath targetDirPath = module_context::resolveAliasedPath(targetPath); if (!targetDirPath.isDirectory()) return Error(json::errc::ParamInvalid, ERROR_LOCATION); // move the files for (std::vector<FilePath>::const_iterator it = filePaths.begin(); it != filePaths.end(); ++it) { // move the file FilePath targetPath = targetDirPath.childPath(it->filename()) ; Error moveError = it->move(targetPath) ; if (moveError) return moveError ; } return Success() ; }
bool copySourceFile(const FilePath& sourceDir, const FilePath& destDir, int level, const FilePath& sourceFilePath) { // compute the target path std::string relativePath = sourceFilePath.relativePath(sourceDir); FilePath targetPath = destDir.complete(relativePath); // if the copy item is a directory just create it if (sourceFilePath.isDirectory()) { Error error = targetPath.ensureDirectory(); if (error) LOG_ERROR(error); } // otherwise copy it else { Error error = sourceFilePath.copy(targetPath); if (error) LOG_ERROR(error); } return true; }
// IN: String sourcePath, String targetPath Error copyFile(const ::core::json::JsonRpcRequest& request, json::JsonRpcResponse* pResponse) { // read params std::string sourcePath, targetPath; bool overwrite; Error error = json::readParams(request.params, &sourcePath, &targetPath, &overwrite); if (error) return error; FilePath targetFilePath = module_context::resolveAliasedPath(targetPath); // make sure the target path doesn't exist if (targetFilePath.exists()) { if (overwrite) { Error error = targetFilePath.remove(); if (error) { LOG_ERROR(error); return fileExistsError(ERROR_LOCATION); } } else { return fileExistsError(ERROR_LOCATION); } } // compute the source file path FilePath sourceFilePath = module_context::resolveAliasedPath(sourcePath); // copy directories recursively Error copyError ; if (sourceFilePath.isDirectory()) { // create the target directory Error error = targetFilePath.ensureDirectory(); if (error) return error ; // iterate over the source copyError = sourceFilePath.childrenRecursive( boost::bind(copySourceFile, sourceFilePath, targetFilePath, _1, _2)); } else { copyError = sourceFilePath.copy(targetFilePath); } // check quota after copies quotas::checkQuotaStatus(); // return error status return copyError; }
void FileDialog::setPath(const FilePath &path) { if(path.isDirectory()) { m_dir_path = path; m_edit_box->setText({}); } else { m_dir_path = path.parent(); m_edit_box->setText(toUTF32Checked(path.fileName())); } updateList(); updateButtons(); }
Error checkForExternalEdit(const json::JsonRpcRequest& request, json::JsonRpcResponse* pResponse) { pResponse->setSuppressDetectChanges(true); // params std::string id ; Error error = json::readParams(request.params, &id); if (error) return error; boost::shared_ptr<SourceDocument> pDoc(new SourceDocument()) ; error = source_database::get(id, pDoc); if (error) return error ; json::Object result; result["modified"] = false; result["deleted"] = false; // Only check if this document has ever been saved if (!pDoc->path().empty()) { FilePath docFile = module_context::resolveAliasedPath(pDoc->path()); if (!docFile.exists() || docFile.isDirectory()) { result["deleted"] = true; pDoc->setDirty(true); error = source_database::put(pDoc); if (error) return error; } else { std::time_t lastWriteTime ; pDoc->checkForExternalEdit(&lastWriteTime); if (lastWriteTime) { FilePath filePath = module_context::resolveAliasedPath(pDoc->path()) ; json::Object fsItem = module_context::createFileSystemItem(filePath); result["item"] = fsItem; result["modified"] = true; } } } pResponse->setResult(result); return Success(); }
void handleFilesRequest(const http::Request& request, http::Response* pResponse) { Options& options = session::options(); if (options.programMode() != kSessionProgramModeServer) { pResponse->setError(http::status::NotFound, request.uri() + " not found"); return; } // get prefix and uri std::string prefix = "/files/"; std::string uri = request.uri(); // validate the uri if (prefix.length() >= uri.length() || // prefix longer than uri uri.find(prefix) != 0 || // uri doesn't start with prefix uri.find("..") != std::string::npos) // uri has inavlid char sequence { pResponse->setError(http::status::NotFound, request.uri() + " not found"); return; } // compute path to file int prefixLen = prefix.length(); std::string relativePath = http::util::urlDecode(uri.substr(prefixLen)); if (relativePath.empty()) { pResponse->setError(http::status::NotFound, request.uri() + " not found"); return; } // complete path to file FilePath filePath = module_context::userHomePath().complete(relativePath); // no directory listing available if (filePath.isDirectory()) { pResponse->setError(http::status::NotFound, "No listing available for " + request.uri()); return; } pResponse->setNoCacheHeaders(); pResponse->setFile(filePath, request); }
FilePath getInitialWorkingDirectory() { // check for a project if (projects::projectContext().hasProject()) { return projects::projectContext().directory(); } // check for working dir in project none else if (options().sessionScope().isProjectNone()) { // if this is the initial session then use the default working directory // (reset initial to false so this is one shot thing) using namespace module_context; if (activeSession().initial()) { activeSession().setInitial(false); return getDefaultWorkingDirectory(); } FilePath workingDirPath = module_context::resolveAliasedPath( module_context::activeSession().workingDir()); if (workingDirPath.exists()) return workingDirPath; else return getDefaultWorkingDirectory(); } // see if there is an override from the environment (perhaps based // on a folder drag and drop or other file association) FilePath workingDirPath = session::options().initialWorkingDirOverride(); if (workingDirPath.exists() && workingDirPath.isDirectory()) { return workingDirPath; } else { // if not then just return default working dir return getDefaultWorkingDirectory(); } }