aboutsummaryrefslogtreecommitdiff
path: root/bin
diff options
context:
space:
mode:
authorZac Medico <zmedico@gentoo.org>2010-08-28 15:33:49 -0700
committerZac Medico <zmedico@gentoo.org>2010-08-28 15:33:49 -0700
commit678f3895b1a3d8c33977c2946aa520440c8d2461 (patch)
treec6ec153ff2cc18f2af807b6dd9ab087aba291de8 /bin
parentSimplify the empty USE conditional /var/db/pkg metadata code in (diff)
downloadportage-678f3895b1a3d8c33977c2946aa520440c8d2461.tar.gz
portage-678f3895b1a3d8c33977c2946aa520440c8d2461.tar.bz2
portage-678f3895b1a3d8c33977c2946aa520440c8d2461.zip
For egencache --preserve-comments, probe comments in binary mode in
order to avoid potential character encoding issues (both in terms of decoding and in calculating file position given variable-width character encodings like UTF-8.
Diffstat (limited to 'bin')
-rwxr-xr-xbin/egencache20
1 files changed, 11 insertions, 9 deletions
diff --git a/bin/egencache b/bin/egencache
index b0d727e96..7a3a9f218 100755
--- a/bin/egencache
+++ b/bin/egencache
@@ -342,7 +342,9 @@ class GenUseLocalDesc(object):
desc_path = self._output
if self._preserve_comments:
- output_mode = 'r+'
+ # Probe in binary mode, in order to avoid
+ # potential character encoding issues.
+ output_mode = 'rb'
else:
output_mode = 'w'
@@ -359,15 +361,15 @@ class GenUseLocalDesc(object):
if self._preserve_comments:
while True:
- l = output.readline()
- # Seek through comments, until the first non-comment.
- # If we hit empty line, then great.
- # Otherwise, we need to seek back and add an empty line.
- if not l.startswith('#'):
- if l.rstrip() != '':
- output.seek(-len(l), os.SEEK_CUR)
- output.write('\n')
+ pos = output.tell()
+ if not output.readline().startswith(b'#'):
break
+ output.close()
+
+ # Finished probing comments, now open for write.
+ output = open(desc_path, 'r+')
+ output.seek(pos)
+ output.write('\n')
output.truncate()
else:
output.write('''