/*++ /* NAME /* edit_file 3 /* SUMMARY /* simple cooperative file updating protocol /* SYNOPSIS /* #include <edit_file.h> /* /* typedef struct { /* .in +4
/* char *tmp_path; /* temp. pathname */ /* VSTREAM *tmp_fp; /* temp. stream */ /* /* private members... */ /* .in -4 /* } EDIT_FILE; /* /* EDIT_FILE *edit_file_open(original_path, output_flags, output_mode) /* const char *original_path; /* int output_flags; /* mode_t output_mode; /* /* int edit_file_close(edit_file) /* EDIT_FILE *edit_file; /* /* void edit_file_cleanup(edit_file) /* EDIT_FILE *edit_file; /* DESCRIPTION /* This module implements a simple protocol for cooperative /* processes to update one file. The idea is to 1) create a /* new file under a deterministic temporary pathname, 2) /* populate the new file with updated information, and 3) /* rename the new file into the place of the original file. /* This module provides 1) and 3), and leaves 2) to the /* application. The temporary pathname is deterministic to /* avoid accumulation of thrash after program crashes. /* /* edit_file_open() implements the first phase of the protocol. /* It creates or opens an output file with a deterministic /* temporary pathname, obtained by appending the suffix defined /* with EDIT_FILE_SUFFIX to the specified original file pathname. /* The original file itself is not opened. edit_file_open() /* then locks the output file for exclusive access, and verifies /* that the file still exists under the temporary pathname. /* At this point in the protocol, the current process controls /* both the output file content and its temporary pathname. /* /* In the second phase, the application opens the original /* file if needed, and updates the output file via the /* \fBtmp_fp\fR member of the EDIT_FILE data structure. This /* phase is not implemented by the edit_file() module. /* /* edit_file_close() implements the third and final phase of /* the protocol. It flushes the output file to persistent /* storage, and renames the output file from its temporary /* pathname into the place of the original file. When any of /* these operations fails, edit_file_close() behaves as if /* edit_file_cleanup() was called. Regardless of whether these /* operations succeed, edit_file_close() releases the exclusive /* lock, closes the output file, and frees up memory that was /* allocated by edit_file_open(). /* /* edit_file_cleanup() aborts the protocol. It discards the /* output file, releases the exclusive lock, closes the output /* file, and frees up memory that was allocated by edit_file_open(). /* /* Arguments: /* .IP original_path /* The pathname of the original file that will be replaced by /* the output file. The temporary pathname for the output file /* is obtained by appending the suffix defined with EDIT_FILE_SUFFIX /* to a copy of the specified original file pathname, and is /* made available via the \fBtmp_path\fR member of the EDIT_FILE /* data structure. /* .IP output_flags /* Flags for opening the output file. These are as with open(2), /* except that the O_TRUNC flag is ignored. edit_file_open() /* always truncates the output file after it has obtained /* exclusive control over the output file content and temporary /* pathname. /* .IP output_mode /* Permissions for the output file. These are as with open(2), /* except that the output file is initially created with no /* group or other access permissions. The specified output /* file permissions are applied by edit_file_close(). /* .IP edit_file /* Pointer to data structure that is returned upon successful /* completion by edit_file_open(), and that must be passed to /* edit_file_close() or edit_file_cleanup(). /* DIAGNOSTICS /* Fatal errors: memory allocation failure, fstat() failure, /* unlink() failure, lock failure, ftruncate() failure. /* /* edit_file_open() immediately returns a null pointer when /* it cannot open the output file. /* /* edit_file_close() returns zero on success, VSTREAM_EOF on /* failure. /* /* With both functions, the global errno variable indicates /* the nature of the problem. All errors are relative to the /* temporary output's pathname. With both functions, this /* pathname is not available via the EDIT_FILE data structure, /* because that structure was already destroyed, or not created. /* BUGS /* In the non-error case, edit_file_open() will not return /* until it obtains exclusive control over the output file /* content and temporary pathname. Applications that are /* concerned about deadlock should protect the edit_file_open() /* call with a watchdog timer. /* /* When interrupted, edit_file_close() may leave behind a /* world-readable output file under the temporary pathname. /* On some systems this can be used to inflict a shared-lock /* DOS on the protocol. Applications that are concerned about /* maximal safety should protect the edit_file_close() call /* with sigdelay() and sigresume() calls, but this introduces /* the risk that the program will get stuck forever. /* LICENSE /* .ad /* .fi /* The Secure Mailer license must be distributed with this software. /* AUTHOR(S) /* Based on code originally by: /* Victor Duchovni /* Morgan Stanley /* /* Packaged into one module with minor improvements by: /* Wietse Venema /* IBM T.J. Watson Research /* P.O. Box 704 /* Yorktown Heights, NY 10598, USA
/*--*/
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.