* EOAccess/EOModel.m ([EOModel writeToFile:]) Set path (and

name implicitly) of the receiver.  Create backup file when
        necessary after removing the potentially existing backup file.
        Raise NSInvalidArgumentExceptions upon failures.  Add support
        for .eomodel files.  Document.
        ([EOModel _verifyBuiltEntityObject:named:]): Add support for
        .eomodel files.


git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gdl2/trunk@19129 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
David Ayers 2004-04-16 13:10:43 +00:00
parent 3403bac575
commit 6d8f87b9a5
2 changed files with 135 additions and 35 deletions

View file

@ -1,3 +1,13 @@
2004-04-16 David Ayers <d.ayers@inode.at>
* EOAccess/EOModel.m ([EOModel writeToFile:]) Set path (and
name implicitly) of the receiver. Create backup file when
necessary after removing the potentially existing backup file.
Raise NSInvalidArgumentExceptions upon failures. Add support
for .eomodel files. Document.
([EOModel _verifyBuiltEntityObject:named:]): Add support for
.eomodel files.
2004-04-15 David Ayers <d.ayers@inode.at>
* EOAdaptors/Postgres95/Makefile.preamble: Add link flags

View file

@ -598,6 +598,20 @@ NSString *EOEntityLoadedNotification = @"EOEntityLoadedNotification";
return self;
}
/**
* Writes the receivers plist representation into an
* .eomodeld file wrapper located at path.
* Depending on the the path extension .eomodeld or .eomodel
* the corresponding format will be used.
* If the path has neither .eomodeld nor .eomodel path
* extension, .eomodeld will be used.
* If the file located at path already exists, a back is created
* by appending a '~' character to file name.
* If a backup file already exists, when trying to create a backup,
* the old backup will be deleted.
* If any of the file operations fail, an NSInvalidArgumentException
* will be raised.
*/
- (void) writeToFile: (NSString *)path
{
NSFileManager *mgr = [NSFileManager defaultManager];
@ -605,10 +619,50 @@ NSString *EOEntityLoadedNotification = @"EOEntityLoadedNotification";
NSDictionary *attributes;
NSDictionary *entityPList;
NSEnumerator *entityEnum;
NSString *fileName;
NSString *extension;
BOOL writeSingleFile;
path = [path stringByStandardizingPath];
path = [[path stringByDeletingPathExtension]
stringByAppendingPathExtension: @"eomodeld"];
extension = [path pathExtension];
if ([extension isEqualToString: @"eomodeld"] == NO
&& [extension isEqualToString: @"eomodel"] == NO)
{
path = [path stringByAppendingPathExtension: @"eomodeld"];
extension = [path pathExtension];
}
writeSingleFile = [extension isEqualToString: @"eomodel"] ? YES : NO;
[self _setPath: path];
if ([mgr fileExistsAtPath: path])
{
NSString *backupPath;
backupPath = [path stringByAppendingString: @"~"];
if ([mgr fileExistsAtPath: backupPath])
{
if ([mgr removeFileAtPath: backupPath handler: nil] == NO)
{
NSString *fmt;
fmt = [NSString stringWithFormat: @"Could not remove %@",
backupPath];
[NSException raise: NSInvalidArgumentException
format: fmt];
}
}
if ([mgr movePath: path toPath: backupPath handler: nil] == NO)
{
NSString *fmt;
fmt = [NSString stringWithFormat: @"Could not move %@ to %@",
path, backupPath];
[NSException raise: NSInvalidArgumentException
format: fmt];
}
}
pList = [NSMutableDictionary dictionaryWithCapacity: 10];
@ -617,28 +671,55 @@ NSString *EOEntityLoadedNotification = @"EOEntityLoadedNotification";
attributes = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithUnsignedLong: 0777], NSFilePosixPermissions,
nil];
[mgr createDirectoryAtPath: path attributes: attributes];
if (writeSingleFile == NO
&& [mgr createDirectoryAtPath: path attributes: attributes] == NO)
{
NSString *fmt;
fmt = [NSString stringWithFormat: @"Could not create directory: %@",
path];
[NSException raise: NSInvalidArgumentException
format: fmt];
}
entityEnum = [[pList objectForKey: @"entities"] objectEnumerator];
while ((entityPList = [entityEnum nextObject]))
while (writeSingleFile == NO
&& (entityPList = [entityEnum nextObject]))
{
NSString *fileName;
fileName = [path stringByAppendingPathComponent:
[NSString stringWithFormat: @"%@.plist",
[entityPList objectForKey: @"name"]]];
[entityPList writeToFile: fileName atomically: YES];
if ([entityPList writeToFile: fileName atomically: YES] == NO)
{
NSString *fmt;
fmt = [NSString stringWithFormat: @"Could not create file: %@",
fileName];
[NSException raise: NSInvalidArgumentException
format: fmt];
}
}
{
NSString *fileName;
if (writeSingleFile == NO)
{
fileName = [path stringByAppendingPathComponent: @"index.eomodeld"];
[pList removeAllObjects];
[self encodeTableOfContentsIntoPropertyList: pList];
}
else
{
fileName = path;
}
fileName = [path stringByAppendingPathComponent: @"index.eomodeld"];
[pList removeAllObjects];
[self encodeTableOfContentsIntoPropertyList: pList];
[pList writeToFile: fileName atomically: YES];
}
if ([pList writeToFile: fileName atomically: YES] == NO)
{
NSString *fmt;
fmt = [NSString stringWithFormat: @"Could not create file: %@",
fileName];
[NSException raise: NSInvalidArgumentException
format: fmt];
}
}
@end
@ -1665,30 +1746,39 @@ NSString *EOEntityLoadedNotification = @"EOEntityLoadedNotification";
EOFLOGObjectLevelArgs(@"gsdb", @"basePath =%@", basePath);
plistPathName = [[basePath stringByAppendingPathComponent: name]
stringByAppendingPathExtension: @"plist"];
if ([basePath hasSuffix: @"eomodel"])
{
propList = entity;
}
else
{
plistPathName = [[basePath stringByAppendingPathComponent: name]
stringByAppendingPathExtension: @"plist"];
EOFLOGObjectLevelArgs(@"gsdb", @"entity plistPathName =%@",
EOFLOGObjectLevelArgs(@"gsdb", @"entity plistPathName =%@",
plistPathName);
propList
= [NSDictionary dictionaryWithContentsOfFile: plistPathName];
EOFLOGObjectLevelArgs(@"gsdb", @"entity propList=%@", propList);
if (!propList)
{
if ([[NSFileManager defaultManager]
fileExistsAtPath: plistPathName])
{
NSAssert1(NO,
@"%@ is not a dictionary or is not readable.",
plistPathName);
propList = [NSDictionary dictionaryWithContentsOfFile: plistPathName];
EOFLOGObjectLevelArgs(@"gsdb", @"entity propList=%@", propList);
if (!propList)
{
if ([[NSFileManager defaultManager]
fileExistsAtPath: plistPathName])
{
NSAssert1(NO, @"%@ is not a dictionary or is not readable.",
plistPathName);
}
else
{
propList = entity;
NSWarnLog(@"%@ doesn't exists. Using %@",
plistPathName, propList);
}
}
}
else
{
propList = entity;
NSWarnLog(@"%@ doesn't exists. Using %@",
plistPathName, propList);
}
}
}
[self _removeEntity: entity];
EOFLOGObjectLevelArgs(@"gsdb", @"entity propList=%@", propList);