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