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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
use ;
use ;
use FromStr;
use Result;
use async_trait;
use Bytes;
use debug;
use cratenormalize_srl_path;
use Transport;
// import logging
// import os
// import shutil
// import re
// from typing import AnyStr, Iterable, Optional
// from assemblyline.common.exceptions import ChainAll
// from assemblyline.common.path import strip_path_inclusion
// from assemblyline.common.uid import get_random_id
// from assemblyline.filestore.transport.base import Transport, TransportException, normalize_srl_path
// NORMALIZED = re.compile('[a-z0-9]/[a-z0-9]/[a-z0-9]/[a-z0-9]/[a-z0-9]{64}')
// @ChainAll(TransportException)
// class TransportLocal(Transport):
// """
// Local file system Transport class.
// """
// def __init__(self, base=None, normalize=None):
// self.log = logging.getLogger('assemblyline.transport.local')
// self.base = base
// self.host = "localhost"
// def local_normalize(path):
// # If they've provided an absolute path. Leave it a is.
// if path.startswith('/'):
// s = path
// # Relative paths
// elif '/' in path or len(path) != 64:
// s = _join(self.base, path)
// else:
// s = _join(self.base, normalize_srl_path(path))
// self.log.debug('local normalized: %s -> %s', path, s)
// return s
// if not normalize:
// normalize = local_normalize
// super(TransportLocal, self).__init__(normalize=normalize)
// def delete(self, path):
// normal_path = self.normalize(path)
// try:
// os.unlink(normal_path)
// except FileNotFoundError:
// pass
// except OSError as error:
// raise ValueError(f"Error erasing {path} as {normal_path}: {error}") from error
// def exists(self, path):
// path = self.normalize(path)
// return os.path.exists(path)
// def makedirs(self, path):
// path = self.normalize(path)
// try:
// os.makedirs(path)
// except OSError as e:
// if e.errno == 17:
// pass
// else:
// raise e
// # File based functions
// def download(self, src_path, dst_path):
// if src_path == dst_path:
// return
// src_path = self.normalize(src_path)
// dir_path = os.path.dirname(dst_path)
// if not os.path.exists(dir_path):
// os.makedirs(dir_path)
// shutil.copy(src_path, dst_path)
// def upload(self, src_path, dst_path):
// dst_path = self.normalize(dst_path)
// if src_path == dst_path:
// return
// dirname = os.path.dirname(dst_path)
// filename = os.path.basename(dst_path)
// tempname = get_random_id()
// temppath = _join(dirname, tempname)
// finalpath = _join(dirname, filename)
// assert (finalpath == dst_path)
// self.makedirs(dirname)
// shutil.copy(src_path, temppath)
// shutil.move(temppath, finalpath)
// assert (self.exists(dst_path))
// # Buffer based functions
// def get(self, path: str) -> bytes:
// path = self.normalize(path)
// fh = None
// try:
// fh = open(path, "rb")
// return fh.read()
// finally:
// if fh:
// fh.close()
// def put(self, path: str, content: AnyStr):
// path = self.normalize(path)
// dirname = os.path.dirname(path)
// filename = os.path.basename(path)
// tempname = get_random_id()
// temppath = _join(dirname, tempname)
// finalpath = _join(dirname, filename)
// assert (finalpath == path)
// self.makedirs(dirname)
// fh = None
// try:
// fh = open(temppath, "wb")
// if isinstance(content, str):
// return fh.write(content.encode())
// else:
// return fh.write(content)
// finally:
// if fh:
// fh.close()
// try:
// shutil.move(temppath, finalpath)
// except shutil.Error:
// pass
// assert (self.exists(path))
// def _denormalize(self, name):
// if NORMALIZED.fullmatch(name):
// return name.split('/')[-1]
// return name
// def list(self, prefix: Optional[str] = None) -> Iterable[str]:
// for name in self._list(self.base, prefix, prefix):
// yield self._denormalize(name)
// def _list(self, path: str, dir_prefix: Optional[str], file_prefix: Optional[str]) -> Iterable[str]:
// for listed in os.listdir(path):
// listed_path = os.path.join(path, listed)
// if os.path.isdir(listed_path):
// if dir_prefix:
// if listed.startswith(dir_prefix) or dir_prefix.startswith(listed):
// for subfile in self._list(listed_path, dir_prefix[len(listed):], file_prefix):
// yield os.path.join(listed, subfile)
// else:
// for subfile in self._list(listed_path, None, file_prefix):
// yield os.path.join(listed, subfile)
// if os.path.isfile(listed_path):
// if not file_prefix or listed.startswith(file_prefix) or not dir_prefix or listed.startswith(dir_prefix):
// yield listed
// ###############################
// # Helper functions.
// ###############################
// def _join(base, path):
// path = path.replace("\\", "/").replace("//", "/")
// if base is None:
// return path
// return os.path.join(base, strip_path_inclusion(path, base)).replace("\\", "/")