summaryrefslogtreecommitdiff
path: root/mydisks.py
blob: 624e3a06beb103d678206d1cacaa844690e20cfb (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
#!/usr/bin/env python

# RaidGuessFS, a FUSE pseudo-filesystem to guess RAID parameters of a damaged device
# Copyright (C) 2015 Ludovic Pouzenc <ludovic@pouzenc.fr>
#
# This file is part of RaidGuessFS.
#
# RaidGuessFS 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.
#
# RaidGuessFS 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 RaidGuessFS. If not, see <http://www.gnu.org/licenses/>

import os, sys, logging

class MyDisks():
    """Auxiliary class, managing disk layer"""

    def __init__(self):
        self.disks = []
        self.sector_size = 512
        self.disk_count = 0
        self.disks_size = [0]

    def get_disk_count(self):
        return self.disk_count

    def open_disks(self, base_path, new_disk_count):
        """(re)open all disks"""
        logging.debug("Enter open_disks()")
        for fh in self.disks:
            try:
                fh.close()
            except:
                pass

        self.disk_count = 0
        self.disks = [ None for d in range(new_disk_count) ]
        self.disks_size = [ 0 for d in range(new_disk_count) ]

        for d in range(new_disk_count):
            path = '%s/disk%02d.img'%(base_path,d)
            logging.debug("Try to open disk #%2d"%d)
            try:
                # TODO : follow symlinks
                fh = open(path, "rb")
                self.disks_size[d] = os.lseek(fh.fileno(), 0, os.SEEK_END)
                self.disks[d] = fh
                logging.debug("Opened disk #%2d"%d)
            except IOError as e:
                logging.error("Can't open disk #%2d ('%s') : %s"%(d, path, e.strerror))
                logging.exception(e)
                self.disks_size[d] = 0
            except Exception as e:
                logging.error("Can't open disk #%2d ('%s') : unhandled exception"%(d, path))
                self.disks_size[d] = 0
                logging.exception(e)
            self.disk_count += 1

        logging.debug("Exit. open_disks()")


    def is_readable(self,disk_no,offset,size):
        return True
        #import random
        #return random.randint(0,100) > 1 # FIXME : implement this (parse ddrescue log files)

    def read(self,disk_no,offset,size):
        # XXX : Consider pread() ?
        self.disks[disk_no].seek(offset)
        return self.disks[disk_no].read(size)