[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> | ||||
iannucci | 16abc4a | 2017-04-03 20:04:32 | [diff] [blame] | 10 | #include <string.h> |
[email protected] | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 11 | |
Hans Wennborg | a47ddf8 | 2020-05-05 18:08:07 | [diff] [blame] | 12 | #include "base/check_op.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 13 | #include "base/files/file_path.h" |
[email protected] | b6b7222 | 2012-02-11 02:04:13 | [diff] [blame] | 14 | #include "base/mac/foundation_util.h" |
[email protected] | 251cd6e5 | 2013-06-11 13:36:37 | [diff] [blame] | 15 | #include "base/strings/string_util.h" |
Etienne Pierre-Doray | 3879b05 | 2018-09-17 14:17:22 | [diff] [blame] | 16 | #include "base/threading/scoped_blocking_call.h" |
[email protected] | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 17 | |
[email protected] | f0ff2ad | 2013-07-09 17:42:26 | [diff] [blame] | 18 | namespace base { |
[email protected] | f0ff2ad | 2013-07-09 17:42:26 | [diff] [blame] | 19 | |
cmumford | 620ace8 | 2014-11-17 18:58:20 | [diff] [blame] | 20 | bool CopyFile(const FilePath& from_path, const FilePath& to_path) { |
Etienne Bergeron | 436d4221 | 2019-02-26 17:15:12 | [diff] [blame] | 21 | ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK); |
cmumford | 620ace8 | 2014-11-17 18:58:20 | [diff] [blame] | 22 | if (from_path.ReferencesParent() || to_path.ReferencesParent()) |
23 | return false; | ||||
[email protected] | f0ff2ad | 2013-07-09 17:42:26 | [diff] [blame] | 24 | return (copyfile(from_path.value().c_str(), |
[email protected] | 94b9692 | 2014-02-06 03:42:39 | [diff] [blame] | 25 | to_path.value().c_str(), NULL, COPYFILE_DATA) == 0); |
[email protected] | f0ff2ad | 2013-07-09 17:42:26 | [diff] [blame] | 26 | } |
27 | |||||
[email protected] | aaa6df4 | 2013-02-17 19:36:03 | [diff] [blame] | 28 | bool GetTempDir(base::FilePath* path) { |
iannucci | 16abc4a | 2017-04-03 20:04:32 | [diff] [blame] | 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"); | ||||
iannucci | 95fcf47 | 2017-03-01 07:00:32 | [diff] [blame] | 34 | if (env_tmpdir) { |
iannucci | 16abc4a | 2017-04-03 20:04:32 | [diff] [blame] | 35 | DCHECK_LT(strlen(env_tmpdir), 50u) |
36 | << "too-long TMPDIR causes socket name length issues."; | ||||
iannucci | 95fcf47 | 2017-03-01 07:00:32 | [diff] [blame] | 37 | *path = base::FilePath(env_tmpdir); |
38 | return true; | ||||
39 | } | ||||
40 | |||||
41 | // If we didn't find it, fall back to the native function. | ||||
[email protected] | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 42 | NSString* tmp = NSTemporaryDirectory(); |
43 | if (tmp == nil) | ||||
44 | return false; | ||||
[email protected] | b6b7222 | 2012-02-11 02:04:13 | [diff] [blame] | 45 | *path = base::mac::NSStringToFilePath(tmp); |
[email protected] | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 46 | return true; |
47 | } | ||||
[email protected] | 49df602 | 2008-08-27 19:03:43 | [diff] [blame] | 48 | |
[email protected] | ffaee18e | 2014-02-19 20:34:23 | [diff] [blame] | 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 | |||||
[email protected] | fb4bcfa3 | 2013-12-02 18:55:49 | [diff] [blame] | 66 | } // namespace base |