1/* 2 * Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 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. 16 * 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. 27 */ 28 29#import <WebKit/WebNSFileManagerExtras.h> 30 31#import "WebKitNSStringExtras.h" 32#import "WebNSURLExtras.h" 33#import <wtf/Assertions.h> 34#import <WebKitSystemInterface.h> 35#import <sys/stat.h> 36#import <wtf/ObjcRuntimeExtras.h> 37#import <wtf/RetainPtr.h> 38 39#if __MAC_OS_X_VERSION_MIN_REQUIRED == 1060 40extern "C" DADiskRef DADiskCreateFromVolumePath(CFAllocatorRef allocator, DASessionRef session, CFURLRef path); 41#endif 42 43@implementation NSFileManager (WebNSFileManagerExtras) 44 45 46typedef struct MetaDataInfo 47{ 48 CFStringRef URLString; 49 CFStringRef referrer; 50 CFStringRef path; 51} MetaDataInfo; 52 53static void *setMetaData(void* context) 54{ 55 MetaDataInfo *info = (MetaDataInfo *)context; 56 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 57 WKSetMetadataURL((NSString *)info->URLString, (NSString *)info->referrer, (NSString *)info->path); 58 59 if (info->URLString) 60 CFRelease(info->URLString); 61 if (info->referrer) 62 CFRelease(info->referrer); 63 if (info->path) 64 CFRelease(info->path); 65 66 free(info); 67 [pool drain]; 68 69 return 0; 70} 71 72- (void)_webkit_setMetadataURL:(NSString *)URLString referrer:(NSString *)referrer atPath:(NSString *)path 73{ 74 ASSERT(URLString); 75 ASSERT(path); 76 77 NSURL *URL = [NSURL _web_URLWithUserTypedString:URLString]; 78 if (URL) 79 URLString = [[URL _web_URLByRemovingUserInfo] _web_userVisibleString]; 80 81 // Spawn a background thread for WKSetMetadataURL because this function will not return until mds has 82 // journaled the data we're're trying to set. Depending on what other I/O is going on, it can take some 83 // time. 84 pthread_t tid; 85 pthread_attr_t attr; 86 pthread_attr_init(&attr); 87 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); 88 89 MetaDataInfo *info = static_cast<MetaDataInfo *>(malloc(sizeof(MetaDataInfo))); 90 91 info->URLString = URLString ? CFStringCreateCopy(0, (CFStringRef)URLString) : 0; 92 info->referrer = referrer ? CFStringCreateCopy(0, (CFStringRef)referrer) : 0; 93 info->path = path ? CFStringCreateCopy(0, (CFStringRef)path) : 0; 94 95 pthread_create(&tid, &attr, setMetaData, info); 96 pthread_attr_destroy(&attr); 97} 98 99- (NSString *)_webkit_startupVolumeName 100{ 101 RetainPtr<DASessionRef> session = adoptCF(DASessionCreate(kCFAllocatorDefault)); 102 RetainPtr<DADiskRef> disk = adoptCF(DADiskCreateFromVolumePath(kCFAllocatorDefault, session.get(), (CFURLRef)[NSURL fileURLWithPath:@"/"])); 103 RetainPtr<CFDictionaryRef> diskDescription = adoptCF(DADiskCopyDescription(disk.get())); 104 RetainPtr<NSString> diskName = (NSString *)CFDictionaryGetValue(diskDescription.get(), kDADiskDescriptionVolumeNameKey); 105 return HardAutorelease(diskName.leakRef()); 106} 107 108// -[NSFileManager fileExistsAtPath:] returns NO if there is a broken symlink at the path. 109// So we use this function instead, which returns YES if there is anything there, including 110// a broken symlink. 111static BOOL fileExists(NSString *path) 112{ 113 struct stat statBuffer; 114 return !lstat([path fileSystemRepresentation], &statBuffer); 115} 116 117- (NSString *)_webkit_pathWithUniqueFilenameForPath:(NSString *)path 118{ 119 // "Fix" the filename of the path. 120 NSString *filename = [[path lastPathComponent] _webkit_filenameByFixingIllegalCharacters]; 121 path = [[path stringByDeletingLastPathComponent] stringByAppendingPathComponent:filename]; 122 123 if (fileExists(path)) { 124 // Don't overwrite existing file by appending "-n", "-n.ext" or "-n.ext.ext" to the filename. 125 NSString *extensions = nil; 126 NSString *pathWithoutExtensions; 127 NSString *lastPathComponent = [path lastPathComponent]; 128 NSRange periodRange = [lastPathComponent rangeOfString:@"."]; 129 130 if (periodRange.location == NSNotFound) { 131 pathWithoutExtensions = path; 132 } else { 133 extensions = [lastPathComponent substringFromIndex:periodRange.location + 1]; 134 lastPathComponent = [lastPathComponent substringToIndex:periodRange.location]; 135 pathWithoutExtensions = [[path stringByDeletingLastPathComponent] stringByAppendingPathComponent:lastPathComponent]; 136 } 137 138 for (unsigned i = 1; ; i++) { 139 NSString *pathWithAppendedNumber = [NSString stringWithFormat:@"%@-%d", pathWithoutExtensions, i]; 140 path = [extensions length] ? [pathWithAppendedNumber stringByAppendingPathExtension:extensions] : pathWithAppendedNumber; 141 if (!fileExists(path)) 142 break; 143 } 144 } 145 146 return path; 147} 148 149@end 150 151