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