updaterfileutils_osx.mm 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. #include "updaterfileutils_osx.h"
  6. #include <Cocoa/Cocoa.h>
  7. bool IsRecursivelyWritable(const char* aPath)
  8. {
  9. NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
  10. NSString* rootPath = [NSString stringWithUTF8String:aPath];
  11. NSFileManager* fileManager = [NSFileManager defaultManager];
  12. NSError* error = nil;
  13. NSArray* subPaths =
  14. [fileManager subpathsOfDirectoryAtPath:rootPath
  15. error:&error];
  16. NSMutableArray* paths =
  17. [NSMutableArray arrayWithCapacity:[subPaths count] + 1];
  18. [paths addObject:@""];
  19. [paths addObjectsFromArray:subPaths];
  20. if (error) {
  21. [pool drain];
  22. return false;
  23. }
  24. for (NSString* currPath in paths) {
  25. NSString* child = [rootPath stringByAppendingPathComponent:currPath];
  26. NSDictionary* attributes =
  27. [fileManager attributesOfItemAtPath:child
  28. error:&error];
  29. if (error) {
  30. [pool drain];
  31. return false;
  32. }
  33. // Don't check for writability of files pointed to by symlinks, as they may
  34. // not be descendants of the root path.
  35. if ([attributes fileType] != NSFileTypeSymbolicLink &&
  36. [fileManager isWritableFileAtPath:child] == NO) {
  37. [pool drain];
  38. return false;
  39. }
  40. }
  41. [pool drain];
  42. return true;
  43. }