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 "WebNSObjectExtras.h"
33
#import "WebNSURLExtras.h"
34
#import <wtf/Assertions.h>
35
#import <WebKitSystemInterface.h>
37
#import <wtf/RetainPtr.h>
39
#if __MAC_OS_X_VERSION_MIN_REQUIRED == 1060
40
extern "C" DADiskRef DADiskCreateFromVolumePath(CFAllocatorRef allocator, DASessionRef session, CFURLRef path);
43
@implementation NSFileManager (WebNSFileManagerExtras)
46
typedef struct MetaDataInfo
48
CFStringRef URLString;
53
static void *setMetaData(void* context)
55
MetaDataInfo *info = (MetaDataInfo *)context;
56
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
57
WKSetMetadataURL((NSString *)info->URLString, (NSString *)info->referrer, (NSString *)info->path);
60
CFRelease(info->URLString);
62
CFRelease(info->referrer);
64
CFRelease(info->path);
72
- (void)_webkit_setMetadataURL:(NSString *)URLString referrer:(NSString *)referrer atPath:(NSString *)path
77
NSURL *URL = [NSURL _web_URLWithUserTypedString:URLString];
79
URLString = [[URL _web_URLByRemovingUserInfo] _web_userVisibleString];
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
86
pthread_attr_init(&attr);
87
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
89
MetaDataInfo *info = static_cast<MetaDataInfo *>(malloc(sizeof(MetaDataInfo)));
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;
95
pthread_create(&tid, &attr, setMetaData, info);
96
pthread_attr_destroy(&attr);
99
- (NSString *)_webkit_startupVolumeName
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 WebCFAutorelease(diskName.leakRef());
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
111
static BOOL fileExists(NSString *path)
113
struct stat statBuffer;
114
return !lstat([path fileSystemRepresentation], &statBuffer);
117
- (NSString *)_webkit_pathWithUniqueFilenameForPath:(NSString *)path
119
// "Fix" the filename of the path.
120
NSString *filename = [[path lastPathComponent] _webkit_filenameByFixingIllegalCharacters];
121
path = [[path stringByDeletingLastPathComponent] stringByAppendingPathComponent:filename];
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:@"."];
130
if (periodRange.location == NSNotFound) {
131
pathWithoutExtensions = path;
133
extensions = [lastPathComponent substringFromIndex:periodRange.location + 1];
134
lastPathComponent = [lastPathComponent substringToIndex:periodRange.location];
135
pathWithoutExtensions = [[path stringByDeletingLastPathComponent] stringByAppendingPathComponent:lastPathComponent];
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))