[778] | 1 | #!/usr/bin/env python
|
---|
| 2 | # -*- coding: utf-8 -*-
|
---|
| 3 |
|
---|
[783] | 4 | # $URL: baculafs/trunk/baculafs.py $
|
---|
| 5 | # $Id: baculafs.py 784 2009-08-25 22:30:54Z joergs $
|
---|
| 6 |
|
---|
[778] | 7 | import logging
|
---|
| 8 | import fuse
|
---|
| 9 | import stat
|
---|
| 10 | import errno
|
---|
| 11 | import os
|
---|
| 12 | import pexpect
|
---|
| 13 | import sys
|
---|
[784] | 14 | import re
|
---|
[778] | 15 |
|
---|
| 16 | fuse.fuse_python_api = (0, 2)
|
---|
| 17 |
|
---|
| 18 | ###### bconsole
|
---|
| 19 | ################
|
---|
| 20 |
|
---|
[783] | 21 | BACULA_FS_VERSION = "$Rev: 784 $"
|
---|
[782] | 22 |
|
---|
[778] | 23 | LOG_FILENAME = '/tmp/baculafs.log'
|
---|
[780] | 24 | #LOG_BCONSOLE = '/tmp/bconsole.log'
|
---|
[778] | 25 | LOG_BCONSOLE_DUMP = '/tmp/bconsole.out'
|
---|
| 26 |
|
---|
| 27 |
|
---|
| 28 | #BACULA_CMD = 'strace -f -o /tmp/bconsole.strace /usr/sbin/bconsole -n'
|
---|
| 29 | BACULA_CMD = '/usr/sbin/bconsole -n'
|
---|
| 30 | # .clients
|
---|
| 31 | # 5: ting-fd
|
---|
| 32 | BACULA_CLIENT='5'
|
---|
| 33 |
|
---|
| 34 | BCONSOLE_CMD_PROMPT='\*'
|
---|
[784] | 35 | BCONSOLE_SELECT_PROMPT='Select .*:.*'
|
---|
[778] | 36 | BCONSOLE_RESTORE_PROMPT='\$ '
|
---|
| 37 |
|
---|
| 38 |
|
---|
| 39 | class Bconsole:
|
---|
| 40 |
|
---|
| 41 | def __init__(self, client=BACULA_CLIENT):
|
---|
[780] | 42 | #logging.debug('BC init')
|
---|
[784] | 43 |
|
---|
| 44 | self.cwd = "/"
|
---|
| 45 |
|
---|
[778] | 46 | self.bconsole = pexpect.spawn( BACULA_CMD, logfile=file(LOG_BCONSOLE_DUMP, 'w'), timeout=10 )
|
---|
| 47 | self.bconsole.setecho( False )
|
---|
| 48 | self.bconsole.expect( BCONSOLE_CMD_PROMPT )
|
---|
| 49 | self.bconsole.sendline( 'restore' )
|
---|
[784] | 50 | self.bconsole.expect( BCONSOLE_SELECT_PROMPT )
|
---|
[778] | 51 | self.bconsole.sendline( "5" )
|
---|
| 52 | #self.bconsole.expect( BCONSOLE_SELECT_PROMPT )
|
---|
[784] | 53 |
|
---|
| 54 |
|
---|
| 55 |
|
---|
| 56 | #self.bconsole.sendline( BACULA_CLIENT )
|
---|
| 57 | #self.bconsole.expect( BCONSOLE_RESTORE_PROMPT )
|
---|
[780] | 58 | #logging.debug( "BC alive: " + str(self.bconsole.isalive()) )
|
---|
| 59 | #logging.debug('BC init done')
|
---|
[778] | 60 |
|
---|
[784] | 61 | def _normalize_dir( self, path ):
|
---|
| 62 | # guaranty, that directory path ends with (exactly one) "/"
|
---|
| 63 | return path.rstrip('/') + "/"
|
---|
| 64 |
|
---|
| 65 | #def _strip_select_item(self,line):
|
---|
| 66 | #re_remove = re.compile('\s*[0-9]+:\s*')
|
---|
| 67 | #return re_remove.sub( '', line )
|
---|
| 68 |
|
---|
| 69 | def _get_select_items(self,lines):
|
---|
| 70 | re_select_items = re.compile('\s*[0-9]+:\s*.+')
|
---|
| 71 | return filter( re_select_items.match, lines )
|
---|
| 72 |
|
---|
| 73 | def _get_item_dict( self,lines ):
|
---|
| 74 | dictionary = {}
|
---|
| 75 | for line in self._get_select_items(lines):
|
---|
| 76 | number,item = line.split( ":", 1 )
|
---|
| 77 | item = item.strip()
|
---|
| 78 | number = number.strip()
|
---|
| 79 | dictionary[item]=number
|
---|
| 80 | return dictionary
|
---|
| 81 |
|
---|
| 82 |
|
---|
| 83 | def cd(self,path):
|
---|
| 84 | path = self._normalize_dir( path )
|
---|
[780] | 85 | logging.debug( "(" + path + ")" )
|
---|
[778] | 86 |
|
---|
[784] | 87 | if path = "":
|
---|
| 88 | return True
|
---|
| 89 |
|
---|
| 90 | try:
|
---|
| 91 | index = self.bconsole.expect( [ BCONSOLE_SELECT_PROMPT, BCONSOLE_RESTORE_PROMPT ] )
|
---|
| 92 | if index == 0:
|
---|
| 93 | # SELECT_PROMPT
|
---|
| 94 | return self.cd_select( path )
|
---|
| 95 | elif index == 1:
|
---|
| 96 | # RESTORE_PROMPT
|
---|
| 97 | return self.cd_restore( path )
|
---|
| 98 | except pexpect.EOF:
|
---|
| 99 | logging.error( "EOF bconsole" )
|
---|
| 100 | except pexpect.TIMEOUT:
|
---|
| 101 | logging.error( "TIMEOUT bconsole" )
|
---|
| 102 |
|
---|
| 103 |
|
---|
| 104 | def cd_select(self, path):
|
---|
| 105 | logging.debug( "(" + path + ")" )
|
---|
| 106 |
|
---|
| 107 | lines = self.bconsole.before.splitlines()
|
---|
| 108 | items = self._get_item_dict(lines)
|
---|
| 109 | logging.debug( str( items ) )
|
---|
| 110 |
|
---|
| 111 |
|
---|
| 112 | directory,sep,path=path.lstrip( "/" ).partition( "/" )
|
---|
| 113 | logging.debug( "directory: " + directory )
|
---|
| 114 |
|
---|
| 115 | if items[directory]:
|
---|
| 116 | logging.debug( "directory: " + directory + " (" + items[directory] + ")" )
|
---|
| 117 | self.bconsole.sendline( items[directory] )
|
---|
| 118 | self.cd( path )
|
---|
| 119 | return True
|
---|
| 120 |
|
---|
| 121 | return False
|
---|
| 122 |
|
---|
| 123 |
|
---|
| 124 |
|
---|
| 125 | def cd_restore(self, path):
|
---|
| 126 | #path = path + "/"
|
---|
| 127 | logging.debug( "(" + path + ")" )
|
---|
| 128 |
|
---|
| 129 | # TODO:
|
---|
| 130 | # parse for BCONSOLE_SELECT_PROMPT or BCONSOLE_RESTORE_PROMPT
|
---|
| 131 | # BCONSOLE_SELECT_PROMPT: take first part of path and try to match. send number. iterate
|
---|
| 132 | # BCONSOLE_RESTORE_PROMPT: cd to directory (as before)
|
---|
| 133 |
|
---|
[778] | 134 | self.bconsole.sendline( 'cd ' + path )
|
---|
| 135 | #self.bconsole.expect( BCONSOLE_RESTORE_PROMPT, timeout=10 )
|
---|
| 136 | #self.bconsole.sendline( 'pwd' )
|
---|
| 137 |
|
---|
| 138 | index = self.bconsole.expect( ["cwd is: " + path + "[/]?", BCONSOLE_RESTORE_PROMPT, pexpect.EOF, pexpect.TIMEOUT ] )
|
---|
[780] | 139 | logging.debug( "cd result: " + str(index) )
|
---|
[778] | 140 |
|
---|
| 141 | if index == 0:
|
---|
| 142 | # path ok, now wait for prompt
|
---|
| 143 | self.bconsole.expect( BCONSOLE_RESTORE_PROMPT )
|
---|
[781] | 144 | return True
|
---|
[778] | 145 | elif index == 1:
|
---|
| 146 | #print "wrong path"
|
---|
[781] | 147 | return False
|
---|
[778] | 148 | elif index == 2:
|
---|
[781] | 149 | logging.error( "EOF bconsole" )
|
---|
| 150 | #raise?
|
---|
| 151 | return False
|
---|
[778] | 152 | elif index == 3:
|
---|
[781] | 153 | logging.error( "TIMEOUT bconsole" )
|
---|
| 154 | return False
|
---|
| 155 |
|
---|
| 156 | def ls(self, path):
|
---|
| 157 | logging.debug( "(" + path + ")" )
|
---|
| 158 |
|
---|
| 159 | if self.cd( path ):
|
---|
| 160 | self.bconsole.sendline( 'ls' )
|
---|
| 161 | self.bconsole.expect( BCONSOLE_RESTORE_PROMPT )
|
---|
| 162 | lines = self.bconsole.before.splitlines()
|
---|
| 163 | #logging.debug( str(lines) )
|
---|
| 164 | return lines
|
---|
| 165 | else:
|
---|
[778] | 166 | return
|
---|
| 167 |
|
---|
| 168 | ###############
|
---|
| 169 |
|
---|
| 170 | class BaculaFS(fuse.Fuse):
|
---|
| 171 |
|
---|
| 172 | TYPE_NONE = 0
|
---|
| 173 | TYPE_FILE = 1
|
---|
| 174 | TYPE_DIR = 2
|
---|
| 175 |
|
---|
| 176 | files = { '': {'type': TYPE_DIR} }
|
---|
| 177 |
|
---|
| 178 | def __init__(self, *args, **kw):
|
---|
| 179 | logging.debug('init')
|
---|
| 180 | #self.console = Bconsole()
|
---|
| 181 | fuse.Fuse.__init__(self, *args, **kw)
|
---|
| 182 | #logging.debug('init finished')
|
---|
| 183 |
|
---|
[781] | 184 |
|
---|
| 185 | def _getattr(self,path):
|
---|
| 186 | # TODO: may cause problems with filenames that ends with "/"
|
---|
| 187 | path = path.rstrip( '/' )
|
---|
| 188 | logging.debug( '"' + path + '"' )
|
---|
| 189 |
|
---|
| 190 | if (path in self.files):
|
---|
| 191 | #logging.debug( "(" + path + ")=" + str(self.files[path]) )
|
---|
| 192 | return self.files[path]
|
---|
| 193 |
|
---|
| 194 | if Bconsole().cd(path):
|
---|
| 195 | # don't define files, because these have not been checked
|
---|
| 196 | self.files[path] = { 'type': self.TYPE_DIR, 'dirs': [ ".", ".." ] }
|
---|
| 197 |
|
---|
| 198 | return self.files[path]
|
---|
| 199 |
|
---|
| 200 |
|
---|
| 201 |
|
---|
| 202 |
|
---|
[778] | 203 | def _getdir(self, path):
|
---|
| 204 |
|
---|
| 205 | # TODO: may cause problems with filenames that ends with "/"
|
---|
| 206 | path = path.rstrip( '/' )
|
---|
[780] | 207 | logging.debug( '"' + path + '"' )
|
---|
[778] | 208 |
|
---|
| 209 | if (path in self.files):
|
---|
[780] | 210 | #logging.debug( "(" + path + ")=" + str(self.files[path]) )
|
---|
[781] | 211 | if self.files[path]['type'] == self.TYPE_NONE:
|
---|
| 212 | logging.info( '"' + path + '" does not exist (cached)' )
|
---|
[778] | 213 | return self.files[path]
|
---|
[781] | 214 | elif self.files[path]['type'] == self.TYPE_FILE:
|
---|
| 215 | logging.info( '"' + path + '"=file (cached)' )
|
---|
| 216 | return self.files[path]
|
---|
[778] | 217 | elif ((self.files[path]['type'] == self.TYPE_DIR) and ('files' in self.files[path])):
|
---|
[781] | 218 | logging.info( '"' + path + '"=dir (cached)' )
|
---|
[778] | 219 | return self.files[path]
|
---|
| 220 |
|
---|
| 221 | try:
|
---|
| 222 | files = Bconsole().ls(path)
|
---|
[781] | 223 | logging.debug( " files: " + str( files ) )
|
---|
[780] | 224 |
|
---|
| 225 | # setting initial empty directory. Add entires later in this function
|
---|
| 226 | self.files[path] = { 'type': self.TYPE_DIR, 'dirs': [ ".", ".." ], 'files': [] }
|
---|
[778] | 227 | for i in files:
|
---|
| 228 | if i.endswith('/'):
|
---|
| 229 | # we expect a directory
|
---|
| 230 | # TODO: error with filesnames, that ends with '/'
|
---|
| 231 | i = i.rstrip( '/' )
|
---|
| 232 | self.files[path]['dirs'].append(i)
|
---|
| 233 | if not (i in self.files):
|
---|
| 234 | self.files[path + "/" + i] = { 'type': self.TYPE_DIR }
|
---|
| 235 | else:
|
---|
| 236 | self.files[path]['files'].append(i)
|
---|
| 237 | self.files[path + "/" + i] = { 'type': self.TYPE_FILE }
|
---|
| 238 |
|
---|
[780] | 239 | except Exception as e:
|
---|
| 240 | logging.exception(e)
|
---|
[778] | 241 | logging.error( "no access to path " + path )
|
---|
| 242 | self.files[path] = { 'type': TYPE_NONE }
|
---|
[784] | 243 |
|
---|
[780] | 244 | logging.debug( '"' + path + '"=' + str( self.files[path] ) )
|
---|
[778] | 245 | return self.files[path]
|
---|
| 246 |
|
---|
| 247 |
|
---|
| 248 |
|
---|
| 249 | # TODO: only works after readdir for the directory (eg. ls)
|
---|
| 250 | def getattr(self, path):
|
---|
| 251 |
|
---|
[780] | 252 | # TODO: may cause problems with filenames that ends with "/"
|
---|
| 253 | path = path.rstrip( '/' )
|
---|
| 254 | logging.debug( '"' + path + '"' )
|
---|
| 255 |
|
---|
[778] | 256 | st = fuse.Stat()
|
---|
| 257 |
|
---|
[781] | 258 | if not (path in self.files):
|
---|
| 259 | self._getattr(path)
|
---|
[778] | 260 |
|
---|
[781] | 261 | if not (path in self.files):
|
---|
| 262 | return -errno.ENOENT
|
---|
| 263 |
|
---|
| 264 | file = self.files[path]
|
---|
| 265 |
|
---|
[778] | 266 | if file['type'] == self.TYPE_FILE:
|
---|
| 267 | st.st_mode = stat.S_IFREG | 0444
|
---|
| 268 | st.st_nlink = 1
|
---|
| 269 | st.st_size = 0
|
---|
[781] | 270 | return st
|
---|
[778] | 271 | elif file['type'] == self.TYPE_DIR:
|
---|
| 272 | st.st_mode = stat.S_IFDIR | 0755
|
---|
[779] | 273 | if 'dirs' in file:
|
---|
| 274 | st.st_nlink = len(file['dirs'])
|
---|
| 275 | else:
|
---|
| 276 | st.st_nlink = 2
|
---|
[781] | 277 | return st
|
---|
[778] | 278 |
|
---|
[781] | 279 | # TODO: check for existens
|
---|
| 280 | return -errno.ENOENT
|
---|
| 281 |
|
---|
[778] | 282 |
|
---|
| 283 |
|
---|
[781] | 284 |
|
---|
[778] | 285 | def readdir(self, path, offset):
|
---|
[780] | 286 | logging.debug( '"' + path + '", offset=' + str(offset) + ')' )
|
---|
[778] | 287 |
|
---|
| 288 | dir = self._getdir( path )
|
---|
| 289 |
|
---|
| 290 | #logging.debug( " readdir: type: " + str( dir['type'] ) )
|
---|
| 291 | #logging.debug( " readdir: dirs: " + str( dir['dirs'] ) )
|
---|
| 292 | #logging.debug( " readdir: file: " + str( dir['files'] ) )
|
---|
| 293 |
|
---|
| 294 | if dir['type'] != self.TYPE_DIR:
|
---|
| 295 | return -errno.ENOENT
|
---|
| 296 | else:
|
---|
| 297 | return [fuse.Direntry(f) for f in dir['files'] + dir['dirs']]
|
---|
| 298 |
|
---|
| 299 | #def open( self, path, flags ):
|
---|
| 300 | #logging.debug( "open " + path )
|
---|
| 301 | #return -errno.ENOENT
|
---|
| 302 |
|
---|
| 303 | #def read( self, path, length, offset):
|
---|
| 304 | #logging.debug( "read " + path )
|
---|
| 305 | #return -errno.ENOENT
|
---|
| 306 |
|
---|
| 307 | if __name__ == "__main__":
|
---|
[780] | 308 | # initialize logging
|
---|
| 309 | logging.basicConfig(filename=LOG_FILENAME,level=logging.DEBUG,format="%(asctime)s %(process)5d(%(threadName)s) %(levelname)-7s %(funcName)s( %(message)s )")
|
---|
| 310 |
|
---|
[782] | 311 |
|
---|
| 312 | usage = """
|
---|
| 313 | Bacula filesystem: displays files from Bacula backups as a (userspace) filesystem.
|
---|
| 314 | Internaly, it uses Baculas bconsole.
|
---|
| 315 |
|
---|
| 316 | """ + fuse.Fuse.fusage
|
---|
| 317 |
|
---|
| 318 |
|
---|
| 319 | fs = BaculaFS(
|
---|
| 320 | version="%prog: " + BACULA_FS_VERSION,
|
---|
| 321 | usage=usage,
|
---|
| 322 | # required to let "-s" set single-threaded execution
|
---|
| 323 | dash_s_do='setsingle'
|
---|
| 324 | )
|
---|
| 325 |
|
---|
| 326 | #server.parser.add_option(mountopt="root", metavar="PATH", default='/',help="mirror filesystem from under PATH [default: %default]")
|
---|
| 327 | #server.parse(values=server, errex=1)
|
---|
[778] | 328 | fs.parse()
|
---|
[782] | 329 |
|
---|
[778] | 330 | fs.main()
|
---|