diff options
author | Philip P. Moltmann <moltmann@google.com> | 2017-07-17 12:41:53 -0700 |
---|---|---|
committer | Andreas Gampe <agampe@google.com> | 2017-11-20 10:01:08 -0800 |
commit | 7e37d48b0a58aa5414c59c4c3a9c1a455932de90 (patch) | |
tree | be89125f34246d74d453fecd3d2c94fc312a0bd2 | |
parent | 2610ad0556e49979d3bf88d51d89ef2065f5bbf3 (diff) | |
download | base-7e37d48b0a58aa5414c59c4c3a9c1a455932de90.tar.gz |
Make SharedPreferences check with higher precision
When two processes modify shared preferences we use the timestamp to
figure out if the file was changes underneath. Do this with the highest
precision available (instead of sec) as before.
It would be possible to make the check more reliable by writing a unique
id to the shared pref file, but this would make this check much more
expensive in the common case that nothing changed. Considering that this
has not been a problem and we don't officially give any guarantee for
this sounds like a good middle-ground.
(cherry picked from commit ffe74357aec5330098795000e131e8d581b8b274)
Merged-In: I04c96b6a946618d5599c26410c88d7cd654d31fb
Change-Id: I04c96b6a946618d5599c26410c88d7cd654d31fb
Test: SharedPreferencesTest
Fixes: 62949739
-rw-r--r-- | core/java/android/app/SharedPreferencesImpl.java | 17 |
1 files changed, 9 insertions, 8 deletions
diff --git a/core/java/android/app/SharedPreferencesImpl.java b/core/java/android/app/SharedPreferencesImpl.java index 063ad24cd947..aab7d77315f3 100644 --- a/core/java/android/app/SharedPreferencesImpl.java +++ b/core/java/android/app/SharedPreferencesImpl.java @@ -23,16 +23,19 @@ import android.os.Looper; import android.system.ErrnoException; import android.system.Os; import android.system.StructStat; +import android.system.StructTimespec; import android.util.Log; -import com.google.android.collect.Maps; - import com.android.internal.annotations.GuardedBy; import com.android.internal.util.ExponentiallyBucketedHistogram; import com.android.internal.util.XmlUtils; import dalvik.system.BlockGuard; +import libcore.io.IoUtils; + +import com.google.android.collect.Maps; + import org.xmlpull.v1.XmlPullParserException; import java.io.BufferedInputStream; @@ -50,8 +53,6 @@ import java.util.Set; import java.util.WeakHashMap; import java.util.concurrent.CountDownLatch; -import libcore.io.IoUtils; - final class SharedPreferencesImpl implements SharedPreferences { private static final String TAG = "SharedPreferencesImpl"; private static final boolean DEBUG = false; @@ -80,7 +81,7 @@ final class SharedPreferencesImpl implements SharedPreferences { private boolean mLoaded = false; @GuardedBy("mLock") - private long mStatTimestamp; + private StructTimespec mStatTimestamp; @GuardedBy("mLock") private long mStatSize; @@ -162,7 +163,7 @@ final class SharedPreferencesImpl implements SharedPreferences { mLoaded = true; if (map != null) { mMap = map; - mStatTimestamp = stat.st_mtime; + mStatTimestamp = stat.st_mtim; mStatSize = stat.st_size; } else { mMap = new HashMap<>(); @@ -209,7 +210,7 @@ final class SharedPreferencesImpl implements SharedPreferences { } synchronized (mLock) { - return mStatTimestamp != stat.st_mtime || mStatSize != stat.st_size; + return !stat.st_mtim.equals(mStatTimestamp) || mStatSize != stat.st_size; } } @@ -744,7 +745,7 @@ final class SharedPreferencesImpl implements SharedPreferences { try { final StructStat stat = Os.stat(mFile.getPath()); synchronized (mLock) { - mStatTimestamp = stat.st_mtime; + mStatTimestamp = stat.st_mtim; mStatSize = stat.st_size; } } catch (ErrnoException e) { |