field_ios

In-the-field app for Radroots on iOS
git clone https://radroots.dev/git/field_ios.git
Log | Files | Refs | README | LICENSE

TeraOpenedMediaCloseFixture.swift (1328B)


      1 import Darwin
      2 import Foundation
      3 @testable import TeraApp
      4 
      5 enum TeraOpenedMediaCloseFixture {
      6   static func closeConcurrently(_ owner: TeraOpenedMedia) async {
      7     await withTaskGroup(of: Void.self) { group in
      8       for _ in 0 ..< 64 {
      9         group.addTask {
     10           for _ in 0 ..< 64 {
     11             owner.close()
     12           }
     13         }
     14       }
     15     }
     16   }
     17 }
     18 
     19 /// The unique fixture stays linked throughout inspection. An inode comparison
     20 /// remains valid if another simulator task reuses a released descriptor slot.
     21 struct TeraOpenFileProbe: Sendable {
     22   private let device: Int32
     23   private let inode: UInt64
     24 
     25   init(url: URL) throws {
     26     var value = stat()
     27     guard Darwin.lstat(url.path, &value) == 0 else { throw ProbeError.unavailable }
     28     device = value.st_dev
     29     inode = value.st_ino
     30   }
     31 
     32   func owns(_ descriptor: Int32) -> Bool {
     33     var value = stat()
     34     return Darwin.fstat(descriptor, &value) == 0
     35       && value.st_dev == device && value.st_ino == inode
     36   }
     37 
     38   var descriptorCount: Int {
     39     // The operating system bounds the descriptor table; no pathname is opened
     40     // by this scan, and descriptors belonging to other files are ignored.
     41     (0 ..< getdtablesize()).reduce(0) { count, descriptor in
     42       count + (owns(descriptor) ? 1 : 0)
     43     }
     44   }
     45 
     46   private enum ProbeError: Error { case unavailable }
     47 }