Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/files/file_util.h" | 5 #include "base/files/file_util.h" |
| 6 | 6 |
| 7 #include <copyfile.h> | 7 #include <copyfile.h> |
| 8 #import <Foundation/Foundation.h> | 8 #import <Foundation/Foundation.h> |
| 9 | 9 |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 11 #include "base/mac/foundation_util.h" | 11 #include "base/mac/foundation_util.h" |
| 12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 13 #include "base/threading/thread_restrictions.h" | 13 #include "base/threading/thread_restrictions.h" |
| 14 | 14 |
| 15 namespace base { | 15 namespace base { |
| 16 | 16 |
| 17 bool CopyFile(const FilePath& from_path, const FilePath& to_path) { | 17 bool CopyFile(const FilePath& from_path, const FilePath& to_path) { |
| 18 ThreadRestrictions::AssertIOAllowed(); | 18 ThreadRestrictions::AssertIOAllowed(); |
| 19 if (from_path.ReferencesParent() || to_path.ReferencesParent()) | 19 if (from_path.ReferencesParent() || to_path.ReferencesParent()) |
| 20 return false; | 20 return false; |
| 21 return (copyfile(from_path.value().c_str(), | 21 return (copyfile(from_path.value().c_str(), |
| 22 to_path.value().c_str(), NULL, COPYFILE_DATA) == 0); | 22 to_path.value().c_str(), NULL, COPYFILE_DATA) == 0); |
| 23 } | 23 } |
| 24 | 24 |
| 25 bool GetTempDir(base::FilePath* path) { | 25 bool GetTempDir(base::FilePath* path) { |
| 26 NSString* tmp = NSTemporaryDirectory(); | 26 // In order to facilitate hermetic runs on OS X, first check $TMPDIR |
|
Mark Mentovai
2017/02/28 21:59:21
“first check” makes it sound like TMPDIR might not
iannucci
2017/03/01 02:22:21
Actually now that I've thought about it a bit more
| |
| 27 NSProcessInfo* procInfo = [NSProcessInfo processInfo]; | |
|
Mark Mentovai
2017/02/28 21:59:20
Meh. Just do
#include <stdlib.h>
const char* e
iannucci
2017/03/01 02:22:21
That was actually my first instinct, but I wasn't
| |
| 28 NSString* tmp = [[procInfo environment] objectForKey: @"TMPDIR"]; | |
| 29 if (tmp == nil) { | |
| 30 // Then fallback to the immutable NSTemporaryDirectory value. | |
|
Nico
2017/02/27 01:16:20
nit: "fallback" is a noun, "fall back" is the verb
Mark Mentovai
2017/02/28 21:59:20
Fix this :)
iannucci
2017/03/01 02:22:21
Done.
| |
| 31 tmp = NSTemporaryDirectory(); | |
| 32 } | |
| 27 if (tmp == nil) | 33 if (tmp == nil) |
| 28 return false; | 34 return false; |
| 29 *path = base::mac::NSStringToFilePath(tmp); | 35 *path = base::mac::NSStringToFilePath(tmp); |
| 30 return true; | 36 return true; |
| 31 } | 37 } |
| 32 | 38 |
| 33 FilePath GetHomeDir() { | 39 FilePath GetHomeDir() { |
| 34 NSString* tmp = NSHomeDirectory(); | 40 NSString* tmp = NSHomeDirectory(); |
| 35 if (tmp != nil) { | 41 if (tmp != nil) { |
| 36 FilePath mac_home_dir = base::mac::NSStringToFilePath(tmp); | 42 FilePath mac_home_dir = base::mac::NSStringToFilePath(tmp); |
| 37 if (!mac_home_dir.empty()) | 43 if (!mac_home_dir.empty()) |
| 38 return mac_home_dir; | 44 return mac_home_dir; |
| 39 } | 45 } |
| 40 | 46 |
| 41 // Fall back on temp dir if no home directory is defined. | 47 // Fall back on temp dir if no home directory is defined. |
| 42 FilePath rv; | 48 FilePath rv; |
| 43 if (GetTempDir(&rv)) | 49 if (GetTempDir(&rv)) |
| 44 return rv; | 50 return rv; |
| 45 | 51 |
| 46 // Last resort. | 52 // Last resort. |
| 47 return FilePath("/tmp"); | 53 return FilePath("/tmp"); |
| 48 } | 54 } |
| 49 | 55 |
| 50 } // namespace base | 56 } // namespace base |
| OLD | NEW |