home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 16 / 16.iso / w / w055 / 4.ddi / SOURCES.LIF / LOCKS.PEL < prev    next >
Encoding:
Text File  |  1990-09-27  |  6.1 KB  |  224 lines

  1. # $Header:   P:/source/ppee/macros/locks.pev   1.8   09 Aug 1990 15:53:40   ericj  $
  2.  
  3. ##############################################################################
  4. #
  5. #           Sage Software - POLYTRON Division
  6. #             1700 NW 167th Place
  7. #               Beaverton, OR 97006
  8. #
  9. #   Copyright 1990, Sage Software, Inc.
  10. #
  11. #   Permission is hereby granted for licensed users of Sage Professional
  12. #   Editor and PolyAwk to copy and modify this source code for their own
  13. #   personal use.  These derivative works may be distributed only to other
  14. #   licensed Sage Professional Editor and PolyAwk users.  All other usage
  15. #   is prohibited without express written permission from Sage Software.
  16. #
  17. ##############################################################################
  18.  
  19. #### $Workfile:   locks.pel  $: file locking mechanism
  20.  
  21.  
  22. local locked_filenames[];        # List of all semaphore file names 
  23.                     #   to be deleted upon exit
  24.  
  25. local file_locking_enabled = 0;         # File locking is enabled
  26.  
  27. local file_locking_cleanup_enabled = 0; # File locking has been enabled and 
  28.                                         #    cleanup needs to occur upon exit
  29.  
  30. local LOCKED_EXT_CHAR   = "&"    # extension character for locking files
  31.  
  32.  
  33. ## toggle_file_locking()
  34. #
  35. #  Enable or disable the ability to lock files.
  36. #
  37. global function toggle_file_locking( on ) {
  38.  
  39.     if( argcount() < 1 )
  40.         on = !file_locking_enabled;
  41.     else
  42.         on = 0+on
  43.     if (on)
  44.         enable_file_locking();
  45.     else
  46.         disable_file_locking();
  47. }
  48.  
  49. ## enable_file_locking()
  50. #
  51. #  Enable file locking by attaching an event handler which will be called
  52. #  every time a new file is about to be edited.
  53. #
  54. #  Also attach an event handler to cleanup all locked files upon exit.
  55. #
  56. local function enable_file_locking(){
  57.     if (!file_locking_enabled) {
  58.         # need a handler to create semaphore files for
  59.         # each file edited.
  60.         #
  61.         attach_event_handler( EVENT_NEW_EDIT_FILE, \
  62.             function_id( "lock_edit_file" ));
  63.  
  64.         # need a handler to cleanup a semaphore files
  65.         #
  66.         if (!file_locking_cleanup_enabled) {
  67.             attach_event_handler( EVENT_EXIT_EDITOR, \
  68.                 function_id( "unlock_edit_files" ));
  69.             file_locking_cleanup_enabled = 1;
  70.         }
  71.     }
  72. }
  73.  
  74. ## disable_file_locking()
  75. #
  76. #  Disable file locking by deleting the event handler attached to the
  77. #  NEW_EDIT_FILE event.
  78. #
  79. local function disable_file_locking(){
  80.     if (file_locking_enabled) {
  81.         delete_event( EVENT_NEW_EDIT_FILE, \
  82.             function_id( "lock_edit_file" ));
  83.     }
  84. }
  85.  
  86.  
  87. ## lock_edit_file()
  88. #
  89. #  This function is attached to the NEW_EDIT_FILE event and is called
  90. #  for every file that is about to be loaded into the system either
  91. #  through edit_file() or create_buffer().
  92. #
  93. #  This function only allows locking of non-SYSTEM buffers because multiple
  94. #  system buffers may need to be created without locking.
  95. #
  96. #  Locking is obtained by taking the original filename and modifying the
  97. #  extension.  If the new filename exists in the original directory, then
  98. #  the file is assummed to be locked and an error is generated.  If the
  99. #  file can be created, it is and the original file is considered locked.
  100. #  If the new filename cannot be created, the original directory is assummed
  101. #  to be read-only and can't be written to anyway, so why lock it.
  102. #
  103.  
  104. global function lock_edit_file(){
  105.     local fname;
  106.     local fid;
  107.     local bname = buffer_original_filename;
  108.     local pbuf = current_buffer;
  109.     local prev_pe = pause_on_error;
  110.  
  111.  
  112.  
  113.     if ((buffer_original_filename) && (!and(buffer_flags, BUFFER_SYSTEM))){
  114.         while (next_buffer( "", 1 ) != pbuf) {
  115.             if ((buffer_original_filename == bname) \
  116.                 && (!and(buffer_flags, BUFFER_SYSTEM))){
  117.                 current_buffer = pbuf;
  118.                 return;
  119.             }
  120.         }
  121.  
  122.         current_buffer = pbuf;
  123.  
  124.         #
  125.         # only worry about locking non-system buffer files
  126.         #
  127.         fname = create_semaphore_fname( bname = buffer_original_filename, \
  128.             LOCKED_EXT_CHAR );
  129.  
  130.         if (fname == bname){
  131.             # We are trying to edit one of the semaphore files.
  132.             # Could have been called to edit x.* and x.__& was
  133.             # created and found before findnext() was done.
  134.             # Just ignore the file for now.
  135.             buffer_filename = "";
  136.             return;
  137.         } else if (filetime( fname )) {
  138.             # the file exists so it must be locked by
  139.             # another user.
  140.  
  141.  
  142.             pause_on_error = 1;
  143.             buffer_flags = or( buffer_flags, BUFFER_READ_ONLY )
  144.             warning( "File `" bname "' already locked, loaded as read-only buffer." );
  145.             pause_on_error = prev_pe;
  146.             return;
  147.  
  148.  
  149.             ## uncomment the next 2 lines and comment the 
  150.             ## previous 5 lines to prevent multiple 
  151.             ## users from seeing the same file
  152.  
  153.             # buffer_filename = "";
  154.             # error( "File `" bname "' already locked." );
  155.  
  156.  
  157.         } else {
  158.             fid = fopen( fname, 1 )        # open for Write only
  159.             if (fid == -1){            # error occurred
  160.                 if (filetime( fname )){    # created by another user
  161.                     # the file exists so it must be locked by
  162.                     # another user.
  163.  
  164.  
  165.                     pause_on_error = 1;
  166.                     buffer_flags = or( buffer_flags, BUFFER_READ_ONLY )
  167.                     warning( "File `" bname "' locked, buffer is read-only." );
  168.                     pause_on_error = prev_pe;
  169.                     return;
  170.  
  171.                     ## uncomment the next 2 lines and comment the 
  172.                     ## previous 5 lines to prevent multiple 
  173.                     ## users from seeing the same file
  174.  
  175.  
  176.                     # buffer_filename = "";
  177.                     # error( "File `" bname "' already locked." );
  178.                 } else {
  179.                     # can't open file so must be 
  180.                     # read-only directory
  181.                     return;
  182.                 }
  183.             } else {
  184.                 #
  185.                 # the file is now open
  186.                 # save the name so we can delete it when the
  187.                 # file is closed
  188.                 #
  189.                 locked_filenames[ fname ] = 0;
  190.                 fclose( fid );
  191.             }
  192.         }
  193.     }
  194. }
  195.  
  196.  
  197. ## unlock_edit_files()
  198. #
  199. #  All files which have been locked are unlocked by deleting the associated
  200. #  semaphore file.
  201. #
  202. global function unlock_edit_files(){
  203.     local fn
  204.  
  205.     for (fn in locked_filenames){
  206.         unlink( fn );
  207.     }
  208. }
  209.  
  210. ## unlock_file()
  211. #
  212. #  All files which have been locked are unlocked by deleting the associated
  213. #  semaphore file.
  214. #
  215. global function unlock_file( fn ){
  216.     fn = create_semaphore_fname( fn, LOCKED_EXT_CHAR );
  217.  
  218.     if (fn in locked_filenames){
  219.         unlink( fn );
  220.         delete( locked_filenames[ fn ] );
  221.     }
  222. }
  223.  
  224.