aboutsummaryrefslogtreecommitdiffstats
path: root/lib/raw_write.py
blob: 92261f8ab98d143dadd0d6c4e6e3d898fdc8a5b1 (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
#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 gettext import gettext as _
import pydbus
       
class Dumper(object):

    def do_write(self,source,dev):
        target = dev.split('(')[1].split(')')[0]
        self.do_umount(target)
        # Writing step
        #Dump mode
        self.returncode=0
        b=os.path.getsize(source)
        self.operation=True
        bs=4096*128
        try:
            ifc=io.open(source, "rb",1)
        except:
             message = _('Reading error.')+ source
             self.logger(message)
             return False,message
        else:
            try:
                ofc= io.open(target, 'wb',0)
            except:
                 message = _("You don't have permission to write to the device {}").format(target)
                 self.logger(message)
                 return False,message
            else:
                self.progress.setLabel(_('Writing {source} to {target}').format(source=source.split('/')[-1],target=target.split('/')[-1])
                self.logger(_('Executing copy from ')+source+_(' to ')+target)
                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.")
                        self.logger(message)
                        return False, message
                    try:
                        ofc.write(buf)
                    except:
                        message = _("Writing error.")
                        self.logger(message)
                        return False, message
                    written+=len(buf)
                    if written > steps[indice]:
                        if indice%1==0:
                            self.logger(_('Wrote: ')+str(indice)+'% '+str(written)+' bytes')
                        self.sendProgress(indice)
                        self.dialog.pollEvent()
                        indice +=1
                        try:
                            os.fsync(ofc)
                        except:
                           message = _("Writing error.")
                           self.logger(message)
                           return False,message
                self.progress.setValue(100)
                self.logger(_('Image ')+source.split('/')[-1]+_(' successfully written to ')+target)
                self.logger(_('Bytes written: ')+str(written))
                try:
                    ofc.close()
                except:
                   message = _("Writing error.")
                   self.logger(message)
            ifc.close()
            return True, _("Succes")

    def do_umount(self, target):
        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 !')
             self.logger(message)
             return False, message
        if mounts:
            self.logger(_('Unmounting all partitions of ')+target+':')
        for mount in mounts:
            self.logger(_('Trying to unmount ')+mount[0]+'...')
            try:
                retcode = call('umount '+mount[0], shell=True)
                if retcode == 32:
                    message = _('Partition %s is busy')%mount[0]
               elif retcode< 0:
                    message = _('Error, umount ')+mount[0]+_(' was terminated by signal ')+str(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))
            return retcode, message

    def __init__(self):
        gettext.bindtextdomain(APP, DIR)
        gettext.textdomain(APP)

        # Operation running
        self.operation=False