1 // Copyright 2007, Google Inc.
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are met:
6 // 1. Redistributions of source code must retain the above copyright notice,
7 // this list of conditions and the following disclaimer.
8 // 2. Redistributions in binary form must reproduce the above copyright notice,
9 // this list of conditions and the following disclaimer in the documentation
10 // and/or other materials provided with the distribution.
11 // 3. Neither the name of Google Inc. nor the names of its contributors may be
12 // used to endorse or promote products derived from this software without
13 // specific prior written permission.
15 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
16 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
17 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
18 // EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #import "common/genfiles/product_name_constants.h" // from OUTDIR
27 #import "gears/base/safari/scoped_cf.h"
28 #import "gears/base/common/string_utils.h"
29 #import "gears/base/common/paths_sf_more.h"
31 @implementation GearsPathUtilities
32 //------------------------------------------------------------------------------
33 + (BOOL)ensureDirectoryPathExists:(NSString *)dirPath {
34 NSFileManager *mgr = [NSFileManager defaultManager];
37 // Standardize the path
38 dirPath = [dirPath stringByStandardizingPath];
40 // If we got a relative path, prepend the current directory
41 if (![dirPath isAbsolutePath]) {
42 NSString *currentPath = [mgr currentDirectoryPath];
43 dirPath = [currentPath stringByAppendingPathComponent:dirPath];
46 NSString *path = dirPath;
48 // Ensure that no file exists within the path which would block creation
50 if ([mgr fileExistsAtPath:path isDirectory:&isDir]) {
54 MethodLog("File exists at %@", path);
58 NSString *parentPath = [path stringByDeletingLastPathComponent];
60 if (([parentPath length] == [path length]) || (![parentPath length]))
66 // Path now contains the first valid directory (or is empty)
70 // If dirPath already exists, we can exit now
71 if ([path isEqualToString:dirPath])
74 // Break up the difference into components
75 NSString *diff = [dirPath substringFromIndex:[path length] + 1];
76 NSArray *components = [diff pathComponents];
77 unsigned count = [components count];
78 NSDictionary *attrs = [NSDictionary dictionaryWithObject:
79 [NSNumber numberWithUnsignedLong:0700] forKey:NSFilePosixPermissions];
81 // Rebuild the path one component at a time
82 for (unsigned i = 0; i < count; ++i) {
83 NSString *subPath = [components objectAtIndex:i];
85 // We can skip trailing "/"
86 if ([subPath isEqualToString:@"/"])
89 path = [path stringByAppendingPathComponent:subPath];
91 if (![mgr createDirectoryAtPath:path attributes:attrs]) {
92 MethodLog("Unable to create directory: %@", path);
100 //------------------------------------------------------------------------------
101 + (NSString *)pathForFolder:(OSType)folderType domain:(short)domain
102 create:(BOOL)create {
106 if (FSFindFolder(domain, folderType, create, &fileRef) == noErr) {
107 CFURLRef url = CFURLCreateFromFSRef(NULL, &fileRef);
110 dir = (NSString *)CFURLCopyFileSystemPath(url, kCFURLPOSIXPathStyle);
119 //------------------------------------------------------------------------------
120 + (NSString *)gearsComponentsDirectory {
121 NSBundle *pluginBundle = [NSBundle bundleForClass:[self class]];
123 return [pluginBundle resourcePath];
126 //------------------------------------------------------------------------------
127 + (NSString *)gearsDataDirectory {
129 [GearsPathUtilities pathForFolder:kApplicationSupportFolderType
130 domain:kUserDomain create:YES];
131 NSString *subdir = [NSString stringWithFormat:@"Google/%s for Safari",
132 PRODUCT_FRIENDLY_NAME_ASCII];
134 return [appDir stringByAppendingPathComponent:subdir];
137 //------------------------------------------------------------------------------
138 + (NSString *)gearsUniqueTempPath {
139 NSString *tempDir = NSTemporaryDirectory();
140 scoped_CFUUID uuid(CFUUIDCreate(NULL));
141 NSString *uuidStr = (NSString *)CFUUIDCreateString(NULL, uuid.get());
144 return [tempDir stringByAppendingPathComponent:uuidStr];