aboutsummaryrefslogtreecommitdiffstats
path: root/backend/raw_write.py
blob: 40dc99bce7ee831739126331f23db09d535e32b7 (plain)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#coding:utf-8

#!/usr/bin/python3
#
#  Copyright (c) 2007-2009 Canonical Ltd.
#
#  Author: Oliver Grawert <ogra@ubuntu.com>
#
#  Modifications 2013 from papoteur <papoteur@mageialinux-online.org>
#  and Geiger David <david.david@mageialinux-online.org>
#
#  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 2 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, write to the Free Software Foundation,
#  Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.

###########
# imports #
###########
#import locale
import os
import io
import gettext
from subprocess import call
import hashlib
import gnupg
       
class Dumper(object):

    def _do_write(self,source,target, b):
        # Writing step
        #Dump mode
        self.returncode=0
        self.operation=True
        bs=4096*128
        try:
            ifc=io.open(source, "rb",1)
        except:
            message = _('Reading error.')+ source
            self.return_state = False
            self.return_message = message
            self.finished.set()
            return
        else:
            try:
                ofc= io.open(target, 'wb',0)
            except:
                 message = _("You don't have permission to write to the device {}").format(target)
                 logging.error(message)
                 self.return_state = False
                 self.return_message = message
                 self.finished.set()
                 return
            else:
                self.operation=True
                steps=list(range(0, b+1, int(b/100)))
                steps.append(b)
                indice=1
                written=0
                ncuts=int(b/bs)
                while ncuts <= 100:
                    bs=int(bs/2)
                    ncuts=int(b/bs)
                for i in range(0,int(ncuts)+1):
                    try:
                        buf=ifc.read(bs)
                    except:
                        message = _("Reading error.")
                        logging.error(message)
                        self.return_state = False
                        self.return_message = message
                        self.finished.set()
                        return
                    try:
                        ofc.write(buf)
                    except:
                        message = _("Writing error.")
                        self.return_state = False
                        self.return_message = message
                        self.finished.set()
                        return
                    written+=len(buf)
                    if written > steps[indice]:
                        if indice%1==0:
                            self._progress = indice
                        indice +=1
                        try:
                            os.fsync(ofc)
                        except:
                           message = _("Writing error.")
                           self.return_state = False
                           self.return_message = message
                           self.finished.set()
                           return
                try:
                    ofc.close()
                except:
                   message = _("Writing error.")
                   logging.error(message)
                   self.return_message = message
                   self.return_state = False
                   return
            ifc.close()
            self._progress = 100
            self.finished.set()
            self.return_state = True
            self.return_message = _("Success")
            return

    def _do_unmount(self, target):
        target = target[0]
        message = _("No partition is mounted.")
        retcode = 0
        try:
            lines = [line.strip("\n").split(" ") for line in open ("/etc/mtab", "r").readlines()]
            mounts = [mount for mount in lines if mount[0].startswith(target)]
        except:
             message = _('Could not read mtab ! {} {}'.format(os.getuid(),target))
             return False, message
        if mounts:
            message =_("Unmounting all partitions of  {}:\n").format(target)
            print(message)
        for mount in mounts:
            message +=_("Trying to unmount {}...\n").format(mount[0])
            try:
                retcode = call('umount '+mount[0], shell=True)
                if retcode == 32:
                    message += _('Partition {} is busy').format(mount[0])
                elif retcode< 0:
                    message += _('Error, umount {} was terminated by signal {}').format(mount[0],retcode)
                elif retcode == 0:
                        message += _('{} successfully unmounted').format(mount[0])
                else:
                        message += _('Error, umount returned {}').format(str(retcode))
            except OSError as e:
                message += _('Execution failed: {}').format(str(e))
        print(message)
        return not bool(retcode), message

    def _check_write(self, target, source):
        self.return_state = False
        self.return_message = ""
        b = os.path.getsize(source)
        #       Check if the sum file has a valid signature
        signature_checked = False
        gpg = gnupg.GPG()
        gpg.encoding = 'utf-8'
        # Use Mageia public key
        sig_file = source +'.sha512.gpg'
        sumcheck = ""
        try:
            gpg.recv_keys('pgp.mit.edu', 'EDCA7A90')
            with open(sig_file, 'rb') as g:
                verified = gpg.verify_file(g)
                if verified.valid: 
                    signature_checked = True
                    # Look for sum files in the same directory as source
                    fs=open(source+'.sha512','r')
                    # Read the sum in the file
                    sumcheck=(fs.readline()).split()[0]
                else:
                    self.return_message = _('Invalid signature for %s.sha512')%source
                    self.return_state = True
                    self.finished.set()
                    return 
        except:
            self.return_message = _("Signature file {} not found\n").format(sig_file)
        
        # Compute the sum from the written device
        steps=list(range(0, b+1, int(b/100)))
        steps.append(b)
        indice=0
        checked=0
        sha512func=hashlib.sha512()
        ncuts=int(b/1024)
        #try:
        with open(target, 'rb') as f:
            for x in range(0,ncuts):
                block = f.read(1024)
                sha512func.update(block)
                if checked > steps[indice]:
                    self._progress = indice
                    indice +=1
                checked+=1024                         
            block = f.read(b-ncuts*1024)
            sha512func.update(block)
            sha512sumcalc=sha512func.hexdigest()
        f.close()
        if (sumcheck == "") :
                self.return_message += _('SHA512 sum: {}').format(sha512sumcalc)
        #  compare the sums
        elif (sha512sumcalc == sumcheck) :
            if signature_checked:
                self.return_message += _("The sha512 sum check is OK and the sum is signed")
            else :
                self.return_message += _("The sha512 sum check is OK but the signature can't be found")
        else: 
            self.return_message += _("/!\\The computed and stored sums don't match")
        #except:
        	#pass
        self._progress = 100
        
        self.return_state = True
        self.finished.set()

    def __init__(self):
        gettext.install('isodumper', localedir='/usr/share/locale')
        # Operation running
        self.operation=False