FileLock             package:R.filelocks             R Documentation

_T_h_e _F_i_l_e_L_o_c_k _c_l_a_s_s

_D_e_s_c_r_i_p_t_i_o_n:

     Package:  R.filelocks 
      *Class FileLock*

     'Object'
      '~~|'
      '~~+--''FileLock'

     *Directly known subclasses:*
      NullFileLock

     public static class *FileLock*
      extends Object

     The FileLock class provides method for locking and releasing
     files.

_U_s_a_g_e:

     FileLock(con=NULL, pathname=NULL, pathnameL=NULL, ..., .core=TRUE)

_A_r_g_u_m_e_n_t_s:

     con: A 'connection'.

pathname, pathnameL: Pathnames.

     ...: Not used.

   .core: Internal only.

_D_e_t_a_i_l_s:

     This class is instantiated via static methods '*tryLock()' and
     '*lock()'.  It should never be instantiated via the constructor.

_F_i_e_l_d_s _a_n_d _M_e_t_h_o_d_s:

     *Methods:*

         'as.character'   -
         'getConnection'  -
         'getPathname'    -
         'isLocked'       -
         'lock'           -
         'release'        -
         'tryLock'        -

     *Methods inherited from Object*:
      $, $<-, [[, [[<-, as.character, attach, attachLocally,
     clearCache, clone, detach, equals, extend, finalize, gc,
     getEnvironment, getFields, getInstantiationTime,
     getStaticInstance, hasField, hashCode, ll, load, objectSize,
     print, save

_A_u_t_h_o_r(_s):

     Henrik Bengtsson (<URL: http://www.braju.com/R/>)

_R_e_f_e_r_e_n_c_e_s:

     [1] <URL: http://en.wikipedia.org/wiki/File_locking>

_E_x_a_m_p_l_e_s:

     # The file to be locked (note, it does not have to exists)
     pathname <- "foo.txt"

     # Try to lock the above file
     lockA <- FileLock$tryLock(pathname)
     print(lockA)

     # Try to lock it again (should not work)
     lockB <- FileLock$tryLock(pathname)
     print(lockB)

     # Release the lock
     release(lockA)

     # Try to lock again
     lockC <- FileLock$lock(pathname);
     print(lockC)

     # Oops, we forget to release...
     rm(lockC)

     # However, the garbage collector will release it,
     # which is forced to run whenever the file appears
     # to be locked.  This is why the following works.

     # Try to lock it
     lockA <- FileLock$tryLock(pathname)
     print(lockA)

     # Try to lock it again (should not work)
     lockB <- FileLock$tryLock(pathname)
     print(lockB)

     # Clean up and remove any stray file locks
     rm(lockA, lockB)
     gc()

