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.annotation.Nullable; 20 import android.app.ActivityManager; 21 import android.app.AppOpsManager; 22 import android.app.BroadcastOptions; 23 import android.content.BroadcastReceiver; 24 import android.content.ContentResolver; 25 import android.content.Context; 26 import android.content.Intent; 27 import android.content.IntentFilter; 28 import android.content.pm.PackageManager; 29 import android.content.res.Resources; 30 import android.database.ContentObserver; 31 import android.net.Uri; 32 import android.os.Binder; 33 import android.os.Bundle; 34 import android.os.BundleMerger; 35 import android.os.Debug; 36 import android.os.DropBoxManager; 37 import android.os.FileUtils; 38 import android.os.Handler; 39 import android.os.Looper; 40 import android.os.Message; 41 import android.os.ParcelFileDescriptor; 42 import android.os.ResultReceiver; 43 import android.os.ShellCallback; 44 import android.os.ShellCommand; 45 import android.os.StatFs; 46 import android.os.SystemClock; 47 import android.os.UserHandle; 48 import android.provider.Settings; 49 import android.service.dropbox.DropBoxManagerServiceDumpProto; 50 import android.system.ErrnoException; 51 import android.system.Os; 52 import android.system.OsConstants; 53 import android.system.StructStat; 54 import android.text.TextUtils; 55 import android.text.format.TimeMigrationUtils; 56 import android.util.ArrayMap; 57 import android.util.ArraySet; 58 import android.util.Slog; 59 import android.util.proto.ProtoOutputStream; 60 61 import com.android.internal.R; 62 import com.android.internal.annotations.GuardedBy; 63 import com.android.internal.annotations.VisibleForTesting; 64 import com.android.internal.os.IDropBoxManagerService; 65 import com.android.internal.util.DumpUtils; 66 import com.android.internal.util.FrameworkStatsLog; 67 import com.android.internal.util.ObjectUtils; 68 import com.android.server.DropBoxManagerInternal.EntrySource; 69 70 import libcore.io.IoUtils; 71 72 import java.io.ByteArrayInputStream; 73 import java.io.File; 74 import java.io.FileDescriptor; 75 import java.io.FileOutputStream; 76 import java.io.IOException; 77 import java.io.InputStream; 78 import java.io.InputStreamReader; 79 import java.io.PrintWriter; 80 import java.util.ArrayList; 81 import java.util.Arrays; 82 import java.util.List; 83 import java.util.SortedSet; 84 import java.util.TreeSet; 85 import java.util.zip.GZIPOutputStream; 86 87 /** 88 * Implementation of {@link IDropBoxManagerService} using the filesystem. 89 * Clients use {@link DropBoxManager} to access this service. 90 */ 91 public final class DropBoxManagerService extends SystemService { 92 private static final String TAG = "DropBoxManagerService"; 93 private static final int DEFAULT_AGE_SECONDS = 3 * 86400; 94 private static final int DEFAULT_MAX_FILES = 1000; 95 private static final int DEFAULT_MAX_FILES_LOWRAM = 300; 96 private static final int DEFAULT_QUOTA_KB = 10 * 1024; 97 private static final int DEFAULT_QUOTA_PERCENT = 10; 98 private static final int DEFAULT_RESERVE_PERCENT = 0; 99 private static final int QUOTA_RESCAN_MILLIS = 5000; 100 101 private static final boolean PROFILE_DUMP = false; 102 103 // Max number of bytes of a dropbox entry to write into protobuf. 104 private static final int PROTO_MAX_DATA_BYTES = 256 * 1024; 105 106 // Size beyond which to force-compress newly added entries. 107 private static final long COMPRESS_THRESHOLD_BYTES = 16_384; 108 109 // Tags that we should drop by default. 110 private static final List<String> DISABLED_BY_DEFAULT_TAGS = 111 List.of("data_app_wtf", "system_app_wtf", "system_server_wtf"); 112 113 // TODO: This implementation currently uses one file per entry, which is 114 // inefficient for smallish entries -- consider using a single queue file 115 // per tag (or even globally) instead. 116 117 // The cached context and derived objects 118 119 private final ContentResolver mContentResolver; 120 private final File mDropBoxDir; 121 122 // Accounting of all currently written log files (set in init()). 123 124 private FileList mAllFiles = null; 125 private ArrayMap<String, FileList> mFilesByTag = null; 126 127 private long mLowPriorityRateLimitPeriod = 0; 128 private ArraySet<String> mLowPriorityTags = null; 129 130 // Various bits of disk information 131 132 private StatFs mStatFs = null; 133 private int mBlockSize = 0; 134 private int mCachedQuotaBlocks = 0; // Space we can use: computed from free space, etc. 135 private long mCachedQuotaUptimeMillis = 0; 136 137 private volatile boolean mBooted = false; 138 139 // Provide a way to perform sendBroadcast asynchronously to avoid deadlocks. 140 private final DropBoxManagerBroadcastHandler mHandler; 141 142 private int mMaxFiles = -1; // -1 means uninitialized. 143 144 /** Receives events that might indicate a need to clean up files. */ 145 private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 146 @Override 147 public void onReceive(Context context, Intent intent) { 148 // For ACTION_DEVICE_STORAGE_LOW: 149 mCachedQuotaUptimeMillis = 0; // Force a re-check of quota size 150 151 // Run the initialization in the background (not this main thread). 152 // The init() and trimToFit() methods are synchronized, so they still 153 // block other users -- but at least the onReceive() call can finish. 154 new Thread() { 155 public void run() { 156 try { 157 init(); 158 trimToFit(); 159 } catch (IOException e) { 160 Slog.e(TAG, "Can't init", e); 161 } 162 } 163 }.start(); 164 } 165 }; 166 167 private final IDropBoxManagerService.Stub mStub = new IDropBoxManagerService.Stub() { 168 @Override 169 public void addData(String tag, byte[] data, int flags) { 170 DropBoxManagerService.this.addData(tag, data, flags); 171 } 172 173 @Override 174 public void addFile(String tag, ParcelFileDescriptor fd, int flags) { 175 DropBoxManagerService.this.addFile(tag, fd, flags); 176 } 177 178 @Override 179 public boolean isTagEnabled(String tag) { 180 return DropBoxManagerService.this.isTagEnabled(tag); 181 } 182 183 @Override 184 public DropBoxManager.Entry getNextEntry(String tag, long millis, String callingPackage) { 185 return getNextEntryWithAttribution(tag, millis, callingPackage, null); 186 } 187 188 @Override 189 public DropBoxManager.Entry getNextEntryWithAttribution(String tag, long millis, 190 String callingPackage, String callingAttributionTag) { 191 return DropBoxManagerService.this.getNextEntry(tag, millis, callingPackage, 192 callingAttributionTag); 193 } 194 195 @Override 196 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { 197 DropBoxManagerService.this.dump(fd, pw, args); 198 } 199 200 @Override 201 public void onShellCommand(FileDescriptor in, FileDescriptor out, 202 FileDescriptor err, String[] args, ShellCallback callback, 203 ResultReceiver resultReceiver) { 204 (new ShellCmd()).exec(this, in, out, err, args, callback, resultReceiver); 205 } 206 }; 207 208 private class ShellCmd extends ShellCommand { 209 @Override onCommand(String cmd)210 public int onCommand(String cmd) { 211 if (cmd == null) { 212 return handleDefaultCommands(cmd); 213 } 214 final PrintWriter pw = getOutPrintWriter(); 215 try { 216 switch (cmd) { 217 case "set-rate-limit": 218 final long period = Long.parseLong(getNextArgRequired()); 219 DropBoxManagerService.this.setLowPriorityRateLimit(period); 220 break; 221 case "add-low-priority": 222 final String addedTag = getNextArgRequired(); 223 DropBoxManagerService.this.addLowPriorityTag(addedTag); 224 break; 225 case "remove-low-priority": 226 final String removeTag = getNextArgRequired(); 227 DropBoxManagerService.this.removeLowPriorityTag(removeTag); 228 break; 229 case "restore-defaults": 230 DropBoxManagerService.this.restoreDefaults(); 231 break; 232 default: 233 return handleDefaultCommands(cmd); 234 } 235 } catch (Exception e) { 236 pw.println(e); 237 } 238 return 0; 239 } 240 241 @Override onHelp()242 public void onHelp() { 243 PrintWriter pw = getOutPrintWriter(); 244 pw.println("Dropbox manager service commands:"); 245 pw.println(" help"); 246 pw.println(" Print this help text."); 247 pw.println(" set-rate-limit PERIOD"); 248 pw.println(" Sets low priority broadcast rate limit period to PERIOD ms"); 249 pw.println(" add-low-priority TAG"); 250 pw.println(" Add TAG to dropbox low priority list"); 251 pw.println(" remove-low-priority TAG"); 252 pw.println(" Remove TAG from dropbox low priority list"); 253 pw.println(" restore-defaults"); 254 pw.println(" restore dropbox settings to defaults"); 255 } 256 } 257 258 private class DropBoxManagerBroadcastHandler extends Handler { 259 private final Object mLock = new Object(); 260 261 static final int MSG_SEND_BROADCAST = 1; 262 static final int MSG_SEND_DEFERRED_BROADCAST = 2; 263 264 @GuardedBy("mLock") 265 private final ArrayMap<String, Intent> mDeferredMap = new ArrayMap(); 266 DropBoxManagerBroadcastHandler(Looper looper)267 DropBoxManagerBroadcastHandler(Looper looper) { 268 super(looper); 269 } 270 271 @Override handleMessage(Message msg)272 public void handleMessage(Message msg) { 273 switch (msg.what) { 274 case MSG_SEND_BROADCAST: 275 prepareAndSendBroadcast((Intent) msg.obj, null); 276 break; 277 case MSG_SEND_DEFERRED_BROADCAST: 278 Intent deferredIntent; 279 synchronized (mLock) { 280 deferredIntent = mDeferredMap.remove((String) msg.obj); 281 } 282 if (deferredIntent != null) { 283 prepareAndSendBroadcast(deferredIntent, 284 createBroadcastOptions(deferredIntent)); 285 } 286 break; 287 } 288 } 289 prepareAndSendBroadcast(Intent intent, Bundle options)290 private void prepareAndSendBroadcast(Intent intent, Bundle options) { 291 if (!DropBoxManagerService.this.mBooted) { 292 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); 293 } 294 getContext().sendBroadcastAsUser(intent, UserHandle.ALL, 295 android.Manifest.permission.READ_LOGS, options); 296 } 297 createIntent(String tag, long time)298 private Intent createIntent(String tag, long time) { 299 final Intent dropboxIntent = new Intent(DropBoxManager.ACTION_DROPBOX_ENTRY_ADDED); 300 dropboxIntent.putExtra(DropBoxManager.EXTRA_TAG, tag); 301 dropboxIntent.putExtra(DropBoxManager.EXTRA_TIME, time); 302 dropboxIntent.putExtra(DropBoxManager.EXTRA_DROPPED_COUNT, 0); 303 return dropboxIntent; 304 } 305 createBroadcastOptions(Intent intent)306 private Bundle createBroadcastOptions(Intent intent) { 307 final BundleMerger extrasMerger = new BundleMerger(); 308 extrasMerger.setDefaultMergeStrategy(BundleMerger.STRATEGY_FIRST); 309 extrasMerger.setMergeStrategy(DropBoxManager.EXTRA_TIME, 310 BundleMerger.STRATEGY_COMPARABLE_MAX); 311 extrasMerger.setMergeStrategy(DropBoxManager.EXTRA_DROPPED_COUNT, 312 BundleMerger.STRATEGY_NUMBER_INCREMENT_FIRST_AND_ADD); 313 314 return BroadcastOptions.makeBasic() 315 .setDeliveryGroupPolicy(BroadcastOptions.DELIVERY_GROUP_POLICY_MERGED) 316 .setDeliveryGroupMatchingKey(DropBoxManager.ACTION_DROPBOX_ENTRY_ADDED, 317 intent.getStringExtra(DropBoxManager.EXTRA_TAG)) 318 .setDeliveryGroupExtrasMerger(extrasMerger) 319 .setDeferralPolicy(BroadcastOptions.DEFERRAL_POLICY_UNTIL_ACTIVE) 320 .toBundle(); 321 } 322 323 /** 324 * Schedule a dropbox broadcast to be sent asynchronously. 325 */ sendBroadcast(String tag, long time)326 public void sendBroadcast(String tag, long time) { 327 sendMessage(obtainMessage(MSG_SEND_BROADCAST, createIntent(tag, time))); 328 } 329 330 /** 331 * Possibly schedule a delayed dropbox broadcast. The broadcast will only be scheduled if 332 * no broadcast is currently scheduled. Otherwise updated the scheduled broadcast with the 333 * new intent information, effectively dropping the previous broadcast. 334 */ maybeDeferBroadcast(String tag, long time)335 public void maybeDeferBroadcast(String tag, long time) { 336 synchronized (mLock) { 337 final Intent intent = mDeferredMap.get(tag); 338 if (intent == null) { 339 // Schedule new delayed broadcast. 340 mDeferredMap.put(tag, createIntent(tag, time)); 341 sendMessageDelayed(obtainMessage(MSG_SEND_DEFERRED_BROADCAST, tag), 342 mLowPriorityRateLimitPeriod); 343 } else { 344 // Broadcast is already scheduled. Update intent with new data. 345 intent.putExtra(DropBoxManager.EXTRA_TIME, time); 346 final int dropped = intent.getIntExtra(DropBoxManager.EXTRA_DROPPED_COUNT, 0); 347 intent.putExtra(DropBoxManager.EXTRA_DROPPED_COUNT, dropped + 1); 348 return; 349 } 350 } 351 } 352 } 353 354 /** 355 * Creates an instance of managed drop box storage using the default dropbox 356 * directory. 357 * 358 * @param context to use for receiving free space & gservices intents 359 */ DropBoxManagerService(final Context context)360 public DropBoxManagerService(final Context context) { 361 this(context, new File("/data/system/dropbox"), FgThread.get().getLooper()); 362 } 363 364 /** 365 * Creates an instance of managed drop box storage. Normally there is one of these 366 * run by the system, but others can be created for testing and other purposes. 367 * 368 * @param context to use for receiving free space & gservices intents 369 * @param path to store drop box entries in 370 */ 371 @VisibleForTesting DropBoxManagerService(final Context context, File path, Looper looper)372 public DropBoxManagerService(final Context context, File path, Looper looper) { 373 super(context); 374 mDropBoxDir = path; 375 mContentResolver = getContext().getContentResolver(); 376 mHandler = new DropBoxManagerBroadcastHandler(looper); 377 LocalServices.addService(DropBoxManagerInternal.class, new DropBoxManagerInternalImpl()); 378 } 379 380 @Override onStart()381 public void onStart() { 382 publishBinderService(Context.DROPBOX_SERVICE, mStub); 383 384 // The real work gets done lazily in init() -- that way service creation always 385 // succeeds, and things like disk problems cause individual method failures. 386 } 387 388 @Override onBootPhase(int phase)389 public void onBootPhase(int phase) { 390 switch (phase) { 391 case PHASE_SYSTEM_SERVICES_READY: 392 IntentFilter filter = new IntentFilter(); 393 filter.addAction(Intent.ACTION_DEVICE_STORAGE_LOW); 394 getContext().registerReceiver(mReceiver, filter); 395 396 mContentResolver.registerContentObserver( 397 Settings.Global.CONTENT_URI, true, 398 new ContentObserver(new Handler()) { 399 @Override 400 public void onChange(boolean selfChange) { 401 mReceiver.onReceive(getContext(), (Intent) null); 402 } 403 }); 404 405 getLowPriorityResourceConfigs(); 406 break; 407 408 case PHASE_BOOT_COMPLETED: 409 mBooted = true; 410 break; 411 } 412 } 413 414 /** Retrieves the binder stub -- for test instances */ getServiceStub()415 public IDropBoxManagerService getServiceStub() { 416 return mStub; 417 } 418 addData(String tag, byte[] data, int flags)419 public void addData(String tag, byte[] data, int flags) { 420 addEntry(tag, new ByteArrayInputStream(data), data.length, flags); 421 } 422 addFile(String tag, ParcelFileDescriptor fd, int flags)423 public void addFile(String tag, ParcelFileDescriptor fd, int flags) { 424 final StructStat stat; 425 try { 426 stat = Os.fstat(fd.getFileDescriptor()); 427 428 // Verify caller isn't playing games with pipes or sockets 429 if (!OsConstants.S_ISREG(stat.st_mode)) { 430 throw new IllegalArgumentException(tag + " entry must be real file"); 431 } 432 } catch (ErrnoException e) { 433 throw new IllegalArgumentException(e); 434 } 435 436 addEntry(tag, new ParcelFileDescriptor.AutoCloseInputStream(fd), stat.st_size, flags); 437 } 438 addEntry(String tag, InputStream in, long length, int flags)439 public void addEntry(String tag, InputStream in, long length, int flags) { 440 // If entry being added is large, and if it's not already compressed, 441 // then we'll force compress it during write 442 boolean forceCompress = false; 443 if ((flags & DropBoxManager.IS_GZIPPED) == 0 444 && length > COMPRESS_THRESHOLD_BYTES) { 445 forceCompress = true; 446 flags |= DropBoxManager.IS_GZIPPED; 447 } 448 449 addEntry(tag, new SimpleEntrySource(in, length, forceCompress), flags); 450 } 451 452 /** 453 * Simple entry which contains data ready to be written. 454 */ 455 public static class SimpleEntrySource implements EntrySource { 456 private final InputStream in; 457 private final long length; 458 private final boolean forceCompress; 459 SimpleEntrySource(InputStream in, long length, boolean forceCompress)460 public SimpleEntrySource(InputStream in, long length, boolean forceCompress) { 461 this.in = in; 462 this.length = length; 463 this.forceCompress = forceCompress; 464 } 465 length()466 public long length() { 467 return length; 468 } 469 470 @Override writeTo(FileDescriptor fd)471 public void writeTo(FileDescriptor fd) throws IOException { 472 // No need to buffer the output here, since data is either coming 473 // from an in-memory buffer, or another file on disk; if we buffered 474 // we'd lose out on sendfile() optimizations 475 if (forceCompress) { 476 final GZIPOutputStream gzipOutputStream = 477 new GZIPOutputStream(new FileOutputStream(fd)); 478 FileUtils.copy(in, gzipOutputStream); 479 gzipOutputStream.close(); 480 } else { 481 FileUtils.copy(in, new FileOutputStream(fd)); 482 } 483 } 484 485 @Override close()486 public void close() throws IOException { 487 FileUtils.closeQuietly(in); 488 } 489 } 490 addEntry(String tag, EntrySource entry, int flags)491 public void addEntry(String tag, EntrySource entry, int flags) { 492 File temp = null; 493 try { 494 Slog.i(TAG, "add tag=" + tag + " isTagEnabled=" + isTagEnabled(tag) 495 + " flags=0x" + Integer.toHexString(flags)); 496 if ((flags & DropBoxManager.IS_EMPTY) != 0) throw new IllegalArgumentException(); 497 498 init(); 499 500 // Bail early if we know tag is disabled 501 if (!isTagEnabled(tag)) return; 502 503 // Drop entries which are too large for our quota 504 final long length = entry.length(); 505 final long max = trimToFit(); 506 if (length > max) { 507 // Log and fall through to create empty tombstone below 508 Slog.w(TAG, "Dropping: " + tag + " (" + length + " > " + max + " bytes)"); 509 logDropboxDropped( 510 FrameworkStatsLog.DROPBOX_ENTRY_DROPPED__DROP_REASON__ENTRY_TOO_LARGE, 511 tag, 512 0); 513 } else { 514 temp = new File(mDropBoxDir, "drop" + Thread.currentThread().getId() + ".tmp"); 515 try (FileOutputStream out = new FileOutputStream(temp)) { 516 entry.writeTo(out.getFD()); 517 } 518 } 519 520 // Writing above succeeded, so create the finalized entry 521 long time = createEntry(temp, tag, flags); 522 temp = null; 523 524 // Call sendBroadcast after returning from this call to avoid deadlock. In particular 525 // the caller may be holding the WindowManagerService lock but sendBroadcast requires a 526 // lock in ActivityManagerService. ActivityManagerService has been caught holding that 527 // very lock while waiting for the WindowManagerService lock. 528 if (mLowPriorityTags != null && mLowPriorityTags.contains(tag)) { 529 // Rate limit low priority Dropbox entries 530 mHandler.maybeDeferBroadcast(tag, time); 531 } else { 532 mHandler.sendBroadcast(tag, time); 533 } 534 } catch (IOException e) { 535 Slog.e(TAG, "Can't write: " + tag, e); 536 logDropboxDropped( 537 FrameworkStatsLog.DROPBOX_ENTRY_DROPPED__DROP_REASON__WRITE_FAILURE, 538 tag, 539 0); 540 } finally { 541 IoUtils.closeQuietly(entry); 542 if (temp != null) temp.delete(); 543 } 544 } 545 logDropboxDropped(int reason, String tag, long entryAge)546 private void logDropboxDropped(int reason, String tag, long entryAge) { 547 FrameworkStatsLog.write(FrameworkStatsLog.DROPBOX_ENTRY_DROPPED, reason, tag, entryAge); 548 } 549 isTagEnabled(String tag)550 public boolean isTagEnabled(String tag) { 551 final long token = Binder.clearCallingIdentity(); 552 try { 553 if (DISABLED_BY_DEFAULT_TAGS.contains(tag)) { 554 return "enabled".equals(Settings.Global.getString( 555 mContentResolver, Settings.Global.DROPBOX_TAG_PREFIX + tag)); 556 } else { 557 return !"disabled".equals(Settings.Global.getString( 558 mContentResolver, Settings.Global.DROPBOX_TAG_PREFIX + tag)); 559 } 560 } finally { 561 Binder.restoreCallingIdentity(token); 562 } 563 } 564 checkPermission(int callingUid, String callingPackage, @Nullable String callingAttributionTag)565 private boolean checkPermission(int callingUid, String callingPackage, 566 @Nullable String callingAttributionTag) { 567 // If callers have this permission, then we don't need to check 568 // USAGE_STATS, because they are part of the system and have agreed to 569 // check USAGE_STATS before passing the data along. 570 if (getContext().checkCallingPermission(android.Manifest.permission.PEEK_DROPBOX_DATA) 571 == PackageManager.PERMISSION_GRANTED) { 572 return true; 573 } 574 575 // Callers always need this permission 576 getContext().enforceCallingOrSelfPermission( 577 android.Manifest.permission.READ_LOGS, TAG); 578 579 // Callers also need the ability to read usage statistics 580 switch (getContext().getSystemService(AppOpsManager.class).noteOp( 581 AppOpsManager.OP_GET_USAGE_STATS, callingUid, callingPackage, callingAttributionTag, 582 null)) { 583 case AppOpsManager.MODE_ALLOWED: 584 return true; 585 case AppOpsManager.MODE_DEFAULT: 586 getContext().enforceCallingOrSelfPermission( 587 android.Manifest.permission.PACKAGE_USAGE_STATS, TAG); 588 return true; 589 default: 590 return false; 591 } 592 } 593 getNextEntry(String tag, long millis, String callingPackage, @Nullable String callingAttributionTag)594 public synchronized DropBoxManager.Entry getNextEntry(String tag, long millis, 595 String callingPackage, @Nullable String callingAttributionTag) { 596 if (!checkPermission(Binder.getCallingUid(), callingPackage, callingAttributionTag)) { 597 return null; 598 } 599 600 try { 601 init(); 602 } catch (IOException e) { 603 Slog.e(TAG, "Can't init", e); 604 return null; 605 } 606 607 FileList list = tag == null ? mAllFiles : mFilesByTag.get(tag); 608 if (list == null) return null; 609 610 for (EntryFile entry : list.contents.tailSet(new EntryFile(millis + 1))) { 611 if (entry.tag == null) continue; 612 if ((entry.flags & DropBoxManager.IS_EMPTY) != 0) { 613 return new DropBoxManager.Entry(entry.tag, entry.timestampMillis); 614 } 615 final File file = entry.getFile(mDropBoxDir); 616 try { 617 return new DropBoxManager.Entry( 618 entry.tag, entry.timestampMillis, file, entry.flags); 619 } catch (IOException e) { 620 Slog.wtf(TAG, "Can't read: " + file, e); 621 // Continue to next file 622 } 623 } 624 625 return null; 626 } 627 setLowPriorityRateLimit(long period)628 private synchronized void setLowPriorityRateLimit(long period) { 629 mLowPriorityRateLimitPeriod = period; 630 } 631 addLowPriorityTag(String tag)632 private synchronized void addLowPriorityTag(String tag) { 633 mLowPriorityTags.add(tag); 634 } 635 removeLowPriorityTag(String tag)636 private synchronized void removeLowPriorityTag(String tag) { 637 mLowPriorityTags.remove(tag); 638 } 639 restoreDefaults()640 private synchronized void restoreDefaults() { 641 getLowPriorityResourceConfigs(); 642 } 643 dump(FileDescriptor fd, PrintWriter pw, String[] args)644 public synchronized void dump(FileDescriptor fd, PrintWriter pw, String[] args) { 645 if (!DumpUtils.checkDumpAndUsageStatsPermission(getContext(), TAG, pw)) return; 646 647 try { 648 init(); 649 } catch (IOException e) { 650 pw.println("Can't initialize: " + e); 651 Slog.e(TAG, "Can't init", e); 652 return; 653 } 654 655 if (PROFILE_DUMP) Debug.startMethodTracing("/data/trace/dropbox.dump"); 656 657 StringBuilder out = new StringBuilder(); 658 boolean doPrint = false, doFile = false; 659 boolean dumpProto = false; 660 ArrayList<String> searchArgs = new ArrayList<String>(); 661 for (int i = 0; args != null && i < args.length; i++) { 662 if (args[i].equals("-p") || args[i].equals("--print")) { 663 doPrint = true; 664 } else if (args[i].equals("-f") || args[i].equals("--file")) { 665 doFile = true; 666 } else if (args[i].equals("--proto")) { 667 dumpProto = true; 668 } else if (args[i].equals("-h") || args[i].equals("--help")) { 669 pw.println("Dropbox (dropbox) dump options:"); 670 pw.println(" [-h|--help] [-p|--print] [-f|--file] [timestamp]"); 671 pw.println(" -h|--help: print this help"); 672 pw.println(" -p|--print: print full contents of each entry"); 673 pw.println(" -f|--file: print path of each entry's file"); 674 pw.println(" --proto: dump data to proto"); 675 pw.println(" [timestamp] optionally filters to only those entries."); 676 return; 677 } else if (args[i].startsWith("-")) { 678 out.append("Unknown argument: ").append(args[i]).append("\n"); 679 } else { 680 searchArgs.add(args[i]); 681 } 682 } 683 684 if (dumpProto) { 685 dumpProtoLocked(fd, searchArgs); 686 return; 687 } 688 689 out.append("Drop box contents: ").append(mAllFiles.contents.size()).append(" entries\n"); 690 out.append("Max entries: ").append(mMaxFiles).append("\n"); 691 692 out.append("Low priority rate limit period: "); 693 out.append(mLowPriorityRateLimitPeriod).append(" ms\n"); 694 out.append("Low priority tags: ").append(mLowPriorityTags).append("\n"); 695 696 if (!searchArgs.isEmpty()) { 697 out.append("Searching for:"); 698 for (String a : searchArgs) out.append(" ").append(a); 699 out.append("\n"); 700 } 701 702 int numFound = 0; 703 out.append("\n"); 704 for (EntryFile entry : mAllFiles.contents) { 705 if (!matchEntry(entry, searchArgs)) continue; 706 707 numFound++; 708 if (doPrint) out.append("========================================\n"); 709 710 String date = TimeMigrationUtils.formatMillisWithFixedFormat(entry.timestampMillis); 711 out.append(date).append(" ").append(entry.tag == null ? "(no tag)" : entry.tag); 712 713 final File file = entry.getFile(mDropBoxDir); 714 if (file == null) { 715 out.append(" (no file)\n"); 716 continue; 717 } else if ((entry.flags & DropBoxManager.IS_EMPTY) != 0) { 718 out.append(" (contents lost)\n"); 719 continue; 720 } else { 721 out.append(" ("); 722 if ((entry.flags & DropBoxManager.IS_GZIPPED) != 0) out.append("compressed "); 723 out.append((entry.flags & DropBoxManager.IS_TEXT) != 0 ? "text" : "data"); 724 out.append(", ").append(file.length()).append(" bytes)\n"); 725 } 726 727 if (doFile || (doPrint && (entry.flags & DropBoxManager.IS_TEXT) == 0)) { 728 if (!doPrint) out.append(" "); 729 out.append(file.getPath()).append("\n"); 730 } 731 732 if ((entry.flags & DropBoxManager.IS_TEXT) != 0 && doPrint) { 733 DropBoxManager.Entry dbe = null; 734 InputStreamReader isr = null; 735 try { 736 dbe = new DropBoxManager.Entry( 737 entry.tag, entry.timestampMillis, file, entry.flags); 738 739 if (doPrint) { 740 isr = new InputStreamReader(dbe.getInputStream()); 741 char[] buf = new char[4096]; 742 boolean newline = false; 743 for (;;) { 744 int n = isr.read(buf); 745 if (n <= 0) break; 746 out.append(buf, 0, n); 747 newline = (buf[n - 1] == '\n'); 748 749 // Flush periodically when printing to avoid out-of-memory. 750 if (out.length() > 65536) { 751 pw.write(out.toString()); 752 out.setLength(0); 753 } 754 } 755 if (!newline) out.append("\n"); 756 } 757 } catch (IOException e) { 758 out.append("*** ").append(e.toString()).append("\n"); 759 Slog.e(TAG, "Can't read: " + file, e); 760 } finally { 761 if (dbe != null) dbe.close(); 762 if (isr != null) { 763 try { 764 isr.close(); 765 } catch (IOException unused) { 766 } 767 } 768 } 769 } 770 771 if (doPrint) out.append("\n"); 772 } 773 774 if (numFound == 0) out.append("(No entries found.)\n"); 775 776 if (args == null || args.length == 0) { 777 if (!doPrint) out.append("\n"); 778 out.append("Usage: dumpsys dropbox [--print|--file] [YYYY-mm-dd] [HH:MM:SS] [tag]\n"); 779 } 780 781 pw.write(out.toString()); 782 if (PROFILE_DUMP) Debug.stopMethodTracing(); 783 } 784 matchEntry(EntryFile entry, ArrayList<String> searchArgs)785 private boolean matchEntry(EntryFile entry, ArrayList<String> searchArgs) { 786 String date = TimeMigrationUtils.formatMillisWithFixedFormat(entry.timestampMillis); 787 boolean match = true; 788 int numArgs = searchArgs.size(); 789 for (int i = 0; i < numArgs && match; i++) { 790 String arg = searchArgs.get(i); 791 match = (date.contains(arg) || arg.equals(entry.tag)); 792 } 793 return match; 794 } 795 dumpProtoLocked(FileDescriptor fd, ArrayList<String> searchArgs)796 private void dumpProtoLocked(FileDescriptor fd, ArrayList<String> searchArgs) { 797 final ProtoOutputStream proto = new ProtoOutputStream(fd); 798 799 for (EntryFile entry : mAllFiles.contents) { 800 if (!matchEntry(entry, searchArgs)) continue; 801 802 final File file = entry.getFile(mDropBoxDir); 803 if ((file == null) || ((entry.flags & DropBoxManager.IS_EMPTY) != 0)) { 804 continue; 805 } 806 807 final long bToken = proto.start(DropBoxManagerServiceDumpProto.ENTRIES); 808 proto.write(DropBoxManagerServiceDumpProto.Entry.TIME_MS, entry.timestampMillis); 809 try ( 810 DropBoxManager.Entry dbe = new DropBoxManager.Entry( 811 entry.tag, entry.timestampMillis, file, entry.flags); 812 InputStream is = dbe.getInputStream(); 813 ) { 814 if (is != null) { 815 byte[] buf = new byte[PROTO_MAX_DATA_BYTES]; 816 int readBytes = 0; 817 int n = 0; 818 while (n >= 0 && (readBytes += n) < PROTO_MAX_DATA_BYTES) { 819 n = is.read(buf, readBytes, PROTO_MAX_DATA_BYTES - readBytes); 820 } 821 proto.write(DropBoxManagerServiceDumpProto.Entry.DATA, 822 Arrays.copyOf(buf, readBytes)); 823 } 824 } catch (IOException e) { 825 Slog.e(TAG, "Can't read: " + file, e); 826 } 827 828 proto.end(bToken); 829 } 830 831 proto.flush(); 832 } 833 834 /////////////////////////////////////////////////////////////////////////// 835 836 /** Chronologically sorted list of {@link EntryFile} */ 837 private static final class FileList implements Comparable<FileList> { 838 public int blocks = 0; 839 public final TreeSet<EntryFile> contents = new TreeSet<EntryFile>(); 840 841 /** Sorts bigger FileList instances before smaller ones. */ compareTo(FileList o)842 public final int compareTo(FileList o) { 843 if (blocks != o.blocks) return o.blocks - blocks; 844 if (this == o) return 0; 845 if (hashCode() < o.hashCode()) return -1; 846 if (hashCode() > o.hashCode()) return 1; 847 return 0; 848 } 849 } 850 851 /** 852 * Metadata describing an on-disk log file. 853 * 854 * Note its instances do no have knowledge on what directory they're stored, just to save 855 * 4/8 bytes per instance. Instead, {@link #getFile} takes a directory so it can build a 856 * fullpath. 857 */ 858 @VisibleForTesting 859 static final class EntryFile implements Comparable<EntryFile> { 860 public final String tag; 861 public final long timestampMillis; 862 public final int flags; 863 public final int blocks; 864 865 /** Sorts earlier EntryFile instances before later ones. */ compareTo(EntryFile o)866 public final int compareTo(EntryFile o) { 867 int comp = Long.compare(timestampMillis, o.timestampMillis); 868 if (comp != 0) return comp; 869 870 comp = ObjectUtils.compare(tag, o.tag); 871 if (comp != 0) return comp; 872 873 comp = Integer.compare(flags, o.flags); 874 if (comp != 0) return comp; 875 876 return Integer.compare(hashCode(), o.hashCode()); 877 } 878 879 /** 880 * Moves an existing temporary file to a new log filename. 881 * 882 * @param temp file to rename 883 * @param dir to store file in 884 * @param tag to use for new log file name 885 * @param timestampMillis of log entry 886 * @param flags for the entry data 887 * @param blockSize to use for space accounting 888 * @throws IOException if the file can't be moved 889 */ EntryFile(File temp, File dir, String tag,long timestampMillis, int flags, int blockSize)890 public EntryFile(File temp, File dir, String tag,long timestampMillis, 891 int flags, int blockSize) throws IOException { 892 if ((flags & DropBoxManager.IS_EMPTY) != 0) throw new IllegalArgumentException(); 893 894 this.tag = TextUtils.safeIntern(tag); 895 this.timestampMillis = timestampMillis; 896 this.flags = flags; 897 898 final File file = this.getFile(dir); 899 if (!temp.renameTo(file)) { 900 throw new IOException("Can't rename " + temp + " to " + file); 901 } 902 this.blocks = (int) ((file.length() + blockSize - 1) / blockSize); 903 } 904 905 /** 906 * Creates a zero-length tombstone for a file whose contents were lost. 907 * 908 * @param dir to store file in 909 * @param tag to use for new log file name 910 * @param timestampMillis of log entry 911 * @throws IOException if the file can't be created. 912 */ EntryFile(File dir, String tag, long timestampMillis)913 public EntryFile(File dir, String tag, long timestampMillis) throws IOException { 914 this.tag = TextUtils.safeIntern(tag); 915 this.timestampMillis = timestampMillis; 916 this.flags = DropBoxManager.IS_EMPTY; 917 this.blocks = 0; 918 new FileOutputStream(getFile(dir)).close(); 919 } 920 921 /** 922 * Extracts metadata from an existing on-disk log filename. 923 * 924 * Note when a filename is not recognizable, it will create an instance that 925 * {@link #hasFile()} would return false on, and also remove the file. 926 * 927 * @param file name of existing log file 928 * @param blockSize to use for space accounting 929 */ EntryFile(File file, int blockSize)930 public EntryFile(File file, int blockSize) { 931 932 boolean parseFailure = false; 933 934 String name = file.getName(); 935 int flags = 0; 936 String tag = null; 937 long millis = 0; 938 939 final int at = name.lastIndexOf('@'); 940 if (at < 0) { 941 parseFailure = true; 942 } else { 943 tag = Uri.decode(name.substring(0, at)); 944 if (name.endsWith(".gz")) { 945 flags |= DropBoxManager.IS_GZIPPED; 946 name = name.substring(0, name.length() - 3); 947 } 948 if (name.endsWith(".lost")) { 949 flags |= DropBoxManager.IS_EMPTY; 950 name = name.substring(at + 1, name.length() - 5); 951 } else if (name.endsWith(".txt")) { 952 flags |= DropBoxManager.IS_TEXT; 953 name = name.substring(at + 1, name.length() - 4); 954 } else if (name.endsWith(".dat")) { 955 name = name.substring(at + 1, name.length() - 4); 956 } else { 957 parseFailure = true; 958 } 959 if (!parseFailure) { 960 try { 961 millis = Long.parseLong(name); 962 } catch (NumberFormatException e) { 963 parseFailure = true; 964 } 965 } 966 } 967 if (parseFailure) { 968 Slog.wtf(TAG, "Invalid filename: " + file); 969 970 // Remove the file and return an empty instance. 971 file.delete(); 972 this.tag = null; 973 this.flags = DropBoxManager.IS_EMPTY; 974 this.timestampMillis = 0; 975 this.blocks = 0; 976 return; 977 } 978 979 this.blocks = (int) ((file.length() + blockSize - 1) / blockSize); 980 this.tag = TextUtils.safeIntern(tag); 981 this.flags = flags; 982 this.timestampMillis = millis; 983 } 984 985 /** 986 * Creates a EntryFile object with only a timestamp for comparison purposes. 987 * @param millis to compare with. 988 */ EntryFile(long millis)989 public EntryFile(long millis) { 990 this.tag = null; 991 this.timestampMillis = millis; 992 this.flags = DropBoxManager.IS_EMPTY; 993 this.blocks = 0; 994 } 995 996 /** 997 * @return whether an entry actually has a backing file, or it's an empty "tombstone" 998 * entry. 999 */ hasFile()1000 public boolean hasFile() { 1001 return tag != null; 1002 } 1003 1004 /** @return File extension for the flags. */ getExtension()1005 private String getExtension() { 1006 if ((flags & DropBoxManager.IS_EMPTY) != 0) { 1007 return ".lost"; 1008 } 1009 return ((flags & DropBoxManager.IS_TEXT) != 0 ? ".txt" : ".dat") + 1010 ((flags & DropBoxManager.IS_GZIPPED) != 0 ? ".gz" : ""); 1011 } 1012 1013 /** 1014 * @return filename for this entry without the pathname. 1015 */ getFilename()1016 public String getFilename() { 1017 return hasFile() ? Uri.encode(tag) + "@" + timestampMillis + getExtension() : null; 1018 } 1019 1020 /** 1021 * Get a full-path {@link File} representing this entry. 1022 * @param dir Parent directly. The caller needs to pass it because {@link EntryFile}s don't 1023 * know in which directory they're stored. 1024 */ getFile(File dir)1025 public File getFile(File dir) { 1026 return hasFile() ? new File(dir, getFilename()) : null; 1027 } 1028 1029 /** 1030 * If an entry has a backing file, remove it. 1031 */ deleteFile(File dir)1032 public void deleteFile(File dir) { 1033 if (hasFile()) { 1034 getFile(dir).delete(); 1035 } 1036 } 1037 } 1038 1039 /////////////////////////////////////////////////////////////////////////// 1040 1041 /** If never run before, scans disk contents to build in-memory tracking data. */ init()1042 private synchronized void init() throws IOException { 1043 if (mStatFs == null) { 1044 if (!mDropBoxDir.isDirectory() && !mDropBoxDir.mkdirs()) { 1045 throw new IOException("Can't mkdir: " + mDropBoxDir); 1046 } 1047 try { 1048 mStatFs = new StatFs(mDropBoxDir.getPath()); 1049 mBlockSize = mStatFs.getBlockSize(); 1050 } catch (IllegalArgumentException e) { // StatFs throws this on error 1051 throw new IOException("Can't statfs: " + mDropBoxDir); 1052 } 1053 } 1054 1055 if (mAllFiles == null) { 1056 File[] files = mDropBoxDir.listFiles(); 1057 if (files == null) throw new IOException("Can't list files: " + mDropBoxDir); 1058 1059 mAllFiles = new FileList(); 1060 mFilesByTag = new ArrayMap<>(); 1061 1062 // Scan pre-existing files. 1063 for (File file : files) { 1064 if (file.getName().endsWith(".tmp")) { 1065 Slog.i(TAG, "Cleaning temp file: " + file); 1066 file.delete(); 1067 continue; 1068 } 1069 1070 EntryFile entry = new EntryFile(file, mBlockSize); 1071 1072 if (entry.hasFile()) { 1073 // Enroll only when the filename is valid. Otherwise the above constructor 1074 // has removed the file already. 1075 enrollEntry(entry); 1076 } 1077 } 1078 } 1079 } 1080 1081 /** Adds a disk log file to in-memory tracking for accounting and enumeration. */ enrollEntry(EntryFile entry)1082 private synchronized void enrollEntry(EntryFile entry) { 1083 mAllFiles.contents.add(entry); 1084 mAllFiles.blocks += entry.blocks; 1085 1086 // mFilesByTag is used for trimming, so don't list empty files. 1087 // (Zero-length/lost files are trimmed by date from mAllFiles.) 1088 1089 if (entry.hasFile() && entry.blocks > 0) { 1090 FileList tagFiles = mFilesByTag.get(entry.tag); 1091 if (tagFiles == null) { 1092 tagFiles = new FileList(); 1093 mFilesByTag.put(TextUtils.safeIntern(entry.tag), tagFiles); 1094 } 1095 tagFiles.contents.add(entry); 1096 tagFiles.blocks += entry.blocks; 1097 } 1098 } 1099 1100 /** Moves a temporary file to a final log filename and enrolls it. */ createEntry(File temp, String tag, int flags)1101 private synchronized long createEntry(File temp, String tag, int flags) throws IOException { 1102 long t = System.currentTimeMillis(); 1103 1104 // Require each entry to have a unique timestamp; if there are entries 1105 // >10sec in the future (due to clock skew), drag them back to avoid 1106 // keeping them around forever. 1107 1108 SortedSet<EntryFile> tail = mAllFiles.contents.tailSet(new EntryFile(t + 10000)); 1109 EntryFile[] future = null; 1110 if (!tail.isEmpty()) { 1111 future = tail.toArray(new EntryFile[tail.size()]); 1112 tail.clear(); // Remove from mAllFiles 1113 } 1114 1115 if (!mAllFiles.contents.isEmpty()) { 1116 t = Math.max(t, mAllFiles.contents.last().timestampMillis + 1); 1117 } 1118 1119 if (future != null) { 1120 for (EntryFile late : future) { 1121 mAllFiles.blocks -= late.blocks; 1122 FileList tagFiles = mFilesByTag.get(late.tag); 1123 if (tagFiles != null && tagFiles.contents.remove(late)) { 1124 tagFiles.blocks -= late.blocks; 1125 } 1126 if ((late.flags & DropBoxManager.IS_EMPTY) == 0) { 1127 enrollEntry(new EntryFile(late.getFile(mDropBoxDir), mDropBoxDir, 1128 late.tag, t++, late.flags, mBlockSize)); 1129 } else { 1130 enrollEntry(new EntryFile(mDropBoxDir, late.tag, t++)); 1131 } 1132 } 1133 } 1134 1135 if (temp == null) { 1136 enrollEntry(new EntryFile(mDropBoxDir, tag, t)); 1137 } else { 1138 enrollEntry(new EntryFile(temp, mDropBoxDir, tag, t, flags, mBlockSize)); 1139 } 1140 return t; 1141 } 1142 1143 /** 1144 * Trims the files on disk to make sure they aren't using too much space. 1145 * @return the overall quota for storage (in bytes) 1146 */ trimToFit()1147 private synchronized long trimToFit() throws IOException { 1148 // Expunge aged items (including tombstones marking deleted data). 1149 1150 int ageSeconds = Settings.Global.getInt(mContentResolver, 1151 Settings.Global.DROPBOX_AGE_SECONDS, DEFAULT_AGE_SECONDS); 1152 mMaxFiles = Settings.Global.getInt(mContentResolver, 1153 Settings.Global.DROPBOX_MAX_FILES, 1154 (ActivityManager.isLowRamDeviceStatic() 1155 ? DEFAULT_MAX_FILES_LOWRAM : DEFAULT_MAX_FILES)); 1156 long curTimeMillis = System.currentTimeMillis(); 1157 long cutoffMillis = curTimeMillis - ageSeconds * 1000; 1158 while (!mAllFiles.contents.isEmpty()) { 1159 EntryFile entry = mAllFiles.contents.first(); 1160 if (entry.timestampMillis > cutoffMillis && mAllFiles.contents.size() < mMaxFiles) { 1161 break; 1162 } 1163 1164 logDropboxDropped( 1165 FrameworkStatsLog.DROPBOX_ENTRY_DROPPED__DROP_REASON__AGED, 1166 entry.tag, 1167 curTimeMillis - entry.timestampMillis); 1168 1169 FileList tag = mFilesByTag.get(entry.tag); 1170 if (tag != null && tag.contents.remove(entry)) tag.blocks -= entry.blocks; 1171 if (mAllFiles.contents.remove(entry)) mAllFiles.blocks -= entry.blocks; 1172 entry.deleteFile(mDropBoxDir); 1173 } 1174 1175 // Compute overall quota (a fraction of available free space) in blocks. 1176 // The quota changes dynamically based on the amount of free space; 1177 // that way when lots of data is available we can use it, but we'll get 1178 // out of the way if storage starts getting tight. 1179 1180 long uptimeMillis = SystemClock.uptimeMillis(); 1181 if (uptimeMillis > mCachedQuotaUptimeMillis + QUOTA_RESCAN_MILLIS) { 1182 int quotaPercent = Settings.Global.getInt(mContentResolver, 1183 Settings.Global.DROPBOX_QUOTA_PERCENT, DEFAULT_QUOTA_PERCENT); 1184 int reservePercent = Settings.Global.getInt(mContentResolver, 1185 Settings.Global.DROPBOX_RESERVE_PERCENT, DEFAULT_RESERVE_PERCENT); 1186 int quotaKb = Settings.Global.getInt(mContentResolver, 1187 Settings.Global.DROPBOX_QUOTA_KB, DEFAULT_QUOTA_KB); 1188 1189 String dirPath = mDropBoxDir.getPath(); 1190 try { 1191 mStatFs.restat(dirPath); 1192 } catch (IllegalArgumentException e) { // restat throws this on error 1193 throw new IOException("Can't restat: " + mDropBoxDir); 1194 } 1195 long available = mStatFs.getAvailableBlocksLong(); 1196 long nonreserved = available - mStatFs.getBlockCountLong() * reservePercent / 100; 1197 long maxAvailableLong = nonreserved * quotaPercent / 100; 1198 int maxAvailable = Math.toIntExact(Math.max(0, 1199 Math.min(maxAvailableLong, Integer.MAX_VALUE))); 1200 int maximum = quotaKb * 1024 / mBlockSize; 1201 mCachedQuotaBlocks = Math.min(maximum, maxAvailable); 1202 mCachedQuotaUptimeMillis = uptimeMillis; 1203 } 1204 1205 // If we're using too much space, delete old items to make room. 1206 // 1207 // We trim each tag independently (this is why we keep per-tag lists). 1208 // Space is "fairly" shared between tags -- they are all squeezed 1209 // equally until enough space is reclaimed. 1210 // 1211 // A single circular buffer (a la logcat) would be simpler, but this 1212 // way we can handle fat/bursty data (like 1MB+ bugreports, 300KB+ 1213 // kernel crash dumps, and 100KB+ ANR reports) without swamping small, 1214 // well-behaved data streams (event statistics, profile data, etc). 1215 // 1216 // Deleted files are replaced with zero-length tombstones to mark what 1217 // was lost. Tombstones are expunged by age (see above). 1218 1219 if (mAllFiles.blocks > mCachedQuotaBlocks) { 1220 // Find a fair share amount of space to limit each tag 1221 int unsqueezed = mAllFiles.blocks, squeezed = 0; 1222 TreeSet<FileList> tags = new TreeSet<FileList>(mFilesByTag.values()); 1223 for (FileList tag : tags) { 1224 if (squeezed > 0 && tag.blocks <= (mCachedQuotaBlocks - unsqueezed) / squeezed) { 1225 break; 1226 } 1227 unsqueezed -= tag.blocks; 1228 squeezed++; 1229 } 1230 int tagQuota = (mCachedQuotaBlocks - unsqueezed) / squeezed; 1231 1232 // Remove old items from each tag until it meets the per-tag quota. 1233 for (FileList tag : tags) { 1234 if (mAllFiles.blocks < mCachedQuotaBlocks) break; 1235 while (tag.blocks > tagQuota && !tag.contents.isEmpty()) { 1236 EntryFile entry = tag.contents.first(); 1237 logDropboxDropped( 1238 FrameworkStatsLog.DROPBOX_ENTRY_DROPPED__DROP_REASON__CLEARING_DATA, 1239 entry.tag, 1240 curTimeMillis - entry.timestampMillis); 1241 1242 if (tag.contents.remove(entry)) tag.blocks -= entry.blocks; 1243 if (mAllFiles.contents.remove(entry)) mAllFiles.blocks -= entry.blocks; 1244 1245 try { 1246 entry.deleteFile(mDropBoxDir); 1247 enrollEntry(new EntryFile(mDropBoxDir, entry.tag, entry.timestampMillis)); 1248 } catch (IOException e) { 1249 Slog.e(TAG, "Can't write tombstone file", e); 1250 } 1251 } 1252 } 1253 } 1254 1255 return mCachedQuotaBlocks * mBlockSize; 1256 } 1257 getLowPriorityResourceConfigs()1258 private void getLowPriorityResourceConfigs() { 1259 mLowPriorityRateLimitPeriod = Resources.getSystem().getInteger( 1260 R.integer.config_dropboxLowPriorityBroadcastRateLimitPeriod); 1261 1262 final String[] lowPrioritytags = Resources.getSystem().getStringArray( 1263 R.array.config_dropboxLowPriorityTags); 1264 final int size = lowPrioritytags.length; 1265 if (size == 0) { 1266 mLowPriorityTags = null; 1267 return; 1268 } 1269 mLowPriorityTags = new ArraySet(size); 1270 for (int i = 0; i < size; i++) { 1271 mLowPriorityTags.add(lowPrioritytags[i]); 1272 } 1273 } 1274 1275 private final class DropBoxManagerInternalImpl extends DropBoxManagerInternal { 1276 @Override addEntry(String tag, EntrySource entry, int flags)1277 public void addEntry(String tag, EntrySource entry, int flags) { 1278 DropBoxManagerService.this.addEntry(tag, entry, flags); 1279 } 1280 } 1281 } 1282