MCPcopy Create free account
hub / github.com/TeXworks/texworks / mayReadFile

Method mayReadFile

src/scripting/ScriptAPI.cpp:420–458  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

418}
419
420bool ScriptAPI::mayReadFile(const QString& filename, QObject * context) const
421{
422 Tw::Settings settings;
423 if (!m_script)
424 return false;
425
426 QDir scriptDir(QFileInfo(m_script->getFilename()).absoluteDir());
427 QVariant targetFile;
428 QDir targetDir;
429
430 if (settings.value(QString::fromLatin1("allowScriptFileReading"), kDefault_AllowScriptFileReading).toBool())
431 return true;
432
433 // even if global reading is disallowed, some exceptions may apply
434 QFileInfo fi(QDir::cleanPath(filename));
435
436 // reading in subdirectories of the script file's directory is always allowed
437 if (!scriptDir.relativeFilePath(fi.absolutePath()).startsWith(QLatin1String("..")))
438 return true;
439
440 if (context) {
441 // reading subdirectories of the current file is always allowed
442 targetFile = context->property("fileName");
443 if (targetFile.isValid() && !targetFile.toString().isEmpty()) {
444 targetDir = QFileInfo(targetFile.toString()).absoluteDir();
445 if (!targetDir.relativeFilePath(fi.absolutePath()).startsWith(QLatin1String("..")))
446 return true;
447 }
448 // reading subdirectories of the root file is always allowed
449 targetFile = context->property("rootFileName");
450 if (targetFile.isValid() && !targetFile.toString().isEmpty()) {
451 targetDir = QFileInfo(targetFile.toString()).absoluteDir();
452 if (!targetDir.relativeFilePath(fi.absolutePath()).startsWith(QLatin1String("..")))
453 return true;
454 }
455 }
456
457 return false;
458}
459
460} // namespace Scripting
461} // namespace Tw

Callers 1

openFileFromScriptMethod · 0.45

Calls 3

getFilenameMethod · 0.45
valueMethod · 0.45
isValidMethod · 0.45

Tested by

no test coverage detected