Skip to content

Commit 0acd2e0

Browse files
committed
Implemented "size" function on file.py module
1 parent 4608131 commit 0acd2e0

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

bridge/files.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,19 @@ def isDir(self, filename):
9696
from os import path
9797
return path.isdir(filename)
9898

99+
def size(self, id):
100+
from os import SEEK_END, SEEK_SET
101+
if id not in self.files:
102+
return [None, None]
103+
file = self.files[id]
104+
try:
105+
old_position = file.tell()
106+
file.seek(0, SEEK_END)
107+
size = file.tell()
108+
file.seek(old_position, SEEK_SET)
109+
return [0, size]
110+
except IOError, e:
111+
return [e.errno, None]
99112

100113
files = Files()
101114

@@ -156,6 +169,21 @@ def run(self, data):
156169
res += chr(pos & 0xFF)
157170
return res
158171

172+
class SIZE_Command:
173+
def run(self, data):
174+
id = ord(data[0])
175+
[err, size] = files.size(id)
176+
if size is None:
177+
size = 0
178+
if err is None:
179+
err = 255
180+
res = chr(err)
181+
res += chr((size>>24) & 0xFF)
182+
res += chr((size>>16) & 0xFF)
183+
res += chr((size>>8) & 0xFF)
184+
res += chr(size & 0xFF)
185+
return res
186+
159187

160188
class ISDIRECTORY_Command:
161189
def run(self, data):
@@ -175,4 +203,5 @@ def init(command_processor):
175203
command_processor.register('i', ISDIRECTORY_Command())
176204
command_processor.register('s', SEEK_Command())
177205
command_processor.register('S', TELL_Command())
206+
command_processor.register('t', SIZE_Command())
178207

0 commit comments

Comments
 (0)