Debian Codegraph @

examples/sparsestream.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 """
3 Either uploads local FILE to libvirt VOLUME, or downloads libvirt
4 VOLUME into local FILE while preserving FILE/VOLUME sparseness
5 """
6 # Example of sparse streams usage
7 #
8 # Authors:
9 # Michal Privoznik <[email protected]>
10
11 import libvirt
12 import os
13 from argparse import ArgumentParser
14
15
16 def bytesWriteHandler(stream: libvirt.virStream, buf: bytes, opaque: int) -> int:
17 fd = opaque
18 return os.write(fd, buf)
19
20
21 def bytesReadHandler(stream: libvirt.virStream, nbytes: int, opaque: int) -> bytes:
22 fd = opaque
23 return os.read(fd, nbytes)
24
25
26 def recvSkipHandler(stream: libvirt.virStream, length: int, opaque: int) -> None:
27 fd = opaque
28 cur = os.lseek(fd, length, os.SEEK_CUR)
29 return os.ftruncate(fd, cur)
30
31
32 def sendSkipHandler(stream: libvirt.virStream, length: int, opaque: int) -> int:
33 fd = opaque
34 return os.lseek(fd, length, os.SEEK_CUR)
35
36
37 def holeHandler(stream: libvirt.virStream, opaque: int):
38 fd = opaque
39 cur = os.lseek(fd, 0, os.SEEK_CUR)
40
41 try:
42 data = os.lseek(fd, cur, os.SEEK_DATA)
43 except OSError as e:
44 if e.errno != 6:
45 raise e
46 else:
47 data = -1
48 # There are three options:
49 # 1) data == cur; @cur is in data
50 # 2) data > cur; @cur is in a hole, next data at @data
51 # 3) data < 0; either @cur is in trailing hole, or @cur is beyond EOF.
52 if data < 0:
53 # case 3
54 inData = False
55 eof = os.lseek(fd, 0, os.SEEK_END)
56 if (eof < cur):
57 raise RuntimeError("Current position in file after EOF: %d" % cur)
58 sectionLen = eof - cur
59 else:
60 if (data > cur):
61 # case 2
62 inData = False
63 sectionLen = data - cur
64 else:
65 # case 1
66 inData = True
67
68 # We don't know where does the next hole start. Let's find out.
69 # Here we get the same options as above
70 hole = os.lseek(fd, data, os.SEEK_HOLE)
71 if hole < 0:
72 # case 3. But wait a second. There is always a trailing hole.
73 # Do the best what we can here
74 raise RuntimeError("No trailing hole")
75
76 if (hole == data):
77 # case 1. Again, this is suspicious. The reason we are here is
78 # because we are in data. But at the same time we are in a
79 # hole. WAT?
80 raise RuntimeError("Impossible happened")
81 else:
82 # case 2
83 sectionLen = hole - data
84 os.lseek(fd, cur, os.SEEK_SET)
85 return [inData, sectionLen]
86
87
88 def download(vol: libvirt.virStorageVol, st: libvirt.virStream, filename: str) -> None:
89 offset = 0
90 length = 0
91
92 fd = os.open(filename, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, mode=0o0660)
93 vol.download(st, offset, length, libvirt.VIR_STORAGE_VOL_DOWNLOAD_SPARSE_STREAM)
94 st.sparseRecvAll(bytesWriteHandler, recvSkipHandler, fd)
95
96 os.close(fd)
97
98
99 def upload(vol: libvirt.virStorageVol, st: libvirt.virStream, filename: str) -> None:
100 offset = 0
101 length = 0
102
103 fd = os.open(filename, os.O_RDONLY)
104 vol.upload(st, offset, length, libvirt.VIR_STORAGE_VOL_UPLOAD_SPARSE_STREAM)
105 st.sparseSendAll(bytesReadHandler, holeHandler, sendSkipHandler, fd)
106
107 os.close(fd)
108
109
110 # main
111 parser = ArgumentParser(description=__doc__)
112 parser.add_argument("uri")
113 group = parser.add_mutually_exclusive_group(required=True)
114 group.add_argument("--upload", action="store_const", const=upload, dest="operation")
115 group.add_argument("--download", action="store_const", const=download, dest="operation")
116 parser.add_argument("volume")
117 parser.add_argument("file")
118 args = parser.parse_args()
119
120
121 conn = libvirt.open(args.uri)
122 vol = conn.storageVolLookupByKey(args.volume)
123
124 st = conn.newStream()
125
126 args.operation(vol, st, args.file)
127
128 st.finish()
129 conn.close()