4 Copyright (C) 2000 MDlink GmbH, Helge Hess
7 Author: Helge Hess <helge.hess@mdlink.de>
9 This file is part of libFoundation.
11 Permission to use, copy, modify, and distribute this software and its
12 documentation for any purpose and without fee is hereby granted, provided
13 that the above copyright notice appear in all copies and that both that
14 copyright notice and this permission notice appear in supporting
17 We disclaim all warranties with regard to this software, including all
18 implied warranties of merchantability and fitness, in no event shall
19 we be liable for any special, indirect or consequential damages or any
20 damages whatsoever resulting from loss of use, data or profits, whether in
21 an action of contract, negligence or other tortious action, arising out of
22 or in connection with the use or performance of this software.
25 #include "NSFileURLHandle.h"
26 #include <Foundation/NSURL.h>
27 #include <Foundation/NSFileManager.h>
28 #include <Foundation/NSData.h>
29 #include <Foundation/NSException.h>
31 @implementation NSFileURLHandle
33 + (BOOL)canInitWithURL:(NSURL *)_url
35 return [_url isFileURL];
38 - (id)initWithURL:(NSURL *)_url cached:(BOOL)_flag
40 NSAssert([_url isFileURL],
41 @"file handle can only load 'file' URLs (%@)", _url);
42 self->path = [[_url path] copy];
43 self->cacheData = _flag;
57 - (NSString *)filePath
61 - (NSFileManager *)fileManager
63 return [NSFileManager defaultManager];
68 - (NSData *)loadInForeground
72 RELEASE(self->data); self->data = nil;
74 ldata = [NSData dataWithContentsOfFile:[self filePath]];
76 self->status = (ldata)
77 ? NSURLHandleLoadSucceeded
78 : NSURLHandleLoadFailed;
81 self->data = RETAIN(ldata);
85 - (NSURLHandleStatus)status
92 - (NSData *)resourceData
94 if (self->cacheData && (self->data != nil))
97 return [self loadInForeground];
99 - (void)flushCachedData
101 self->status = NSURLHandleNotLoaded;
102 RELEASE(self->props); self->props = nil;
103 RELEASE(self->data); self->data = nil;
108 - (BOOL)writeData:(NSData *)_data
113 return [_data writeToFile:[self filePath] atomically:YES];
118 - (id)propertyForKey:(NSString *)_propertyKey
120 if (self->props == nil) {
121 self->props = [[[self fileManager]
122 fileAttributesAtPath:[self filePath]
126 return [self->props objectForKey:_propertyKey];
129 - (id)propertyForKeyIfAvailable:(NSString *)_propertyKey
131 return [self->props objectForKey:_propertyKey];
134 - (BOOL)writeProperty:(id)_propValue forKey:(NSString *)_propertyKey
138 if ((_propertyKey == nil) || (_propValue == nil))
141 RELEASE(self->props); self->props = nil;
143 attrs = [NSDictionary dictionaryWithObject:_propValue forKey:_propertyKey];
145 return [[self fileManager] changeFileAttributes:attrs
146 atPath:[self filePath]];
149 @end /* NSFileURLHandle */