diff options
author | clamy@chromium.org <clamy@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-06-06 22:33:56 +0000 |
---|---|---|
committer | clamy@chromium.org <clamy@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-06-06 22:33:56 +0000 |
commit | d530526f2d60ac2e4c76a1a966085b64870bd5e9 (patch) | |
tree | ecc07514661372231ebb6913ce0dedff5565e472 /net | |
parent | 8f81063bc97e6bdb047c824bda7594b9489c4688 (diff) | |
download | chromium_src-d530526f2d60ac2e4c76a1a966085b64870bd5e9.zip chromium_src-d530526f2d60ac2e4c76a1a966085b64870bd5e9.tar.gz chromium_src-d530526f2d60ac2e4c76a1a966085b64870bd5e9.tar.bz2 |
SimpleCache: correct the time resolution of last modified field
Currently the last modified fields of an entry can end up having a time
resolution of seconds. This patch makes it microseconds all the time.
BUG=230306
Review URL: https://chromiumcodereview.appspot.com/16457002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@204631 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net')
-rw-r--r-- | net/disk_cache/simple/simple_index_file.cc | 43 | ||||
-rw-r--r-- | net/disk_cache/simple/simple_synchronous_entry.cc | 7 | ||||
-rw-r--r-- | net/disk_cache/simple/simple_util.cc | 46 | ||||
-rw-r--r-- | net/disk_cache/simple/simple_util.h | 9 |
4 files changed, 63 insertions, 42 deletions
diff --git a/net/disk_cache/simple/simple_index_file.cc b/net/disk_cache/simple/simple_index_file.cc index aa2979d..5316053 100644 --- a/net/disk_cache/simple/simple_index_file.cc +++ b/net/disk_cache/simple/simple_index_file.cc @@ -29,45 +29,6 @@ uint32 CalculatePickleCRC(const Pickle& pickle) { pickle.payload_size()); } -bool GetNanoSecsFromStat(const struct stat& st, long* out_sec, long* out_nsec) { -#if defined(OS_ANDROID) - *out_sec = st.st_mtime; - *out_nsec = st.st_mtime_nsec; -#elif defined(OS_LINUX) - *out_sec = st.st_mtim.tv_sec; - *out_nsec = st.st_mtim.tv_nsec; -#elif defined(OS_MACOSX) || defined(OS_IOS) || defined(OS_BSD) - *out_sec = st.st_mtimespec.tv_sec; - *out_nsec = st.st_mtimespec.tv_nsec; -#else - return false; -#endif - return true; -} - -bool GetMTime(const base::FilePath& path, base::Time* out_mtime) { - DCHECK(out_mtime); -#if defined(OS_POSIX) - base::ThreadRestrictions::AssertIOAllowed(); - struct stat file_stat; - if (stat(path.value().c_str(), &file_stat) != 0) - return false; - long sec; - long nsec; - if (GetNanoSecsFromStat(file_stat, &sec, &nsec)) { - int64 usec = (nsec / base::Time::kNanosecondsPerMicrosecond); - *out_mtime = base::Time::FromTimeT(implicit_cast<time_t>(sec)) - + base::TimeDelta::FromMicroseconds(usec); - return true; - } -#endif - base::PlatformFileInfo file_info; - if (!file_util::GetFileInfo(path, &file_info)) - return false; - *out_mtime = file_info.last_modified; - return true; -} - void DoomEntrySetReply(scoped_ptr<int> result, const base::Callback<void(int)>& reply_callback) { reply_callback.Run(*result.get()); @@ -194,9 +155,9 @@ void SimpleIndexFile::DoomEntrySet( bool SimpleIndexFile::IsIndexFileStale(const base::FilePath& index_filename) { base::Time index_mtime; base::Time dir_mtime; - if (!GetMTime(index_filename.DirName(), &dir_mtime)) + if (!simple_util::GetMTime(index_filename.DirName(), &dir_mtime)) return true; - if (!GetMTime(index_filename, &index_mtime)) + if (!simple_util::GetMTime(index_filename, &index_mtime)) return true; // Index file last_modified must be equal to the directory last_modified since // the last operation we do is ReplaceFile in the diff --git a/net/disk_cache/simple/simple_synchronous_entry.cc b/net/disk_cache/simple/simple_synchronous_entry.cc index 8db3d76..d359bed 100644 --- a/net/disk_cache/simple/simple_synchronous_entry.cc +++ b/net/disk_cache/simple/simple_synchronous_entry.cc @@ -431,12 +431,17 @@ bool SimpleSynchronousEntry::OpenOrCreateFiles(bool create) { for (int i = 0; i < kSimpleEntryFileCount; ++i) { PlatformFileInfo file_info; bool success = GetPlatformFileInfo(files_[i], &file_info); + base::Time file_last_modified; if (!success) { DLOG(WARNING) << "Could not get platform file info."; continue; } last_used_ = std::max(last_used_, file_info.last_accessed); - last_modified_ = std::max(last_modified_, file_info.last_modified); + if (simple_util::GetMTime(path_, &file_last_modified)) + last_modified_ = std::max(last_modified_, file_last_modified); + else + last_modified_ = std::max(last_modified_, file_info.last_modified); + data_size_[i] = GetDataSizeFromKeyAndFileSize(key_, file_info.size); if (data_size_[i] < 0) { // This entry can't possibly be valid, as it does not enough space to diff --git a/net/disk_cache/simple/simple_util.cc b/net/disk_cache/simple/simple_util.cc index 148a2c2..bb15881 100644 --- a/net/disk_cache/simple/simple_util.cc +++ b/net/disk_cache/simple/simple_util.cc @@ -6,11 +6,14 @@ #include <limits> +#include "base/file_util.h" #include "base/format_macros.h" #include "base/logging.h" #include "base/sha1.h" #include "base/stringprintf.h" #include "base/strings/string_number_conversions.h" +#include "base/threading/thread_restrictions.h" +#include "base/time.h" #include "net/disk_cache/simple/simple_entry_format.h" namespace { @@ -18,6 +21,25 @@ namespace { // Size of the uint64 hash_key number in Hex format in a string. const size_t kEntryHashKeyAsHexStringSize = 2 * sizeof(uint64); +// TODO(clamy, gavinp): this should go in base +bool GetNanoSecsFromStat(const struct stat& st, + time_t* out_sec, + long* out_nsec) { +#if defined(OS_ANDROID) + *out_sec = st.st_mtime; + *out_nsec = st.st_mtime_nsec; +#elif defined(OS_LINUX) + *out_sec = st.st_mtim.tv_sec; + *out_nsec = st.st_mtim.tv_nsec; +#elif defined(OS_MACOSX) || defined(OS_IOS) || defined(OS_BSD) + *out_sec = st.st_mtimespec.tv_sec; + *out_nsec = st.st_mtimespec.tv_nsec; +#else + return false; +#endif + return true; +} + } // namespace namespace disk_cache { @@ -82,6 +104,30 @@ int64 GetFileOffsetFromKeyAndDataOffset(const std::string& key, return headers_size + data_offset; } +// TODO(clamy, gavinp): this should go in base +bool GetMTime(const base::FilePath& path, base::Time* out_mtime) { + DCHECK(out_mtime); +#if defined(OS_POSIX) + base::ThreadRestrictions::AssertIOAllowed(); + struct stat file_stat; + if (stat(path.value().c_str(), &file_stat) != 0) + return false; + time_t sec; + long nsec; + if (GetNanoSecsFromStat(file_stat, &sec, &nsec)) { + int64 usec = (nsec / base::Time::kNanosecondsPerMicrosecond); + *out_mtime = base::Time::FromTimeT(sec) + + base::TimeDelta::FromMicroseconds(usec); + return true; + } +#endif + base::PlatformFileInfo file_info; + if (!file_util::GetFileInfo(path, &file_info)) + return false; + *out_mtime = file_info.last_modified; + return true; +} + } // namespace simple_backend } // namespace disk_cache diff --git a/net/disk_cache/simple/simple_util.h b/net/disk_cache/simple/simple_util.h index cb869f8..505501e 100644 --- a/net/disk_cache/simple/simple_util.h +++ b/net/disk_cache/simple/simple_util.h @@ -10,6 +10,11 @@ #include "base/basictypes.h" #include "net/base/net_export.h" +namespace base { +class FilePath; +class Time; +} + namespace disk_cache { namespace simple_util { @@ -57,6 +62,10 @@ NET_EXPORT_PRIVATE int64 GetFileOffsetFromKeyAndDataOffset( const std::string& key, int data_offset); +// Fills |out_time| with the time the file last modified time. Unlike the +// functions in platform_file.h, the time resolution is milliseconds. +NET_EXPORT_PRIVATE bool GetMTime(const base::FilePath& path, + base::Time* out_mtime); } // namespace simple_backend } // namespace disk_cache |