Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/drive/chromeos/file_cache.h" | 5 #include "components/drive/chromeos/file_cache.h" |
| 6 | 6 |
| 7 #include <linux/fs.h> | 7 #include <linux/fs.h> |
| 8 #include <sys/ioctl.h> | 8 #include <sys/ioctl.h> |
| 9 #include <sys/xattr.h> | 9 #include <sys/xattr.h> |
| 10 | 10 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 45 std::string GetIdFromPath(const base::FilePath& path) { | 45 std::string GetIdFromPath(const base::FilePath& path) { |
| 46 return util::UnescapeCacheFileName(path.BaseName().AsUTF8Unsafe()); | 46 return util::UnescapeCacheFileName(path.BaseName().AsUTF8Unsafe()); |
| 47 } | 47 } |
| 48 | 48 |
| 49 base::FilePath GetPathForId(const base::FilePath& cache_directory, | 49 base::FilePath GetPathForId(const base::FilePath& cache_directory, |
| 50 const std::string& id) { | 50 const std::string& id) { |
| 51 return cache_directory.Append( | 51 return cache_directory.Append( |
| 52 base::FilePath::FromUTF8Unsafe(util::EscapeCacheFileName(id))); | 52 base::FilePath::FromUTF8Unsafe(util::EscapeCacheFileName(id))); |
| 53 } | 53 } |
| 54 | 54 |
| 55 // Returns if the filesystem backing |path| supports file attributes. | |
| 56 // This will return false if the filesystem is for example tmpfs, which is used | |
| 57 // for ephemeral mode. | |
| 58 bool IsFileAttributesSupported(const base::FilePath& path) { | |
| 59 getxattr(path.value().c_str(), "user.foo", nullptr, 0); | |
|
hashimoto
2016/10/14 04:16:49
Please check that getxattr actually fails.
oka
2016/10/14 04:36:06
Done.
| |
| 60 return errno != ENOTSUP; | |
| 61 } | |
| 62 | |
| 55 // Sets extended file attribute as |name| |value| pair. | 63 // Sets extended file attribute as |name| |value| pair. |
| 56 bool SetExtendedFileAttributes(const base::FilePath& path, | 64 bool SetExtendedFileAttributes(const base::FilePath& path, |
| 57 const std::string& name, const std::string& value) { | 65 const std::string& name, const std::string& value) { |
| 66 if (!IsFileAttributesSupported(path)) { | |
|
hashimoto
2016/10/14 04:16:49
Isn't it better to check this at the beginning of
oka
2016/10/14 04:36:05
Done.
| |
| 67 return true; | |
| 68 } | |
| 58 return setxattr(path.value().c_str(), name.c_str(), value.c_str(), | 69 return setxattr(path.value().c_str(), name.c_str(), value.c_str(), |
| 59 value.size() + 1, 0) == 0; | 70 value.size() + 1, 0) == 0; |
| 60 } | 71 } |
| 61 | 72 |
| 62 // Changes attributes of the file with |flags|, e.g. FS_NODUMP_FL (cryptohome | 73 // Changes attributes of the file with |flags|, e.g. FS_NODUMP_FL (cryptohome |
| 63 // will remove Drive caches with this attribute). | 74 // will remove Drive caches with this attribute). |
| 64 // See linux/fs.h for available flags, and chattr(1) which does similar thing. | 75 // See linux/fs.h for available flags, and chattr(1) which does similar thing. |
| 65 // Returns whether operation succeeded. | 76 // Returns whether operation succeeded. |
| 66 bool SetFileAttributes(const base::FilePath& path, FileAttributes flags) { | 77 bool SetFileAttributes(const base::FilePath& path, FileAttributes flags) { |
| 67 base::File file(path, base::File::FLAG_OPEN | base::File::FLAG_READ); | 78 base::File file(path, base::File::FLAG_OPEN | base::File::FLAG_READ); |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 87 FileAttributes flags = 0; | 98 FileAttributes flags = 0; |
| 88 if (ioctl(file.GetPlatformFile(), FS_IOC_GETFLAGS, &flags) < 0) { | 99 if (ioctl(file.GetPlatformFile(), FS_IOC_GETFLAGS, &flags) < 0) { |
| 89 PLOG(ERROR) << "ioctl: " << path.value(); | 100 PLOG(ERROR) << "ioctl: " << path.value(); |
| 90 return -1; | 101 return -1; |
| 91 } | 102 } |
| 92 return flags; | 103 return flags; |
| 93 } | 104 } |
| 94 | 105 |
| 95 // Marks the cache file to be removable by cryptohome. | 106 // Marks the cache file to be removable by cryptohome. |
| 96 bool SetRemovable(const base::FilePath& path) { | 107 bool SetRemovable(const base::FilePath& path) { |
| 108 if (!IsFileAttributesSupported(path)) { | |
|
hashimoto
2016/10/14 04:16:48
It'd be nice to have a brief comment to justify th
oka
2016/10/14 04:36:05
Done.
| |
| 109 return true; | |
| 110 } | |
| 97 FileAttributes flags = GetFileAttributes(path); | 111 FileAttributes flags = GetFileAttributes(path); |
| 98 if (flags < 0) return false; | 112 if (flags < 0) return false; |
| 99 if ((flags & FS_NODUMP_FL) == FS_NODUMP_FL) return true; | 113 if ((flags & FS_NODUMP_FL) == FS_NODUMP_FL) return true; |
| 100 | 114 |
| 101 return SetFileAttributes(path, flags | FS_NODUMP_FL); | 115 return SetFileAttributes(path, flags | FS_NODUMP_FL); |
| 102 } | 116 } |
| 103 | 117 |
| 104 // Marks the cache file to be unremovable by cryptohome. | 118 // Marks the cache file to be unremovable by cryptohome. |
| 105 bool UnsetRemovable(const base::FilePath& path) { | 119 bool UnsetRemovable(const base::FilePath& path) { |
| 120 if (!IsFileAttributesSupported(path)) { | |
|
hashimoto
2016/10/14 04:16:48
ditto.
oka
2016/10/14 04:36:06
Done.
| |
| 121 return true; | |
| 122 } | |
| 106 FileAttributes flags = GetFileAttributes(path); | 123 FileAttributes flags = GetFileAttributes(path); |
| 107 if (flags < 0) return false; | 124 if (flags < 0) return false; |
| 108 if ((flags & FS_NODUMP_FL) == 0) return true; | 125 if ((flags & FS_NODUMP_FL) == 0) return true; |
| 109 | 126 |
| 110 return SetFileAttributes(path, flags & ~FS_NODUMP_FL); | 127 return SetFileAttributes(path, flags & ~FS_NODUMP_FL); |
| 111 } | 128 } |
| 112 | 129 |
| 113 // Marks |path| as drive cache dir. | 130 // Marks |path| as drive cache dir. |
| 114 // Returns if the operation succeeded. | 131 // Returns if the operation succeeded. |
| 115 bool MarkAsDriveCacheDir(const base::FilePath& path) { | 132 bool MarkAsDriveCacheDir(const base::FilePath& path) { |
| (...skipping 789 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 905 | 922 |
| 906 bool FileCache::IsEvictable(const std::string& id, const ResourceEntry& entry) { | 923 bool FileCache::IsEvictable(const std::string& id, const ResourceEntry& entry) { |
| 907 return IsPresent(entry) && | 924 return IsPresent(entry) && |
| 908 !entry.file_specific_info().cache_state().is_pinned() && | 925 !entry.file_specific_info().cache_state().is_pinned() && |
| 909 !entry.file_specific_info().cache_state().is_dirty() && | 926 !entry.file_specific_info().cache_state().is_dirty() && |
| 910 !mounted_files_.count(id); | 927 !mounted_files_.count(id); |
| 911 } | 928 } |
| 912 | 929 |
| 913 } // namespace internal | 930 } // namespace internal |
| 914 } // namespace drive | 931 } // namespace drive |
| OLD | NEW |