OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/file_util.h" | 5 #include "base/file_util.h" |
6 | 6 |
| 7 #include <CoreServices/CoreServices.h> |
7 #import <Foundation/Foundation.h> | 8 #import <Foundation/Foundation.h> |
8 #include <copyfile.h> | 9 #include <copyfile.h> |
9 | 10 |
10 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
11 #include "base/file_path.h" | 12 #include "base/file_path.h" |
| 13 #include "base/logging.h" |
| 14 #include "base/mac/mac_util.h" |
12 #include "base/string_util.h" | 15 #include "base/string_util.h" |
13 #include "base/threading/thread_restrictions.h" | 16 #include "base/threading/thread_restrictions.h" |
14 | 17 |
15 namespace file_util { | 18 namespace file_util { |
16 | 19 |
17 bool GetTempDir(FilePath* path) { | 20 bool GetTempDir(FilePath* path) { |
18 NSString* tmp = NSTemporaryDirectory(); | 21 NSString* tmp = NSTemporaryDirectory(); |
19 if (tmp == nil) | 22 if (tmp == nil) |
20 return false; | 23 return false; |
21 *path = FilePath([tmp fileSystemRepresentation]); | 24 *path = FilePath([tmp fileSystemRepresentation]); |
22 return true; | 25 return true; |
23 } | 26 } |
24 | 27 |
25 bool GetShmemTempDir(FilePath* path) { | 28 bool GetShmemTempDir(FilePath* path) { |
26 return GetTempDir(path); | 29 return GetTempDir(path); |
27 } | 30 } |
28 | 31 |
29 bool CopyFile(const FilePath& from_path, const FilePath& to_path) { | 32 bool CopyFile(const FilePath& from_path, const FilePath& to_path) { |
30 base::ThreadRestrictions::AssertIOAllowed(); | 33 base::ThreadRestrictions::AssertIOAllowed(); |
31 return (copyfile(from_path.value().c_str(), | 34 return (copyfile(from_path.value().c_str(), |
32 to_path.value().c_str(), NULL, COPYFILE_ALL) == 0); | 35 to_path.value().c_str(), NULL, COPYFILE_ALL) == 0); |
33 } | 36 } |
34 | 37 |
| 38 bool IsInTrash(const FilePath& path) { |
| 39 FSRef ref; |
| 40 if (!base::mac::FSRefFromPath(path.value(), &ref)) { |
| 41 LOG(ERROR) << "Unable to get FSRef for " << path.value(); |
| 42 return false; |
| 43 } |
| 44 Boolean result = false; |
| 45 OSStatus err = FSDetermineIfRefIsEnclosedByFolder( |
| 46 kOnAppropriateDisk, kTrashFolderType, &ref, &result); |
| 47 // See http://lists.apple.com/archives/filesystem-dev/2009/Sep/msg00008.html |
| 48 // for why this is required. |
| 49 return err == noErr && result; |
| 50 } |
| 51 |
| 52 bool MoveToTrash(const FilePath& path, FilePath* new_path) { |
| 53 FSRef path_ref; |
| 54 FSRef new_path_ref; |
| 55 if (!base::mac::FSRefFromPath(path.value(), &path_ref)) { |
| 56 LOG(ERROR) << "Unable to get FSREf for " << path.value(); |
| 57 return false; |
| 58 } |
| 59 OSStatus status = FSMoveObjectToTrashSync(&path_ref, &new_path_ref, |
| 60 kFSFileOperationDefaultOptions); |
| 61 if (status != noErr) { |
| 62 LOG(ERROR) << "MoveToTrash " << status; |
| 63 return false; |
| 64 } |
| 65 if (new_path) { |
| 66 *new_path = FilePath(base::mac::PathFromFSRef(new_path_ref)); |
| 67 } |
| 68 return true; |
| 69 } |
| 70 |
35 } // namespace | 71 } // namespace |
OLD | NEW |