Loading...
common/FileManager.cpp dyld-1042.1 dyld-1330
--- dyld/dyld-1042.1/common/FileManager.cpp
+++ dyld/dyld-1330/common/FileManager.cpp
@@ -21,6 +21,10 @@
  *
  * @APPLE_LICENSE_HEADER_END@
  */
+
+#include <TargetConditionals.h>
+
+#if !TARGET_OS_EXCLAVEKIT
 
 #include <fcntl.h>
 #include <unistd.h>
@@ -43,6 +47,9 @@
 FileManager::FileManager(Allocator& allocator, const SyscallDelegate* syscall)
 : _syscall(syscall), _allocator(&allocator), _fsUUIDMap(_allocator->makeUnique<OrderedMap<uint64_t,UUID>>(*_allocator)) {}
 
+FileManager::FileManager(Allocator& allocator)
+    : _syscall(nullptr), _allocator(&allocator), _fsUUIDMap(_allocator->makeUnique<OrderedMap<uint64_t,UUID>>(*_allocator)) {}
+
 void FileManager::swap(FileManager& other) {
     using std::swap;
 
@@ -50,13 +57,14 @@
     std::swap(_fsUUIDMap,   other._fsUUIDMap);
 }
 
-
-FileRecord FileManager::fileRecordForPath(const char* filePath) {
-    auto str = _allocator->strdup(filePath);
+FileRecord FileManager::fileRecordForPath(Allocator& allocator, const char* filePath) {
+    const char* str = nullptr;
+    if ( filePath )
+        str = allocator.strdup(filePath);
     return FileRecord(*this, UniquePtr<const char>(str));
 }
 
-FileRecord FileManager::fileRecordForStat(struct stat& sb) {
+FileRecord FileManager::fileRecordForStat(const struct stat& sb) {
     return FileRecord(*this, sb);
 }
 
@@ -89,6 +97,7 @@
     } __attribute__((aligned(4), packed));
     typedef struct VolAttrBuf VolAttrBuf;
 
+    STACK_ALLOCATOR(ephemeralAllocator, 0);
     while (1) {
         int fsCount = getfsstat(nullptr, 0, MNT_NOWAIT);
         if (fsCount == -1) {
@@ -96,10 +105,10 @@
             break;
         }
         int fsInfoSize = fsCount*sizeof(struct statfs);
-        auto fsInfos = (struct statfs *)_allocator->malloc(fsInfoSize);
+        auto fsInfos = (struct statfs *)ephemeralAllocator.malloc(fsInfoSize);
         if (this->getfsstat(fsInfos, fsInfoSize, MNT_NOWAIT) != fsCount) {
             // Retry
-            _allocator->free((void*)fsInfos);
+            ephemeralAllocator.free((void*)fsInfos);
             continue;
         }
         for (auto i = 0; i < fsCount; ++i) {
@@ -108,13 +117,13 @@
             // groups that are not relevent here.
             uint64_t f_fsid = (*((uint64_t*)&fsInfos[i].f_fsid)) & 0x00ffffffff;
             if (_fsUUIDMap->find(f_fsid) != _fsUUIDMap->end()) { continue; }
-#if TARGET_OS_OSX
-            // On macOS getattrlist() can upcall when used against a non-root volume which results in a deadlock.
+
+            // getattrlist() can upcall when used against a non-root volume which results in a deadlock.
             if ((fsInfos[i].f_flags & MNT_ROOTFS) == 0) {
                 _fsUUIDMap->insert({f_fsid, UUID()});
                 continue;
             }
-#endif
+
             int             err;
             attrlist        attrList;
             VolAttrBuf      attrBuf;
@@ -132,7 +141,7 @@
                 _fsUUIDMap->insert({f_fsid, UUID()});
             }
         }
-        _allocator->free((void*)fsInfos);
+        ephemeralAllocator.free((void*)fsInfos);
         break;
     }
 }
@@ -184,13 +193,13 @@
 
 UniquePtr<char> FileManager::getPath(uint64_t fsid, uint64_t OID) {
     if ((fsid == 0) || (OID == 0)) { return nullptr; }
-    char path[MAXPATHLEN];
-    ssize_t result = this->fsgetpath(&path[0], MAXPATHLEN, fsid, OID);
+    char path[PATH_MAX];
+    ssize_t result = this->fsgetpath(&path[0], PATH_MAX, fsid, OID);
 #if !__LP64__
     //FIXME: Workaround for missing stat high bit on 32 bit platforms
     if (result == -1) {
         OID = 0x0fffffff00000000ULL | OID;
-        result = this->fsgetpath(&path[0], MAXPATHLEN, fsid, OID);
+        result = this->fsgetpath(&path[0], PATH_MAX, fsid, OID);
     }
 #endif
     if (result == -1) {
@@ -243,7 +252,7 @@
         }
     }
 
-FileRecord::FileRecord(FileManager& fileManager, struct stat& sb)
+FileRecord::FileRecord(FileManager& fileManager, const struct stat& sb)
     :  _fileManager(&fileManager), _objectID(sb.st_ino), _device(sb.st_dev), _volume(_fileManager->uuidForFileSystem(_device)), _mtime(sb.st_mtime), _statResult(0) {}
 
 FileRecord::FileRecord(const FileRecord& other)
@@ -377,3 +386,5 @@
 }
 
 }; /* namedpace dyld4 */
+
+#endif // !TARGET_OS_EXCLAVEKIT