Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(710)

Side by Side Diff: base/files/file_util_mac.mm

Issue 2713293002: Allow $TMPDIR to set temporary directory on OS X. (Closed)
Patch Set: format Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | chrome/browser/file_select_helper_mac.mm » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/files/file_util.h" 5 #include "base/files/file_util.h"
6 6
7 #import <Foundation/Foundation.h>
7 #include <copyfile.h> 8 #include <copyfile.h>
8 #import <Foundation/Foundation.h> 9 #include <stdlib.h>
iannucci 2017/03/01 02:23:19 `git cl format` made me do this
9 10
10 #include "base/files/file_path.h" 11 #include "base/files/file_path.h"
11 #include "base/mac/foundation_util.h" 12 #include "base/mac/foundation_util.h"
12 #include "base/strings/string_util.h" 13 #include "base/strings/string_util.h"
13 #include "base/threading/thread_restrictions.h" 14 #include "base/threading/thread_restrictions.h"
14 15
15 namespace base { 16 namespace base {
16 17
17 bool CopyFile(const FilePath& from_path, const FilePath& to_path) { 18 bool CopyFile(const FilePath& from_path, const FilePath& to_path) {
18 ThreadRestrictions::AssertIOAllowed(); 19 ThreadRestrictions::AssertIOAllowed();
19 if (from_path.ReferencesParent() || to_path.ReferencesParent()) 20 if (from_path.ReferencesParent() || to_path.ReferencesParent())
20 return false; 21 return false;
21 return (copyfile(from_path.value().c_str(), 22 return (copyfile(from_path.value().c_str(),
22 to_path.value().c_str(), NULL, COPYFILE_DATA) == 0); 23 to_path.value().c_str(), NULL, COPYFILE_DATA) == 0);
23 } 24 }
24 25
25 bool GetTempDir(base::FilePath* path) { 26 bool GetTempDir(base::FilePath* path) {
27 // In order to facilitate hermetic runs on OS X, 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.
26 NSString* tmp = NSTemporaryDirectory(); 36 NSString* tmp = NSTemporaryDirectory();
27 if (tmp == nil) 37 if (tmp == nil)
28 return false; 38 return false;
29 *path = base::mac::NSStringToFilePath(tmp); 39 *path = base::mac::NSStringToFilePath(tmp);
30 return true; 40 return true;
31 } 41 }
32 42
33 FilePath GetHomeDir() { 43 FilePath GetHomeDir() {
34 NSString* tmp = NSHomeDirectory(); 44 NSString* tmp = NSHomeDirectory();
35 if (tmp != nil) { 45 if (tmp != nil) {
36 FilePath mac_home_dir = base::mac::NSStringToFilePath(tmp); 46 FilePath mac_home_dir = base::mac::NSStringToFilePath(tmp);
37 if (!mac_home_dir.empty()) 47 if (!mac_home_dir.empty())
38 return mac_home_dir; 48 return mac_home_dir;
39 } 49 }
40 50
41 // Fall back on temp dir if no home directory is defined. 51 // Fall back on temp dir if no home directory is defined.
42 FilePath rv; 52 FilePath rv;
43 if (GetTempDir(&rv)) 53 if (GetTempDir(&rv))
44 return rv; 54 return rv;
45 55
46 // Last resort. 56 // Last resort.
47 return FilePath("/tmp"); 57 return FilePath("/tmp");
48 } 58 }
49 59
50 } // namespace base 60 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/file_select_helper_mac.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698