liyujie
2025-08-28 786ff4f4ca2374bdd9177f2e24b503d43e7a3b93
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
 
#include "base/test/test_file_util.h"
 
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
 
#if defined(OS_ANDROID)
#include <asm/unistd.h>
#include <errno.h>
#include <linux/fadvise.h>
#include <sys/syscall.h>
#endif
 
#include "base/files/file_path.h"
#include "base/files/scoped_file.h"
 
namespace base {
 
// Inconveniently, the NDK doesn't provide for posix_fadvise
// until native API level = 21, which we don't use yet, so provide a wrapper, at
// least on ARM32
#if defined(OS_ANDROID) && __ANDROID_API__ < 21
 
namespace {
int posix_fadvise(int fd, off_t offset, off_t len, int advice) {
#if defined(ARCH_CPU_ARMEL)
  // Note that the syscall argument order on ARM is different from the C
  // function; this is helpfully documented in the Linux posix_fadvise manpage.
  return syscall(__NR_arm_fadvise64_64, fd, advice,
                 0,  // Upper 32-bits for offset
                 offset,
                 0,  // Upper 32-bits for length
                 len);
#endif
  NOTIMPLEMENTED();
  return ENOSYS;
}
 
}  // namespace
 
#endif  // OS_ANDROID
 
bool EvictFileFromSystemCache(const FilePath& file) {
  ScopedFD fd(open(file.value().c_str(), O_RDONLY));
  if (!fd.is_valid())
    return false;
  if (fdatasync(fd.get()) != 0)
    return false;
  if (posix_fadvise(fd.get(), 0, 0, POSIX_FADV_DONTNEED) != 0)
    return false;
  return true;
}
 
}  // namespace base