raise OSError if acl_to_text / acl_from_text returns NULL

Also did a small structural refactors there.
This commit is contained in:
Thomas Waldmann 2024-02-26 20:07:10 +01:00
parent a75945ed0d
commit b3554cdc0f
No known key found for this signature in database
GPG Key ID: 243ACFA951F78E01
3 changed files with 49 additions and 46 deletions

View File

@ -130,7 +130,7 @@ def acl_get(path, item, st, numeric_ids=False, fd=None):
raise OSError(errno.errno, os.strerror(errno.errno), os.fsdecode(path))
text = acl_to_text(acl, NULL)
if text == NULL:
return
raise OSError(errno.errno, os.strerror(errno.errno), os.fsdecode(path))
if numeric_ids:
item['acl_extended'] = _remove_non_numeric_identifier(text)
else:
@ -145,14 +145,14 @@ def acl_set(path, item, numeric_ids=False, fd=None):
acl_text = item.get('acl_extended')
if acl_text is not None:
try:
if isinstance(path, str):
path = os.fsencode(path)
if numeric_ids:
acl = acl_from_text(acl_text)
else:
acl = acl_from_text(<bytes>_remove_numeric_id_if_possible(acl_text))
if acl == NULL:
return
if isinstance(path, str):
path = os.fsencode(path)
raise OSError(errno.errno, os.strerror(errno.errno), os.fsdecode(path))
if fd is not None:
if acl_set_fd_np(fd, acl, ACL_TYPE_EXTENDED) == -1:
raise OSError(errno.errno, os.strerror(errno.errno), os.fsdecode(path))

View File

@ -122,23 +122,21 @@ def setxattr(path, name, value, *, follow_symlinks=False):
cdef _get_acl(p, type, item, attribute, flags, fd=None):
cdef acl_t acl = NULL
cdef char *text = NULL
try:
cdef acl_t acl
cdef char *text
if fd is not None:
acl = acl_get_fd_np(fd, type)
else:
acl = acl_get_link_np(p, type)
if acl is not NULL:
if acl == NULL:
raise OSError(errno.errno, os.strerror(errno.errno), os.fsdecode(p))
text = acl_to_text_np(acl, NULL, flags)
if text is not NULL:
if text == NULL:
acl_free(acl)
raise OSError(errno.errno, os.strerror(errno.errno), os.fsdecode(p))
item[attribute] = text
finally:
acl_free(text)
acl_free(acl)
else:
raise OSError(errno.errno, os.strerror(errno.errno), os.fsdecode(p))
def acl_get(path, item, st, numeric_ids=False, fd=None):
"""Saves ACL Entries
@ -168,7 +166,8 @@ cdef _set_acl(path, type, item, attribute, numeric_ids=False, fd=None):
elif numeric_ids and type in (ACL_TYPE_ACCESS, ACL_TYPE_DEFAULT):
text = posix_acl_use_stored_uid_gid(text)
acl = acl_from_text(<bytes>text)
if acl:
if acl == NULL:
raise OSError(errno.errno, os.strerror(errno.errno), os.fsdecode(path))
try:
if fd is not None:
if acl_set_fd_np(fd, acl, type) == -1:

View File

@ -261,11 +261,13 @@ def acl_get(path, item, st, numeric_ids=False, fd=None):
raise OSError(errno.errno, os.strerror(errno.errno), os.fsdecode(path))
if access_acl:
access_text = acl_to_text(access_acl, NULL)
if access_text:
if access_text == NULL:
raise OSError(errno.errno, os.strerror(errno.errno), os.fsdecode(path))
item['acl_access'] = converter(access_text)
if default_acl:
default_text = acl_to_text(default_acl, NULL)
if default_text:
if default_text == NULL:
raise OSError(errno.errno, os.strerror(errno.errno), os.fsdecode(path))
item['acl_default'] = converter(default_text)
finally:
acl_free(access_text)
@ -292,7 +294,8 @@ def acl_set(path, item, numeric_ids=False, fd=None):
if access_text is not None:
try:
access_acl = acl_from_text(<bytes>converter(access_text))
if access_acl is not NULL:
if access_acl == NULL:
raise OSError(errno.errno, os.strerror(errno.errno), os.fsdecode(path))
if fd is not None:
if acl_set_fd(fd, access_acl) == -1:
raise OSError(errno.errno, os.strerror(errno.errno), os.fsdecode(path))
@ -305,7 +308,8 @@ def acl_set(path, item, numeric_ids=False, fd=None):
if default_text is not None:
try:
default_acl = acl_from_text(<bytes>converter(default_text))
if default_acl is not NULL:
if default_acl == NULL:
raise OSError(errno.errno, os.strerror(errno.errno), os.fsdecode(path))
# only directories can get a default ACL. there is no fd-based api to set it.
if acl_set_file(path, ACL_TYPE_DEFAULT, default_acl) == -1:
raise OSError(errno.errno, os.strerror(errno.errno), os.fsdecode(path))