1/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2/* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6#include "updaterfileutils_osx.h"
7
8#include <Cocoa/Cocoa.h>
9
10bool IsRecursivelyWritable(const char* aPath) {
11  NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
12
13  NSString* rootPath = [NSString stringWithUTF8String:aPath];
14  NSFileManager* fileManager = [NSFileManager defaultManager];
15  NSError* error = nil;
16  NSArray* subPaths = [fileManager subpathsOfDirectoryAtPath:rootPath error:&error];
17  NSMutableArray* paths = [NSMutableArray arrayWithCapacity:[subPaths count] + 1];
18  [paths addObject:@""];
19  [paths addObjectsFromArray:subPaths];
20
21  if (error) {
22    [pool drain];
23    return false;
24  }
25
26  for (NSString* currPath in paths) {
27    NSString* child = [rootPath stringByAppendingPathComponent:currPath];
28
29    NSDictionary* attributes = [fileManager attributesOfItemAtPath:child error:&error];
30    if (error) {
31      [pool drain];
32      return false;
33    }
34
35    // Don't check for writability of files pointed to by symlinks, as they may
36    // not be descendants of the root path.
37    if ([attributes fileType] != NSFileTypeSymbolicLink &&
38        [fileManager isWritableFileAtPath:child] == NO) {
39      [pool drain];
40      return false;
41    }
42  }
43
44  [pool drain];
45  return true;
46}
47