|  | 
 | ||||
|        | |||||
|  | |||||
| Locking a file | |||||
You can lock a file for such purposes as maintaining a hit counter on a website, by using OmniMark's omvfs library.
To lock a file for such a hit counter, create a program like this one:
    import "omvfs.xmd" prefixed by vfs.
    process
       local vfs.file counter-file
       local stream hit-count
  
       ; set up a vfs.file for the counter
       set counter-file to vfs.open "counter.txt" for vfs.read-write-mode
  
       vfs.lock counter-file
  
       ; read and increment the hit-count
       set hit-count to "d" % vfs.read counter-file + 1
  
       ; output the hit-count
       output hit-count
  
       ; output the updated counter
       vfs.move-cursor counter-file to 0
       set vfs.write counter-file to hit-count
       vfs.unlock counter-file
  
       ; a catch block for handling errors in this program
       catch #external-exception
        identity catch-id
        message catch-msg
          put #ERROR 'An error occurred while accessing a VFS file'
          put #ERROR '%g(catch-id) : %g(catch-msg)%n'
This program does the following:
hit-count, and the VFS file counter-file.
counter-file to the file named "counter.txt" by using open.
counter-file for the duration of this program.
hit-count by adding 1 to the contents of counter-file.
hit-count.
 counter-file to the value of hit-count using write, thus updating the file  "c:\counter.txt".
counter-file with the omvfs library function unlock so another process can update it.
Copyright © Stilo International plc, 1988-2010.