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