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