Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server; |
| 18 | |
| 19 | import android.content.BroadcastReceiver; |
| 20 | import android.content.ContentResolver; |
| 21 | import android.content.Context; |
| 22 | import android.content.Intent; |
| 23 | import android.content.IntentFilter; |
| 24 | import android.content.pm.PackageManager; |
Doug Zongker | 43866e0 | 2010-01-07 12:09:54 -0800 | [diff] [blame] | 25 | import android.database.ContentObserver; |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 26 | import android.net.Uri; |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 27 | import android.os.Binder; |
Dan Egnor | 3d40df3 | 2009-11-17 13:36:31 -0800 | [diff] [blame] | 28 | import android.os.Debug; |
Dan Egnor | f18a01c | 2009-11-12 11:32:50 -0800 | [diff] [blame] | 29 | import android.os.DropBoxManager; |
Dianne Hackborn | 8bdf593 | 2010-10-15 12:54:40 -0700 | [diff] [blame] | 30 | import android.os.FileUtils; |
Doug Zongker | 43866e0 | 2010-01-07 12:09:54 -0800 | [diff] [blame] | 31 | import android.os.Handler; |
Craig Mautner | 26caf7a | 2012-03-04 17:17:59 -0800 | [diff] [blame] | 32 | import android.os.Message; |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 33 | import android.os.StatFs; |
| 34 | import android.os.SystemClock; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 35 | import android.os.UserHandle; |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 36 | import android.provider.Settings; |
Dan Egnor | 3d40df3 | 2009-11-17 13:36:31 -0800 | [diff] [blame] | 37 | import android.text.format.Time; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 38 | import android.util.Slog; |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 39 | |
Tim Kilbourn | 0935f3c | 2015-05-28 11:48:43 -0700 | [diff] [blame] | 40 | import libcore.io.IoUtils; |
| 41 | |
Dan Egnor | f18a01c | 2009-11-12 11:32:50 -0800 | [diff] [blame] | 42 | import com.android.internal.os.IDropBoxManagerService; |
Dan Egnor | 9524027 | 2009-10-27 18:23:39 -0700 | [diff] [blame] | 43 | |
Brad Fitzpatrick | 89647b1 | 2010-09-22 17:49:16 -0700 | [diff] [blame] | 44 | import java.io.BufferedOutputStream; |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 45 | import java.io.File; |
| 46 | import java.io.FileDescriptor; |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 47 | import java.io.FileOutputStream; |
| 48 | import java.io.IOException; |
Dan Egnor | 9524027 | 2009-10-27 18:23:39 -0700 | [diff] [blame] | 49 | import java.io.InputStream; |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 50 | import java.io.InputStreamReader; |
| 51 | import java.io.OutputStream; |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 52 | import java.io.PrintWriter; |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 53 | import java.util.ArrayList; |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 54 | import java.util.HashMap; |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 55 | import java.util.SortedSet; |
| 56 | import java.util.TreeSet; |
| 57 | import java.util.zip.GZIPOutputStream; |
| 58 | |
| 59 | /** |
Dan Egnor | f18a01c | 2009-11-12 11:32:50 -0800 | [diff] [blame] | 60 | * Implementation of {@link IDropBoxManagerService} using the filesystem. |
| 61 | * Clients use {@link DropBoxManager} to access this service. |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 62 | */ |
Tim Kilbourn | 0935f3c | 2015-05-28 11:48:43 -0700 | [diff] [blame] | 63 | public final class DropBoxManagerService extends SystemService { |
Dan Egnor | f18a01c | 2009-11-12 11:32:50 -0800 | [diff] [blame] | 64 | private static final String TAG = "DropBoxManagerService"; |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 65 | private static final int DEFAULT_AGE_SECONDS = 3 * 86400; |
Dan Egnor | 3a8b0c1 | 2010-03-24 17:48:20 -0700 | [diff] [blame] | 66 | private static final int DEFAULT_MAX_FILES = 1000; |
| 67 | private static final int DEFAULT_QUOTA_KB = 5 * 1024; |
| 68 | private static final int DEFAULT_QUOTA_PERCENT = 10; |
| 69 | private static final int DEFAULT_RESERVE_PERCENT = 10; |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 70 | private static final int QUOTA_RESCAN_MILLIS = 5000; |
| 71 | |
Craig Mautner | 26caf7a | 2012-03-04 17:17:59 -0800 | [diff] [blame] | 72 | // mHandler 'what' value. |
| 73 | private static final int MSG_SEND_BROADCAST = 1; |
| 74 | |
Dan Egnor | 3d40df3 | 2009-11-17 13:36:31 -0800 | [diff] [blame] | 75 | private static final boolean PROFILE_DUMP = false; |
| 76 | |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 77 | // TODO: This implementation currently uses one file per entry, which is |
| 78 | // inefficient for smallish entries -- consider using a single queue file |
| 79 | // per tag (or even globally) instead. |
| 80 | |
| 81 | // The cached context and derived objects |
| 82 | |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 83 | private final ContentResolver mContentResolver; |
| 84 | private final File mDropBoxDir; |
| 85 | |
| 86 | // Accounting of all currently written log files (set in init()). |
| 87 | |
| 88 | private FileList mAllFiles = null; |
| 89 | private HashMap<String, FileList> mFilesByTag = null; |
| 90 | |
| 91 | // Various bits of disk information |
| 92 | |
| 93 | private StatFs mStatFs = null; |
| 94 | private int mBlockSize = 0; |
| 95 | private int mCachedQuotaBlocks = 0; // Space we can use: computed from free space, etc. |
| 96 | private long mCachedQuotaUptimeMillis = 0; |
| 97 | |
Brad Fitzpatrick | 34165c6 | 2011-01-17 18:14:18 -0800 | [diff] [blame] | 98 | private volatile boolean mBooted = false; |
| 99 | |
Craig Mautner | 26caf7a | 2012-03-04 17:17:59 -0800 | [diff] [blame] | 100 | // Provide a way to perform sendBroadcast asynchronously to avoid deadlocks. |
| 101 | private final Handler mHandler; |
| 102 | |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 103 | /** Receives events that might indicate a need to clean up files. */ |
| 104 | private final BroadcastReceiver mReceiver = new BroadcastReceiver() { |
| 105 | @Override |
| 106 | public void onReceive(Context context, Intent intent) { |
Tim Kilbourn | 0935f3c | 2015-05-28 11:48:43 -0700 | [diff] [blame] | 107 | // For ACTION_DEVICE_STORAGE_LOW: |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 108 | mCachedQuotaUptimeMillis = 0; // Force a re-check of quota size |
Dan Egnor | 3a8b0c1 | 2010-03-24 17:48:20 -0700 | [diff] [blame] | 109 | |
| 110 | // Run the initialization in the background (not this main thread). |
| 111 | // The init() and trimToFit() methods are synchronized, so they still |
| 112 | // block other users -- but at least the onReceive() call can finish. |
| 113 | new Thread() { |
| 114 | public void run() { |
| 115 | try { |
| 116 | init(); |
| 117 | trimToFit(); |
| 118 | } catch (IOException e) { |
| 119 | Slog.e(TAG, "Can't init", e); |
| 120 | } |
| 121 | } |
| 122 | }.start(); |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 123 | } |
| 124 | }; |
| 125 | |
Tim Kilbourn | 0935f3c | 2015-05-28 11:48:43 -0700 | [diff] [blame] | 126 | private final IDropBoxManagerService.Stub mStub = new IDropBoxManagerService.Stub() { |
| 127 | @Override |
| 128 | public void add(DropBoxManager.Entry entry) { |
| 129 | DropBoxManagerService.this.add(entry); |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public boolean isTagEnabled(String tag) { |
| 134 | return DropBoxManagerService.this.isTagEnabled(tag); |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public DropBoxManager.Entry getNextEntry(String tag, long millis) { |
| 139 | return DropBoxManagerService.this.getNextEntry(tag, millis); |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 144 | DropBoxManagerService.this.dump(fd, pw, args); |
| 145 | } |
| 146 | }; |
| 147 | |
| 148 | /** |
| 149 | * Creates an instance of managed drop box storage using the default dropbox |
| 150 | * directory. |
| 151 | * |
| 152 | * @param context to use for receiving free space & gservices intents |
| 153 | */ |
| 154 | public DropBoxManagerService(final Context context) { |
| 155 | this(context, new File("/data/system/dropbox")); |
| 156 | } |
| 157 | |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 158 | /** |
| 159 | * Creates an instance of managed drop box storage. Normally there is one of these |
| 160 | * run by the system, but others can be created for testing and other purposes. |
| 161 | * |
| 162 | * @param context to use for receiving free space & gservices intents |
| 163 | * @param path to store drop box entries in |
| 164 | */ |
Doug Zongker | 43866e0 | 2010-01-07 12:09:54 -0800 | [diff] [blame] | 165 | public DropBoxManagerService(final Context context, File path) { |
Tim Kilbourn | 0935f3c | 2015-05-28 11:48:43 -0700 | [diff] [blame] | 166 | super(context); |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 167 | mDropBoxDir = path; |
Tim Kilbourn | 0935f3c | 2015-05-28 11:48:43 -0700 | [diff] [blame] | 168 | mContentResolver = getContext().getContentResolver(); |
| 169 | mHandler = new Handler() { |
| 170 | @Override |
| 171 | public void handleMessage(Message msg) { |
| 172 | if (msg.what == MSG_SEND_BROADCAST) { |
Xiaohui Chen | e4de5a0 | 2015-09-22 15:33:31 -0700 | [diff] [blame] | 173 | getContext().sendBroadcastAsUser((Intent)msg.obj, UserHandle.SYSTEM, |
Tim Kilbourn | 0935f3c | 2015-05-28 11:48:43 -0700 | [diff] [blame] | 174 | android.Manifest.permission.READ_LOGS); |
| 175 | } |
| 176 | } |
| 177 | }; |
| 178 | } |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 179 | |
Tim Kilbourn | 0935f3c | 2015-05-28 11:48:43 -0700 | [diff] [blame] | 180 | @Override |
| 181 | public void onStart() { |
Tim Kilbourn | 0935f3c | 2015-05-28 11:48:43 -0700 | [diff] [blame] | 182 | publishBinderService(Context.DROPBOX_SERVICE, mStub); |
Craig Mautner | 26caf7a | 2012-03-04 17:17:59 -0800 | [diff] [blame] | 183 | |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 184 | // The real work gets done lazily in init() -- that way service creation always |
| 185 | // succeeds, and things like disk problems cause individual method failures. |
| 186 | } |
| 187 | |
Tim Kilbourn | 0935f3c | 2015-05-28 11:48:43 -0700 | [diff] [blame] | 188 | @Override |
| 189 | public void onBootPhase(int phase) { |
| 190 | switch (phase) { |
Jeff Sharkey | d79d203 | 2016-08-23 13:39:07 -0600 | [diff] [blame] | 191 | case PHASE_SYSTEM_SERVICES_READY: |
| 192 | IntentFilter filter = new IntentFilter(); |
| 193 | filter.addAction(Intent.ACTION_DEVICE_STORAGE_LOW); |
| 194 | getContext().registerReceiver(mReceiver, filter); |
| 195 | |
| 196 | mContentResolver.registerContentObserver( |
| 197 | Settings.Global.CONTENT_URI, true, |
| 198 | new ContentObserver(new Handler()) { |
| 199 | @Override |
| 200 | public void onChange(boolean selfChange) { |
| 201 | mReceiver.onReceive(getContext(), (Intent) null); |
| 202 | } |
| 203 | }); |
| 204 | break; |
| 205 | |
Tim Kilbourn | 0935f3c | 2015-05-28 11:48:43 -0700 | [diff] [blame] | 206 | case PHASE_BOOT_COMPLETED: |
| 207 | mBooted = true; |
| 208 | break; |
| 209 | } |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Tim Kilbourn | 0935f3c | 2015-05-28 11:48:43 -0700 | [diff] [blame] | 212 | /** Retrieves the binder stub -- for test instances */ |
| 213 | public IDropBoxManagerService getServiceStub() { |
| 214 | return mStub; |
| 215 | } |
| 216 | |
Dan Egnor | f18a01c | 2009-11-12 11:32:50 -0800 | [diff] [blame] | 217 | public void add(DropBoxManager.Entry entry) { |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 218 | File temp = null; |
Tim Kilbourn | 0935f3c | 2015-05-28 11:48:43 -0700 | [diff] [blame] | 219 | InputStream input = null; |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 220 | OutputStream output = null; |
Dan Egnor | 9524027 | 2009-10-27 18:23:39 -0700 | [diff] [blame] | 221 | final String tag = entry.getTag(); |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 222 | try { |
Dan Egnor | 9524027 | 2009-10-27 18:23:39 -0700 | [diff] [blame] | 223 | int flags = entry.getFlags(); |
Dan Egnor | f18a01c | 2009-11-12 11:32:50 -0800 | [diff] [blame] | 224 | if ((flags & DropBoxManager.IS_EMPTY) != 0) throw new IllegalArgumentException(); |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 225 | |
| 226 | init(); |
| 227 | if (!isTagEnabled(tag)) return; |
| 228 | long max = trimToFit(); |
| 229 | long lastTrim = System.currentTimeMillis(); |
| 230 | |
| 231 | byte[] buffer = new byte[mBlockSize]; |
Tim Kilbourn | 0935f3c | 2015-05-28 11:48:43 -0700 | [diff] [blame] | 232 | input = entry.getInputStream(); |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 233 | |
| 234 | // First, accumulate up to one block worth of data in memory before |
| 235 | // deciding whether to compress the data or not. |
| 236 | |
| 237 | int read = 0; |
| 238 | while (read < buffer.length) { |
| 239 | int n = input.read(buffer, read, buffer.length - read); |
| 240 | if (n <= 0) break; |
| 241 | read += n; |
| 242 | } |
| 243 | |
| 244 | // If we have at least one block, compress it -- otherwise, just write |
| 245 | // the data in uncompressed form. |
| 246 | |
| 247 | temp = new File(mDropBoxDir, "drop" + Thread.currentThread().getId() + ".tmp"); |
Brad Fitzpatrick | 89647b1 | 2010-09-22 17:49:16 -0700 | [diff] [blame] | 248 | int bufferSize = mBlockSize; |
| 249 | if (bufferSize > 4096) bufferSize = 4096; |
| 250 | if (bufferSize < 512) bufferSize = 512; |
Dianne Hackborn | 8bdf593 | 2010-10-15 12:54:40 -0700 | [diff] [blame] | 251 | FileOutputStream foutput = new FileOutputStream(temp); |
| 252 | output = new BufferedOutputStream(foutput, bufferSize); |
Dan Egnor | f18a01c | 2009-11-12 11:32:50 -0800 | [diff] [blame] | 253 | if (read == buffer.length && ((flags & DropBoxManager.IS_GZIPPED) == 0)) { |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 254 | output = new GZIPOutputStream(output); |
Dan Egnor | f18a01c | 2009-11-12 11:32:50 -0800 | [diff] [blame] | 255 | flags = flags | DropBoxManager.IS_GZIPPED; |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | do { |
| 259 | output.write(buffer, 0, read); |
| 260 | |
| 261 | long now = System.currentTimeMillis(); |
| 262 | if (now - lastTrim > 30 * 1000) { |
| 263 | max = trimToFit(); // In case data dribbles in slowly |
| 264 | lastTrim = now; |
| 265 | } |
| 266 | |
| 267 | read = input.read(buffer); |
| 268 | if (read <= 0) { |
Dianne Hackborn | 8bdf593 | 2010-10-15 12:54:40 -0700 | [diff] [blame] | 269 | FileUtils.sync(foutput); |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 270 | output.close(); // Get a final size measurement |
| 271 | output = null; |
| 272 | } else { |
| 273 | output.flush(); // So the size measurement is pseudo-reasonable |
| 274 | } |
| 275 | |
| 276 | long len = temp.length(); |
| 277 | if (len > max) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 278 | Slog.w(TAG, "Dropping: " + tag + " (" + temp.length() + " > " + max + " bytes)"); |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 279 | temp.delete(); |
| 280 | temp = null; // Pass temp = null to createEntry() to leave a tombstone |
| 281 | break; |
| 282 | } |
| 283 | } while (read > 0); |
| 284 | |
Hakan Still | b247536 | 2010-12-07 14:05:55 +0100 | [diff] [blame] | 285 | long time = createEntry(temp, tag, flags); |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 286 | temp = null; |
Hakan Still | b247536 | 2010-12-07 14:05:55 +0100 | [diff] [blame] | 287 | |
Craig Mautner | 26caf7a | 2012-03-04 17:17:59 -0800 | [diff] [blame] | 288 | final Intent dropboxIntent = new Intent(DropBoxManager.ACTION_DROPBOX_ENTRY_ADDED); |
Hakan Still | b247536 | 2010-12-07 14:05:55 +0100 | [diff] [blame] | 289 | dropboxIntent.putExtra(DropBoxManager.EXTRA_TAG, tag); |
| 290 | dropboxIntent.putExtra(DropBoxManager.EXTRA_TIME, time); |
Brad Fitzpatrick | 34165c6 | 2011-01-17 18:14:18 -0800 | [diff] [blame] | 291 | if (!mBooted) { |
| 292 | dropboxIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 293 | } |
Craig Mautner | 26caf7a | 2012-03-04 17:17:59 -0800 | [diff] [blame] | 294 | // Call sendBroadcast after returning from this call to avoid deadlock. In particular |
| 295 | // the caller may be holding the WindowManagerService lock but sendBroadcast requires a |
| 296 | // lock in ActivityManagerService. ActivityManagerService has been caught holding that |
| 297 | // very lock while waiting for the WindowManagerService lock. |
| 298 | mHandler.sendMessage(mHandler.obtainMessage(MSG_SEND_BROADCAST, dropboxIntent)); |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 299 | } catch (IOException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 300 | Slog.e(TAG, "Can't write: " + tag, e); |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 301 | } finally { |
Tim Kilbourn | 0935f3c | 2015-05-28 11:48:43 -0700 | [diff] [blame] | 302 | IoUtils.closeQuietly(output); |
| 303 | IoUtils.closeQuietly(input); |
Dan Egnor | 9524027 | 2009-10-27 18:23:39 -0700 | [diff] [blame] | 304 | entry.close(); |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 305 | if (temp != null) temp.delete(); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | public boolean isTagEnabled(String tag) { |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 310 | final long token = Binder.clearCallingIdentity(); |
| 311 | try { |
| 312 | return !"disabled".equals(Settings.Global.getString( |
| 313 | mContentResolver, Settings.Global.DROPBOX_TAG_PREFIX + tag)); |
| 314 | } finally { |
| 315 | Binder.restoreCallingIdentity(token); |
| 316 | } |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 317 | } |
| 318 | |
Dan Egnor | f18a01c | 2009-11-12 11:32:50 -0800 | [diff] [blame] | 319 | public synchronized DropBoxManager.Entry getNextEntry(String tag, long millis) { |
Tim Kilbourn | 0935f3c | 2015-05-28 11:48:43 -0700 | [diff] [blame] | 320 | if (getContext().checkCallingOrSelfPermission(android.Manifest.permission.READ_LOGS) |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 321 | != PackageManager.PERMISSION_GRANTED) { |
| 322 | throw new SecurityException("READ_LOGS permission required"); |
| 323 | } |
| 324 | |
| 325 | try { |
| 326 | init(); |
| 327 | } catch (IOException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 328 | Slog.e(TAG, "Can't init", e); |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 329 | return null; |
| 330 | } |
| 331 | |
Dan Egnor | b3b06fc | 2009-10-20 13:05:17 -0700 | [diff] [blame] | 332 | FileList list = tag == null ? mAllFiles : mFilesByTag.get(tag); |
| 333 | if (list == null) return null; |
| 334 | |
| 335 | for (EntryFile entry : list.contents.tailSet(new EntryFile(millis + 1))) { |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 336 | if (entry.tag == null) continue; |
Dan Egnor | f18a01c | 2009-11-12 11:32:50 -0800 | [diff] [blame] | 337 | if ((entry.flags & DropBoxManager.IS_EMPTY) != 0) { |
| 338 | return new DropBoxManager.Entry(entry.tag, entry.timestampMillis); |
Dan Egnor | 9524027 | 2009-10-27 18:23:39 -0700 | [diff] [blame] | 339 | } |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 340 | try { |
Dan Egnor | f18a01c | 2009-11-12 11:32:50 -0800 | [diff] [blame] | 341 | return new DropBoxManager.Entry( |
| 342 | entry.tag, entry.timestampMillis, entry.file, entry.flags); |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 343 | } catch (IOException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 344 | Slog.e(TAG, "Can't read: " + entry.file, e); |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 345 | // Continue to next file |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | return null; |
| 350 | } |
| 351 | |
| 352 | public synchronized void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Tim Kilbourn | 0935f3c | 2015-05-28 11:48:43 -0700 | [diff] [blame] | 353 | if (getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 354 | != PackageManager.PERMISSION_GRANTED) { |
Dan Egnor | f18a01c | 2009-11-12 11:32:50 -0800 | [diff] [blame] | 355 | pw.println("Permission Denial: Can't dump DropBoxManagerService"); |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 356 | return; |
| 357 | } |
| 358 | |
| 359 | try { |
| 360 | init(); |
| 361 | } catch (IOException e) { |
| 362 | pw.println("Can't initialize: " + e); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 363 | Slog.e(TAG, "Can't init", e); |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 364 | return; |
| 365 | } |
| 366 | |
Dan Egnor | 3d40df3 | 2009-11-17 13:36:31 -0800 | [diff] [blame] | 367 | if (PROFILE_DUMP) Debug.startMethodTracing("/data/trace/dropbox.dump"); |
| 368 | |
Dan Egnor | 5ec249a | 2009-11-25 13:16:47 -0800 | [diff] [blame] | 369 | StringBuilder out = new StringBuilder(); |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 370 | boolean doPrint = false, doFile = false; |
| 371 | ArrayList<String> searchArgs = new ArrayList<String>(); |
| 372 | for (int i = 0; args != null && i < args.length; i++) { |
| 373 | if (args[i].equals("-p") || args[i].equals("--print")) { |
| 374 | doPrint = true; |
| 375 | } else if (args[i].equals("-f") || args[i].equals("--file")) { |
| 376 | doFile = true; |
| 377 | } else if (args[i].startsWith("-")) { |
Dan Egnor | 5ec249a | 2009-11-25 13:16:47 -0800 | [diff] [blame] | 378 | out.append("Unknown argument: ").append(args[i]).append("\n"); |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 379 | } else { |
| 380 | searchArgs.add(args[i]); |
| 381 | } |
| 382 | } |
| 383 | |
Dan Egnor | 5ec249a | 2009-11-25 13:16:47 -0800 | [diff] [blame] | 384 | out.append("Drop box contents: ").append(mAllFiles.contents.size()).append(" entries\n"); |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 385 | |
| 386 | if (!searchArgs.isEmpty()) { |
Dan Egnor | 5ec249a | 2009-11-25 13:16:47 -0800 | [diff] [blame] | 387 | out.append("Searching for:"); |
| 388 | for (String a : searchArgs) out.append(" ").append(a); |
| 389 | out.append("\n"); |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 390 | } |
| 391 | |
Dan Egnor | 3d40df3 | 2009-11-17 13:36:31 -0800 | [diff] [blame] | 392 | int numFound = 0, numArgs = searchArgs.size(); |
| 393 | Time time = new Time(); |
Dan Egnor | 5ec249a | 2009-11-25 13:16:47 -0800 | [diff] [blame] | 394 | out.append("\n"); |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 395 | for (EntryFile entry : mAllFiles.contents) { |
Dan Egnor | 3d40df3 | 2009-11-17 13:36:31 -0800 | [diff] [blame] | 396 | time.set(entry.timestampMillis); |
| 397 | String date = time.format("%Y-%m-%d %H:%M:%S"); |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 398 | boolean match = true; |
Dan Egnor | 3d40df3 | 2009-11-17 13:36:31 -0800 | [diff] [blame] | 399 | for (int i = 0; i < numArgs && match; i++) { |
| 400 | String arg = searchArgs.get(i); |
| 401 | match = (date.contains(arg) || arg.equals(entry.tag)); |
| 402 | } |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 403 | if (!match) continue; |
| 404 | |
| 405 | numFound++; |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 406 | if (doPrint) out.append("========================================\n"); |
Dan Egnor | 5ec249a | 2009-11-25 13:16:47 -0800 | [diff] [blame] | 407 | out.append(date).append(" ").append(entry.tag == null ? "(no tag)" : entry.tag); |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 408 | if (entry.file == null) { |
Dan Egnor | 5ec249a | 2009-11-25 13:16:47 -0800 | [diff] [blame] | 409 | out.append(" (no file)\n"); |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 410 | continue; |
Dan Egnor | f18a01c | 2009-11-12 11:32:50 -0800 | [diff] [blame] | 411 | } else if ((entry.flags & DropBoxManager.IS_EMPTY) != 0) { |
Dan Egnor | 5ec249a | 2009-11-25 13:16:47 -0800 | [diff] [blame] | 412 | out.append(" (contents lost)\n"); |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 413 | continue; |
| 414 | } else { |
Dan Egnor | 5ec249a | 2009-11-25 13:16:47 -0800 | [diff] [blame] | 415 | out.append(" ("); |
| 416 | if ((entry.flags & DropBoxManager.IS_GZIPPED) != 0) out.append("compressed "); |
| 417 | out.append((entry.flags & DropBoxManager.IS_TEXT) != 0 ? "text" : "data"); |
| 418 | out.append(", ").append(entry.file.length()).append(" bytes)\n"); |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 419 | } |
| 420 | |
Dan Egnor | f18a01c | 2009-11-12 11:32:50 -0800 | [diff] [blame] | 421 | if (doFile || (doPrint && (entry.flags & DropBoxManager.IS_TEXT) == 0)) { |
Dan Egnor | 5ec249a | 2009-11-25 13:16:47 -0800 | [diff] [blame] | 422 | if (!doPrint) out.append(" "); |
| 423 | out.append(entry.file.getPath()).append("\n"); |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 424 | } |
| 425 | |
Dan Egnor | f18a01c | 2009-11-12 11:32:50 -0800 | [diff] [blame] | 426 | if ((entry.flags & DropBoxManager.IS_TEXT) != 0 && (doPrint || !doFile)) { |
| 427 | DropBoxManager.Entry dbe = null; |
Brad Fitzpatrick | 0c82240 | 2010-11-23 09:17:56 -0800 | [diff] [blame] | 428 | InputStreamReader isr = null; |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 429 | try { |
Dan Egnor | f18a01c | 2009-11-12 11:32:50 -0800 | [diff] [blame] | 430 | dbe = new DropBoxManager.Entry( |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 431 | entry.tag, entry.timestampMillis, entry.file, entry.flags); |
| 432 | |
| 433 | if (doPrint) { |
Brad Fitzpatrick | 0c82240 | 2010-11-23 09:17:56 -0800 | [diff] [blame] | 434 | isr = new InputStreamReader(dbe.getInputStream()); |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 435 | char[] buf = new char[4096]; |
| 436 | boolean newline = false; |
| 437 | for (;;) { |
Brad Fitzpatrick | 0c82240 | 2010-11-23 09:17:56 -0800 | [diff] [blame] | 438 | int n = isr.read(buf); |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 439 | if (n <= 0) break; |
Dan Egnor | 5ec249a | 2009-11-25 13:16:47 -0800 | [diff] [blame] | 440 | out.append(buf, 0, n); |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 441 | newline = (buf[n - 1] == '\n'); |
Dan Egnor | 42471dd | 2010-01-07 17:25:22 -0800 | [diff] [blame] | 442 | |
| 443 | // Flush periodically when printing to avoid out-of-memory. |
| 444 | if (out.length() > 65536) { |
| 445 | pw.write(out.toString()); |
| 446 | out.setLength(0); |
| 447 | } |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 448 | } |
Dan Egnor | 5ec249a | 2009-11-25 13:16:47 -0800 | [diff] [blame] | 449 | if (!newline) out.append("\n"); |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 450 | } else { |
| 451 | String text = dbe.getText(70); |
Jeff Sharkey | 22510ef | 2014-11-13 12:28:46 -0800 | [diff] [blame] | 452 | out.append(" "); |
| 453 | if (text == null) { |
| 454 | out.append("[null]"); |
| 455 | } else { |
| 456 | boolean truncated = (text.length() == 70); |
| 457 | out.append(text.trim().replace('\n', '/')); |
| 458 | if (truncated) out.append(" ..."); |
| 459 | } |
Dan Egnor | 5ec249a | 2009-11-25 13:16:47 -0800 | [diff] [blame] | 460 | out.append("\n"); |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 461 | } |
| 462 | } catch (IOException e) { |
Dan Egnor | 5ec249a | 2009-11-25 13:16:47 -0800 | [diff] [blame] | 463 | out.append("*** ").append(e.toString()).append("\n"); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 464 | Slog.e(TAG, "Can't read: " + entry.file, e); |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 465 | } finally { |
| 466 | if (dbe != null) dbe.close(); |
Brad Fitzpatrick | 0c82240 | 2010-11-23 09:17:56 -0800 | [diff] [blame] | 467 | if (isr != null) { |
| 468 | try { |
| 469 | isr.close(); |
| 470 | } catch (IOException unused) { |
| 471 | } |
| 472 | } |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 473 | } |
| 474 | } |
| 475 | |
Dan Egnor | 5ec249a | 2009-11-25 13:16:47 -0800 | [diff] [blame] | 476 | if (doPrint) out.append("\n"); |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 477 | } |
| 478 | |
Dan Egnor | 5ec249a | 2009-11-25 13:16:47 -0800 | [diff] [blame] | 479 | if (numFound == 0) out.append("(No entries found.)\n"); |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 480 | |
| 481 | if (args == null || args.length == 0) { |
Dan Egnor | 5ec249a | 2009-11-25 13:16:47 -0800 | [diff] [blame] | 482 | if (!doPrint) out.append("\n"); |
| 483 | out.append("Usage: dumpsys dropbox [--print|--file] [YYYY-mm-dd] [HH:MM:SS] [tag]\n"); |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 484 | } |
Dan Egnor | 3d40df3 | 2009-11-17 13:36:31 -0800 | [diff] [blame] | 485 | |
| 486 | pw.write(out.toString()); |
| 487 | if (PROFILE_DUMP) Debug.stopMethodTracing(); |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | /////////////////////////////////////////////////////////////////////////// |
| 491 | |
Xiaohui Chen | e4de5a0 | 2015-09-22 15:33:31 -0700 | [diff] [blame] | 492 | /** Chronologically sorted list of {@link EntryFile} */ |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 493 | private static final class FileList implements Comparable<FileList> { |
| 494 | public int blocks = 0; |
| 495 | public final TreeSet<EntryFile> contents = new TreeSet<EntryFile>(); |
| 496 | |
| 497 | /** Sorts bigger FileList instances before smaller ones. */ |
| 498 | public final int compareTo(FileList o) { |
| 499 | if (blocks != o.blocks) return o.blocks - blocks; |
| 500 | if (this == o) return 0; |
| 501 | if (hashCode() < o.hashCode()) return -1; |
| 502 | if (hashCode() > o.hashCode()) return 1; |
| 503 | return 0; |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | /** Metadata describing an on-disk log file. */ |
| 508 | private static final class EntryFile implements Comparable<EntryFile> { |
| 509 | public final String tag; |
| 510 | public final long timestampMillis; |
| 511 | public final int flags; |
| 512 | public final File file; |
| 513 | public final int blocks; |
| 514 | |
| 515 | /** Sorts earlier EntryFile instances before later ones. */ |
| 516 | public final int compareTo(EntryFile o) { |
| 517 | if (timestampMillis < o.timestampMillis) return -1; |
| 518 | if (timestampMillis > o.timestampMillis) return 1; |
| 519 | if (file != null && o.file != null) return file.compareTo(o.file); |
| 520 | if (o.file != null) return -1; |
| 521 | if (file != null) return 1; |
| 522 | if (this == o) return 0; |
| 523 | if (hashCode() < o.hashCode()) return -1; |
| 524 | if (hashCode() > o.hashCode()) return 1; |
| 525 | return 0; |
| 526 | } |
| 527 | |
| 528 | /** |
| 529 | * Moves an existing temporary file to a new log filename. |
| 530 | * @param temp file to rename |
| 531 | * @param dir to store file in |
| 532 | * @param tag to use for new log file name |
| 533 | * @param timestampMillis of log entry |
Dan Egnor | 9524027 | 2009-10-27 18:23:39 -0700 | [diff] [blame] | 534 | * @param flags for the entry data |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 535 | * @param blockSize to use for space accounting |
| 536 | * @throws IOException if the file can't be moved |
| 537 | */ |
| 538 | public EntryFile(File temp, File dir, String tag,long timestampMillis, |
| 539 | int flags, int blockSize) throws IOException { |
Dan Egnor | f18a01c | 2009-11-12 11:32:50 -0800 | [diff] [blame] | 540 | if ((flags & DropBoxManager.IS_EMPTY) != 0) throw new IllegalArgumentException(); |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 541 | |
| 542 | this.tag = tag; |
| 543 | this.timestampMillis = timestampMillis; |
| 544 | this.flags = flags; |
| 545 | this.file = new File(dir, Uri.encode(tag) + "@" + timestampMillis + |
Dan Egnor | f18a01c | 2009-11-12 11:32:50 -0800 | [diff] [blame] | 546 | ((flags & DropBoxManager.IS_TEXT) != 0 ? ".txt" : ".dat") + |
| 547 | ((flags & DropBoxManager.IS_GZIPPED) != 0 ? ".gz" : "")); |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 548 | |
| 549 | if (!temp.renameTo(this.file)) { |
| 550 | throw new IOException("Can't rename " + temp + " to " + this.file); |
| 551 | } |
| 552 | this.blocks = (int) ((this.file.length() + blockSize - 1) / blockSize); |
| 553 | } |
| 554 | |
| 555 | /** |
| 556 | * Creates a zero-length tombstone for a file whose contents were lost. |
| 557 | * @param dir to store file in |
| 558 | * @param tag to use for new log file name |
| 559 | * @param timestampMillis of log entry |
| 560 | * @throws IOException if the file can't be created. |
| 561 | */ |
| 562 | public EntryFile(File dir, String tag, long timestampMillis) throws IOException { |
| 563 | this.tag = tag; |
| 564 | this.timestampMillis = timestampMillis; |
Dan Egnor | f18a01c | 2009-11-12 11:32:50 -0800 | [diff] [blame] | 565 | this.flags = DropBoxManager.IS_EMPTY; |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 566 | this.file = new File(dir, Uri.encode(tag) + "@" + timestampMillis + ".lost"); |
| 567 | this.blocks = 0; |
| 568 | new FileOutputStream(this.file).close(); |
| 569 | } |
| 570 | |
| 571 | /** |
| 572 | * Extracts metadata from an existing on-disk log filename. |
| 573 | * @param file name of existing log file |
| 574 | * @param blockSize to use for space accounting |
| 575 | */ |
| 576 | public EntryFile(File file, int blockSize) { |
| 577 | this.file = file; |
| 578 | this.blocks = (int) ((this.file.length() + blockSize - 1) / blockSize); |
| 579 | |
| 580 | String name = file.getName(); |
| 581 | int at = name.lastIndexOf('@'); |
| 582 | if (at < 0) { |
| 583 | this.tag = null; |
| 584 | this.timestampMillis = 0; |
Dan Egnor | f18a01c | 2009-11-12 11:32:50 -0800 | [diff] [blame] | 585 | this.flags = DropBoxManager.IS_EMPTY; |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 586 | return; |
| 587 | } |
| 588 | |
| 589 | int flags = 0; |
| 590 | this.tag = Uri.decode(name.substring(0, at)); |
| 591 | if (name.endsWith(".gz")) { |
Dan Egnor | f18a01c | 2009-11-12 11:32:50 -0800 | [diff] [blame] | 592 | flags |= DropBoxManager.IS_GZIPPED; |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 593 | name = name.substring(0, name.length() - 3); |
| 594 | } |
| 595 | if (name.endsWith(".lost")) { |
Dan Egnor | f18a01c | 2009-11-12 11:32:50 -0800 | [diff] [blame] | 596 | flags |= DropBoxManager.IS_EMPTY; |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 597 | name = name.substring(at + 1, name.length() - 5); |
| 598 | } else if (name.endsWith(".txt")) { |
Dan Egnor | f18a01c | 2009-11-12 11:32:50 -0800 | [diff] [blame] | 599 | flags |= DropBoxManager.IS_TEXT; |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 600 | name = name.substring(at + 1, name.length() - 4); |
| 601 | } else if (name.endsWith(".dat")) { |
| 602 | name = name.substring(at + 1, name.length() - 4); |
| 603 | } else { |
Dan Egnor | f18a01c | 2009-11-12 11:32:50 -0800 | [diff] [blame] | 604 | this.flags = DropBoxManager.IS_EMPTY; |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 605 | this.timestampMillis = 0; |
| 606 | return; |
| 607 | } |
| 608 | this.flags = flags; |
| 609 | |
| 610 | long millis; |
Tobias Thierer | 28532d0 | 2016-04-21 14:52:10 +0100 | [diff] [blame] | 611 | try { millis = Long.parseLong(name); } catch (NumberFormatException e) { millis = 0; } |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 612 | this.timestampMillis = millis; |
| 613 | } |
| 614 | |
| 615 | /** |
| 616 | * Creates a EntryFile object with only a timestamp for comparison purposes. |
Xiaohui Chen | e4de5a0 | 2015-09-22 15:33:31 -0700 | [diff] [blame] | 617 | * @param millis to compare with. |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 618 | */ |
| 619 | public EntryFile(long millis) { |
| 620 | this.tag = null; |
| 621 | this.timestampMillis = millis; |
Dan Egnor | f18a01c | 2009-11-12 11:32:50 -0800 | [diff] [blame] | 622 | this.flags = DropBoxManager.IS_EMPTY; |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 623 | this.file = null; |
| 624 | this.blocks = 0; |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | /////////////////////////////////////////////////////////////////////////// |
| 629 | |
| 630 | /** If never run before, scans disk contents to build in-memory tracking data. */ |
| 631 | private synchronized void init() throws IOException { |
| 632 | if (mStatFs == null) { |
| 633 | if (!mDropBoxDir.isDirectory() && !mDropBoxDir.mkdirs()) { |
| 634 | throw new IOException("Can't mkdir: " + mDropBoxDir); |
| 635 | } |
| 636 | try { |
| 637 | mStatFs = new StatFs(mDropBoxDir.getPath()); |
| 638 | mBlockSize = mStatFs.getBlockSize(); |
| 639 | } catch (IllegalArgumentException e) { // StatFs throws this on error |
| 640 | throw new IOException("Can't statfs: " + mDropBoxDir); |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | if (mAllFiles == null) { |
| 645 | File[] files = mDropBoxDir.listFiles(); |
| 646 | if (files == null) throw new IOException("Can't list files: " + mDropBoxDir); |
| 647 | |
| 648 | mAllFiles = new FileList(); |
| 649 | mFilesByTag = new HashMap<String, FileList>(); |
| 650 | |
| 651 | // Scan pre-existing files. |
| 652 | for (File file : files) { |
| 653 | if (file.getName().endsWith(".tmp")) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 654 | Slog.i(TAG, "Cleaning temp file: " + file); |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 655 | file.delete(); |
| 656 | continue; |
| 657 | } |
| 658 | |
| 659 | EntryFile entry = new EntryFile(file, mBlockSize); |
| 660 | if (entry.tag == null) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 661 | Slog.w(TAG, "Unrecognized file: " + file); |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 662 | continue; |
| 663 | } else if (entry.timestampMillis == 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 664 | Slog.w(TAG, "Invalid filename: " + file); |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 665 | file.delete(); |
| 666 | continue; |
| 667 | } |
| 668 | |
| 669 | enrollEntry(entry); |
| 670 | } |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | /** Adds a disk log file to in-memory tracking for accounting and enumeration. */ |
| 675 | private synchronized void enrollEntry(EntryFile entry) { |
| 676 | mAllFiles.contents.add(entry); |
| 677 | mAllFiles.blocks += entry.blocks; |
| 678 | |
| 679 | // mFilesByTag is used for trimming, so don't list empty files. |
| 680 | // (Zero-length/lost files are trimmed by date from mAllFiles.) |
| 681 | |
| 682 | if (entry.tag != null && entry.file != null && entry.blocks > 0) { |
| 683 | FileList tagFiles = mFilesByTag.get(entry.tag); |
| 684 | if (tagFiles == null) { |
| 685 | tagFiles = new FileList(); |
| 686 | mFilesByTag.put(entry.tag, tagFiles); |
| 687 | } |
| 688 | tagFiles.contents.add(entry); |
| 689 | tagFiles.blocks += entry.blocks; |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | /** Moves a temporary file to a final log filename and enrolls it. */ |
Hakan Still | b247536 | 2010-12-07 14:05:55 +0100 | [diff] [blame] | 694 | private synchronized long createEntry(File temp, String tag, int flags) throws IOException { |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 695 | long t = System.currentTimeMillis(); |
| 696 | |
| 697 | // Require each entry to have a unique timestamp; if there are entries |
| 698 | // >10sec in the future (due to clock skew), drag them back to avoid |
| 699 | // keeping them around forever. |
| 700 | |
| 701 | SortedSet<EntryFile> tail = mAllFiles.contents.tailSet(new EntryFile(t + 10000)); |
| 702 | EntryFile[] future = null; |
| 703 | if (!tail.isEmpty()) { |
| 704 | future = tail.toArray(new EntryFile[tail.size()]); |
| 705 | tail.clear(); // Remove from mAllFiles |
| 706 | } |
| 707 | |
| 708 | if (!mAllFiles.contents.isEmpty()) { |
| 709 | t = Math.max(t, mAllFiles.contents.last().timestampMillis + 1); |
| 710 | } |
| 711 | |
| 712 | if (future != null) { |
| 713 | for (EntryFile late : future) { |
| 714 | mAllFiles.blocks -= late.blocks; |
| 715 | FileList tagFiles = mFilesByTag.get(late.tag); |
Dan Egnor | f283e36 | 2010-03-10 16:49:55 -0800 | [diff] [blame] | 716 | if (tagFiles != null && tagFiles.contents.remove(late)) { |
| 717 | tagFiles.blocks -= late.blocks; |
| 718 | } |
Dan Egnor | f18a01c | 2009-11-12 11:32:50 -0800 | [diff] [blame] | 719 | if ((late.flags & DropBoxManager.IS_EMPTY) == 0) { |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 720 | enrollEntry(new EntryFile( |
| 721 | late.file, mDropBoxDir, late.tag, t++, late.flags, mBlockSize)); |
| 722 | } else { |
| 723 | enrollEntry(new EntryFile(mDropBoxDir, late.tag, t++)); |
| 724 | } |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | if (temp == null) { |
| 729 | enrollEntry(new EntryFile(mDropBoxDir, tag, t)); |
| 730 | } else { |
| 731 | enrollEntry(new EntryFile(temp, mDropBoxDir, tag, t, flags, mBlockSize)); |
| 732 | } |
Hakan Still | b247536 | 2010-12-07 14:05:55 +0100 | [diff] [blame] | 733 | return t; |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | /** |
| 737 | * Trims the files on disk to make sure they aren't using too much space. |
| 738 | * @return the overall quota for storage (in bytes) |
| 739 | */ |
songjinshi | c5e249b | 2016-07-27 20:36:46 +0800 | [diff] [blame] | 740 | private synchronized long trimToFit() throws IOException { |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 741 | // Expunge aged items (including tombstones marking deleted data). |
| 742 | |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 743 | int ageSeconds = Settings.Global.getInt(mContentResolver, |
| 744 | Settings.Global.DROPBOX_AGE_SECONDS, DEFAULT_AGE_SECONDS); |
| 745 | int maxFiles = Settings.Global.getInt(mContentResolver, |
| 746 | Settings.Global.DROPBOX_MAX_FILES, DEFAULT_MAX_FILES); |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 747 | long cutoffMillis = System.currentTimeMillis() - ageSeconds * 1000; |
| 748 | while (!mAllFiles.contents.isEmpty()) { |
| 749 | EntryFile entry = mAllFiles.contents.first(); |
Dan Egnor | 3a8b0c1 | 2010-03-24 17:48:20 -0700 | [diff] [blame] | 750 | if (entry.timestampMillis > cutoffMillis && mAllFiles.contents.size() < maxFiles) break; |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 751 | |
| 752 | FileList tag = mFilesByTag.get(entry.tag); |
| 753 | if (tag != null && tag.contents.remove(entry)) tag.blocks -= entry.blocks; |
| 754 | if (mAllFiles.contents.remove(entry)) mAllFiles.blocks -= entry.blocks; |
| 755 | if (entry.file != null) entry.file.delete(); |
| 756 | } |
| 757 | |
| 758 | // Compute overall quota (a fraction of available free space) in blocks. |
| 759 | // The quota changes dynamically based on the amount of free space; |
| 760 | // that way when lots of data is available we can use it, but we'll get |
| 761 | // out of the way if storage starts getting tight. |
| 762 | |
| 763 | long uptimeMillis = SystemClock.uptimeMillis(); |
| 764 | if (uptimeMillis > mCachedQuotaUptimeMillis + QUOTA_RESCAN_MILLIS) { |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 765 | int quotaPercent = Settings.Global.getInt(mContentResolver, |
| 766 | Settings.Global.DROPBOX_QUOTA_PERCENT, DEFAULT_QUOTA_PERCENT); |
| 767 | int reservePercent = Settings.Global.getInt(mContentResolver, |
| 768 | Settings.Global.DROPBOX_RESERVE_PERCENT, DEFAULT_RESERVE_PERCENT); |
| 769 | int quotaKb = Settings.Global.getInt(mContentResolver, |
| 770 | Settings.Global.DROPBOX_QUOTA_KB, DEFAULT_QUOTA_KB); |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 771 | |
songjinshi | c5e249b | 2016-07-27 20:36:46 +0800 | [diff] [blame] | 772 | String dirPath = mDropBoxDir.getPath(); |
| 773 | try { |
| 774 | mStatFs.restat(dirPath); |
| 775 | } catch (IllegalArgumentException e) { // restat throws this on error |
| 776 | throw new IOException("Can't restat: " + mDropBoxDir); |
| 777 | } |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 778 | int available = mStatFs.getAvailableBlocks(); |
| 779 | int nonreserved = available - mStatFs.getBlockCount() * reservePercent / 100; |
| 780 | int maximum = quotaKb * 1024 / mBlockSize; |
| 781 | mCachedQuotaBlocks = Math.min(maximum, Math.max(0, nonreserved * quotaPercent / 100)); |
| 782 | mCachedQuotaUptimeMillis = uptimeMillis; |
| 783 | } |
| 784 | |
| 785 | // If we're using too much space, delete old items to make room. |
| 786 | // |
| 787 | // We trim each tag independently (this is why we keep per-tag lists). |
| 788 | // Space is "fairly" shared between tags -- they are all squeezed |
| 789 | // equally until enough space is reclaimed. |
| 790 | // |
| 791 | // A single circular buffer (a la logcat) would be simpler, but this |
| 792 | // way we can handle fat/bursty data (like 1MB+ bugreports, 300KB+ |
| 793 | // kernel crash dumps, and 100KB+ ANR reports) without swamping small, |
Dan Egnor | 3a8b0c1 | 2010-03-24 17:48:20 -0700 | [diff] [blame] | 794 | // well-behaved data streams (event statistics, profile data, etc). |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 795 | // |
| 796 | // Deleted files are replaced with zero-length tombstones to mark what |
| 797 | // was lost. Tombstones are expunged by age (see above). |
| 798 | |
| 799 | if (mAllFiles.blocks > mCachedQuotaBlocks) { |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 800 | // Find a fair share amount of space to limit each tag |
| 801 | int unsqueezed = mAllFiles.blocks, squeezed = 0; |
| 802 | TreeSet<FileList> tags = new TreeSet<FileList>(mFilesByTag.values()); |
| 803 | for (FileList tag : tags) { |
| 804 | if (squeezed > 0 && tag.blocks <= (mCachedQuotaBlocks - unsqueezed) / squeezed) { |
| 805 | break; |
| 806 | } |
| 807 | unsqueezed -= tag.blocks; |
| 808 | squeezed++; |
| 809 | } |
| 810 | int tagQuota = (mCachedQuotaBlocks - unsqueezed) / squeezed; |
| 811 | |
| 812 | // Remove old items from each tag until it meets the per-tag quota. |
| 813 | for (FileList tag : tags) { |
| 814 | if (mAllFiles.blocks < mCachedQuotaBlocks) break; |
| 815 | while (tag.blocks > tagQuota && !tag.contents.isEmpty()) { |
| 816 | EntryFile entry = tag.contents.first(); |
| 817 | if (tag.contents.remove(entry)) tag.blocks -= entry.blocks; |
| 818 | if (mAllFiles.contents.remove(entry)) mAllFiles.blocks -= entry.blocks; |
| 819 | |
| 820 | try { |
| 821 | if (entry.file != null) entry.file.delete(); |
| 822 | enrollEntry(new EntryFile(mDropBoxDir, entry.tag, entry.timestampMillis)); |
| 823 | } catch (IOException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 824 | Slog.e(TAG, "Can't write tombstone file", e); |
Dan Egnor | 4410ec8 | 2009-09-11 16:40:01 -0700 | [diff] [blame] | 825 | } |
| 826 | } |
| 827 | } |
| 828 | } |
| 829 | |
| 830 | return mCachedQuotaBlocks * mBlockSize; |
| 831 | } |
| 832 | } |