OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "components/filesystem/lock_table.h" | 5 #include "components/filesystem/lock_table.h" |
6 | 6 |
7 #include "components/filesystem/file_impl.h" | 7 #include "components/filesystem/file_impl.h" |
8 | 8 |
9 namespace filesystem { | 9 namespace filesystem { |
10 | 10 |
11 LockTable::LockTable() {} | 11 LockTable::LockTable() {} |
12 | 12 |
13 LockTable::~LockTable() {} | 13 LockTable::~LockTable() {} |
14 | 14 |
15 base::File::Error LockTable::LockFile(FileImpl* file) { | 15 base::File::Error LockTable::LockFile(FileImpl* file) { |
16 DCHECK(file->IsValid()); | 16 DCHECK(file->IsValid()); |
| 17 DCHECK(file->path().IsAbsolute()); |
17 | 18 |
18 auto it = locked_files_.find(file->path()); | 19 auto it = locked_files_.find(file->path()); |
19 if (it != locked_files_.end()) { | 20 if (it != locked_files_.end()) { |
20 // We're already locked; that's an error condition on Windows. | 21 // We're already locked; that's an error condition on Windows. |
21 return base::File::FILE_ERROR_FAILED; | 22 return base::File::FILE_ERROR_FAILED; |
22 } | 23 } |
23 | 24 |
24 base::File::Error lock_err = file->RawLockFile(); | 25 base::File::Error lock_err = file->RawLockFile(); |
25 if (lock_err != base::File::FILE_OK) { | 26 if (lock_err != base::File::FILE_OK) { |
26 // Locking failed for some reason. | 27 // Locking failed for some reason. |
27 return lock_err; | 28 return lock_err; |
28 } | 29 } |
29 | 30 |
30 locked_files_.insert(file->path()); | 31 locked_files_.insert(file->path()); |
31 return base::File::FILE_OK; | 32 return base::File::FILE_OK; |
32 } | 33 } |
33 | 34 |
34 base::File::Error LockTable::UnlockFile(FileImpl* file) { | 35 base::File::Error LockTable::UnlockFile(FileImpl* file) { |
35 auto it = locked_files_.find(file->path()); | 36 auto it = locked_files_.find(file->path()); |
36 if (it != locked_files_.end()) { | 37 if (it != locked_files_.end()) { |
37 locked_files_.erase(it); | |
38 | |
39 base::File::Error lock_err = file->RawUnlockFile(); | 38 base::File::Error lock_err = file->RawUnlockFile(); |
40 if (lock_err != base::File::FILE_OK) { | 39 if (lock_err != base::File::FILE_OK) { |
41 // TODO(erg): When can we fail to release a lock? | 40 // TODO(erg): When can we fail to release a lock? |
42 NOTREACHED(); | 41 NOTREACHED(); |
43 return lock_err; | 42 return lock_err; |
44 } | 43 } |
| 44 |
| 45 locked_files_.erase(it); |
45 } | 46 } |
46 | 47 |
47 return base::File::FILE_OK; | 48 return base::File::FILE_OK; |
48 } | 49 } |
49 | 50 |
50 void LockTable::RemoveFromLockTable(const base::FilePath& path) { | 51 void LockTable::RemoveFromLockTable(const base::FilePath& path) { |
51 auto it = locked_files_.find(path); | 52 auto it = locked_files_.find(path); |
52 if (it != locked_files_.end()) | 53 if (it != locked_files_.end()) |
53 locked_files_.erase(it); | 54 locked_files_.erase(it); |
54 } | 55 } |
55 | 56 |
56 } // namespace filesystem | 57 } // namespace filesystem |
OLD | NEW |