|
| 1 | +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 5 | + |
| 6 | +#include <ApplicationServices/ApplicationServices.h> |
| 7 | +#include <CoreFoundation/CoreFoundation.h> |
| 8 | +#include <CoreServices/CoreServices.h> |
| 9 | +#include <IOKit/IOKitLib.h> |
| 10 | +#include <string.h> |
| 11 | +#include <sys/mount.h> |
| 12 | +#include <sys/param.h> |
| 13 | + |
| 14 | +#include "MacRunFromDmgUtils.h" |
| 15 | + |
| 16 | +#include "nsCocoaFeatures.h" |
| 17 | +#include "nsCommandLine.h" |
| 18 | +#include "nsCommandLineServiceMac.h" |
| 19 | +#include "nsILocalFileMac.h" |
| 20 | +#include "nsIMacDockSupport.h" |
| 21 | +#include "nsObjCExceptions.h" |
| 22 | +#include "nsString.h" |
| 23 | + |
| 24 | +// For IOKit docs, see: |
| 25 | +// https://developer.apple.com/documentation/iokit |
| 26 | +// https://developer.apple.com/library/archive/documentation/DeviceDrivers/Conceptual/IOKitFundamentals/ |
| 27 | + |
| 28 | +namespace mozilla { |
| 29 | +namespace MacRunFromDmgUtils { |
| 30 | + |
| 31 | +bool IsAppRunningFromDmg() { |
| 32 | + NS_OBJC_BEGIN_TRY_BLOCK_RETURN; |
| 33 | + |
| 34 | + const char* path = [[[NSBundle mainBundle] bundlePath] fileSystemRepresentation]; |
| 35 | + |
| 36 | + struct statfs statfsBuf; |
| 37 | + if (statfs(path, &statfsBuf) != 0) { |
| 38 | + return false; |
| 39 | + } |
| 40 | + |
| 41 | + // Optimization to minimize impact on startup time: |
| 42 | + if (!(statfsBuf.f_flags & MNT_RDONLY)) { |
| 43 | + return false; |
| 44 | + } |
| 45 | + |
| 46 | + // Get the "BSD device name" ("diskXsY", as found in /dev) of the filesystem |
| 47 | + // our app is on so we can use IOKit to get its IOMedia object: |
| 48 | + const char devDirPath[] = "/dev/"; |
| 49 | + const int devDirPathLength = strlen(devDirPath); |
| 50 | + if (strncmp(statfsBuf.f_mntfromname, devDirPath, devDirPathLength) != 0) { |
| 51 | + // Does this ever happen? |
| 52 | + return false; |
| 53 | + } |
| 54 | + const char* bsdDeviceName = statfsBuf.f_mntfromname + devDirPathLength; |
| 55 | + |
| 56 | + // Get the IOMedia object: |
| 57 | + // (Note: IOServiceGetMatchingServices takes ownership of serviceDict's ref.) |
| 58 | + CFMutableDictionaryRef serviceDict = IOBSDNameMatching(kIOMasterPortDefault, 0, bsdDeviceName); |
| 59 | + if (!serviceDict) { |
| 60 | + return false; |
| 61 | + } |
| 62 | + io_service_t media = IOServiceGetMatchingService(kIOMasterPortDefault, serviceDict); |
| 63 | + if (!media || !IOObjectConformsTo(media, "IOMedia")) { |
| 64 | + return false; |
| 65 | + } |
| 66 | + |
| 67 | + // Search the parent chain for a service implementing the disk image class |
| 68 | + // (taking care to start with `media` itself): |
| 69 | + io_service_t imageDrive = IO_OBJECT_NULL; |
| 70 | + io_iterator_t iter; |
| 71 | + if (IORegistryEntryCreateIterator(media, kIOServicePlane, |
| 72 | + kIORegistryIterateRecursively | kIORegistryIterateParents, |
| 73 | + &iter) != KERN_SUCCESS) { |
| 74 | + IOObjectRelease(media); |
| 75 | + return false; |
| 76 | + } |
| 77 | + const char* imageClass = |
| 78 | + nsCocoaFeatures::macOSVersionMajor() >= 12 ? "AppleDiskImageDevice" : "IOHDIXHDDrive"; |
| 79 | + for (imageDrive = media; imageDrive; imageDrive = IOIteratorNext(iter)) { |
| 80 | + if (IOObjectConformsTo(imageDrive, imageClass)) { |
| 81 | + break; |
| 82 | + } |
| 83 | + IOObjectRelease(imageDrive); |
| 84 | + } |
| 85 | + IOObjectRelease(iter); |
| 86 | + |
| 87 | + if (imageDrive) { |
| 88 | + IOObjectRelease(imageDrive); |
| 89 | + return true; |
| 90 | + } |
| 91 | + return false; |
| 92 | + |
| 93 | + NS_OBJC_END_TRY_BLOCK_RETURN(false); |
| 94 | +} |
| 95 | + |
| 96 | +} // namespace MacRunFromDmgUtils |
| 97 | +} // namespace mozilla |
0 commit comments