2 * Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #import <WebKit/WebNSFileManagerExtras.h>
31 #import "WebKitNSStringExtras.h"
32 #import "WebNSURLExtras.h"
33 #import <JavaScriptCore/Assertions.h>
34 #import <WebKitSystemInterface.h>
37 @implementation NSFileManager (WebNSFileManagerExtras)
39 - (NSString *)_webkit_carbonPathForPath:(NSString *)posixPath
42 FSRef ref, rootRef, parentRef;
44 NSMutableArray *carbonPathPieces;
45 HFSUniStr255 nameString;
48 error = FSPathMakeRef((const UInt8 *)[posixPath fileSystemRepresentation], &ref, NULL);
54 error = FSGetCatalogInfo(&ref, kFSCatInfoVolume, &info, NULL, NULL, NULL);
59 // Get root directory FSRef.
60 error = FSGetVolumeInfo(info.volume, 0, NULL, kFSVolInfoNone, NULL, NULL, &rootRef);
65 // Get the pieces of the path.
66 carbonPathPieces = [NSMutableArray array];
68 error = FSGetCatalogInfo(&ref, kFSCatInfoNone, NULL, &nameString, NULL, &parentRef);
72 [carbonPathPieces insertObject:[NSString stringWithCharacters:nameString.unicode length:nameString.length] atIndex:0];
73 if (FSCompareFSRefs(&ref, &rootRef) == noErr) {
79 // Volume names need trailing : character.
80 if ([carbonPathPieces count] == 1) {
81 [carbonPathPieces addObject:@""];
84 return [carbonPathPieces componentsJoinedByString:@":"];
87 typedef struct MetaDataInfo
89 CFStringRef URLString;
94 static void *setMetaData(void* context)
96 MetaDataInfo *info = (MetaDataInfo *)context;
97 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
98 WKSetMetadataURL((NSString *)info->URLString, (NSString *)info->referrer, (NSString *)info->path);
101 CFRelease(info->URLString);
103 CFRelease(info->referrer);
105 CFRelease(info->path);
113 - (void)_webkit_setMetadataURL:(NSString *)URLString referrer:(NSString *)referrer atPath:(NSString *)path
118 NSURL *URL = [NSURL _web_URLWithUserTypedString:URLString];
120 URLString = [[URL _web_URLByRemovingUserInfo] _web_userVisibleString];
122 // Spawn a background thread for WKSetMetadataURL because this function will not return until mds has
123 // journaled the data we're're trying to set. Depending on what other I/O is going on, it can take some
127 pthread_attr_init(&attr);
128 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
130 MetaDataInfo *info = static_cast<MetaDataInfo *>(malloc(sizeof(MetaDataInfo)));
132 info->URLString = URLString ? CFStringCreateCopy(0, (CFStringRef)URLString) : 0;
133 info->referrer = referrer ? CFStringCreateCopy(0, (CFStringRef)referrer) : 0;
134 info->path = path ? CFStringCreateCopy(0, (CFStringRef)path) : 0;
136 pthread_create(&tid, &attr, setMetaData, info);
137 pthread_attr_destroy(&attr);
140 - (NSString *)_webkit_startupVolumeName
142 NSString *path = [self _webkit_carbonPathForPath:@"/"];
143 return [path substringToIndex:[path length]-1];
146 // -[NSFileManager fileExistsAtPath:] returns NO if there is a broken symlink at the path.
147 // So we use this function instead, which returns YES if there is anything there, including
149 static BOOL fileExists(NSString *path)
151 struct stat statBuffer;
152 return !lstat([path fileSystemRepresentation], &statBuffer);
155 - (NSString *)_webkit_pathWithUniqueFilenameForPath:(NSString *)path
157 // "Fix" the filename of the path.
158 NSString *filename = [[path lastPathComponent] _webkit_filenameByFixingIllegalCharacters];
159 path = [[path stringByDeletingLastPathComponent] stringByAppendingPathComponent:filename];
161 if (fileExists(path)) {
162 // Don't overwrite existing file by appending "-n", "-n.ext" or "-n.ext.ext" to the filename.
163 NSString *extensions = nil;
164 NSString *pathWithoutExtensions;
165 NSString *lastPathComponent = [path lastPathComponent];
166 NSRange periodRange = [lastPathComponent rangeOfString:@"."];
168 if (periodRange.location == NSNotFound) {
169 pathWithoutExtensions = path;
171 extensions = [lastPathComponent substringFromIndex:periodRange.location + 1];
172 lastPathComponent = [lastPathComponent substringToIndex:periodRange.location];
173 pathWithoutExtensions = [[path stringByDeletingLastPathComponent] stringByAppendingPathComponent:lastPathComponent];
176 for (unsigned i = 1; ; i++) {
177 NSString *pathWithAppendedNumber = [NSString stringWithFormat:@"%@-%d", pathWithoutExtensions, i];
178 path = [extensions length] ? [pathWithAppendedNumber stringByAppendingPathExtension:extensions] : pathWithAppendedNumber;
179 if (!fileExists(path))