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