home *** CD-ROM | disk | FTP | other *** search
- # $Header: P:/source/ppee/macros/locks.pev 1.8 09 Aug 1990 15:53:40 ericj $
-
- ##############################################################################
- #
- # Sage Software - POLYTRON Division
- # 1700 NW 167th Place
- # Beaverton, OR 97006
- #
- # Copyright 1990, Sage Software, Inc.
- #
- # Permission is hereby granted for licensed users of Sage Professional
- # Editor and PolyAwk to copy and modify this source code for their own
- # personal use. These derivative works may be distributed only to other
- # licensed Sage Professional Editor and PolyAwk users. All other usage
- # is prohibited without express written permission from Sage Software.
- #
- ##############################################################################
-
- #### $Workfile: locks.pel $: file locking mechanism
-
-
- local locked_filenames[]; # List of all semaphore file names
- # to be deleted upon exit
-
- local file_locking_enabled = 0; # File locking is enabled
-
- local file_locking_cleanup_enabled = 0; # File locking has been enabled and
- # cleanup needs to occur upon exit
-
- local LOCKED_EXT_CHAR = "&" # extension character for locking files
-
-
- ## toggle_file_locking()
- #
- # Enable or disable the ability to lock files.
- #
- global function toggle_file_locking( on ) {
-
- if( argcount() < 1 )
- on = !file_locking_enabled;
- else
- on = 0+on
- if (on)
- enable_file_locking();
- else
- disable_file_locking();
- }
-
- ## enable_file_locking()
- #
- # Enable file locking by attaching an event handler which will be called
- # every time a new file is about to be edited.
- #
- # Also attach an event handler to cleanup all locked files upon exit.
- #
- local function enable_file_locking(){
- if (!file_locking_enabled) {
- # need a handler to create semaphore files for
- # each file edited.
- #
- attach_event_handler( EVENT_NEW_EDIT_FILE, \
- function_id( "lock_edit_file" ));
-
- # need a handler to cleanup a semaphore files
- #
- if (!file_locking_cleanup_enabled) {
- attach_event_handler( EVENT_EXIT_EDITOR, \
- function_id( "unlock_edit_files" ));
- file_locking_cleanup_enabled = 1;
- }
- }
- }
-
- ## disable_file_locking()
- #
- # Disable file locking by deleting the event handler attached to the
- # NEW_EDIT_FILE event.
- #
- local function disable_file_locking(){
- if (file_locking_enabled) {
- delete_event( EVENT_NEW_EDIT_FILE, \
- function_id( "lock_edit_file" ));
- }
- }
-
-
- ## lock_edit_file()
- #
- # This function is attached to the NEW_EDIT_FILE event and is called
- # for every file that is about to be loaded into the system either
- # through edit_file() or create_buffer().
- #
- # This function only allows locking of non-SYSTEM buffers because multiple
- # system buffers may need to be created without locking.
- #
- # Locking is obtained by taking the original filename and modifying the
- # extension. If the new filename exists in the original directory, then
- # the file is assummed to be locked and an error is generated. If the
- # file can be created, it is and the original file is considered locked.
- # If the new filename cannot be created, the original directory is assummed
- # to be read-only and can't be written to anyway, so why lock it.
- #
-
- global function lock_edit_file(){
- local fname;
- local fid;
- local bname = buffer_original_filename;
- local pbuf = current_buffer;
- local prev_pe = pause_on_error;
-
-
-
- if ((buffer_original_filename) && (!and(buffer_flags, BUFFER_SYSTEM))){
- while (next_buffer( "", 1 ) != pbuf) {
- if ((buffer_original_filename == bname) \
- && (!and(buffer_flags, BUFFER_SYSTEM))){
- current_buffer = pbuf;
- return;
- }
- }
-
- current_buffer = pbuf;
-
- #
- # only worry about locking non-system buffer files
- #
- fname = create_semaphore_fname( bname = buffer_original_filename, \
- LOCKED_EXT_CHAR );
-
- if (fname == bname){
- # We are trying to edit one of the semaphore files.
- # Could have been called to edit x.* and x.__& was
- # created and found before findnext() was done.
- # Just ignore the file for now.
- buffer_filename = "";
- return;
- } else if (filetime( fname )) {
- # the file exists so it must be locked by
- # another user.
-
-
- pause_on_error = 1;
- buffer_flags = or( buffer_flags, BUFFER_READ_ONLY )
- warning( "File `" bname "' already locked, loaded as read-only buffer." );
- pause_on_error = prev_pe;
- return;
-
-
- ## uncomment the next 2 lines and comment the
- ## previous 5 lines to prevent multiple
- ## users from seeing the same file
-
- # buffer_filename = "";
- # error( "File `" bname "' already locked." );
-
-
- } else {
- fid = fopen( fname, 1 ) # open for Write only
- if (fid == -1){ # error occurred
- if (filetime( fname )){ # created by another user
- # the file exists so it must be locked by
- # another user.
-
-
- pause_on_error = 1;
- buffer_flags = or( buffer_flags, BUFFER_READ_ONLY )
- warning( "File `" bname "' locked, buffer is read-only." );
- pause_on_error = prev_pe;
- return;
-
- ## uncomment the next 2 lines and comment the
- ## previous 5 lines to prevent multiple
- ## users from seeing the same file
-
-
- # buffer_filename = "";
- # error( "File `" bname "' already locked." );
- } else {
- # can't open file so must be
- # read-only directory
- return;
- }
- } else {
- #
- # the file is now open
- # save the name so we can delete it when the
- # file is closed
- #
- locked_filenames[ fname ] = 0;
- fclose( fid );
- }
- }
- }
- }
-
-
- ## unlock_edit_files()
- #
- # All files which have been locked are unlocked by deleting the associated
- # semaphore file.
- #
- global function unlock_edit_files(){
- local fn
-
- for (fn in locked_filenames){
- unlink( fn );
- }
- }
-
- ## unlock_file()
- #
- # All files which have been locked are unlocked by deleting the associated
- # semaphore file.
- #
- global function unlock_file( fn ){
- fn = create_semaphore_fname( fn, LOCKED_EXT_CHAR );
-
- if (fn in locked_filenames){
- unlink( fn );
- delete( locked_filenames[ fn ] );
- }
- }
-
-