Changeset 787 for baculafs/trunk/baculafs.py
- Timestamp:
- Aug 27, 2009, 11:54:48 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
baculafs/trunk/baculafs.py
r786 r787 5 5 # $Id$ 6 6 7 import fuse 7 8 import logging 8 import fuse9 9 import stat 10 10 import errno … … 33 33 BCONSOLE_RESTORE_PROMPT='\$ ' 34 34 35 35 # define the states bconsole can be in 36 36 class BconsoleState: 37 UNKNOWN = '' 38 SELECT_PROMPT = BCONSOLE_SELECT_PROMPT 39 RESTORE_PROMPT = BCONSOLE_RESTORE_PROMPT 40 ERROR = "error" 41 42 37 UNKNOWN = '' 38 CMD_PROMPT = BCONSOLE_CMD_PROMPT 39 SELECT_PROMPT = BCONSOLE_SELECT_PROMPT 40 RESTORE_PROMPT = BCONSOLE_RESTORE_PROMPT 41 ERROR = "error" 42 43 # direct access to the program bconsole 43 44 class Bconsole: 44 45 … … 48 49 self.cwd = "/" 49 50 self.state = BconsoleState.UNKNOWN 51 self.last_items_dict = {} 50 52 51 53 self.bconsole = pexpect.spawn( BACULA_CMD, logfile=file(LOG_BCONSOLE_DUMP, 'w'), timeout=10 ) … … 55 57 self.bconsole.expect( BCONSOLE_SELECT_PROMPT ) 56 58 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') 59 self.wait_for_prompt() 60 63 61 64 62 def _normalize_dir( self, path ): 65 # guarant y,that directory path ends with (exactly one) "/"63 # guarantee that directory path ends with (exactly one) "/" 66 64 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 65 72 66 def _get_select_items(self,lines): … … 78 72 for line in self._get_select_items(lines): 79 73 number,item = line.split( ":", 1 ) 80 item = item.strip()74 item = self._normalize_dir( item.strip() ) 81 75 number = number.strip() 82 76 dictionary[item]=number … … 97 91 # SELECT_PROMPT 98 92 self.state=BconsoleState.SELECT_PROMPT 99 return BCONSOLE_SELECT_PROMPT 93 lines = self.bconsole.before.splitlines() 94 self.last_items_dict = self._get_item_dict(lines) 95 logging.debug( str( self.last_items_dict ) ) 100 96 elif index == 1: 101 97 # RESTORE_PROMPT 102 98 self.state=BconsoleState.RESTORE_PROMPT 103 return BCONSOLE_RESTORE_PROMPT104 99 else: 105 100 logging.error( "unexpected result" ) … … 112 107 113 108 109 114 110 def cd(self,path): 115 111 path = self._normalize_dir( path ) … … 123 119 return True 124 120 125 state = self.wait_for_prompt() 126 if state == BconsoleState.SELECT_PROMPT: 121 if path == "/": 122 return True 123 124 if self.state == BconsoleState.SELECT_PROMPT: 127 125 return self.cd_select( path ) 128 elif s tate == BconsoleState.RESTORE_PROMPT:126 elif self.state == BconsoleState.RESTORE_PROMPT: 129 127 return self.cd_restore( path ) 130 128 # else error … … 135 133 logging.debug( "(" + path + ")" ) 136 134 137 lines = self.bconsole.before.splitlines() 138 items = self._get_item_dict(lines) 139 logging.debug( str( items ) ) 140 141 135 # get top level directory 142 136 directory,sep,path=path.lstrip( "/" ).partition( "/" ) 137 directory=self._normalize_dir(directory) 143 138 logging.debug( "directory: " + directory ) 144 139 145 if items[directory]: 146 logging.debug( "directory: " + directory + " (" + items[directory] + ")" ) 147 self.bconsole.sendline( items[directory] ) 140 if self.last_items_dict[directory]: 141 logging.debug( "directory: " + directory + " (" + self.last_items_dict[directory] + ")" ) 142 self.bconsole.sendline( self.last_items_dict[directory] ) 143 self.wait_for_prompt() 148 144 self.cd( path ) 149 145 return True … … 182 178 183 179 if self.cd( path ): 184 self.bconsole.sendline( 'ls' ) 185 self.bconsole.expect( BCONSOLE_RESTORE_PROMPT ) 186 lines = self.bconsole.before.splitlines() 187 #logging.debug( str(lines) ) 188 return lines 180 if self.state == BconsoleState.SELECT_PROMPT: 181 return self.last_items_dict.keys() 182 elif self.state == BconsoleState.RESTORE_PROMPT: 183 return self.ls_restore( path ) 189 184 else: 190 185 return 186 187 188 def ls_restore( self, path ): 189 self.bconsole.sendline( 'ls' ) 190 self.bconsole.expect( BCONSOLE_RESTORE_PROMPT ) 191 lines = self.bconsole.before.splitlines() 192 #logging.debug( str(lines) ) 193 return lines 194 191 195 192 196 ############### … … 222 226 return self.files[path] 223 227 228 # TODO: only works after readdir for the directory (eg. ls) 229 def getattr(self, path): 230 231 # TODO: may cause problems with filenames that ends with "/" 232 path = path.rstrip( '/' ) 233 logging.debug( '"' + path + '"' ) 234 235 st = fuse.Stat() 236 237 if not (path in self.files): 238 self._getattr(path) 239 240 if not (path in self.files): 241 return -errno.ENOENT 242 243 file = self.files[path] 244 245 if file['type'] == self.TYPE_FILE: 246 st.st_mode = stat.S_IFREG | 0444 247 st.st_nlink = 1 248 st.st_size = 0 249 return st 250 elif file['type'] == self.TYPE_DIR: 251 st.st_mode = stat.S_IFDIR | 0755 252 if 'dirs' in file: 253 st.st_nlink = len(file['dirs']) 254 else: 255 st.st_nlink = 2 256 return st 257 258 # TODO: check for existens 259 return -errno.ENOENT 224 260 225 261 … … 264 300 logging.exception(e) 265 301 logging.error( "no access to path " + path ) 266 self.files[path] = { 'type': TYPE_NONE }302 self.files[path] = { 'type': self.TYPE_NONE } 267 303 268 304 logging.debug( '"' + path + '"=' + str( self.files[path] ) ) 269 305 return self.files[path] 270 271 272 273 # TODO: only works after readdir for the directory (eg. ls)274 def getattr(self, path):275 276 # TODO: may cause problems with filenames that ends with "/"277 path = path.rstrip( '/' )278 logging.debug( '"' + path + '"' )279 280 st = fuse.Stat()281 282 if not (path in self.files):283 self._getattr(path)284 285 if not (path in self.files):286 return -errno.ENOENT287 288 file = self.files[path]289 290 if file['type'] == self.TYPE_FILE:291 st.st_mode = stat.S_IFREG | 0444292 st.st_nlink = 1293 st.st_size = 0294 return st295 elif file['type'] == self.TYPE_DIR:296 st.st_mode = stat.S_IFDIR | 0755297 if 'dirs' in file:298 st.st_nlink = len(file['dirs'])299 else:300 st.st_nlink = 2301 return st302 303 # TODO: check for existens304 return -errno.ENOENT305 306 306 307 307
Note:
See TracChangeset
for help on using the changeset viewer.