1 | #!/usr/bin/env python
|
---|
2 | # -*- coding: utf-8 -*-
|
---|
3 |
|
---|
4 | import logging
|
---|
5 | import fuse
|
---|
6 | import stat
|
---|
7 | import errno
|
---|
8 | import os
|
---|
9 | import pexpect
|
---|
10 | import sys
|
---|
11 |
|
---|
12 | fuse.fuse_python_api = (0, 2)
|
---|
13 |
|
---|
14 | ###### bconsole
|
---|
15 | ################
|
---|
16 |
|
---|
17 | LOG_FILENAME = '/tmp/baculafs.log'
|
---|
18 | LOG_BCONSOLE = '/tmp/bconsole.log'
|
---|
19 | LOG_BCONSOLE_DUMP = '/tmp/bconsole.out'
|
---|
20 |
|
---|
21 |
|
---|
22 | #BACULA_CMD = 'strace -f -o /tmp/bconsole.strace /usr/sbin/bconsole -n'
|
---|
23 | BACULA_CMD = '/usr/sbin/bconsole -n'
|
---|
24 | # .clients
|
---|
25 | # 5: ting-fd
|
---|
26 | BACULA_CLIENT='5'
|
---|
27 |
|
---|
28 | BCONSOLE_CMD_PROMPT='\*'
|
---|
29 | BCONSOLE_SELECT_PROMPT='Select item:.*'
|
---|
30 | BCONSOLE_RESTORE_PROMPT='\$ '
|
---|
31 |
|
---|
32 |
|
---|
33 | class Bconsole:
|
---|
34 |
|
---|
35 | #bconsole = None
|
---|
36 |
|
---|
37 | def __init__(self, client=BACULA_CLIENT):
|
---|
38 | #logging.basicConfig(filename=LOG_BCONSOLE,level=logging.DEBUG,)
|
---|
39 | logging.debug('BC init')
|
---|
40 | self.bconsole = pexpect.spawn( BACULA_CMD, logfile=file(LOG_BCONSOLE_DUMP, 'w'), timeout=10 )
|
---|
41 | self.bconsole.setecho( False )
|
---|
42 | self.bconsole.expect( BCONSOLE_CMD_PROMPT )
|
---|
43 | self.bconsole.sendline( 'restore' )
|
---|
44 | #self.bconsole.expect( BCONSOLE_SELECT_PROMPT )
|
---|
45 | self.bconsole.sendline( "5" )
|
---|
46 | #self.bconsole.expect( BCONSOLE_SELECT_PROMPT )
|
---|
47 | self.bconsole.sendline( BACULA_CLIENT )
|
---|
48 | self.bconsole.expect( BCONSOLE_RESTORE_PROMPT )
|
---|
49 | logging.debug( "BC alive: " + str(self.bconsole.isalive()) )
|
---|
50 | logging.debug('BC init done')
|
---|
51 |
|
---|
52 |
|
---|
53 | def ls(self, path):
|
---|
54 |
|
---|
55 | path = path + "/"
|
---|
56 | logging.debug( "BC ls(" + path + ")" )
|
---|
57 |
|
---|
58 | self.bconsole.sendline( 'cd ' + path )
|
---|
59 | #self.bconsole.expect( BCONSOLE_RESTORE_PROMPT, timeout=10 )
|
---|
60 | #self.bconsole.sendline( 'pwd' )
|
---|
61 |
|
---|
62 | index = self.bconsole.expect( ["cwd is: " + path + "[/]?", BCONSOLE_RESTORE_PROMPT, pexpect.EOF, pexpect.TIMEOUT ] )
|
---|
63 | logging.debug( "BC ls: cd result: " + str(index) )
|
---|
64 |
|
---|
65 | if index == 0:
|
---|
66 | # path ok, now wait for prompt
|
---|
67 | self.bconsole.expect( BCONSOLE_RESTORE_PROMPT )
|
---|
68 | elif index == 1:
|
---|
69 | #print "wrong path"
|
---|
70 | return
|
---|
71 | elif index == 2:
|
---|
72 | print "error EOF"
|
---|
73 | raise
|
---|
74 | elif index == 3:
|
---|
75 | print "error TIMEOUT"
|
---|
76 | return
|
---|
77 |
|
---|
78 | self.bconsole.sendline( 'ls' )
|
---|
79 | self.bconsole.expect( BCONSOLE_RESTORE_PROMPT )
|
---|
80 | lines = self.bconsole.before.splitlines()
|
---|
81 | logging.debug( "BC ls: " + str(lines) )
|
---|
82 | return lines
|
---|
83 |
|
---|
84 | ###############
|
---|
85 |
|
---|
86 | class BaculaFS(fuse.Fuse):
|
---|
87 |
|
---|
88 | TYPE_NONE = 0
|
---|
89 | TYPE_FILE = 1
|
---|
90 | TYPE_DIR = 2
|
---|
91 |
|
---|
92 | files = { '': {'type': TYPE_DIR} }
|
---|
93 |
|
---|
94 | def __init__(self, *args, **kw):
|
---|
95 | logging.basicConfig(filename=LOG_FILENAME,level=logging.DEBUG,)
|
---|
96 | logging.debug('init')
|
---|
97 | #self.console = Bconsole()
|
---|
98 | fuse.Fuse.__init__(self, *args, **kw)
|
---|
99 | #logging.debug('init finished')
|
---|
100 |
|
---|
101 | def _getdir(self, path):
|
---|
102 |
|
---|
103 | # TODO: may cause problems with filenames that ends with "/"
|
---|
104 | path = path.rstrip( '/' )
|
---|
105 | logging.debug( "_getdir(" + path + ")" )
|
---|
106 |
|
---|
107 | if (path in self.files):
|
---|
108 | logging.debug( " _getdir(" + path + ")=" + str(self.files[path]) )
|
---|
109 | if self.files[path]['type'] == self.TYPE_FILE:
|
---|
110 | logging.debug( " type: file" )
|
---|
111 | return self.files[path]
|
---|
112 | elif ((self.files[path]['type'] == self.TYPE_DIR) and ('files' in self.files[path])):
|
---|
113 | logging.debug( " type: dir (cached)" )
|
---|
114 | return self.files[path]
|
---|
115 |
|
---|
116 | try:
|
---|
117 | files = Bconsole().ls(path)
|
---|
118 | logging.debug( " files: " + str( files ) )
|
---|
119 | self.files[path] = {}
|
---|
120 | self.files[path]['type'] = self.TYPE_DIR
|
---|
121 | self.files[path]['dirs'] = [ ".", ".." ]
|
---|
122 | self.files[path]['files'] = []
|
---|
123 | for i in files:
|
---|
124 | if i.endswith('/'):
|
---|
125 | # we expect a directory
|
---|
126 | # TODO: error with filesnames, that ends with '/'
|
---|
127 | i = i.rstrip( '/' )
|
---|
128 | self.files[path]['dirs'].append(i)
|
---|
129 | if not (i in self.files):
|
---|
130 | self.files[path + "/" + i] = { 'type': self.TYPE_DIR }
|
---|
131 | else:
|
---|
132 | self.files[path]['files'].append(i)
|
---|
133 | self.files[path + "/" + i] = { 'type': self.TYPE_FILE }
|
---|
134 |
|
---|
135 | except:
|
---|
136 | logging.debug( "Unexpected error:" + sys.exc_info()[0] )
|
---|
137 | logging.error( "no access to path " + path )
|
---|
138 | self.files[path] = { 'type': TYPE_NONE }
|
---|
139 | logging.debug( " " + str( self.files[path] ) )
|
---|
140 | return self.files[path]
|
---|
141 |
|
---|
142 |
|
---|
143 |
|
---|
144 | # TODO: only works after readdir for the directory (eg. ls)
|
---|
145 | def getattr(self, path):
|
---|
146 | logging.debug( "getattr " + path )
|
---|
147 |
|
---|
148 | st = fuse.Stat()
|
---|
149 |
|
---|
150 | #file = self._getdir( path )
|
---|
151 |
|
---|
152 | # TODO: may cause problems with filenames that ends with "/"
|
---|
153 | path = path.rstrip( '/' )
|
---|
154 | file = self.files[path]
|
---|
155 |
|
---|
156 | if file['type'] == self.TYPE_FILE:
|
---|
157 | st.st_mode = stat.S_IFREG | 0444
|
---|
158 | st.st_nlink = 1
|
---|
159 | st.st_size = 0
|
---|
160 | elif file['type'] == self.TYPE_DIR:
|
---|
161 | st.st_mode = stat.S_IFDIR | 0755
|
---|
162 | if 'dirs' in file:
|
---|
163 | st.st_nlink = len(file['dirs'])
|
---|
164 | else:
|
---|
165 | st.st_nlink = 2
|
---|
166 | else:
|
---|
167 | return -errno.ENOENT
|
---|
168 | return st
|
---|
169 |
|
---|
170 |
|
---|
171 |
|
---|
172 | def readdir(self, path, offset):
|
---|
173 | logging.debug( "readdir " + path + "(offset: " + str(offset) + ")" )
|
---|
174 |
|
---|
175 | dir = self._getdir( path )
|
---|
176 |
|
---|
177 | #logging.debug( " readdir: type: " + str( dir['type'] ) )
|
---|
178 | #logging.debug( " readdir: dirs: " + str( dir['dirs'] ) )
|
---|
179 | #logging.debug( " readdir: file: " + str( dir['files'] ) )
|
---|
180 |
|
---|
181 | if dir['type'] != self.TYPE_DIR:
|
---|
182 | return -errno.ENOENT
|
---|
183 | else:
|
---|
184 | return [fuse.Direntry(f) for f in dir['files'] + dir['dirs']]
|
---|
185 |
|
---|
186 | #def open( self, path, flags ):
|
---|
187 | #logging.debug( "open " + path )
|
---|
188 | #return -errno.ENOENT
|
---|
189 |
|
---|
190 | #def read( self, path, length, offset):
|
---|
191 | #logging.debug( "read " + path )
|
---|
192 | #return -errno.ENOENT
|
---|
193 |
|
---|
194 | if __name__ == "__main__":
|
---|
195 | fs = BaculaFS()
|
---|
196 | fs.parse()
|
---|
197 | fs.main()
|
---|