UTMScriptingGuestFileImpl.swift 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. //
  2. // Copyright © 2023 osy. All rights reserved.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. import Foundation
  17. import QEMUKitInternal
  18. @MainActor
  19. @objc(UTMScriptingGuestFileImpl)
  20. class UTMScriptingGuestFileImpl: NSObject, UTMScriptable {
  21. @objc private(set) var id: Int
  22. private var parent: UTMScriptingVirtualMachineImpl
  23. init(from handle: Int, parent: UTMScriptingVirtualMachineImpl) {
  24. self.id = handle
  25. self.parent = parent
  26. }
  27. override var objectSpecifier: NSScriptObjectSpecifier? {
  28. guard let parentDescription = parent.classDescription as? NSScriptClassDescription else {
  29. return nil
  30. }
  31. let parentSpecifier = parent.objectSpecifier
  32. return NSUniqueIDSpecifier(containerClassDescription: parentDescription,
  33. containerSpecifier: parentSpecifier,
  34. key: "openFiles",
  35. uniqueID: id)
  36. }
  37. private func seek(to offset: Int, whence: AEKeyword?, using guestAgent: QEMUGuestAgent) async throws {
  38. let seek: QEMUGuestAgentSeek
  39. if let whence = whence {
  40. switch UTMScriptingWhence(rawValue: whence) {
  41. case .startPosition: seek = .set
  42. case .currentPosition: seek = .cur
  43. case .endPosition: seek = .end
  44. default: seek = .set
  45. }
  46. } else {
  47. seek = .set
  48. }
  49. try await guestAgent.guestFileSeek(id, offset: offset, whence: seek)
  50. }
  51. @objc func read(_ command: NSScriptCommand) {
  52. let id = self.id
  53. let offset = command.evaluatedArguments?["offset"] as? Int
  54. let whence = command.evaluatedArguments?["whence"] as? AEKeyword
  55. let length = command.evaluatedArguments?["length"] as? Int
  56. let isBase64Encoded = command.evaluatedArguments?["isBase64Encoded"] as? Bool ?? false
  57. let isClosing = command.evaluatedArguments?["isClosing"] as? Bool ?? true
  58. withScriptCommand(command) { [self] in
  59. guard let guestAgent = await parent.guestAgent else {
  60. throw UTMScriptingVirtualMachineImpl.ScriptingError.guestAgentNotRunning
  61. }
  62. defer {
  63. if isClosing {
  64. guestAgent.guestFileClose(id)
  65. }
  66. }
  67. if let offset = offset {
  68. try await seek(to: offset, whence: whence, using: guestAgent)
  69. }
  70. if let length = length {
  71. let data = try await guestAgent.guestFileRead(id, count: length)
  72. return textFromData(data, isBase64Encoded: isBase64Encoded)
  73. }
  74. var data: Data
  75. var allData = Data()
  76. repeat {
  77. data = try await guestAgent.guestFileRead(id, count: 4096)
  78. allData += data
  79. } while data.count > 0
  80. return textFromData(allData, isBase64Encoded: isBase64Encoded)
  81. }
  82. }
  83. @objc func pull(_ command: NSScriptCommand) {
  84. let id = self.id
  85. let file = command.evaluatedArguments?["file"] as? URL
  86. let isClosing = command.evaluatedArguments?["isClosing"] as? Bool ?? true
  87. withScriptCommand(command) { [self] in
  88. guard let guestAgent = await parent.guestAgent else {
  89. throw UTMScriptingVirtualMachineImpl.ScriptingError.guestAgentNotRunning
  90. }
  91. defer {
  92. if isClosing {
  93. guestAgent.guestFileClose(id)
  94. }
  95. }
  96. guard let file = file else {
  97. throw UTMScriptingVirtualMachineImpl.ScriptingError.invalidParameter
  98. }
  99. try await guestAgent.guestFileSeek(id, offset: 0, whence: .set)
  100. _ = file.startAccessingSecurityScopedResource()
  101. defer {
  102. file.stopAccessingSecurityScopedResource()
  103. }
  104. let handle = try FileHandle(forWritingTo: file)
  105. var data: Data
  106. repeat {
  107. data = try await guestAgent.guestFileRead(id, count: 4096)
  108. try handle.write(contentsOf: data)
  109. } while data.count > 0
  110. }
  111. }
  112. @objc func write(_ command: NSScriptCommand) {
  113. let id = self.id
  114. let data = command.evaluatedArguments?["data"] as? String
  115. let offset = command.evaluatedArguments?["offset"] as? Int
  116. let whence = command.evaluatedArguments?["whence"] as? AEKeyword
  117. let isBase64Encoded = command.evaluatedArguments?["isBase64Encoded"] as? Bool ?? false
  118. let isClosing = command.evaluatedArguments?["isClosing"] as? Bool ?? true
  119. withScriptCommand(command) { [self] in
  120. guard let guestAgent = await parent.guestAgent else {
  121. throw UTMScriptingVirtualMachineImpl.ScriptingError.guestAgentNotRunning
  122. }
  123. defer {
  124. if isClosing {
  125. guestAgent.guestFileClose(id)
  126. }
  127. }
  128. guard let data = dataFromText(data, isBase64Encoded: isBase64Encoded) else {
  129. throw UTMScriptingVirtualMachineImpl.ScriptingError.invalidParameter
  130. }
  131. if let offset = offset {
  132. try await seek(to: offset, whence: whence, using: guestAgent)
  133. }
  134. try await guestAgent.guestFileWrite(id, data: data)
  135. try await guestAgent.guestFileFlush(id)
  136. }
  137. }
  138. @objc func push(_ command: NSScriptCommand) {
  139. let id = self.id
  140. let file = command.evaluatedArguments?["file"] as? URL
  141. let isClosing = command.evaluatedArguments?["isClosing"] as? Bool ?? true
  142. withScriptCommand(command) { [self] in
  143. guard let guestAgent = await parent.guestAgent else {
  144. throw UTMScriptingVirtualMachineImpl.ScriptingError.guestAgentNotRunning
  145. }
  146. defer {
  147. if isClosing {
  148. guestAgent.guestFileClose(id)
  149. }
  150. }
  151. guard let file = file else {
  152. throw UTMScriptingVirtualMachineImpl.ScriptingError.invalidParameter
  153. }
  154. try await guestAgent.guestFileSeek(id, offset: 0, whence: .set)
  155. _ = file.startAccessingSecurityScopedResource()
  156. defer {
  157. file.stopAccessingSecurityScopedResource()
  158. }
  159. let handle = try FileHandle(forReadingFrom: file)
  160. var data: Data
  161. repeat {
  162. data = try handle.read(upToCount: 4096) ?? Data()
  163. try await guestAgent.guestFileWrite(id, data: data)
  164. } while data.count > 0
  165. }
  166. }
  167. @objc func close(_ command: NSScriptCommand) {
  168. withScriptCommand(command) { [self] in
  169. guard let guestAgent = await parent.guestAgent else {
  170. throw UTMScriptingVirtualMachineImpl.ScriptingError.guestAgentNotRunning
  171. }
  172. try await guestAgent.guestFileClose(id)
  173. }
  174. }
  175. }