1 // Copyright (c) 2011 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.
5 #include "base/test/test_file_util.h"
9 #include "base/logging.h"
10 #include "base/file_util.h"
14 bool EvictFileFromSystemCache(const FilePath
& file
) {
15 // There aren't any really direct ways to purge a file from the UBC. From
16 // talking with Amit Singh, the safest is to mmap the file with MAP_FILE (the
17 // default) + MAP_SHARED, then do an msync to invalidate the memory. The next
18 // open should then have to load the file from disk.
21 if (!file_util::GetFileSize(file
, &length
)) {
22 DLOG(ERROR
) << "failed to get size of " << file
.value();
26 // When a file is empty, we do not need to evict it from cache.
27 // In fact, an attempt to map it to memory will result in error.
29 DLOG(WARNING
) << "file size is zero, will not attempt to map to memory";
33 file_util::MemoryMappedFile mapped_file
;
34 if (!mapped_file
.Initialize(file
)) {
35 DLOG(WARNING
) << "failed to memory map " << file
.value();
39 if (msync(const_cast<uint8
*>(mapped_file
.data()), mapped_file
.length(),
40 MS_INVALIDATE
) != 0) {
41 DLOG(WARNING
) << "failed to invalidate memory map of " << file
.value()
42 << ", errno: " << errno
;
49 } // namespace file_util