Skip to content

Instantly share code, notes, and snippets.

@smerritt
Created December 5, 2014 23:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smerritt/a415008402d82b03ceb4 to your computer and use it in GitHub Desktop.
Save smerritt/a415008402d82b03ceb4 to your computer and use it in GitHub Desktop.
find max supported xattr size
#!/usr/bin/env python
import xattr
XATTR_NAME = 'user.xattr-test'
MAX_SIZE = 2**20 + 1 # if it's over 1 MiB, call it infinite
def works(fileno, size):
try:
xattr.setxattr(fileno, XATTR_NAME, 'a' * size)
return True
except IOError:
return False
def find_max_xattr_size():
lower = 1
upper = None
with open('xattr-testfile', 'w') as fh:
fileno = fh.fileno()
if not works(fileno, 1):
return None
# try to find an upper bound by repeated doubling
size = 2
while works(fileno, size) and size < MAX_SIZE:
size *= 2
if size >= MAX_SIZE:
return float("inf")
# binary search to find the maximum size that works
upper = size
while upper > (lower + 1):
mid = (upper + lower) / 2
if works(fileno, mid):
lower = mid
else:
upper = mid
return lower
if __name__ == '__main__':
biggest = find_max_xattr_size()
if biggest is None:
print("FS doesn't seem to support extended attributes")
elif biggest == float("inf"):
print("FS supports really big extended attributes (over 1 MiB)")
else:
print("FS supports extended attributes up to %d bytes" % biggest)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment