Wired Foundation  2.0
A foundation framework for the Wired implementation on Mac OS X
NSFileManager-WIFoundation.m
00001 /* $Id$ */
00002 
00003 /*
00004  *  Copyright (c) 2003-2009 Axel Andersson
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *  1. Redistributions of source code must retain the above copyright
00011  *     notice, this list of conditions and the following disclaimer.
00012  *  2. Redistributions in binary form must reproduce the above copyright
00013  *     notice, this list of conditions and the following disclaimer in the
00014  *     documentation and/or other materials provided with the distribution.
00015  *
00016  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00017  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00018  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
00020  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00021  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00023  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
00024  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00025  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00026  * POSSIBILITY OF SUCH DAMAGE.
00027  */
00028 
00029 #import <WiredFoundation/NSError-WIFoundation.h>
00030 #import <WiredFoundation/NSFileManager-WIFoundation.h>
00031 
00032 @implementation NSFileManager(WIFoundation)
00033 
00034 + (NSString *)temporaryPathWithPrefix:(NSString *)prefix {
00035         return [self temporaryPathWithPrefix:prefix suffix:NULL];
00036 }
00037 
00038 
00039 
00040 + (NSString *)temporaryPathWithPrefix:(NSString *)prefix suffix:(NSString *)suffix {
00041         NSString        *string;
00042         char            *path;
00043         
00044         path = tempnam([NSTemporaryDirectory() UTF8String], [[NSSWF:@"%@_", prefix] UTF8String]);
00045         string = [NSString stringWithUTF8String:path];
00046         free(path);
00047         
00048         return suffix ? [NSSWF:@"%@.%@", string, suffix] : string;
00049 }
00050 
00051 
00052 
00053 + (NSString *)temporaryPathWithFilename:(NSString *)filename {
00054         return [NSTemporaryDirectory() stringByAppendingPathComponent:filename];
00055 }
00056 
00057 
00058 
00059 #pragma mark -
00060 
00061 + (NSString *)resourceForkPathForPath:(NSString *)path {
00062         return [[path stringByAppendingPathComponent:@"..namedfork"] stringByAppendingPathComponent:@"rsrc"];
00063 }
00064 
00065 
00066 
00067 #pragma mark -
00068 
00069 - (BOOL)createDirectoryAtPath:(NSString *)path {
00070     NSError *error = nil;
00071     return [self createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error];
00072 }
00073 
00074 
00075 
00076 - (BOOL)createFileAtPath:(NSString *)path {
00077         return [self createFileAtPath:path contents:NULL attributes:NULL];
00078 }
00079 
00080 
00081 
00082 - (BOOL)directoryExistsAtPath:(NSString *)path {
00083         BOOL    isDirectory;
00084 
00085         return ([self fileExistsAtPath:path isDirectory:&isDirectory] && isDirectory);
00086 }
00087 
00088 
00089 
00090 - (WIFileOffset)fileSizeAtPath:(NSString *)path {
00091         NSDictionary    *attributes;
00092     NSError *error = nil;
00093     
00094     attributes = [self attributesOfItemAtPath:path error:&error];
00095     if(error)
00096         return 0;
00097         
00098         return [attributes fileSize];
00099 }
00100 
00101 
00102 
00103 - (NSString *)ownerAtPath:(NSString *)path {
00104     NSError *error = nil;
00105     NSString *ret = nil;
00106     
00107     ret = [[self attributesOfItemAtPath:path error:&error] fileOwnerAccountName];
00108     if(error)
00109         return nil;
00110     
00111         return ret;
00112 }
00113 
00114 
00115 
00116 - (NSArray *)directoryContentsWithFileAtPath:(NSString *)path {
00117         if(![self directoryExistsAtPath:path])
00118                 return [NSArray arrayWithObject:[path lastPathComponent]];
00119         
00120         return [self directoryContentsWithFileAtPath:path];
00121 }
00122 
00123 
00124 
00125 - (id)enumeratorWithFileAtPath:(NSString *)path {
00126         if(![self directoryExistsAtPath:path])
00127                 return [[NSArray arrayWithObject:[path lastPathComponent]] objectEnumerator];
00128 
00129         return [self enumeratorAtPath:path];
00130 }
00131 
00132 
00133 
00134 - (NSArray *)libraryResourcesForTypes:(NSArray *)types inDirectory:(NSString *)directory {
00135         NSDirectoryEnumerator   *directoryEnumerator;
00136         NSEnumerator                    *enumerator;
00137         NSMutableArray                  *resources;
00138         NSArray                                 *paths;
00139         NSString                                *path;
00140         
00141         resources = [NSMutableArray array];
00142         paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSAllDomainsMask, YES);
00143         enumerator = [paths objectEnumerator];
00144         
00145         while((path = [enumerator nextObject])) {
00146                 path = [path stringByAppendingPathComponent:directory];
00147                 directoryEnumerator = [self enumeratorAtPath:path];
00148                 
00149                 while((path = [directoryEnumerator nextObject])) {
00150                         if([types containsObject:[path pathExtension]])
00151                                 [resources addObject:path];
00152                 }
00153         }
00154 
00155         return resources;
00156 }
00157 
00158 
00159 
00160 #pragma mark -
00161 
00162 - (BOOL)setExtendedAttribute:(NSData *)data forName:(NSString *)name atPath:(NSString *)path error:(NSError **)error {
00163         if(setxattr([path fileSystemRepresentation], [name UTF8String], [data bytes], [data length], 0, 0) < 0) {
00164                 if(error)
00165                         *error = [NSError errorWithDomain:NSPOSIXErrorDomain code:errno];
00166                 
00167                 return NO;
00168         }
00169         
00170         return YES;
00171 }
00172 
00173 
00174 
00175 - (NSData *)extendedAttributeForName:(NSString *)name atPath:(NSString *)path error:(NSError **)error {
00176         void            *value;
00177         ssize_t         size;
00178         
00179         if((size = getxattr([path fileSystemRepresentation], [name UTF8String], NULL, 0, 0, 0)) < 0) {
00180                 if(error)
00181                         *error = [NSError errorWithDomain:NSPOSIXErrorDomain code:errno];
00182                 
00183                 return NULL;
00184         }
00185         
00186         value = malloc(size);
00187         
00188         if(getxattr([path fileSystemRepresentation], [name UTF8String], value, size, 0, 0) < 0) {
00189                 if(error)
00190                         *error = [NSError errorWithDomain:NSPOSIXErrorDomain code:errno];
00191                 
00192                 free(value);
00193                 
00194                 return NULL;
00195         }
00196         
00197         return [NSData dataWithBytesNoCopy:value length:size freeWhenDone:YES];
00198 }
00199 
00200 
00201 
00202 - (BOOL)removeExtendedAttributeForName:(NSString *)name atPath:(NSString *)path error:(NSError **)error {
00203         if(removexattr([path fileSystemRepresentation], [name UTF8String], 0) < 0) {
00204                 if(error)
00205                         *error = [NSError errorWithDomain:NSPOSIXErrorDomain code:errno];
00206                 
00207                 return NO;
00208         }
00209         
00210         return YES;
00211 }
00212 
00213 @end
 All Classes