blob: e53732a22ad11494df14b884defe623546b74fbc [file] [log] [blame]
[email protected]b6b72222012-02-11 02:04:131// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]49df6022008-08-27 19:03:432// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
[email protected]5af2edb92008-08-08 20:16:084
[email protected]e3177dd52014-08-13 20:22:145#include "base/files/file_util.h"
[email protected]5af2edb92008-08-08 20:16:086
iannuccicedd4482017-03-01 04:44:597#import <Foundation/Foundation.h>
iannucci95fcf472017-03-01 07:00:328#include <copyfile.h>
9#include <stdlib.h>
iannucci16abc4a2017-04-03 20:04:3210#include <string.h>
[email protected]5af2edb92008-08-08 20:16:0811
Hans Wennborga47ddf82020-05-05 18:08:0712#include "base/check_op.h"
[email protected]57999812013-02-24 05:40:5213#include "base/files/file_path.h"
[email protected]b6b72222012-02-11 02:04:1314#include "base/mac/foundation_util.h"
[email protected]251cd6e52013-06-11 13:36:3715#include "base/strings/string_util.h"
Etienne Pierre-Doray3879b052018-09-17 14:17:2216#include "base/threading/scoped_blocking_call.h"
[email protected]5af2edb92008-08-08 20:16:0817
[email protected]f0ff2ad2013-07-09 17:42:2618namespace base {
[email protected]f0ff2ad2013-07-09 17:42:2619
cmumford620ace82014-11-17 18:58:2020bool CopyFile(const FilePath& from_path, const FilePath& to_path) {
Etienne Bergeron436d42212019-02-26 17:15:1221 ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK);
cmumford620ace82014-11-17 18:58:2022 if (from_path.ReferencesParent() || to_path.ReferencesParent())
23 return false;
[email protected]f0ff2ad2013-07-09 17:42:2624 return (copyfile(from_path.value().c_str(),
[email protected]94b96922014-02-06 03:42:3925 to_path.value().c_str(), NULL, COPYFILE_DATA) == 0);
[email protected]f0ff2ad2013-07-09 17:42:2626}
27
[email protected]aaa6df42013-02-17 19:36:0328bool GetTempDir(base::FilePath* path) {
iannucci16abc4a2017-04-03 20:04:3229 // 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");
iannucci95fcf472017-03-01 07:00:3234 if (env_tmpdir) {
iannucci16abc4a2017-04-03 20:04:3235 DCHECK_LT(strlen(env_tmpdir), 50u)
36 << "too-long TMPDIR causes socket name length issues.";
iannucci95fcf472017-03-01 07:00:3237 *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]5af2edb92008-08-08 20:16:0842 NSString* tmp = NSTemporaryDirectory();
43 if (tmp == nil)
44 return false;
[email protected]b6b72222012-02-11 02:04:1345 *path = base::mac::NSStringToFilePath(tmp);
[email protected]5af2edb92008-08-08 20:16:0846 return true;
47}
[email protected]49df6022008-08-27 19:03:4348
[email protected]ffaee18e2014-02-19 20:34:2349FilePath 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]fb4bcfa32013-12-02 18:55:4966} // namespace base