blob: 5a99aa0e81de2e19110872cc168e53b93555d0c5 [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>
[email protected]5af2edb92008-08-08 20:16:0810
[email protected]57999812013-02-24 05:40:5211#include "base/files/file_path.h"
[email protected]b6b72222012-02-11 02:04:1312#include "base/mac/foundation_util.h"
[email protected]251cd6e52013-06-11 13:36:3713#include "base/strings/string_util.h"
[email protected]3d169832011-01-02 20:10:0514#include "base/threading/thread_restrictions.h"
[email protected]5af2edb92008-08-08 20:16:0815
[email protected]f0ff2ad2013-07-09 17:42:2616namespace base {
[email protected]f0ff2ad2013-07-09 17:42:2617
cmumford620ace82014-11-17 18:58:2018bool CopyFile(const FilePath& from_path, const FilePath& to_path) {
[email protected]f0ff2ad2013-07-09 17:42:2619 ThreadRestrictions::AssertIOAllowed();
cmumford620ace82014-11-17 18:58:2020 if (from_path.ReferencesParent() || to_path.ReferencesParent())
21 return false;
[email protected]f0ff2ad2013-07-09 17:42:2622 return (copyfile(from_path.value().c_str(),
[email protected]94b96922014-02-06 03:42:3923 to_path.value().c_str(), NULL, COPYFILE_DATA) == 0);
[email protected]f0ff2ad2013-07-09 17:42:2624}
25
[email protected]aaa6df42013-02-17 19:36:0326bool GetTempDir(base::FilePath* path) {
iannucci95fcf472017-03-01 07:00:3227 // 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]5af2edb92008-08-08 20:16:0836 NSString* tmp = NSTemporaryDirectory();
37 if (tmp == nil)
38 return false;
[email protected]b6b72222012-02-11 02:04:1339 *path = base::mac::NSStringToFilePath(tmp);
[email protected]5af2edb92008-08-08 20:16:0840 return true;
41}
[email protected]49df6022008-08-27 19:03:4342
[email protected]ffaee18e2014-02-19 20:34:2343FilePath 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]fb4bcfa32013-12-02 18:55:4960} // namespace base