xpcom/io/CocoaFileUtils.mm

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 // vim:set ts=2 sts=2 sw=2 et cin:
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #include "CocoaFileUtils.h"
michael@0 8 #include "nsCocoaUtils.h"
michael@0 9 #include <Cocoa/Cocoa.h>
michael@0 10 #include "nsObjCExceptions.h"
michael@0 11 #include "nsDebug.h"
michael@0 12
michael@0 13 namespace CocoaFileUtils {
michael@0 14
michael@0 15 nsresult RevealFileInFinder(CFURLRef url)
michael@0 16 {
michael@0 17 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
michael@0 18
michael@0 19 if (NS_WARN_IF(!url))
michael@0 20 return NS_ERROR_INVALID_ARG;
michael@0 21
michael@0 22 NSAutoreleasePool* ap = [[NSAutoreleasePool alloc] init];
michael@0 23 BOOL success = [[NSWorkspace sharedWorkspace] selectFile:[(NSURL*)url path] inFileViewerRootedAtPath:@""];
michael@0 24 [ap release];
michael@0 25
michael@0 26 return (success ? NS_OK : NS_ERROR_FAILURE);
michael@0 27
michael@0 28 NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
michael@0 29 }
michael@0 30
michael@0 31 nsresult OpenURL(CFURLRef url)
michael@0 32 {
michael@0 33 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
michael@0 34
michael@0 35 if (NS_WARN_IF(!url))
michael@0 36 return NS_ERROR_INVALID_ARG;
michael@0 37
michael@0 38 NSAutoreleasePool* ap = [[NSAutoreleasePool alloc] init];
michael@0 39 BOOL success = [[NSWorkspace sharedWorkspace] openURL:(NSURL*)url];
michael@0 40 [ap release];
michael@0 41
michael@0 42 return (success ? NS_OK : NS_ERROR_FAILURE);
michael@0 43
michael@0 44 NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
michael@0 45 }
michael@0 46
michael@0 47 nsresult GetFileCreatorCode(CFURLRef url, OSType *creatorCode)
michael@0 48 {
michael@0 49 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
michael@0 50
michael@0 51 if (NS_WARN_IF(!url) || NS_WARN_IF(!creatorCode))
michael@0 52 return NS_ERROR_INVALID_ARG;
michael@0 53
michael@0 54 nsAutoreleasePool localPool;
michael@0 55
michael@0 56 NSString *resolvedPath = [[(NSURL*)url path] stringByResolvingSymlinksInPath];
michael@0 57 if (!resolvedPath) {
michael@0 58 return NS_ERROR_FAILURE;
michael@0 59 }
michael@0 60
michael@0 61 NSDictionary* dict = [[NSFileManager defaultManager] attributesOfItemAtPath:resolvedPath error:nil];
michael@0 62 if (!dict) {
michael@0 63 return NS_ERROR_FAILURE;
michael@0 64 }
michael@0 65
michael@0 66 NSNumber* creatorNum = (NSNumber*)[dict objectForKey:NSFileHFSCreatorCode];
michael@0 67 if (!creatorNum) {
michael@0 68 return NS_ERROR_FAILURE;
michael@0 69 }
michael@0 70
michael@0 71 *creatorCode = [creatorNum unsignedLongValue];
michael@0 72 return NS_OK;
michael@0 73
michael@0 74 NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
michael@0 75 }
michael@0 76
michael@0 77 nsresult SetFileCreatorCode(CFURLRef url, OSType creatorCode)
michael@0 78 {
michael@0 79 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
michael@0 80
michael@0 81 if (NS_WARN_IF(!url))
michael@0 82 return NS_ERROR_INVALID_ARG;
michael@0 83
michael@0 84 NSAutoreleasePool* ap = [[NSAutoreleasePool alloc] init];
michael@0 85 NSDictionary* dict = [NSDictionary dictionaryWithObject:[NSNumber numberWithUnsignedLong:creatorCode] forKey:NSFileHFSCreatorCode];
michael@0 86 BOOL success = [[NSFileManager defaultManager] setAttributes:dict ofItemAtPath:[(NSURL*)url path] error:nil];
michael@0 87 [ap release];
michael@0 88 return (success ? NS_OK : NS_ERROR_FAILURE);
michael@0 89
michael@0 90 NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
michael@0 91 }
michael@0 92
michael@0 93 nsresult GetFileTypeCode(CFURLRef url, OSType *typeCode)
michael@0 94 {
michael@0 95 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
michael@0 96
michael@0 97 if (NS_WARN_IF(!url) || NS_WARN_IF(!typeCode))
michael@0 98 return NS_ERROR_INVALID_ARG;
michael@0 99
michael@0 100 nsAutoreleasePool localPool;
michael@0 101
michael@0 102 NSString *resolvedPath = [[(NSURL*)url path] stringByResolvingSymlinksInPath];
michael@0 103 if (!resolvedPath) {
michael@0 104 return NS_ERROR_FAILURE;
michael@0 105 }
michael@0 106
michael@0 107 NSDictionary* dict = [[NSFileManager defaultManager] attributesOfItemAtPath:resolvedPath error:nil];
michael@0 108 if (!dict) {
michael@0 109 return NS_ERROR_FAILURE;
michael@0 110 }
michael@0 111
michael@0 112 NSNumber* typeNum = (NSNumber*)[dict objectForKey:NSFileHFSTypeCode];
michael@0 113 if (!typeNum) {
michael@0 114 return NS_ERROR_FAILURE;
michael@0 115 }
michael@0 116
michael@0 117 *typeCode = [typeNum unsignedLongValue];
michael@0 118 return NS_OK;
michael@0 119
michael@0 120 NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
michael@0 121 }
michael@0 122
michael@0 123 nsresult SetFileTypeCode(CFURLRef url, OSType typeCode)
michael@0 124 {
michael@0 125 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
michael@0 126
michael@0 127 if (NS_WARN_IF(!url))
michael@0 128 return NS_ERROR_INVALID_ARG;
michael@0 129
michael@0 130 NSAutoreleasePool* ap = [[NSAutoreleasePool alloc] init];
michael@0 131 NSDictionary* dict = [NSDictionary dictionaryWithObject:[NSNumber numberWithUnsignedLong:typeCode] forKey:NSFileHFSTypeCode];
michael@0 132 BOOL success = [[NSFileManager defaultManager] setAttributes:dict ofItemAtPath:[(NSURL*)url path] error:nil];
michael@0 133 [ap release];
michael@0 134 return (success ? NS_OK : NS_ERROR_FAILURE);
michael@0 135
michael@0 136 NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
michael@0 137 }
michael@0 138
michael@0 139 } // namespace CocoaFileUtils

mercurial