[email protected] | b6b7222 | 2012-02-11 02:04:13 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 49df602 | 2008-08-27 19:03:43 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
[email protected] | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 4 | |
[email protected] | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 5 | #include "base/files/file_util.h" |
[email protected] | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 6 | |
iannucci | cedd448 | 2017-03-01 04:44:59 | [diff] [blame] | 7 | #import <Foundation/Foundation.h> |
iannucci | 95fcf47 | 2017-03-01 07:00:32 | [diff] [blame^] | 8 | #include <copyfile.h> |
9 | #include <stdlib.h> | ||||
[email protected] | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 10 | |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 11 | #include "base/files/file_path.h" |
[email protected] | b6b7222 | 2012-02-11 02:04:13 | [diff] [blame] | 12 | #include "base/mac/foundation_util.h" |
[email protected] | 251cd6e5 | 2013-06-11 13:36:37 | [diff] [blame] | 13 | #include "base/strings/string_util.h" |
[email protected] | 3d16983 | 2011-01-02 20:10:05 | [diff] [blame] | 14 | #include "base/threading/thread_restrictions.h" |
[email protected] | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 15 | |
[email protected] | f0ff2ad | 2013-07-09 17:42:26 | [diff] [blame] | 16 | namespace base { |
[email protected] | f0ff2ad | 2013-07-09 17:42:26 | [diff] [blame] | 17 | |
cmumford | 620ace8 | 2014-11-17 18:58:20 | [diff] [blame] | 18 | bool CopyFile(const FilePath& from_path, const FilePath& to_path) { |
[email protected] | f0ff2ad | 2013-07-09 17:42:26 | [diff] [blame] | 19 | ThreadRestrictions::AssertIOAllowed(); |
cmumford | 620ace8 | 2014-11-17 18:58:20 | [diff] [blame] | 20 | if (from_path.ReferencesParent() || to_path.ReferencesParent()) |
21 | return false; | ||||
[email protected] | f0ff2ad | 2013-07-09 17:42:26 | [diff] [blame] | 22 | return (copyfile(from_path.value().c_str(), |
[email protected] | 94b9692 | 2014-02-06 03:42:39 | [diff] [blame] | 23 | to_path.value().c_str(), NULL, COPYFILE_DATA) == 0); |
[email protected] | f0ff2ad | 2013-07-09 17:42:26 | [diff] [blame] | 24 | } |
25 | |||||
[email protected] | aaa6df4 | 2013-02-17 19:36:03 | [diff] [blame] | 26 | bool GetTempDir(base::FilePath* path) { |
iannucci | 95fcf47 | 2017-03-01 07:00:32 | [diff] [blame^] | 27 | // In order to facilitate hermetic runs on macOS, first check $TMPDIR. |
28 | // NOTE: $TMPDIR is ALMOST ALWAYS set on macOS (unless the user un-set it). | ||||
29 | const char* env_tmpdir = getenv("TMPDIR"); | ||||
30 | if (env_tmpdir) { | ||||
31 | *path = base::FilePath(env_tmpdir); | ||||
32 | return true; | ||||
33 | } | ||||
34 | |||||
35 | // If we didn't find it, fall back to the native function. | ||||
[email protected] | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 36 | NSString* tmp = NSTemporaryDirectory(); |
37 | if (tmp == nil) | ||||
38 | return false; | ||||
[email protected] | b6b7222 | 2012-02-11 02:04:13 | [diff] [blame] | 39 | *path = base::mac::NSStringToFilePath(tmp); |
[email protected] | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 40 | return true; |
41 | } | ||||
[email protected] | 49df602 | 2008-08-27 19:03:43 | [diff] [blame] | 42 | |
[email protected] | ffaee18e | 2014-02-19 20:34:23 | [diff] [blame] | 43 | FilePath GetHomeDir() { |
44 | NSString* tmp = NSHomeDirectory(); | ||||
45 | if (tmp != nil) { | ||||
46 | FilePath mac_home_dir = base::mac::NSStringToFilePath(tmp); | ||||
47 | if (!mac_home_dir.empty()) | ||||
48 | return mac_home_dir; | ||||
49 | } | ||||
50 | |||||
51 | // Fall back on temp dir if no home directory is defined. | ||||
52 | FilePath rv; | ||||
53 | if (GetTempDir(&rv)) | ||||
54 | return rv; | ||||
55 | |||||
56 | // Last resort. | ||||
57 | return FilePath("/tmp"); | ||||
58 | } | ||||
59 | |||||
[email protected] | fb4bcfa3 | 2013-12-02 18:55:49 | [diff] [blame] | 60 | } // namespace base |