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