Improve parsing

To make the logic more resilient towards potential errors or future
output changes, not only consider the string index but also validate
it against a set of expected positions.

Signed-off-by: Georg Pfuetzenreuter <mail@georg-pfuetzenreuter.net>
This commit is contained in:
Georg Pfuetzenreuter 2024-09-16 16:14:25 +02:00
parent 3f58651401
commit 48b54bbd71
Signed by: Georg
GPG Key ID: 1ED2F138E7E6FF57

View File

@ -43,22 +43,32 @@ def parse_permission(strpermission):
if len(strpermission) != MAX_PERMBITS: if len(strpermission) != MAX_PERMBITS:
return ValueError('Invalid permission') return ValueError('Invalid permission')
permap = { permap_i = {
0: 'read', 0: 'read',
1: 'write', 1: 'write',
2: 'execute', 2: 'execute',
} }
permap_s = {
'r': 'read',
'w': 'write',
'e': 'execute',
}
outmap = DEFAULT_PERMISSIONS.copy() outmap = DEFAULT_PERMISSIONS.copy()
for i, perm in permap.items(): for i, spval in enumerate(strpermission):
permval = strpermission[i] permval = permap_i[i]
if permval in ['r', 'w', 'x']: if spval == '-':
outmap[perm] = True outmap[permval] = False
elif permval == '-':
outmap[perm] = False
else: else:
return ValueError('Invalid permission flag') spermval = permap_s.get(spval)
if spermval and spermval in outmap:
if spermval != permval:
raise ValueError('Unexpected permission mismatch')
outmap[spermval] = True
else:
return ValueError('Invalid permission flag')
return outmap return outmap