WebNSFileManagerExtras.mm 6.1 KB

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