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