Skip to content

Commit d395626

Browse files
authored
Fix path handling in archive.py to always be posix and not native paths (#1892)
* Fix path handling in archive.py to always be posix and not native paths * Readd mistakenly removed pax_headers handling
1 parent 454981e commit d395626

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

container/archive.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import os
2020
import subprocess
2121
import tarfile
22+
import posixpath
2223

2324
# Use a deterministic mtime that doesn't confuse other programs.
2425
# See: https://github.com/bazelbuild/bazel/issues/1299
@@ -183,10 +184,10 @@ def add_dir(self,
183184
"""
184185
if not (name == self.root_directory or name.startswith('/') or
185186
name.startswith(self.root_directory + '/')):
186-
name = os.path.join(self.root_directory, name)
187+
name = posixpath.join(self.root_directory, name)
187188
if mtime is None:
188189
mtime = self.default_mtime
189-
if os.path.isdir(path):
190+
if posixpath.isdir(path):
190191
# Remove trailing '/' (index -1 => last character)
191192
if name[-1] == '/':
192193
name = name[:-1]
@@ -208,8 +209,8 @@ def add_dir(self,
208209
filelist = os.listdir(path)
209210
filelist.sort()
210211
for f in filelist:
211-
new_name = os.path.join(name, f)
212-
new_path = os.path.join(path, f)
212+
new_name = posixpath.join(name, f)
213+
new_path = posixpath.join(path, f)
213214
self.add_dir(new_name, new_path, uid, gid, uname, gname, mtime, mode,
214215
depth - 1)
215216
else:
@@ -262,13 +263,13 @@ def add_file(self,
262263
mtime: modification time to put in the archive.
263264
mode: unix permission mode of the file, default 0644 (0755).
264265
"""
265-
if file_content and os.path.isdir(file_content):
266+
if file_content and posixpath.isdir(file_content):
266267
# Recurse into directory
267268
self.add_dir(name, file_content, uid, gid, uname, gname, mtime, mode)
268269
return
269270
if not (name == self.root_directory or name.startswith('/') or
270271
name.startswith(self.root_directory + '/')):
271-
name = os.path.join(self.root_directory, name)
272+
name = posixpath.join(self.root_directory, name)
272273
if kind == tarfile.DIRTYPE:
273274
name = name.rstrip('/')
274275
if name in self.directories:
@@ -338,7 +339,7 @@ def add_tar(self,
338339
if root and root[0] not in ['/', '.']:
339340
# Root prefix should start with a '/', adds it if missing
340341
root = '/' + root
341-
compression = os.path.splitext(tar)[-1][1:]
342+
compression = posixpath.splitext(tar)[-1][1:]
342343
if compression == 'tgz':
343344
compression = 'gz'
344345
elif compression == 'bzip2':
@@ -390,7 +391,7 @@ def add_tar(self,
390391
name = tarinfo.name
391392
if (not name.startswith('/') and
392393
not name.startswith(self.root_directory)):
393-
name = os.path.join(self.root_directory, name)
394+
name = posixpath.join(self.root_directory, name)
394395
if root is not None:
395396
if name.startswith('.'):
396397
name = '.' + root + name.lstrip('.')

0 commit comments

Comments
 (0)