1 // Copyright 2013 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 // A utility to clear the operating system's cache for a file or directory.
7 // USAGE: clear_system_cache [--recurse] <files or directories>
11 #include "base/basictypes.h"
12 #include "base/command_line.h"
13 #include "base/files/file_enumerator.h"
14 #include "base/files/file_path.h"
15 #include "base/files/file_util.h"
16 #include "base/logging.h"
17 #include "base/test/test_file_util.h"
19 void ClearCacheForFile(const base::FilePath
& path
) {
20 VLOG(1) << "Clearing " << path
.MaybeAsASCII();
21 base::EvictFileFromSystemCache(path
);
24 int main(int argc
, const char* argv
[]) {
25 base::CommandLine::Init(argc
, argv
);
26 const base::CommandLine
& parsed_command_line
=
27 *base::CommandLine::ForCurrentProcess();
28 bool should_recurse
= parsed_command_line
.HasSwitch("recurse");
29 const base::CommandLine::StringVector
& args
= parsed_command_line
.GetArgs();
31 if (args
.size() < 1) {
32 printf("USAGE: %s [--recurse] <files or directories>\n", argv
[0]);
36 for (size_t i
= 0; i
< args
.size(); ++i
) {
37 base::FilePath
path(args
[i
]);
38 if (!base::PathExists(path
)) {
39 LOG(ERROR
) << "Couldn't find " << path
.MaybeAsASCII();
43 if (base::DirectoryExists(path
)) {
44 base::FileEnumerator
enumerator(path
, should_recurse
,
45 base::FileEnumerator::FILES
);
46 for (base::FilePath next
= enumerator
.Next(); !next
.empty();
47 next
= enumerator
.Next()) {
48 ClearCacheForFile(next
);
51 ClearCacheForFile(path
);