-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinuxDiskClonerInterface.py
112 lines (88 loc) · 3.1 KB
/
LinuxDiskClonerInterface.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
###################
# This file implements an interface for Linux FileCloner with Python
# Copyright (C) 2023 DiskCloner
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
###################
"""
This file implements an interface for Linux
FileCloner with python to clone hard disk.
"""
__version__ = "0.0.2"
__author__ = "Maurice Lambert"
__author_email__ = "mauricelambert434@gmail.com"
__maintainer__ = "Maurice Lambert"
__maintainer_email__ = "mauricelambert434@gmail.com"
__description__ = """
This file implements an interface for Linux
FileCloner with python to clone hard disk.
"""
__url__ = "https://github.com/mauricelambert/DiskCloner"
__all__ = ["main"]
__license__ = "GPL-3.0 License"
__copyright__ = """
DiskCloner Copyright (C) 2023 Maurice Lambert
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions.
"""
copyright = __copyright__
license = __license__
print(copyright)
from sys import stderr, exit, argv, executable
from os.path import join, exists, dirname
from subprocess import check_output
from ctypes import c_char, cdll
from os import getcwd
filename = "libFileCloner.so"
filenames = (join(dirname(__file__), filename), join(getcwd(), filename))
bufferSize = 131072
for filename in filenames:
if exists(filename):
break
else:
print(f"Library {filename!r} is missing", file=stderr)
exit(1)
libfilecloner = cdll.LoadLibrary(filename)
def main() -> int:
"""
This function starts the script from the command line.
"""
disks = filter(
lambda x: x.startswith("s"),
{
x.split()[0].replace("└─", "")
for x in check_output("lsblk", text=True).splitlines()
},
)
if len(argv) != 3:
print(
f"USAGES: {executable} {argv[0]} diskName"
" destinationFilePath\nDisks names:\n -",
"\n - ".join(disks),
file=stderr,
)
return 2
_, sourcefilepath, destinationfilepath = argv
if sourcefilepath in disks:
sourcefilepath = join("/dev", sourcefilepath)
buffer = (c_char * bufferSize)(b"\0")
libfilecloner.clone_from_filename(
sourcefilepath.encode(),
destinationfilepath.encode(),
buffer,
bufferSize,
)
return 0
if __name__ == "__main__":
exit(main())