All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
Functions | Variables
xorn.fileutils Namespace Reference

Writing files in a safe way. More...


def follow_symlinks
 Follow a series of symbolic links and return the destination path. More...
def umask
 Return the process's file mode creation mask. More...
def write
 Write some data to a file in a reasonably safe way. More...


 How many symlinks to follow before returning ELOOP. More...

Detailed Description

Writing files in a safe way.

Function Documentation

def xorn.fileutils.follow_symlinks (   filename)

Follow a series of symbolic links and return the destination path.

This function calls readlink(2) recursively until some place that doesn't contain a symlink is found—either some other kind of file, or a non-existing file if the last symlink was broken. There may still be symlinks in the directory components of the returned path.

Definition at line 39 of file fileutils.py.

def xorn.fileutils.umask ( )

Return the process's file mode creation mask.

Definition at line 75 of file fileutils.py.

def xorn.fileutils.write (   filename,
  overwrite = False,
  backup = True,
  fsync = True 

Write some data to a file in a reasonably safe way.

Calls write_func to write some data to the file named filename. The data is first written to a temporary file which is then renamed to the final name. If write_func raises an exception, the temporary file is removed, and the original file is left untouched.

If backup is True (the default), an existing regular file called filename will be backed up after the data has successfully been written, replacing an existing backup file. Otherwise, it will be overwritten. An exception to this is if the user doesn't have write permission to the existing file and overwrite is False. (This solves gEDA bug #698565.) If filename exists but is not a regular file, an exception is raised.

If filename is a symbolic link, the (ultimately) linked file is replaced, not the link. The backup is created in the same directory as the linked file.

Hard links to filename will break. Also, since the file is recreated, existing access control lists, metadata etc. may be lost. The function tries to preserve owner, group and attributes but doen't treat failure to do so as an error.

[in]filenameThe name of a file to write to.
[in]write_funcA function that takes a file-like object as an argument and writes the data to it.
[in]overwriteOverwrite the file even if the user doen't have write permission.
[in]backupBack up the original file. Set this to False if the file was already saved in this invocation of the program.
[in]fsyncSync the temporary file to ensure the data is on disk when it is renamed over the destination. This may cause significant lag; but otherwise, if the system crashes, both the new and the old file may be lost on some filesystems (i.e. those that don't guarantee the data is written to the disk before the metadata).
IOError,OSErrorif a filesystem error occurred

Definition at line 132 of file fileutils.py.

Variable Documentation

int xorn.fileutils.MAXSYMLINKS = 256

How many symlinks to follow before returning ELOOP.

A more conventional value for this is 20 or even 8, but gEDA uses 256, so we're using that, too.

Definition at line 30 of file fileutils.py.