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 3 // found in the LICENSE file. 4 5 #include "base/files/file_util.h" 6 7 #include <copyfile.h> 8 #import <Foundation/Foundation.h> 9 10 #include "base/files/file_path.h" 11 #include "base/mac/foundation_util.h" 12 #include "base/strings/string_util.h" 13 #include "base/threading/thread_restrictions.h" 14 15 namespace base { 16 17 bool CopyFile(const FilePath& from_path, const FilePath& to_path) { 18 ThreadRestrictions::AssertIOAllowed(); 19 if (from_path.ReferencesParent() || to_path.ReferencesParent()) 20 return false; 21 return (copyfile(from_path.value().c_str(), 22 to_path.value().c_str(), NULL, COPYFILE_DATA) == 0); 23 } 24 25 bool GetTempDir(base::FilePath* path) { 26 NSString* tmp = NSTemporaryDirectory(); 27 if (tmp == nil) 28 return false; 29 *path = base::mac::NSStringToFilePath(tmp); 30 return true; 31 } 32 33 FilePath GetHomeDir() { 34 NSString* tmp = NSHomeDirectory(); 35 if (tmp != nil) { 36 FilePath mac_home_dir = base::mac::NSStringToFilePath(tmp); 37 if (!mac_home_dir.empty()) 38 return mac_home_dir; 39 } 40 41 // Fall back on temp dir if no home directory is defined. 42 FilePath rv; 43 if (GetTempDir(&rv)) 44 return rv; 45 46 // Last resort. 47 return FilePath("/tmp"); 48 } 49 50 } // namespace base 51