-
Notifications
You must be signed in to change notification settings - Fork 1
/
tr_fileabstraction.r2py
150 lines (120 loc) · 3.92 KB
/
tr_fileabstraction.r2py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
"""
<Program Name>
tr_fileabstraction.r2py
<Started>
March, 2011
<Author>
Lukas Puehringer, University of Vienna
lukas.puehringer@univie.ac.at
<Purpose>
The file abstraction is a collection of wrapping functions
that extend the Repy API file operations, adding a user property
and a namespace translation.
"""
dy_import_module_symbols("base64.r2py")
dy_import_module_symbols("upper_dot_lower.py")
def _encode_path(path):
# Base64 encode a path with a user prefix (urlsafe, -_)
path_enc = base64_urlsafe_b64encode(path)
# Replace trailing output padding equal sign(s) with dots
# for RepyV2 valid filenames (part 1)
path_enc = path_enc.replace("=", ".")
# Replace upper case with dot-lower for RepyV2
# valid filenames (part 2)
return encode_dot_lower(path_enc)
def _decode_path(path_enc):
# Convert dot-lower character converted characters back to upper case
path = decode_dot_lower(path_enc)
# Convert trailing dots back to equal signs
path = path.replace(".", "=")
# Base64 ecnode a urlsafe filename
path = base64_urlsafe_b64decode(path)
return fn
def _is_user_file(user, encoded_path):
"""
<Purpose>
Helper method to find out if a file belongs to a user.
Identifies user ownership by decoding a base64 encoded path.
<Arguments>
user (string)
Sandbox User Name
encoded_path (string)
The b64 encoded filename under which the file is
stored in the vessel.
(The decoded path is in the form:
"<user>/<path>/<to>/<filename>")
<Returns>
user_path (string) | flase
The decoded path or False if file does not belong to theuser.
"""
path = _decode_path(encoded_path)
if path.startswith(user):
return True
return False
def tr_listdir(user):
"""
<Purpose>
Emulates repy's listdir() method. But only lists files
that are owned by the calling user.
<Arguments>
user (string)
The sandbox user whose files should be listed.
<Returns>
file_list_user
A list with the files' decoded paths, without the usernames.
"""
file_list_user = []
# Decode each path and return only paths that start with the username
# Left strip the username and sort the result before returning
for encoded_path in listfiles():
path = _decode_path(encoded_path)
if path.startswith(user):
virtual_path = path[len(user):]
file_list_user.append(virtual_path)
return file_list_user.sort()
def tr_open(user, virtual_path, mode=False):
"""
<Purpose>
Emulates repy's open() method.
<Arguments>
user (string)
The sandbox user whose file should be opened.
virtual_path (string)
the path which will be prefixed with the user name and
b64 encoded before it is safed to the vessel.
create (boolean)
A Boolean flag which specifies if the file should be created
if it does not exist. If the file exists, this flag has no effect.
(optional, default is false)
<Exceptions>
IOError
if the file does not exist and is opened in wrong mode.
<Returns>
a repy file object.
"""
encoded_path = _encode_path(user + virtual_path)
try:
return openfile(encoded_path, mode)
except Exception as e:
raise IOError("Exception (with type 'exceptions.IOError'): [Errno 2] " \
+ "No such file or directory: '" + virtual_path + "'")
def tr_removefile(user, virtual_path):
"""
<Purpose>
Emulates repy's removefile method.
<Arguments>
user (string)
The sandbox user whose file should be removed.
virtual_path (string)
The virtual_real path of the file, not as it is stored in
the vessel as it is known to the user.
<Exceptions>
IOError
If the file does not exist.
"""
encoded_path = _encode_path(user + virtual_path)
try:
removefile(encoded_path)
except Exception:
raise IOError("Exception (with type 'exceptions.IOError'): [Errno 2] " \
+ "No such file or directory: '" + virtual_path + "'")