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 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 <WebKitLegacy/WebNSFileManagerExtras.h>
31 #import "WebKitNSStringExtras.h"
32 #import "WebNSURLExtras.h"
33 #import <wtf/Assertions.h>
34 #import <WebKitSystemInterface.h>
36 #import <wtf/ObjcRuntimeExtras.h>
37 #import <wtf/RetainPtr.h>
40 #import <WebCore/FileSystemIOS.h>
43 @implementation NSFileManager (WebNSFileManagerExtras)
47 typedef struct MetaDataInfo
49 CFStringRef URLString;
54 static void *setMetaData(void* context)
56 MetaDataInfo *info = (MetaDataInfo *)context;
57 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
58 WKSetMetadataURL((NSString *)info->URLString, (NSString *)info->referrer, (NSString *)info->path);
61 CFRelease(info->URLString);
63 CFRelease(info->referrer);
65 CFRelease(info->path);
73 - (void)_webkit_setMetadataURL:(NSString *)URLString referrer:(NSString *)referrer atPath:(NSString *)path
78 NSURL *URL = [NSURL _web_URLWithUserTypedString:URLString];
80 URLString = [[URL _web_URLByRemovingUserInfo] _web_userVisibleString];
82 // Spawn a background thread for WKSetMetadataURL because this function will not return until mds has
83 // journaled the data we're're trying to set. Depending on what other I/O is going on, it can take some
87 pthread_attr_init(&attr);
88 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
90 MetaDataInfo *info = static_cast<MetaDataInfo *>(malloc(sizeof(MetaDataInfo)));
92 info->URLString = URLString ? CFStringCreateCopy(0, (CFStringRef)URLString) : 0;
93 info->referrer = referrer ? CFStringCreateCopy(0, (CFStringRef)referrer) : 0;
94 info->path = path ? CFStringCreateCopy(0, (CFStringRef)path) : 0;
96 pthread_create(&tid, &attr, setMetaData, info);
97 pthread_attr_destroy(&attr);
100 - (NSString *)_webkit_startupVolumeName
102 RetainPtr<DASessionRef> session = adoptCF(DASessionCreate(kCFAllocatorDefault));
103 RetainPtr<DADiskRef> disk = adoptCF(DADiskCreateFromVolumePath(kCFAllocatorDefault, session.get(), (CFURLRef)[NSURL fileURLWithPath:@"/"]));
104 return [[(NSString *)CFDictionaryGetValue(adoptCF(DADiskCopyDescription(disk.get())).get(), kDADiskDescriptionVolumeNameKey) copy] autorelease];
107 #endif // !PLATFORM(IOS)
109 // -[NSFileManager fileExistsAtPath:] returns NO if there is a broken symlink at the path.
110 // So we use this function instead, which returns YES if there is anything there, including
112 static BOOL fileExists(NSString *path)
114 struct stat statBuffer;
115 return !lstat([path fileSystemRepresentation], &statBuffer);
118 - (NSString *)_webkit_pathWithUniqueFilenameForPath:(NSString *)path
120 // "Fix" the filename of the path.
121 NSString *filename = [[path lastPathComponent] _webkit_filenameByFixingIllegalCharacters];
122 path = [[path stringByDeletingLastPathComponent] stringByAppendingPathComponent:filename];
124 if (fileExists(path)) {
125 // Don't overwrite existing file by appending "-n", "-n.ext" or "-n.ext.ext" to the filename.
126 NSString *extensions = nil;
127 NSString *pathWithoutExtensions;
128 NSString *lastPathComponent = [path lastPathComponent];
129 NSRange periodRange = [lastPathComponent rangeOfString:@"."];
131 if (periodRange.location == NSNotFound) {
132 pathWithoutExtensions = path;
134 extensions = [lastPathComponent substringFromIndex:periodRange.location + 1];
135 lastPathComponent = [lastPathComponent substringToIndex:periodRange.location];
136 pathWithoutExtensions = [[path stringByDeletingLastPathComponent] stringByAppendingPathComponent:lastPathComponent];
139 for (unsigned i = 1; ; i++) {
140 NSString *pathWithAppendedNumber = [NSString stringWithFormat:@"%@-%d", pathWithoutExtensions, i];
141 path = [extensions length] ? [pathWithAppendedNumber stringByAppendingPathExtension:extensions] : pathWithAppendedNumber;
142 if (!fileExists(path))
151 - (NSString *)_webkit_createTemporaryDirectoryWithTemplatePrefix:(NSString *)prefix
153 return WebCore::createTemporaryDirectory(prefix);