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