From 0a235650cc6597b159a32104d3d778153ad31ddd Mon Sep 17 00:00:00 2001 From: Erik Verbruggen Date: Thu, 15 Sep 2022 15:30:16 +0200 Subject: [PATCH] Creaate `LaunchAgents` directory if it doesn't exist Signed-off-by: Camila Ayres --- src/common/utility_mac.mm | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/common/utility_mac.mm b/src/common/utility_mac.mm index 8d3cde02a95f0..6ace02b7bad9f 100644 --- a/src/common/utility_mac.mm +++ b/src/common/utility_mac.mm @@ -66,6 +66,25 @@ static Result writePlistToFile(NSString *plistFile, NSDictionary *plist) { NSError *error = nil; + + // Check if the directory exists. On a fresh installation for example, the directory does not + // exist, so writing the plist file below will fail. + QDir plistDir = QFileInfo(QString::fromNSString(plistFile)).dir(); + if (!plistDir.exists()) { + if (!QDir().mkpath(plistDir.path())) { + return QString(QStringLiteral("Failed to create directory '%1'")).arg(plistDir.path()); + } + + // Permissions always seem to be 0700, so set that. + // Note: the Permission enum documentation states that on unix the owner permissions are + // returned, but: "This behavior might change in a future Qt version." So we play it safe, + // and set both the user and the owner permissions to rwx. + if (!QFile(plistDir.path()).setPermissions(QFileDevice::ReadOwner | QFileDevice::ReadUser | QFileDevice::WriteOwner | QFileDevice::WriteUser | QFileDevice::ExeOwner | QFileDevice::ExeUser)) { + qCInfo(lcUtility()) << "Failed to set directory permmissions for" << plistDir.path(); + } + } + + // Now write the file. if (![plist writeToURL:[NSURL fileURLWithPath:plistFile isDirectory:NO] error:&error]) { return QString::fromNSString(error.localizedDescription); }