Home | History | Annotate | Download | only in test
      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.
      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 #if defined(OS_ANDROID)
     13 #include <asm/unistd.h>
     14 #include <errno.h>
     15 #include <linux/fadvise.h>
     16 #include <sys/syscall.h>
     17 #endif
     18 
     19 #include "base/files/file_path.h"
     20 #include "base/files/scoped_file.h"
     21 
     22 namespace base {
     23 
     24 // Inconveniently, the NDK doesn't provide for posix_fadvise
     25 // until native API level = 21, which we don't use yet, so provide a wrapper, at
     26 // least on ARM32
     27 #if defined(OS_ANDROID) && __ANDROID_API__ < 21
     28 
     29 namespace {
     30 int posix_fadvise(int fd, off_t offset, off_t len, int advice) {
     31 #if defined(ARCH_CPU_ARMEL)
     32   // Note that the syscall argument order on ARM is different from the C
     33   // function; this is helpfully documented in the Linux posix_fadvise manpage.
     34   return syscall(__NR_arm_fadvise64_64, fd, advice,
     35                  0,  // Upper 32-bits for offset
     36                  offset,
     37                  0,  // Upper 32-bits for length
     38                  len);
     39 #endif
     40   NOTIMPLEMENTED();
     41   return ENOSYS;
     42 }
     43 
     44 }  // namespace
     45 
     46 #endif  // OS_ANDROID
     47 
     48 bool EvictFileFromSystemCache(const FilePath& file) {
     49   ScopedFD fd(open(file.value().c_str(), O_RDONLY));
     50   if (!fd.is_valid())
     51     return false;
     52   if (fdatasync(fd.get()) != 0)
     53     return false;
     54   if (posix_fadvise(fd.get(), 0, 0, POSIX_FADV_DONTNEED) != 0)
     55     return false;
     56   return true;
     57 }
     58 
     59 }  // namespace base
     60