From 2ab4447ad20bdbf664c00de5f47c2c638ee83241 Mon Sep 17 00:00:00 2001 From: Yurii Zubrytskyi Date: Wed, 30 Nov 2022 00:56:22 -0800 Subject: [res] Don't stat asset providers on RO filesystems IsUpToDate() is one of the most often called functions, let's make sure it only performs a syscall if it makes any sense and the underlying file can really change. Bug: 237583012 Test: build + boot + UTs Change-Id: Ie5999ddadf10b56f35354d00ad3402b229ffa2c3 --- libs/androidfw/misc.cpp | 44 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 6 deletions(-) (limited to 'libs/androidfw/misc.cpp') diff --git a/libs/androidfw/misc.cpp b/libs/androidfw/misc.cpp index 52854205207c..7af506638ebc 100644 --- a/libs/androidfw/misc.cpp +++ b/libs/androidfw/misc.cpp @@ -21,12 +21,17 @@ // #include -#include +#include "android-base/logging.h" + +#ifndef _WIN32 +#include +#include +#endif // _WIN32 + #include -#include #include - -using namespace android; +#include +#include namespace android { @@ -41,8 +46,7 @@ FileType getFileType(const char* fileName) if (errno == ENOENT || errno == ENOTDIR) return kFileTypeNonexistent; else { - fprintf(stderr, "getFileType got errno=%d on '%s'\n", - errno, fileName); + PLOG(ERROR) << "getFileType(): stat(" << fileName << ") failed"; return kFileTypeUnknown; } } else { @@ -82,4 +86,32 @@ time_t getFileModDate(const char* fileName) return sb.st_mtime; } +#ifdef _WIN32 +// No need to implement these for Windows, the functions only matter on a device. +bool isReadonlyFilesystem(const char*) { + return false; +} +bool isReadonlyFilesystem(int) { + return false; +} +#else // _WIN32 +bool isReadonlyFilesystem(const char* path) { + struct statfs sfs; + if (::statfs(path, &sfs)) { + PLOG(ERROR) << "isReadonlyFilesystem(): statfs(" << path << ") failed"; + return false; + } + return (sfs.f_flags & ST_RDONLY) != 0; +} + +bool isReadonlyFilesystem(int fd) { + struct statfs sfs; + if (::fstatfs(fd, &sfs)) { + PLOG(ERROR) << "isReadonlyFilesystem(): fstatfs(" << fd << ") failed"; + return false; + } + return (sfs.f_flags & ST_RDONLY) != 0; +} +#endif // _WIN32 + }; // namespace android -- cgit v1.2.3-59-g8ed1b