| 7 | #import <Foundation/Foundation.h> |
| 8 | #include <copyfile.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <string.h> |
| 11 |
| 12 | #include "base/files/file_path.h" |
| 13 | #include "base/logging.h" |
| 14 | #include "base/mac/foundation_util.h" |
| 15 | #include "base/strings/string_util.h" |
| 16 | #include "base/threading/scoped_blocking_call.h" |
| 17 |
| 18 | namespace base { |
| 19 |
| 20 | bool CopyFile(const FilePath& from_path, const FilePath& to_path) { |
| 21 | ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK); |
| 22 | if (from_path.ReferencesParent() || to_path.ReferencesParent()) |
| 23 | return false; |
| 24 | return (copyfile(from_path.value().c_str(), |
| 25 | to_path.value().c_str(), NULL, COPYFILE_DATA) == 0); |
| 26 | } |
| 27 |
| 28 | bool GetTempDir(base::FilePath* path) { |
| 29 | // In order to facilitate hermetic runs on macOS, first check |
| 30 | // $MAC_CHROMIUM_TMPDIR. We check this instead of $TMPDIR because external |
| 31 | // programs currently set $TMPDIR with no effect, but when we respect it |
| 32 | // directly it can cause crashes (like crbug.com/698759). |
| 33 | const char* env_tmpdir = getenv("MAC_CHROMIUM_TMPDIR"); |
| 34 | if (env_tmpdir) { |
| 35 | DCHECK_LT(strlen(env_tmpdir), 50u) |
| 36 | << "too-long TMPDIR causes socket name length issues."; |
| 37 | *path = base::FilePath(env_tmpdir); |
| 38 | return true; |
| 39 | } |
| 40 |
| 41 | // If we didn't find it, fall back to the native function. |
| 42 | NSString* tmp = NSTemporaryDirectory(); |
| 43 | if (tmp == nil) |
| 44 | return false; |
| 45 | *path = base::mac::NSStringToFilePath(tmp); |
| 46 | return true; |
| 47 | } |
| 48 |
| 49 | FilePath GetHomeDir() { |
| 50 | NSString* tmp = NSHomeDirectory(); |
| 51 | if (tmp != nil) { |
| 52 | FilePath mac_home_dir = base::mac::NSStringToFilePath(tmp); |
| 53 | if (!mac_home_dir.empty()) |
| 54 | return mac_home_dir; |
| 55 | } |
| 56 |
| 57 | // Fall back on temp dir if no home directory is defined. |
| 58 | FilePath rv; |
| 59 | if (GetTempDir(&rv)) |
| 60 | return rv; |
| 61 |
| 62 | // Last resort. |
| 63 | return FilePath("/tmp"); |
| 64 | } |
| 65 |
| 66 | } // namespace base |
| 67 |