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