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 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
|
# Copyright (C) 2013, Martin Abente Lahaye - tch@sugarlabs.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 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 <http://www.gnu.org/licenses/>.
import os
import shutil
import tarfile
import logging
from datetime import datetime
from gettext import gettext as _
from gi.repository import Gio
from gi.repository import GLib
from sugar3 import env
from sugar3 import profile
from jarabe.journal import model
from .backend_tools import Backend, PreConditionsError, PreConditionsChoose
from .backend_tools import get_valid_file_name
DIR_SIZE = 4096
DS_SOURCE_NAME = 'datastore'
SN_PATH_X86 = '/ofw/serial-number/serial-number'
SN_PATH_ARM = '/proc/device-tree/serial-number'
class Backup(Backend):
BACKUP_NAME = '%s_%s.xob'
def __init__(self):
Backend.__init__(self)
self._volume = None
self._percent = 0
def _set_volume(self, option):
if self._volume is not None:
return
if option is not None and 'volume' in option:
self._volume = option['volume']
return
volume_options = _get_volume_options()
logging.debug('volume_options %s', volume_options)
if not volume_options['options']:
raise PreConditionsError(_('Please connect a device to continue'))
volume = _value_if_one_option(volume_options)
if volume is not None:
self._volume = volume
return
raise PreConditionsChoose(_('Select your volume'), volume_options)
def verify_preconditions(self, option=None):
"""
option: dictionary
"""
self._set_volume(option)
self._uncompressed_size = _get_datastore_size()
if _get_volume_space(self._volume) < self._uncompressed_size:
raise PreConditionsError(_('Not enough space in volume'))
def _get_datastore_entries(self):
''' gathers datastore top level directories only '''
ds_path = _get_datastore_path()
entries = []
for item in os.listdir(ds_path):
entry = os.path.join(ds_path, item)
if os.path.isdir(entry):
entries.append(entry)
return entries
def _generate_checkpoint(self):
backup_file_name = self.BACKUP_NAME % (
_get_identifier(), datetime.now().strftime('%Y%m%d'))
backup_file_name = get_valid_file_name(backup_file_name)
return os.path.join(self._volume, backup_file_name)
def _do_continue(self):
self._tarfile.add(self._entries.pop())
percent = int((1.0 - float(len(self._entries)) / self._total) * 100)
if percent != self._percent:
self._percent = percent
logging.debug('backup-local progress is %f', percent)
self.emit('progress', float(percent) / 100.0)
if self._cancelled:
self._do_cancel()
elif len(self._entries) == 0:
self._do_finish()
else:
GLib.idle_add(self._do_continue)
def _do_cancel(self):
self._tarfile.close()
logging.debug('Cancel backup operation, remove file %s',
self._checkpoint)
os.remove(self._checkpoint)
self.emit('cancelled')
def _do_finish(self):
self._tarfile.close()
# Add metadata to the file created
metadata = model.get(self._checkpoint)
metadata['description'] = _('Backup from user %s') % \
profile.get_nick_name()
metadata['icon_color'] = profile.get_color().to_string()
metadata['uncompressed_size'] = self._uncompressed_size
metadata['mime_type'] = 'application/vnd.olpc-journal-backup'
model.write(metadata, self._checkpoint)
self.emit('finished')
def start(self):
self.emit('started')
self._checkpoint = self._generate_checkpoint()
self._tarfile = tarfile.open(self._checkpoint, 'w:gz')
self._entries = self._get_datastore_entries()
self._total = len(self._entries)
self._cancelled = False
GLib.idle_add(self._do_continue)
def cancel(self):
self._cancelled = True
class Restore(Backend):
def __init__(self):
Backend.__init__(self)
self._volume = None
self._checkpoint = None
self._checkpoint_size = None
self._percent = 0
self._cancellable = True
def _reset_datastore(self):
''' erase all contents from current datastore '''
datastore_path = _get_datastore_path()
if os.path.exists(datastore_path):
shutil.rmtree(datastore_path)
os.makedirs(datastore_path)
def _set_volume(self, option):
if self._volume is not None:
return
if option is not None and 'volume' in option:
self._volume = option['volume']
return
volume_options = _get_volume_options()
logging.error('volume_options %s', volume_options)
if not volume_options['options']:
raise PreConditionsError(_('Please connect a device to continue'))
volume = _value_if_one_option(volume_options)
if volume is not None:
self._volume = volume
return
raise PreConditionsChoose(_('Select your volume'), volume_options)
def _set_checkpoint(self, option):
if self._checkpoint is not None:
return
if option is not None and 'checkpoint' in option:
self._checkpoint = option['checkpoint']
return
checkpoint_options = _get_checkpoint_options(self._volume)
if not checkpoint_options['options']:
raise PreConditionsError(_('No checkpoints found in the device'))
checkpoint = _value_if_one_option(checkpoint_options)
if checkpoint is not None:
self._checkpoint = checkpoint
return
raise PreConditionsChoose(_('Select your checkpoint'),
checkpoint_options)
def _set_checkpoint_size(self):
self._checkpoint_size = _get_checkpoint_size(self._checkpoint)
def verify_preconditions(self, option=None):
self._set_volume(option)
self._set_checkpoint(option)
self._set_checkpoint_size()
if _get_volume_space(env.get_profile_path()) < self._checkpoint_size:
raise PreConditionsError(_('Not enough space in disk'))
def _do_continue(self):
tarinfo = next(self._tarfile)
if tarinfo is not None:
self._tarfile.extract(tarinfo, path='/')
self._bytes += DIR_SIZE if tarinfo.isdir() else tarinfo.size
percent = int(self._bytes / self._checkpoint_size * 100)
if percent != self._percent:
self._percent = percent
logging.debug('restore-local progress is %f', percent)
self.emit('progress', float(percent) / 100.0)
GLib.idle_add(self._do_continue)
else:
self._do_finish()
def _do_finish(self):
self._tarfile.close()
self._cancellable = True
self.emit('finished')
def start(self):
self._cancellable = False
self.emit('started')
logging.debug('Starting with checkpoint %s', self._checkpoint)
self._tarfile = tarfile.open(self._checkpoint, 'r:gz')
self._bytes = 0.0
self._reset_datastore()
GLib.idle_add(self._do_continue)
def cancel(self):
if not self._cancellable:
# restore can't be interrupted
raise Exception()
def _get_volume_space(path):
stat = os.statvfs(path)
return stat[0] * stat[4]
def _get_datastore_size():
# XXX: is there a better way?
size = 0
for root, dirnames, filenames in os.walk(_get_datastore_path()):
for d in dirnames:
size += os.path.getsize(os.path.join(root, d))
for f in filenames:
size += os.path.getsize(os.path.join(root, f))
return size
def _get_checkpoint_size(path):
# read information in the metadata
metadata = model.get(path)
if 'uncompressed_size' in metadata:
size = int(metadata['uncompressed_size'])
logging.error('size from metadata = %d', size)
else:
size = 0
with tarfile.open(path, 'r:gz') as file:
for tarinfo in file:
size += DIR_SIZE if tarinfo.isdir() else tarinfo.size
return size
def _get_identifier():
path = None
if os.path.exists(SN_PATH_X86):
path = SN_PATH_X86
elif os.path.exists(SN_PATH_ARM):
path = SN_PATH_ARM
if path is not None:
with open(path, 'r') as file:
return file.read().rstrip('\0\n')
return profile.get_nick_name()
def _get_datastore_path():
return os.path.join(env.get_profile_path(), DS_SOURCE_NAME)
def _value_if_one_option(options):
if len(options['options']) == 1:
return options['options'][0]['value']
return None
def _get_checkpoint_options(volume):
options = {}
options['parameter'] = 'checkpoint'
options['options'] = []
for checkpoint in os.listdir(volume):
if not checkpoint.endswith('.xob'):
continue
option = {}
option['description'] = checkpoint
option['value'] = os.path.join(volume, checkpoint)
options['options'].append(option)
return options
def _get_volume_options():
options = {}
options['parameter'] = 'volume'
options['options'] = []
for mount in Gio.VolumeMonitor.get().get_mounts():
option = {}
option['description'] = mount.get_name()
option['value'] = mount.get_root().get_path()
options['options'].append(option)
return options
def get_name():
return _('Local Device Backup')
def get_backup():
return Backup()
def get_restore():
return Restore()
|