catch file read errors

This commit is contained in:
Dimitur Kirov 2005-09-10 11:56:25 +00:00
parent b416cf60d7
commit 676b7600cf

View file

@ -1,3 +1,4 @@
## common/xmpp/socks5.py ## common/xmpp/socks5.py
## ##
## Gajim Team: ## Gajim Team:
@ -264,7 +265,7 @@ running instance of Gajim. \nFile Transfer will be canceled.\n==================
break break
result = sender.write_next() result = sender.write_next()
self.process_result(result, sender) self.process_result(result, sender)
if result <= 0: if result is None or result <= 0:
break break
elif sender.state == 8: elif sender.state == 8:
self.remove_sender(idx) self.remove_sender(idx)
@ -357,10 +358,15 @@ class Socks5:
self.pauses = 0 self.pauses = 0
self.size = 0 self.size = 0
self.remaining_buff = '' self.remaining_buff = ''
self.fd = None
def open_file_for_reading(self): def open_file_for_reading(self):
if self.fd == None:
try:
self.fd = open(self.file_props['file-name'],'rb') self.fd = open(self.file_props['file-name'],'rb')
self.fd.seek(self.size) except IOError, e:
self.close_file()
raise IOError, e
def close_file(self): def close_file(self):
try: try:
@ -420,9 +426,14 @@ class Socks5:
buff = self.remaining_buff buff = self.remaining_buff
self.remaining_buff = '' self.remaining_buff = ''
else: else:
try:
self.open_file_for_reading() self.open_file_for_reading()
except IOError, e:
self.state = 8 # end connection
self.disconnect()
self.file_props['error'] = -7 # unable to read from file
return -1
buff = self.fd.read(MAX_BUFF_LEN) buff = self.fd.read(MAX_BUFF_LEN)
self.close_file()
if len(buff) > 0: if len(buff) > 0:
lenn = 0 lenn = 0
try: try:
@ -444,6 +455,7 @@ class Socks5:
if self.size == int(self.file_props['size']): if self.size == int(self.file_props['size']):
self.state = 8 # end connection self.state = 8 # end connection
self.file_props['error'] = 0 self.file_props['error'] = 0
self.close_file()
self.disconnect() self.disconnect()
return -1 return -1
if lenn != len(buff): if lenn != len(buff):
@ -463,9 +475,8 @@ class Socks5:
return None return None
return lenn return lenn
else: else:
return 0
self.state = 8 # end connection self.state = 8 # end connection
self.close_file()
self.disconnect() self.disconnect()
return -1 return -1
@ -661,7 +672,6 @@ class Socks5Sender(Socks5):
def send_file(self): def send_file(self):
''' start sending the file over verified connection ''' ''' start sending the file over verified connection '''
self.open_file_for_reading()
self.file_props['error'] = 0 self.file_props['error'] = 0
self.file_props['disconnect_cb'] = self.disconnect self.file_props['disconnect_cb'] = self.disconnect
self.file_props['started'] = True self.file_props['started'] = True