| 1 | def __del__(self) -> None: |
| 2 | if hasattr(self, "cb"): |
| 3 | libvirtmod.virStreamEventRemoveCallback(self._o) |
| 4 | |
| 5 | if self._o is not None: |
| 6 | libvirtmod.virStreamFree(self._o) |
| 7 | self._o = None |
| 8 | |
| 9 | def _dispatchStreamEventCallback(self, events: int, cbData: dict[str, Any]) -> int: |
| 10 | """ |
| 11 | Dispatches events to python user's stream event callbacks |
| 12 | """ |
| 13 | cb = cbData["cb"] |
| 14 | opaque = cbData["opaque"] |
| 15 | |
| 16 | cb(self, events, opaque) |
| 17 | return 0 |
| 18 | |
| 19 | def eventAddCallback(self, events: int, cb: Callable[['virStream', int, _T], None], opaque: _T) -> None: |
| 20 | self.cb = cb |
| 21 | cbData = {"stream": self, "cb": cb, "opaque": opaque} |
| 22 | ret = libvirtmod.virStreamEventAddCallback(self._o, events, cbData) |
| 23 | if ret == -1: |
| 24 | raise libvirtError('virStreamEventAddCallback() failed') |
| 25 | |
| 26 | def recvAll(self, handler: Callable[['virStream', bytes, _T], int], opaque: _T) -> None: |
| 27 | """Receive the entire data stream, sending the data to the |
| 28 | requested data sink. This is simply a convenient alternative |
| 29 | to virStreamRecv, for apps that do blocking-I/O. |
| 30 | |
| 31 | A hypothetical handler function looks like: |
| 32 | |
| 33 | def handler(stream, # virStream instance |
| 34 | buf, # string containing received data |
| 35 | opaque): # extra data passed to recvAll as opaque |
| 36 | fd = opaque |
| 37 | return os.write(fd, buf) |
| 38 | """ |
| 39 | while True: |
| 40 | got = self.recv(virStorageVol.streamBufSize) |
| 41 | if got == -2: |
| 42 | raise libvirtError("cannot use recvAll with " |
| 43 | "nonblocking stream") |
| 44 | if len(got) == 0: |
| 45 | break |
| 46 | |
| 47 | try: |
| 48 | ret = handler(self, got, opaque) |
| 49 | if isinstance(ret, int) and ret < 0: |
| 50 | raise RuntimeError("recvAll handler returned %d" % ret) |
| 51 | except BaseException: |
| 52 | try: |
| 53 | self.abort() |
| 54 | except Exception: |
| 55 | pass |
| 56 | raise |
| 57 | |
| 58 | def sendAll(self, handler: Callable[['virStream', int, _T], bytes], opaque: _T) -> None: |
| 59 | """ |
| 60 | Send the entire data stream, reading the data from the |
| 61 | requested data source. This is simply a convenient alternative |
| 62 | to virStreamSend, for apps that do blocking-I/O. |
| 63 | |
| 64 | A hypothetical handler function looks like: |
| 65 | |
| 66 | def handler(stream, # virStream instance |
| 67 | nbytes, # int amt of data to read |
| 68 | opaque): # extra data passed to recvAll as opaque |
| 69 | fd = opaque |
| 70 | return os.read(fd, nbytes) |
| 71 | """ |
| 72 | while True: |
| 73 | try: |
| 74 | got = handler(self, virStorageVol.streamBufSize, opaque) |
| 75 | except BaseException: |
| 76 | try: |
| 77 | self.abort() |
| 78 | except Exception: |
| 79 | pass |
| 80 | raise |
| 81 | |
| 82 | if not got: |
| 83 | break |
| 84 | |
| 85 | ret = self.send(got) |
| 86 | if ret == -2: |
| 87 | raise libvirtError("cannot use sendAll with " |
| 88 | "nonblocking stream") |
| 89 | |
| 90 | def recv(self, nbytes: int) -> bytes: |
| 91 | """Reads a series of bytes from the stream. This method may |
| 92 | block the calling application for an arbitrary amount |
| 93 | of time. |
| 94 | |
| 95 | Errors are not guaranteed to be reported synchronously |
| 96 | with the call, but may instead be delayed until a |
| 97 | subsequent call. |
| 98 | |
| 99 | On success, the received data is returned. On failure, an |
| 100 | exception is raised. If the stream is a NONBLOCK stream and |
| 101 | the request would block, integer -2 is returned. |
| 102 | """ |
| 103 | ret = libvirtmod.virStreamRecv(self._o, nbytes) |
| 104 | if ret is None: |
| 105 | raise libvirtError('virStreamRecv() failed') |
| 106 | return ret |
| 107 | |
| 108 | def send(self, data: bytes) -> int: |
| 109 | """Write a series of bytes to the stream. This method may |
| 110 | block the calling application for an arbitrary amount |
| 111 | of time. Once an application has finished sending data |
| 112 | it should call virStreamFinish to wait for successful |
| 113 | confirmation from the driver, or detect any error |
| 114 | |
| 115 | This method may not be used if a stream source has been |
| 116 | registered |
| 117 | |
| 118 | Errors are not guaranteed to be reported synchronously |
| 119 | with the call, but may instead be delayed until a |
| 120 | subsequent call. |
| 121 | """ |
| 122 | ret = libvirtmod.virStreamSend(self._o, data) |
| 123 | if ret == -1: |
| 124 | raise libvirtError('virStreamSend() failed') |
| 125 | return ret |
| 126 | |
| 127 | def recvHole(self, flags: Optional[int] = 0) -> int: |
| 128 | """This method is used to determine the length in bytes |
| 129 | of the empty space to be created in a stream's target |
| 130 | file when uploading or downloading sparsely populated |
| 131 | files. This is the counterpart to sendHole. |
| 132 | """ |
| 133 | ret = libvirtmod.virStreamRecvHole(self._o, flags) |
| 134 | if ret is None: |
| 135 | raise libvirtError('virStreamRecvHole() failed') |
| 136 | return ret |
| 137 | |
| 138 | def sendHole(self, length: int, flags: Optional[int] = 0) -> int: |
| 139 | """Rather than transmitting empty file space, this method |
| 140 | directs the stream target to create length bytes of empty |
| 141 | space. This method would be used when uploading or |
| 142 | downloading sparsely populated files to avoid the |
| 143 | needless copy of empty file space. |
| 144 | """ |
| 145 | ret = libvirtmod.virStreamSendHole(self._o, length, flags) |
| 146 | if ret == -1: |
| 147 | raise libvirtError('virStreamSendHole() failed') |
| 148 | return ret |
| 149 | |
| 150 | def recvFlags(self, nbytes: int, flags: Optional[int] = 0) -> Union[bytes, int]: |
| 151 | """Reads a series of bytes from the stream. This method may |
| 152 | block the calling application for an arbitrary amount |
| 153 | of time. This is just like recv except it has flags |
| 154 | argument. |
| 155 | |
| 156 | Errors are not guaranteed to be reported synchronously |
| 157 | with the call, but may instead be delayed until a |
| 158 | subsequent call. |
| 159 | |
| 160 | On success, the received data is returned. On failure, an |
| 161 | exception is raised. If the stream is a NONBLOCK stream and |
| 162 | the request would block, integer -2 is returned. |
| 163 | """ |
| 164 | ret = libvirtmod.virStreamRecvFlags(self._o, nbytes, flags) |
| 165 | if ret is None: |
| 166 | raise libvirtError('virStreamRecvFlags() failed') |
| 167 | return ret |
| 168 | |
| 169 | def sparseRecvAll(self, handler: Callable[['virStream', bytes, _T], Union[bytes, int]], holeHandler: Callable[['virStream', int, _T], Optional[int]], opaque: _T) -> None: |
| 170 | """Receive the entire data stream, sending the data to |
| 171 | the requested data sink handler and calling the skip |
| 172 | holeHandler to generate holes for sparse stream targets. |
| 173 | This is simply a convenient alternative to recvFlags, for |
| 174 | apps that do blocking-I/O and want to preserve sparseness. |
| 175 | |
| 176 | Hypothetical callbacks can look like this: |
| 177 | |
| 178 | def handler(stream, # virStream instance |
| 179 | buf, # string containing received data |
| 180 | opaque): # extra data passed to sparseRecvAll as opaque |
| 181 | fd = opaque |
| 182 | return os.write(fd, buf) |
| 183 | |
| 184 | def holeHandler(stream, # virStream instance |
| 185 | length, # number of bytes to skip |
| 186 | opaque): # extra data passed to sparseRecvAll as opaque |
| 187 | fd = opaque |
| 188 | cur = os.lseek(fd, length, os.SEEK_CUR) |
| 189 | return os.ftruncate(fd, cur) # take this extra step to |
| 190 | # actually allocate the hole |
| 191 | """ |
| 192 | while True: |
| 193 | want = virStorageVol.streamBufSize |
| 194 | got = self.recvFlags(want, VIR_STREAM_RECV_STOP_AT_HOLE) |
| 195 | if got == -2: |
| 196 | raise libvirtError("cannot use sparseRecvAll with " |
| 197 | "nonblocking stream") |
| 198 | elif got == -3: |
| 199 | length = self.recvHole() |
| 200 | if length is None: |
| 201 | self.abort() |
| 202 | raise RuntimeError("recvHole handler failed") |
| 203 | ret_hole = holeHandler(self, length, opaque) |
| 204 | if isinstance(ret_hole, int) and ret_hole < 0: |
| 205 | self.abort() |
| 206 | raise RuntimeError("holeHandler handler returned %d" % ret_hole) |
| 207 | continue |
| 208 | elif isinstance(got, int): |
| 209 | raise ValueError(got) |
| 210 | elif not isinstance(got, bytes): |
| 211 | raise TypeError(type(got)) |
| 212 | |
| 213 | if len(got) == 0: |
| 214 | break |
| 215 | |
| 216 | ret_data = handler(self, got, opaque) |
| 217 | if isinstance(ret_data, int) and ret_data < 0: |
| 218 | self.abort() |
| 219 | raise RuntimeError("sparseRecvAll handler returned %d" % ret_data) |
| 220 | |
| 221 | def sparseSendAll(self, handler: Callable[['virStream', int, _T], Union[bytes, int]], holeHandler: Callable[['virStream', _T], tuple[bool, int]], skipHandler: Callable[['virStream', int, _T], int], opaque: _T) -> None: |
| 222 | """Send the entire data stream, reading the data from the |
| 223 | requested data source. This is simply a convenient |
| 224 | alternative to virStreamSend, for apps that do |
| 225 | blocking-I/O and want to preserve sparseness. |
| 226 | |
| 227 | Hypothetical callbacks can look like this: |
| 228 | |
| 229 | def handler(stream, # virStream instance |
| 230 | nbytes, # int amt of data to read |
| 231 | opaque): # extra data passed to sparseSendAll as opaque |
| 232 | fd = opaque |
| 233 | return os.read(fd, nbytes) |
| 234 | |
| 235 | def holeHandler(stream, # virStream instance |
| 236 | opaque): # extra data passed to sparseSendAll as opaque |
| 237 | fd = opaque |
| 238 | cur = os.lseek(fd, 0, os.SEEK_CUR) |
| 239 | # ... find out current section and its boundaries |
| 240 | # and set inData = True/False and sectionLen correspondingly |
| 241 | os.lseek(fd, cur, os.SEEK_SET) |
| 242 | return [inData, sectionLen] |
| 243 | |
| 244 | def skipHandler(stream, # virStream instance |
| 245 | length, # number of bytes to skip |
| 246 | opaque): # extra data passed to sparseSendAll as opaque |
| 247 | fd = opaque |
| 248 | return os.lseek(fd, length, os.SEEK_CUR) |
| 249 | |
| 250 | """ |
| 251 | while True: |
| 252 | [inData, sectionLen] = holeHandler(self, opaque) |
| 253 | if not inData and sectionLen > 0: |
| 254 | if (self.sendHole(sectionLen) < 0 or |
| 255 | skipHandler(self, sectionLen, opaque) < 0): |
| 256 | self.abort() |
| 257 | continue |
| 258 | |
| 259 | want = virStorageVol.streamBufSize |
| 260 | if (want > sectionLen): |
| 261 | want = sectionLen |
| 262 | |
| 263 | got = handler(self, want, opaque) |
| 264 | if isinstance(got, int) and got < 0: |
| 265 | self.abort() |
| 266 | raise RuntimeError("sparseSendAll handler returned %d" % got) |
| 267 | |
| 268 | if not got: |
| 269 | break |
| 270 | |
| 271 | assert isinstance(got, bytes) |
| 272 | ret = self.send(got) |
| 273 | if ret == -2: |
| 274 | raise libvirtError("cannot use sparseSendAll with " |
| 275 | "nonblocking stream") |