home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1994 June / NEBULA_SE.ISO / SourceCode / MiscKit / Source / MiscLockFile.m < prev    next >
Encoding:
Text File  |  1994-01-17  |  3.0 KB  |  120 lines

  1. //
  2. //    MiscLockFile.m -- a generic class to simplify keeping an atomic lock file
  3. //        Written by Don Yacktman (c) 1993 by Don Yacktman.
  4. //                Version 1.0.  All rights reserved.
  5. //
  6. //        This notice may not be removed from this source code.
  7. //
  8. //    This object is included in the MiscKit by permission from the author
  9. //    and its use is governed by the MiscKit license, found in the file
  10. //    "LICENSE.rtf" in the MiscKit distribution.  Please refer to that file
  11. //    for a list of all applicable permissions and restrictions.
  12. //    
  13.  
  14. #import <misckit/misckit.h>
  15. #import <sys/file.h>
  16. #import <sys/vnode.h>
  17.  
  18. @implementation MiscLockFile
  19.  
  20. - init
  21. {
  22.     id ret = [super init];
  23.     haveLock = NO;
  24.     return ret;
  25. }
  26.  
  27. - fileName { return lockFileName; }
  28. - setFileName:aString
  29. {
  30.     if (haveLock) [self unlock];
  31.     lockFileName = aString;
  32.     return self;
  33. }
  34.  
  35. - lock  // atomically create a lock file.  returns YES on success
  36. {    // lock file contains the PID of the locking process.
  37.     int lockFileFD; char buffer[16];
  38.     if (!lockFileName || ![lockFileName stringValue])
  39.         return self; // always "have" lock if no lock file used
  40.     if (haveLock) return self; // already been done
  41.     lockFileFD = open([lockFileName stringValue],    // file name
  42.             (O_EXCL | O_CREAT | O_RDWR),    // error if exists; create if doesn't
  43.             (VREAD | VWRITE)); // mode is owner r/w
  44.     if (lockFileFD < 0) return nil; // error opening the lock file
  45.     sprintf(buffer, "%d", getpid());
  46.     if (write(lockFileFD, buffer, strlen(buffer)) < 0) return nil; // couldn't write
  47.     if (close(lockFileFD)) return nil; // error closing the lock file
  48.     haveLock = YES;
  49.     return self;
  50. }
  51.  
  52. - unlock    // remove the lock file.
  53. {
  54.     if (!lockFileName || ![lockFileName stringValue])
  55.         return self; // always "had" lock if no lock file used
  56.     if (!haveLock) return nil;
  57.     if (unlink([lockFileName stringValue])) return nil;  // error in unlink()
  58.     haveLock = NO; // the lock was successfully removed.
  59.     return self;
  60. }
  61.  
  62. - (BOOL)haveLock { return (haveLock || !lockFileName); }
  63.  
  64. - copy
  65. {    // copies do not have locks or open logs... only original can!
  66.     id myCopy = [[MiscLockFile alloc] init];
  67.     [myCopy setFileName:lockFileName];
  68.     return myCopy;
  69. }
  70.  
  71. - read:(NXTypedStream *)stream
  72. {
  73.     [super read:stream];
  74.     lockFileName = NXReadObject(stream);
  75.     haveLock = NO;
  76.     return self;
  77. }
  78.  
  79. - write:(NXTypedStream *)stream
  80. {
  81.     [super write:stream];
  82.     NXWriteObject(stream, lockFileName);
  83.     return self;
  84. }
  85.  
  86. // NXTransport protocol implementation:
  87. - encodeUsing:(id <NXEncoding>)portal
  88. {
  89.     [portal encodeObjectBycopy:lockFileName];
  90.     return self;
  91. }
  92.  
  93. - decodeUsing:(id <NXDecoding>)portal
  94. {
  95.     lockFileName = [portal decodeObject];
  96.     return self;
  97. }
  98.  
  99. - encodeRemotelyFor:(NXConnection *)connection
  100.     freeAfterEncoding:(BOOL *)flagp isBycopy:(BOOL)isByCopy
  101. {
  102.     if (isByCopy) {
  103.         *flagp = NO; // object will copy.
  104.         return self; //encode object (copy it)
  105.     }
  106.     *flagp = NO; // object will copy.
  107.     // super will encode the proxy otherwise
  108.     return [super encodeRemotelyFor:connection
  109.                 freeAfterEncoding:flagp isBycopy:isByCopy];
  110. }
  111.  
  112. - free
  113. {
  114.     if (haveLock) [self unlock];
  115.     [lockFileName free];
  116.     return [super free];
  117. }
  118.  
  119. @end
  120.