file_util_mac.mm 1.3 KB
Newer Older
gejun's avatar
gejun committed
1 2 3 4
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
#include "butil/file_util.h"
gejun's avatar
gejun committed
6 7 8 9

#import <Foundation/Foundation.h>
#include <copyfile.h>

10 11 12 13 14
#include "butil/basictypes.h"
#include "butil/files/file_path.h"
#include "butil/mac/foundation_util.h"
#include "butil/strings/string_util.h"
#include "butil/threading/thread_restrictions.h"
gejun's avatar
gejun committed
15

16
namespace butil {
gejun's avatar
gejun committed
17 18 19 20 21 22 23 24 25 26
namespace internal {

bool CopyFileUnsafe(const FilePath& from_path, const FilePath& to_path) {
  ThreadRestrictions::AssertIOAllowed();
  return (copyfile(from_path.value().c_str(),
                   to_path.value().c_str(), NULL, COPYFILE_DATA) == 0);
}

}  // namespace internal

27
bool GetTempDir(butil::FilePath* path) {
gejun's avatar
gejun committed
28 29 30
  NSString* tmp = NSTemporaryDirectory();
  if (tmp == nil)
    return false;
31
  *path = butil::mac::NSStringToFilePath(tmp);
gejun's avatar
gejun committed
32 33 34 35 36 37
  return true;
}

FilePath GetHomeDir() {
  NSString* tmp = NSHomeDirectory();
  if (tmp != nil) {
38
    FilePath mac_home_dir = butil::mac::NSStringToFilePath(tmp);
gejun's avatar
gejun committed
39 40 41 42 43 44 45 46 47 48 49 50 51
    if (!mac_home_dir.empty())
      return mac_home_dir;
  }

  // Fall back on temp dir if no home directory is defined.
  FilePath rv;
  if (GetTempDir(&rv))
    return rv;

  // Last resort.
  return FilePath("/tmp");
}

52
}  // namespace butil