fsnix.util - Higher Level Tools

Module API

fsnix.util.setfdcloexec(fd)

Set the file descriptor to be automatically closed if the process exec’s. Returns fd. This is a convenience function wrapping fcntl module calls.

fsnix.util.closingfd(fd)

Context manager that automatically calls os.close() on the fd. Behaves in a similar manner to contextlib.closing(), but for file descriptors instead of file objects.

fsnix.util.removeall(dirfd, path, errback=None, _fs=None)

Recursively delete a file or directory tree indicated by path (relative to directory file descriptor dirfd). Similar in intent to shutil.rmtree() but is more robust in the face of symlink attack.

If errback is specified it must be a callable that accepts the arguments (error, path). Error will be the exception encountered and path is the object being deleted. The _fs argument allows the user to pass in a custom “fslib” module if needed.

fsnix.util.opendir(path)

Return a directory object corresponding to the specified path.

fsnix.util.fdopendir(dirfd)

Return a directory object corresponding to already opened directory file descriptor dirfd.

fsnix.util.opendirat(dirfd, path, _fs=None)

Return a directory object corresponding to the specified directory file descriptor dirfd and path.

The _fs argument allows the user to pass in a custom “fslib” module if needed.

fsnix.util.walk(top, topdown=True, onerror=None, followlinks=False, _fs=None)

An alternate implementation of os.walk() which demonstrates the use of some of the lower level fslib functions.

Note

followlinks is not supported by this function.

Directory Objects

You should not try to instantiate a directory object directly. Instead, use opendir(), opendirat(), or fdopendir().

class fsnix.util.directory

Directory objects are intended to mimic the API of Python file objects to a limited degree. They are context managers and support the fileno method to get the file descriptor value. Instead of supporting IO methods some simple directory listing wrappers are supported.

name

The path name of the directory, or None if not available.

closed

A boolean indicating that the directory has been closed.

fileno()

Return the value of the open file descriptor for this directory. If you get confused and want to make sure you are using a directory object dirno is available as an alias to fileno.

close()

Closes the directory.

listdir(_fs=None)

Return a list of entries in the open directory. Specify _fs if you need to use an alternate fslib module.