file_util_mac.mm 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. #include "base/files/file_util.h"
  5. #import <Foundation/Foundation.h>
  6. #include <copyfile.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include "base/check_op.h"
  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/scoped_blocking_call.h"
  14. namespace base {
  15. bool CopyFile(const FilePath& from_path, const FilePath& to_path) {
  16. ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK);
  17. if (from_path.ReferencesParent() || to_path.ReferencesParent())
  18. return false;
  19. return (copyfile(from_path.value().c_str(),
  20. to_path.value().c_str(), NULL, COPYFILE_DATA) == 0);
  21. }
  22. bool GetTempDir(base::FilePath* path) {
  23. // In order to facilitate hermetic runs on macOS, first check
  24. // MAC_CHROMIUM_TMPDIR. This is used instead of TMPDIR for historical reasons.
  25. // This was originally done for https://crbug.com/698759 (TMPDIR too long for
  26. // process singleton socket path), but is hopefully obsolete as of
  27. // https://crbug.com/1266817 (allows a longer process singleton socket path).
  28. // Continue tracking MAC_CHROMIUM_TMPDIR as that's what build infrastructure
  29. // sets on macOS.
  30. const char* env_tmpdir = getenv("MAC_CHROMIUM_TMPDIR");
  31. if (env_tmpdir) {
  32. *path = base::FilePath(env_tmpdir);
  33. return true;
  34. }
  35. // If we didn't find it, fall back to the native function.
  36. NSString* tmp = NSTemporaryDirectory();
  37. if (tmp == nil)
  38. return false;
  39. *path = base::mac::NSStringToFilePath(tmp);
  40. return true;
  41. }
  42. FilePath GetHomeDir() {
  43. NSString* tmp = NSHomeDirectory();
  44. if (tmp != nil) {
  45. FilePath mac_home_dir = base::mac::NSStringToFilePath(tmp);
  46. if (!mac_home_dir.empty())
  47. return mac_home_dir;
  48. }
  49. // Fall back on temp dir if no home directory is defined.
  50. FilePath rv;
  51. if (GetTempDir(&rv))
  52. return rv;
  53. // Last resort.
  54. return FilePath("/tmp");
  55. }
  56. } // namespace base