mirror of
https://github.com/gnustep/libs-base.git
synced 2025-04-23 09:04:13 +00:00
NSDistributedLock from Frith-MacDonald
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@2613 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
parent
69d86d8223
commit
6fbf88955a
7 changed files with 132 additions and 11 deletions
18
ChangeLog
18
ChangeLog
|
@ -1,3 +1,21 @@
|
|||
Sat Nov 1 06:45:00 1997 Richard Frith-Macdonald <richard@brainstorm.co.uk>
|
||||
|
||||
* src/NSDistributedLock: New OPENSTEP class implementation.
|
||||
* src/GNUmakefile: Add new class to list.
|
||||
|
||||
* src/NSFileManager: implemented method ([-removeFileAtPath:handler:])
|
||||
|
||||
* src/NSString.m: Added two new (OPENSTEP) methods -
|
||||
([-fileSystemRepresentation]) and
|
||||
([-fileSystemRepresentation:maxLength:])
|
||||
|
||||
* src/NSUserDefaults.m: ([-synchronize]) modified to do locking
|
||||
using NSDistributedLock
|
||||
|
||||
* src/Foundation/NSDistributedLock.h: New OPENSTEP class definition.
|
||||
|
||||
* src/Foundation/NSString.h: Added two new (OPENSTEP) methods.
|
||||
|
||||
Fri Oct 31 17:00:29 1997 Adam Fedor <fedor@doc.com>
|
||||
|
||||
* GNUmakefile (DIST_FILES): Update files.
|
||||
|
|
|
@ -13,6 +13,8 @@ The currently released version of the library is
|
|||
@item Use the new GNUstep Makefile Package to compile the library, tools
|
||||
checks, and everything else.
|
||||
|
||||
@item An implementation of NSDistributedLock
|
||||
|
||||
@item NSData implemented as a class cluster including a class to
|
||||
allocate shared VM memory.
|
||||
|
||||
|
|
|
@ -184,6 +184,8 @@ typedef enum _NSStringEncoding
|
|||
caseSensitive: (BOOL)flag
|
||||
matchesIntoArray: (NSArray**)outputArray
|
||||
filterTypes: (NSArray*)filterTypes;
|
||||
- (const char*) fileSystemRepresentation;
|
||||
- (BOOL) getFileSystemRepresentation: (char*)buffer maxLength: (unsigned int)l;
|
||||
- (NSString*) lastPathComponent;
|
||||
- (NSString*) pathExtension;
|
||||
- (NSString*) stringByAbbreviatingWithTildeInPath;
|
||||
|
|
|
@ -311,6 +311,7 @@ NSDebug.m \
|
|||
NSDeallocateObject.m \
|
||||
NSDictionary.m \
|
||||
NSDistantObject.m \
|
||||
NSDistributedLock.m \
|
||||
NSEnumerator.m \
|
||||
NSException.m \
|
||||
NSFileHandle.m \
|
||||
|
@ -401,6 +402,7 @@ Foundation/NSDate.h \
|
|||
Foundation/NSDebug.h \
|
||||
Foundation/NSDictionary.h \
|
||||
Foundation/NSDistantObject.h \
|
||||
Foundation/NSDistributedLock.h \
|
||||
Foundation/NSException.h \
|
||||
Foundation/NSFileHandle.h \
|
||||
Foundation/NSFileManager.h \
|
||||
|
|
|
@ -27,6 +27,7 @@
|
|||
#include <gnustep/base/preface.h>
|
||||
#include <Foundation/NSFileManager.h>
|
||||
#include <Foundation/NSException.h>
|
||||
#include <Foundation/NSAutoreleasePool.h>
|
||||
#include <Foundation/NSLock.h>
|
||||
|
||||
/* determine directory reading files */
|
||||
|
@ -275,11 +276,84 @@ static NSFileManager* defaultManager = nil;
|
|||
return NO;
|
||||
}
|
||||
|
||||
- (BOOL)removeFileAtPath:(NSString*)path
|
||||
handler:handler
|
||||
- (BOOL)removeFileAtPath: (NSString*)path
|
||||
handler: handler
|
||||
{
|
||||
// TODO
|
||||
return NO;
|
||||
NSArray *contents;
|
||||
|
||||
if (handler)
|
||||
[handler fileManager: self willProcessPath: path];
|
||||
|
||||
contents = [self directoryContentsAtPath: path];
|
||||
if (contents == nil)
|
||||
{
|
||||
if (unlink([path fileSystemRepresentation]) < 0)
|
||||
{
|
||||
BOOL result;
|
||||
|
||||
if (handler)
|
||||
{
|
||||
NSMutableDictionary *info;
|
||||
|
||||
info = [[NSMutableDictionary alloc] initWithCapacity: 3];
|
||||
[info setObject: path forKey: @"Path"];
|
||||
[info setObject: [NSString stringWithCString: strerror(errno)]
|
||||
forKey: @"Error"];
|
||||
result = [handler fileManager: self
|
||||
shouldProceedAfterError: info];
|
||||
[info release];
|
||||
}
|
||||
else
|
||||
result = NO;
|
||||
return result;
|
||||
}
|
||||
else
|
||||
return YES;
|
||||
}
|
||||
else
|
||||
{
|
||||
int i;
|
||||
|
||||
contents = [self directoryContentsAtPath: path];
|
||||
for (i = 0; i < [contents count]; i++)
|
||||
{
|
||||
NSAutoreleasePool *arp;
|
||||
NSString *item;
|
||||
NSString *next;
|
||||
BOOL result;
|
||||
|
||||
arp = [[NSAutoreleasePool alloc] init];
|
||||
item = [contents objectAtIndex: i];
|
||||
next = [path stringByAppendingPathComponent: item];
|
||||
result = [self removeFileAtPath: next handler: handler];
|
||||
[arp release];
|
||||
if (result == NO)
|
||||
return NO;
|
||||
}
|
||||
|
||||
if (rmdir([path fileSystemRepresentation]) < 0)
|
||||
{
|
||||
BOOL result;
|
||||
|
||||
if (handler)
|
||||
{
|
||||
NSMutableDictionary *info;
|
||||
|
||||
info = [[NSMutableDictionary alloc] initWithCapacity: 3];
|
||||
[info setObject: path forKey: @"Path"];
|
||||
[info setObject: [NSString stringWithCString: strerror(errno)]
|
||||
forKey: @"Error"];
|
||||
result = [handler fileManager: self
|
||||
shouldProceedAfterError: info];
|
||||
[info release];
|
||||
}
|
||||
else
|
||||
result = NO;
|
||||
return result;
|
||||
}
|
||||
else
|
||||
return YES;
|
||||
}
|
||||
}
|
||||
|
||||
- (BOOL)createFileAtPath:(NSString*)path contents:(NSData*)contents
|
||||
|
|
|
@ -1824,6 +1824,21 @@ else
|
|||
return 0;
|
||||
}
|
||||
|
||||
/* Return a string for passing to OS calls to handle file system objects. */
|
||||
- (const char*)fileSystemRepresentation
|
||||
{
|
||||
return [self cString];
|
||||
}
|
||||
|
||||
- (BOOL)fileSystemRepresentation: (char*)buffer maxLength: (unsigned int)size
|
||||
{
|
||||
const char* ptr = [self cStringNoCopy];
|
||||
if (strlen(ptr) > size)
|
||||
return NO;
|
||||
strcpy(buffer, ptr);
|
||||
return YES;
|
||||
}
|
||||
|
||||
/* Returns a new string containing the last path component of the receiver. The
|
||||
path component is any substring after the last '/' character. If the last
|
||||
character is a '/', then the substring before the last '/', but after the
|
||||
|
|
|
@ -47,6 +47,7 @@
|
|||
#include <Foundation/NSNotification.h>
|
||||
#include <Foundation/NSTimer.h>
|
||||
#include <Foundation/NSProcessInfo.h>
|
||||
#include <Foundation/NSDistributedLock.h>
|
||||
|
||||
/* Wait for access */
|
||||
#define _MAX_COUNT 5 /* Max 10 sec. */
|
||||
|
@ -70,7 +71,8 @@ static NSString* GNU_UserDefaultsDatabaseLock = @"GNUstep/.GNUstepUDLock";
|
|||
*************************************************************************/
|
||||
static NSUserDefaults *sharedDefaults = nil;
|
||||
static NSMutableString *defaultsDatabase = nil;
|
||||
static NSMutableString *defaultsDatabaseLock = nil;
|
||||
static NSMutableString *defaultsDatabaseLockName = nil;
|
||||
static NSDistributedLock *defaultsDatabaseLock = nil;
|
||||
static NSMutableString *processName = nil;
|
||||
|
||||
/*************************************************************************
|
||||
|
@ -182,10 +184,12 @@ static NSMutableString *processName = nil;
|
|||
[[NSMutableString stringWithFormat:@"%@/%@",
|
||||
NSHomeDirectoryForUser(NSUserName()),
|
||||
GNU_UserDefaultsDatabase] retain];
|
||||
defaultsDatabaseLock =
|
||||
defaultsDatabaseLockName =
|
||||
[[NSMutableString stringWithFormat:@"%@/%@",
|
||||
NSHomeDirectoryForUser(NSUserName()),
|
||||
GNU_UserDefaultsDatabaseLock] retain];
|
||||
defaultsDatabaseLock =
|
||||
[[NSDistributedLock lockWithPath: defaultsDatabaseLockName] retain];
|
||||
processName = [[[NSProcessInfo processInfo] processName] retain];
|
||||
#if 0
|
||||
processName = [[NSMutableString stringWithFormat:@"TestApp"] retain];
|
||||
|
@ -199,7 +203,6 @@ static NSMutableString *processName = nil;
|
|||
persDomains = [[NSMutableDictionary dictionaryWithCapacity:10] retain];
|
||||
if ([self synchronize] == NO)
|
||||
{
|
||||
NSLog(@"unable to load defaults - %s", strerror(errno));
|
||||
[self dealloc];
|
||||
return self = nil;
|
||||
}
|
||||
|
@ -465,8 +468,12 @@ static NSMutableString *processName = nil;
|
|||
tickingTimer = NO;
|
||||
|
||||
// Get file lock
|
||||
if (mkdir([defaultsDatabaseLock cString],0755) == -1)
|
||||
return NO;
|
||||
if ([defaultsDatabaseLock tryLock] == NO)
|
||||
{
|
||||
NSLog(@"unable to access defaults database - locked on (%@) since %@\n",
|
||||
defaultsDatabaseLockName, [defaultsDatabaseLock lockDate]);
|
||||
return NO;
|
||||
}
|
||||
|
||||
// Read the persistent data from the stored database
|
||||
newDict = [[NSMutableDictionary allocWithZone:[self zone]]
|
||||
|
@ -493,7 +500,8 @@ static NSMutableString *processName = nil;
|
|||
// Save the changes
|
||||
if (![persDomains writeToFile:defaultsDatabase atomically:YES])
|
||||
{
|
||||
rmdir([defaultsDatabaseLock cString]); // release file lock
|
||||
NSLog(@"failed to write defaults to '%@'\n", defaultsDatabase);
|
||||
[defaultsDatabaseLock unlock];
|
||||
return NO;
|
||||
}
|
||||
}
|
||||
|
@ -503,7 +511,7 @@ static NSMutableString *processName = nil;
|
|||
persDomains = newDict;
|
||||
}
|
||||
|
||||
rmdir([defaultsDatabaseLock cString]); // release file lock
|
||||
[defaultsDatabaseLock unlock]; // release file lock
|
||||
|
||||
return YES;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue