Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added utilities for handling temporary files.
- Loading branch information
1 parent
62b1b3b
commit b2f3135
Showing
6 changed files
with
438 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* NSFileHandle+TempFile.h | ||
* AQToolkit | ||
* | ||
* Created by Jim Dovey on 10/4/2009. | ||
* | ||
* Copyright (c) 2009 Jim Dovey | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions | ||
* are met: | ||
* | ||
* Redistributions of source code must retain the above copyright notice, | ||
* this list of conditions and the following disclaimer. | ||
* | ||
* Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* | ||
* Neither the name of the project's author nor the names of its | ||
* contributors may be used to endorse or promote products derived from | ||
* this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | ||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED | ||
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | ||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | ||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
* | ||
*/ | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
/*! | ||
@category | ||
@abstract Temporary file support. | ||
@discussion | ||
Routines to create and open a temporary file handle. This uses | ||
mkstemp() to avoid the race condition between test and creation. | ||
*/ | ||
@interface NSFileHandle (AQTempFileSupport) | ||
|
||
/*! | ||
@method | ||
@abstract Returns a path for a new temporary file. | ||
@discussion | ||
The returned path is based on NSTemporaryDirectory()/#{appName}/#{tempName}. | ||
*/ | ||
- (id) initTempFile; | ||
|
||
/*! | ||
@method | ||
@abstract Returns a path for a new temporary file within the given folder. | ||
@discussion | ||
The returned path is based on folderName/#{tempName}. | ||
This routine is this category's 'designated initializer'; the others all | ||
call this method to initialize the returned object. | ||
*/ | ||
- (id) initTempFileInFolder: (NSString *) folderPath; | ||
|
||
/*! | ||
@method | ||
@abstract Create a temporary file inside NSTemporaryDirectory(), inside a given folder. | ||
@param subfolderName The name of the subfolder of NSTemporaryDirectory() in which to create the temp file. | ||
@discussion | ||
The returned path is based on NSTemporaryDirectory()/folderName/#{tempName} | ||
*/ | ||
- (id) initTempFileUnderSubfolder: (NSString *) subfolderName; | ||
|
||
// conveniences for the above | ||
+ (NSFileHandle *) tempFile; | ||
+ (NSFileHandle *) tempFileInFolder: (NSString *) folderPath; | ||
+ (NSFileHandle *) tempFileUnderSubfolder: (NSString *) subfolderName; | ||
|
||
/*! | ||
@method | ||
@abstract Returns the path of a file handle based around a file descriptor. | ||
@discussion | ||
Note that this function can return nil if the receiver was not initialized from | ||
a file on the local filesystem. | ||
*/ | ||
- (NSString *) filePath; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* NSFileHandle+TempFile.m | ||
* AQToolkit | ||
* | ||
* Created by Jim Dovey on 10/4/2009. | ||
* | ||
* Copyright (c) 2009 Jim Dovey | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions | ||
* are met: | ||
* | ||
* Redistributions of source code must retain the above copyright notice, | ||
* this list of conditions and the following disclaimer. | ||
* | ||
* Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* | ||
* Neither the name of the project's author nor the names of its | ||
* contributors may be used to endorse or promote products derived from | ||
* this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | ||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED | ||
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | ||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | ||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
* | ||
*/ | ||
|
||
#import "NSFileHandle+TempFile.h" | ||
#import <sys/fcntl.h> | ||
#import <sys/param.h> | ||
|
||
@implementation NSFileHandle (AQTempFile) | ||
|
||
+ (NSFileHandle *) tempFile | ||
{ | ||
return ( [[[self alloc] initTempFile] autorelease] ); | ||
} | ||
|
||
+ (NSFileHandle *) tempFileInFolder: (NSString *) folderPath | ||
{ | ||
return ( [[[self alloc] initTempFileInFolder: folderPath] autorelease] ); | ||
} | ||
|
||
+ (NSFileHandle *) tempFileUnderSubfolder: (NSString *) folderName | ||
{ | ||
return ( [[[self alloc] initTempFileUnderSubfolder: folderName] autorelease] ); | ||
} | ||
|
||
- (id) initTempFile | ||
{ | ||
return ( [self initTempFileInFolder: [NSTemporaryDirectory() stringByAppendingPathComponent: [[NSProcessInfo processInfo] processName]]] ); | ||
} | ||
|
||
- (id) initTempFileInFolder: (NSString *) folderPath | ||
{ | ||
NSString * path = [folderPath stringByAppendingPathComponent: @"XXXXXXXXXXXX"]; | ||
char cStr[PATH_MAX]; | ||
strlcpy( cStr, [path fileSystemRepresentation], PATH_MAX ); | ||
|
||
int fd = mkstemp( cStr ); | ||
if ( fd == -1 ) | ||
return ( nil ); | ||
|
||
return ( [self initWithFileDescriptor: fd] ); | ||
} | ||
|
||
- (id) initTempFileUnderSubfolder: (NSString *) folderName | ||
{ | ||
return ( [self initTempFileInFolder: [NSTemporaryDirectory() stringByAppendingPathComponent: folderName]] ); | ||
} | ||
|
||
- (NSString *) filePath | ||
{ | ||
int fd = [self fileDescriptor]; | ||
if ( fd == -1 ) | ||
return ( nil ); | ||
|
||
char buf[MAXPATHLEN]; | ||
buf[0] = '\0'; | ||
|
||
int err = fcntl( fd, F_GETPATH, buf ); | ||
if ( err == -1 ) | ||
return ( nil ); | ||
|
||
if ( buf[0] == '\0' ) | ||
return ( nil ); | ||
|
||
return ( [NSString stringWithUTF8String: buf] ); | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* NSFileManager+TempFile.h | ||
* AQToolkit | ||
* | ||
* Created by Jim Dovey on 10/4/2009. | ||
* | ||
* Copyright (c) 2009 Jim Dovey | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions | ||
* are met: | ||
* | ||
* Redistributions of source code must retain the above copyright notice, | ||
* this list of conditions and the following disclaimer. | ||
* | ||
* Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* | ||
* Neither the name of the project's author nor the names of its | ||
* contributors may be used to endorse or promote products derived from | ||
* this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | ||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED | ||
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | ||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | ||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
* | ||
*/ | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
/*! | ||
@category AQTempFileSupport | ||
@abstract Routines to generate temporary file paths. | ||
*/ | ||
@interface NSFileManager (AQTempFileSupport) | ||
|
||
/*! | ||
@method | ||
@abstract Returns a path for a new temporary file. | ||
@discussion | ||
The returned path is based on NSTemporaryDirectory()/#{appName}/#{tempName}. | ||
*/ | ||
- (NSString *) tempFilePath; | ||
|
||
/*! | ||
@method | ||
@abstract Append a temp file name to the given path. | ||
@param folderPath Path to the parent folder of the new temp file. | ||
@discussion | ||
The returned path is based on folderPath/#{tempName}. | ||
*/ | ||
- (NSString *) tempFileInFolder: (NSString *) folderPath; | ||
|
||
/*! | ||
@method | ||
@abstract Create a temporary file inside NSTemporaryDirectory(), inside a given folder. | ||
@param folderName The name of the subfolder of NSTemporaryDirectory() in which to create the temp file. | ||
@discussion | ||
The returned path is based on NSTemporaryDirectory()/folderName/#{tempName} | ||
*/ | ||
- (NSString *) tempFileUnderSubfolder: (NSString *) folderName; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* NSFileManager+TempFile.m | ||
* AQToolkit | ||
* | ||
* Created by Jim Dovey on 10/4/2009. | ||
* | ||
* Copyright (c) 2009 Jim Dovey | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions | ||
* are met: | ||
* | ||
* Redistributions of source code must retain the above copyright notice, | ||
* this list of conditions and the following disclaimer. | ||
* | ||
* Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* | ||
* Neither the name of the project's author nor the names of its | ||
* contributors may be used to endorse or promote products derived from | ||
* this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | ||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED | ||
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | ||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | ||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
* | ||
*/ | ||
|
||
#import "NSFileManager+TempFile.h" | ||
|
||
@implementation NSFileManager (AQTempFileSupport) | ||
|
||
- (NSString *) tempFilePath | ||
{ | ||
// NSTemporaryDirectory() / appName / tempFile | ||
return ( [self tempFileInFolder: [NSTemporaryDirectory() stringByAppendingPathComponent: [[NSProcessInfo processInfo] processName]]] ); | ||
} | ||
|
||
- (NSString *) tempFileInFolder: (NSString *) folderPath | ||
{ | ||
if ( [self createDirectoryAtPath: folderPath withIntermediateDirectories: YES attributes: nil error: NULL] == NO ) | ||
return ( nil ); | ||
|
||
char tempName[13]; | ||
strlcpy( tempName, "XXXXXXXXXXXX", 13 ); | ||
char * fname = mktemp( tempName ); | ||
return ( [folderPath stringByAppendingPathComponent: [NSString stringWithUTF8String: fname]] ); | ||
} | ||
|
||
- (NSString *) tempFileUnderSubfolder: (NSString *) folderName | ||
{ | ||
return ( [self tempFileInFolder: [NSTemporaryDirectory() stringByAppendingPathComponent: folderName]] ); | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* NSString+UUID.h | ||
* AQToolkit | ||
* | ||
* Created by Jim Dovey on 10/4/2009. | ||
* | ||
* Copyright (c) 2009 Jim Dovey | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions | ||
* are met: | ||
* | ||
* Redistributions of source code must retain the above copyright notice, | ||
* this list of conditions and the following disclaimer. | ||
* | ||
* Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* | ||
* Neither the name of the project's author nor the names of its | ||
* contributors may be used to endorse or promote products derived from | ||
* this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | ||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED | ||
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | ||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | ||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
* | ||
*/ | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
/*! | ||
@category AQUUIDSupport | ||
@abstract Simple UUID string generation. | ||
@discussion | ||
All methods in this category are compatible with both garbage collection | ||
and memory management. | ||
*/ | ||
@interface NSString (AQUUIDSupport) | ||
|
||
/*! | ||
@method uuidString | ||
@abstract Returns a string representation of a new UUID. | ||
*/ | ||
+ (NSString *) uuidString; | ||
|
||
@end |
Oops, something went wrong.