blob: 2cabce10b6d9395f95c3a9514377f04f668e0347 [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;
Doug Zongker43866e02010-01-07 12:09:54 -080025import android.database.ContentObserver;
Dan Egnor4410ec82009-09-11 16:40:01 -070026import android.net.Uri;
Dan Egnor3d40df32009-11-17 13:36:31 -080027import android.os.Debug;
Dan Egnorf18a01c2009-11-12 11:32:50 -080028import android.os.DropBoxManager;
Doug Zongker43866e02010-01-07 12:09:54 -080029import android.os.Handler;
Dan Egnor4410ec82009-09-11 16:40:01 -070030import android.os.ParcelFileDescriptor;
31import android.os.StatFs;
32import android.os.SystemClock;
33import android.provider.Settings;
Dan Egnor3d40df32009-11-17 13:36:31 -080034import android.text.format.Time;
Joe Onorato8a9b2202010-02-26 18:56:32 -080035import android.util.Slog;
Dan Egnor4410ec82009-09-11 16:40:01 -070036
Dan Egnorf18a01c2009-11-12 11:32:50 -080037import com.android.internal.os.IDropBoxManagerService;
Dan Egnor95240272009-10-27 18:23:39 -070038
Dan Egnor4410ec82009-09-11 16:40:01 -070039import java.io.File;
40import java.io.FileDescriptor;
Dan Egnor4410ec82009-09-11 16:40:01 -070041import java.io.FileOutputStream;
42import java.io.IOException;
Dan Egnor95240272009-10-27 18:23:39 -070043import java.io.InputStream;
Dan Egnor4410ec82009-09-11 16:40:01 -070044import java.io.InputStreamReader;
45import java.io.OutputStream;
46import java.io.OutputStreamWriter;
47import java.io.PrintWriter;
48import java.io.UnsupportedEncodingException;
49import java.util.ArrayList;
50import java.util.Comparator;
Dan Egnor4410ec82009-09-11 16:40:01 -070051import java.util.HashMap;
52import java.util.Iterator;
53import java.util.Map;
54import java.util.SortedSet;
55import java.util.TreeSet;
56import java.util.zip.GZIPOutputStream;
57
58/**
Dan Egnorf18a01c2009-11-12 11:32:50 -080059 * Implementation of {@link IDropBoxManagerService} using the filesystem.
60 * Clients use {@link DropBoxManager} to access this service.
Dan Egnor4410ec82009-09-11 16:40:01 -070061 */
Dan Egnorf18a01c2009-11-12 11:32:50 -080062public final class DropBoxManagerService extends IDropBoxManagerService.Stub {
63 private static final String TAG = "DropBoxManagerService";
Dan Egnor4410ec82009-09-11 16:40:01 -070064 private static final int DEFAULT_AGE_SECONDS = 3 * 86400;
Dan Egnor3a8b0c12010-03-24 17:48:20 -070065 private static final int DEFAULT_MAX_FILES = 1000;
66 private static final int DEFAULT_QUOTA_KB = 5 * 1024;
67 private static final int DEFAULT_QUOTA_PERCENT = 10;
68 private static final int DEFAULT_RESERVE_PERCENT = 10;
Dan Egnor4410ec82009-09-11 16:40:01 -070069 private static final int QUOTA_RESCAN_MILLIS = 5000;
70
Dan Egnor3d40df32009-11-17 13:36:31 -080071 private static final boolean PROFILE_DUMP = false;
72
Dan Egnor4410ec82009-09-11 16:40:01 -070073 // TODO: This implementation currently uses one file per entry, which is
74 // inefficient for smallish entries -- consider using a single queue file
75 // per tag (or even globally) instead.
76
77 // The cached context and derived objects
78
79 private final Context mContext;
80 private final ContentResolver mContentResolver;
81 private final File mDropBoxDir;
82
83 // Accounting of all currently written log files (set in init()).
84
85 private FileList mAllFiles = null;
86 private HashMap<String, FileList> mFilesByTag = null;
87
88 // Various bits of disk information
89
90 private StatFs mStatFs = null;
91 private int mBlockSize = 0;
92 private int mCachedQuotaBlocks = 0; // Space we can use: computed from free space, etc.
93 private long mCachedQuotaUptimeMillis = 0;
94
95 // Ensure that all log entries have a unique timestamp
96 private long mLastTimestamp = 0;
97
98 /** Receives events that might indicate a need to clean up files. */
99 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
100 @Override
101 public void onReceive(Context context, Intent intent) {
102 mCachedQuotaUptimeMillis = 0; // Force a re-check of quota size
Dan Egnor3a8b0c12010-03-24 17:48:20 -0700103
104 // Run the initialization in the background (not this main thread).
105 // The init() and trimToFit() methods are synchronized, so they still
106 // block other users -- but at least the onReceive() call can finish.
107 new Thread() {
108 public void run() {
109 try {
110 init();
111 trimToFit();
112 } catch (IOException e) {
113 Slog.e(TAG, "Can't init", e);
114 }
115 }
116 }.start();
Dan Egnor4410ec82009-09-11 16:40:01 -0700117 }
118 };
119
120 /**
121 * Creates an instance of managed drop box storage. Normally there is one of these
122 * run by the system, but others can be created for testing and other purposes.
123 *
124 * @param context to use for receiving free space & gservices intents
125 * @param path to store drop box entries in
126 */
Doug Zongker43866e02010-01-07 12:09:54 -0800127 public DropBoxManagerService(final Context context, File path) {
Dan Egnor4410ec82009-09-11 16:40:01 -0700128 mDropBoxDir = path;
129
130 // Set up intent receivers
131 mContext = context;
132 mContentResolver = context.getContentResolver();
133 context.registerReceiver(mReceiver, new IntentFilter(Intent.ACTION_DEVICE_STORAGE_LOW));
Doug Zongker43866e02010-01-07 12:09:54 -0800134
135 mContentResolver.registerContentObserver(
136 Settings.Secure.CONTENT_URI, true,
137 new ContentObserver(new Handler()) {
138 public void onChange(boolean selfChange) {
139 mReceiver.onReceive(context, (Intent) null);
140 }
141 });
Dan Egnor4410ec82009-09-11 16:40:01 -0700142
143 // The real work gets done lazily in init() -- that way service creation always
144 // succeeds, and things like disk problems cause individual method failures.
145 }
146
147 /** Unregisters broadcast receivers and any other hooks -- for test instances */
148 public void stop() {
149 mContext.unregisterReceiver(mReceiver);
150 }
151
Dan Egnorf18a01c2009-11-12 11:32:50 -0800152 public void add(DropBoxManager.Entry entry) {
Dan Egnor4410ec82009-09-11 16:40:01 -0700153 File temp = null;
154 OutputStream output = null;
Dan Egnor95240272009-10-27 18:23:39 -0700155 final String tag = entry.getTag();
Dan Egnor4410ec82009-09-11 16:40:01 -0700156 try {
Dan Egnor95240272009-10-27 18:23:39 -0700157 int flags = entry.getFlags();
Dan Egnorf18a01c2009-11-12 11:32:50 -0800158 if ((flags & DropBoxManager.IS_EMPTY) != 0) throw new IllegalArgumentException();
Dan Egnor4410ec82009-09-11 16:40:01 -0700159
160 init();
161 if (!isTagEnabled(tag)) return;
162 long max = trimToFit();
163 long lastTrim = System.currentTimeMillis();
164
165 byte[] buffer = new byte[mBlockSize];
Dan Egnor95240272009-10-27 18:23:39 -0700166 InputStream input = entry.getInputStream();
Dan Egnor4410ec82009-09-11 16:40:01 -0700167
168 // First, accumulate up to one block worth of data in memory before
169 // deciding whether to compress the data or not.
170
171 int read = 0;
172 while (read < buffer.length) {
173 int n = input.read(buffer, read, buffer.length - read);
174 if (n <= 0) break;
175 read += n;
176 }
177
178 // If we have at least one block, compress it -- otherwise, just write
179 // the data in uncompressed form.
180
181 temp = new File(mDropBoxDir, "drop" + Thread.currentThread().getId() + ".tmp");
182 output = new FileOutputStream(temp);
Dan Egnorf18a01c2009-11-12 11:32:50 -0800183 if (read == buffer.length && ((flags & DropBoxManager.IS_GZIPPED) == 0)) {
Dan Egnor4410ec82009-09-11 16:40:01 -0700184 output = new GZIPOutputStream(output);
Dan Egnorf18a01c2009-11-12 11:32:50 -0800185 flags = flags | DropBoxManager.IS_GZIPPED;
Dan Egnor4410ec82009-09-11 16:40:01 -0700186 }
187
188 do {
189 output.write(buffer, 0, read);
190
191 long now = System.currentTimeMillis();
192 if (now - lastTrim > 30 * 1000) {
193 max = trimToFit(); // In case data dribbles in slowly
194 lastTrim = now;
195 }
196
197 read = input.read(buffer);
198 if (read <= 0) {
199 output.close(); // Get a final size measurement
200 output = null;
201 } else {
202 output.flush(); // So the size measurement is pseudo-reasonable
203 }
204
205 long len = temp.length();
206 if (len > max) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800207 Slog.w(TAG, "Dropping: " + tag + " (" + temp.length() + " > " + max + " bytes)");
Dan Egnor4410ec82009-09-11 16:40:01 -0700208 temp.delete();
209 temp = null; // Pass temp = null to createEntry() to leave a tombstone
210 break;
211 }
212 } while (read > 0);
213
Hakan Stillb2475362010-12-07 14:05:55 +0100214 long time = createEntry(temp, tag, flags);
Dan Egnor4410ec82009-09-11 16:40:01 -0700215 temp = null;
Hakan Stillb2475362010-12-07 14:05:55 +0100216
217 Intent dropboxIntent = new Intent(DropBoxManager.ACTION_DROPBOX_ENTRY_ADDED);
218 dropboxIntent.putExtra(DropBoxManager.EXTRA_TAG, tag);
219 dropboxIntent.putExtra(DropBoxManager.EXTRA_TIME, time);
220 mContext.sendBroadcast(dropboxIntent, android.Manifest.permission.READ_LOGS);
221
Dan Egnor4410ec82009-09-11 16:40:01 -0700222 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800223 Slog.e(TAG, "Can't write: " + tag, e);
Dan Egnor4410ec82009-09-11 16:40:01 -0700224 } finally {
225 try { if (output != null) output.close(); } catch (IOException e) {}
Dan Egnor95240272009-10-27 18:23:39 -0700226 entry.close();
Dan Egnor4410ec82009-09-11 16:40:01 -0700227 if (temp != null) temp.delete();
228 }
229 }
230
231 public boolean isTagEnabled(String tag) {
Doug Zongker43866e02010-01-07 12:09:54 -0800232 return !"disabled".equals(Settings.Secure.getString(
233 mContentResolver, Settings.Secure.DROPBOX_TAG_PREFIX + tag));
Dan Egnor4410ec82009-09-11 16:40:01 -0700234 }
235
Dan Egnorf18a01c2009-11-12 11:32:50 -0800236 public synchronized DropBoxManager.Entry getNextEntry(String tag, long millis) {
Dan Egnor4410ec82009-09-11 16:40:01 -0700237 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.READ_LOGS)
238 != PackageManager.PERMISSION_GRANTED) {
239 throw new SecurityException("READ_LOGS permission required");
240 }
241
242 try {
243 init();
244 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800245 Slog.e(TAG, "Can't init", e);
Dan Egnor4410ec82009-09-11 16:40:01 -0700246 return null;
247 }
248
Dan Egnorb3b06fc2009-10-20 13:05:17 -0700249 FileList list = tag == null ? mAllFiles : mFilesByTag.get(tag);
250 if (list == null) return null;
251
252 for (EntryFile entry : list.contents.tailSet(new EntryFile(millis + 1))) {
Dan Egnor4410ec82009-09-11 16:40:01 -0700253 if (entry.tag == null) continue;
Dan Egnorf18a01c2009-11-12 11:32:50 -0800254 if ((entry.flags & DropBoxManager.IS_EMPTY) != 0) {
255 return new DropBoxManager.Entry(entry.tag, entry.timestampMillis);
Dan Egnor95240272009-10-27 18:23:39 -0700256 }
Dan Egnor4410ec82009-09-11 16:40:01 -0700257 try {
Dan Egnorf18a01c2009-11-12 11:32:50 -0800258 return new DropBoxManager.Entry(
259 entry.tag, entry.timestampMillis, entry.file, entry.flags);
Dan Egnor4410ec82009-09-11 16:40:01 -0700260 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800261 Slog.e(TAG, "Can't read: " + entry.file, e);
Dan Egnor4410ec82009-09-11 16:40:01 -0700262 // Continue to next file
263 }
264 }
265
266 return null;
267 }
268
269 public synchronized void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
270 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
271 != PackageManager.PERMISSION_GRANTED) {
Dan Egnorf18a01c2009-11-12 11:32:50 -0800272 pw.println("Permission Denial: Can't dump DropBoxManagerService");
Dan Egnor4410ec82009-09-11 16:40:01 -0700273 return;
274 }
275
276 try {
277 init();
278 } catch (IOException e) {
279 pw.println("Can't initialize: " + e);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800280 Slog.e(TAG, "Can't init", e);
Dan Egnor4410ec82009-09-11 16:40:01 -0700281 return;
282 }
283
Dan Egnor3d40df32009-11-17 13:36:31 -0800284 if (PROFILE_DUMP) Debug.startMethodTracing("/data/trace/dropbox.dump");
285
Dan Egnor5ec249a2009-11-25 13:16:47 -0800286 StringBuilder out = new StringBuilder();
Dan Egnor4410ec82009-09-11 16:40:01 -0700287 boolean doPrint = false, doFile = false;
288 ArrayList<String> searchArgs = new ArrayList<String>();
289 for (int i = 0; args != null && i < args.length; i++) {
290 if (args[i].equals("-p") || args[i].equals("--print")) {
291 doPrint = true;
292 } else if (args[i].equals("-f") || args[i].equals("--file")) {
293 doFile = true;
294 } else if (args[i].startsWith("-")) {
Dan Egnor5ec249a2009-11-25 13:16:47 -0800295 out.append("Unknown argument: ").append(args[i]).append("\n");
Dan Egnor4410ec82009-09-11 16:40:01 -0700296 } else {
297 searchArgs.add(args[i]);
298 }
299 }
300
Dan Egnor5ec249a2009-11-25 13:16:47 -0800301 out.append("Drop box contents: ").append(mAllFiles.contents.size()).append(" entries\n");
Dan Egnor4410ec82009-09-11 16:40:01 -0700302
303 if (!searchArgs.isEmpty()) {
Dan Egnor5ec249a2009-11-25 13:16:47 -0800304 out.append("Searching for:");
305 for (String a : searchArgs) out.append(" ").append(a);
306 out.append("\n");
Dan Egnor4410ec82009-09-11 16:40:01 -0700307 }
308
Dan Egnor3d40df32009-11-17 13:36:31 -0800309 int numFound = 0, numArgs = searchArgs.size();
310 Time time = new Time();
Dan Egnor5ec249a2009-11-25 13:16:47 -0800311 out.append("\n");
Dan Egnor4410ec82009-09-11 16:40:01 -0700312 for (EntryFile entry : mAllFiles.contents) {
Dan Egnor3d40df32009-11-17 13:36:31 -0800313 time.set(entry.timestampMillis);
314 String date = time.format("%Y-%m-%d %H:%M:%S");
Dan Egnor4410ec82009-09-11 16:40:01 -0700315 boolean match = true;
Dan Egnor3d40df32009-11-17 13:36:31 -0800316 for (int i = 0; i < numArgs && match; i++) {
317 String arg = searchArgs.get(i);
318 match = (date.contains(arg) || arg.equals(entry.tag));
319 }
Dan Egnor4410ec82009-09-11 16:40:01 -0700320 if (!match) continue;
321
322 numFound++;
Dan Egnor42471dd2010-01-07 17:25:22 -0800323 if (doPrint) out.append("========================================\n");
Dan Egnor5ec249a2009-11-25 13:16:47 -0800324 out.append(date).append(" ").append(entry.tag == null ? "(no tag)" : entry.tag);
Dan Egnor4410ec82009-09-11 16:40:01 -0700325 if (entry.file == null) {
Dan Egnor5ec249a2009-11-25 13:16:47 -0800326 out.append(" (no file)\n");
Dan Egnor4410ec82009-09-11 16:40:01 -0700327 continue;
Dan Egnorf18a01c2009-11-12 11:32:50 -0800328 } else if ((entry.flags & DropBoxManager.IS_EMPTY) != 0) {
Dan Egnor5ec249a2009-11-25 13:16:47 -0800329 out.append(" (contents lost)\n");
Dan Egnor4410ec82009-09-11 16:40:01 -0700330 continue;
331 } else {
Dan Egnor5ec249a2009-11-25 13:16:47 -0800332 out.append(" (");
333 if ((entry.flags & DropBoxManager.IS_GZIPPED) != 0) out.append("compressed ");
334 out.append((entry.flags & DropBoxManager.IS_TEXT) != 0 ? "text" : "data");
335 out.append(", ").append(entry.file.length()).append(" bytes)\n");
Dan Egnor4410ec82009-09-11 16:40:01 -0700336 }
337
Dan Egnorf18a01c2009-11-12 11:32:50 -0800338 if (doFile || (doPrint && (entry.flags & DropBoxManager.IS_TEXT) == 0)) {
Dan Egnor5ec249a2009-11-25 13:16:47 -0800339 if (!doPrint) out.append(" ");
340 out.append(entry.file.getPath()).append("\n");
Dan Egnor4410ec82009-09-11 16:40:01 -0700341 }
342
Dan Egnorf18a01c2009-11-12 11:32:50 -0800343 if ((entry.flags & DropBoxManager.IS_TEXT) != 0 && (doPrint || !doFile)) {
344 DropBoxManager.Entry dbe = null;
Dan Egnor4410ec82009-09-11 16:40:01 -0700345 try {
Dan Egnorf18a01c2009-11-12 11:32:50 -0800346 dbe = new DropBoxManager.Entry(
Dan Egnor4410ec82009-09-11 16:40:01 -0700347 entry.tag, entry.timestampMillis, entry.file, entry.flags);
348
349 if (doPrint) {
350 InputStreamReader r = new InputStreamReader(dbe.getInputStream());
351 char[] buf = new char[4096];
352 boolean newline = false;
353 for (;;) {
354 int n = r.read(buf);
355 if (n <= 0) break;
Dan Egnor5ec249a2009-11-25 13:16:47 -0800356 out.append(buf, 0, n);
Dan Egnor4410ec82009-09-11 16:40:01 -0700357 newline = (buf[n - 1] == '\n');
Dan Egnor42471dd2010-01-07 17:25:22 -0800358
359 // Flush periodically when printing to avoid out-of-memory.
360 if (out.length() > 65536) {
361 pw.write(out.toString());
362 out.setLength(0);
363 }
Dan Egnor4410ec82009-09-11 16:40:01 -0700364 }
Dan Egnor5ec249a2009-11-25 13:16:47 -0800365 if (!newline) out.append("\n");
Dan Egnor4410ec82009-09-11 16:40:01 -0700366 } else {
367 String text = dbe.getText(70);
368 boolean truncated = (text.length() == 70);
Dan Egnor5ec249a2009-11-25 13:16:47 -0800369 out.append(" ").append(text.trim().replace('\n', '/'));
370 if (truncated) out.append(" ...");
371 out.append("\n");
Dan Egnor4410ec82009-09-11 16:40:01 -0700372 }
373 } catch (IOException e) {
Dan Egnor5ec249a2009-11-25 13:16:47 -0800374 out.append("*** ").append(e.toString()).append("\n");
Joe Onorato8a9b2202010-02-26 18:56:32 -0800375 Slog.e(TAG, "Can't read: " + entry.file, e);
Dan Egnor4410ec82009-09-11 16:40:01 -0700376 } finally {
377 if (dbe != null) dbe.close();
378 }
379 }
380
Dan Egnor5ec249a2009-11-25 13:16:47 -0800381 if (doPrint) out.append("\n");
Dan Egnor4410ec82009-09-11 16:40:01 -0700382 }
383
Dan Egnor5ec249a2009-11-25 13:16:47 -0800384 if (numFound == 0) out.append("(No entries found.)\n");
Dan Egnor4410ec82009-09-11 16:40:01 -0700385
386 if (args == null || args.length == 0) {
Dan Egnor5ec249a2009-11-25 13:16:47 -0800387 if (!doPrint) out.append("\n");
388 out.append("Usage: dumpsys dropbox [--print|--file] [YYYY-mm-dd] [HH:MM:SS] [tag]\n");
Dan Egnor4410ec82009-09-11 16:40:01 -0700389 }
Dan Egnor3d40df32009-11-17 13:36:31 -0800390
391 pw.write(out.toString());
392 if (PROFILE_DUMP) Debug.stopMethodTracing();
Dan Egnor4410ec82009-09-11 16:40:01 -0700393 }
394
395 ///////////////////////////////////////////////////////////////////////////
396
397 /** Chronologically sorted list of {@link #EntryFile} */
398 private static final class FileList implements Comparable<FileList> {
399 public int blocks = 0;
400 public final TreeSet<EntryFile> contents = new TreeSet<EntryFile>();
401
402 /** Sorts bigger FileList instances before smaller ones. */
403 public final int compareTo(FileList o) {
404 if (blocks != o.blocks) return o.blocks - blocks;
405 if (this == o) return 0;
406 if (hashCode() < o.hashCode()) return -1;
407 if (hashCode() > o.hashCode()) return 1;
408 return 0;
409 }
410 }
411
412 /** Metadata describing an on-disk log file. */
413 private static final class EntryFile implements Comparable<EntryFile> {
414 public final String tag;
415 public final long timestampMillis;
416 public final int flags;
417 public final File file;
418 public final int blocks;
419
420 /** Sorts earlier EntryFile instances before later ones. */
421 public final int compareTo(EntryFile o) {
422 if (timestampMillis < o.timestampMillis) return -1;
423 if (timestampMillis > o.timestampMillis) return 1;
424 if (file != null && o.file != null) return file.compareTo(o.file);
425 if (o.file != null) return -1;
426 if (file != null) return 1;
427 if (this == o) return 0;
428 if (hashCode() < o.hashCode()) return -1;
429 if (hashCode() > o.hashCode()) return 1;
430 return 0;
431 }
432
433 /**
434 * Moves an existing temporary file to a new log filename.
435 * @param temp file to rename
436 * @param dir to store file in
437 * @param tag to use for new log file name
438 * @param timestampMillis of log entry
Dan Egnor95240272009-10-27 18:23:39 -0700439 * @param flags for the entry data
Dan Egnor4410ec82009-09-11 16:40:01 -0700440 * @param blockSize to use for space accounting
441 * @throws IOException if the file can't be moved
442 */
443 public EntryFile(File temp, File dir, String tag,long timestampMillis,
444 int flags, int blockSize) throws IOException {
Dan Egnorf18a01c2009-11-12 11:32:50 -0800445 if ((flags & DropBoxManager.IS_EMPTY) != 0) throw new IllegalArgumentException();
Dan Egnor4410ec82009-09-11 16:40:01 -0700446
447 this.tag = tag;
448 this.timestampMillis = timestampMillis;
449 this.flags = flags;
450 this.file = new File(dir, Uri.encode(tag) + "@" + timestampMillis +
Dan Egnorf18a01c2009-11-12 11:32:50 -0800451 ((flags & DropBoxManager.IS_TEXT) != 0 ? ".txt" : ".dat") +
452 ((flags & DropBoxManager.IS_GZIPPED) != 0 ? ".gz" : ""));
Dan Egnor4410ec82009-09-11 16:40:01 -0700453
454 if (!temp.renameTo(this.file)) {
455 throw new IOException("Can't rename " + temp + " to " + this.file);
456 }
457 this.blocks = (int) ((this.file.length() + blockSize - 1) / blockSize);
458 }
459
460 /**
461 * Creates a zero-length tombstone for a file whose contents were lost.
462 * @param dir to store file in
463 * @param tag to use for new log file name
464 * @param timestampMillis of log entry
465 * @throws IOException if the file can't be created.
466 */
467 public EntryFile(File dir, String tag, long timestampMillis) throws IOException {
468 this.tag = tag;
469 this.timestampMillis = timestampMillis;
Dan Egnorf18a01c2009-11-12 11:32:50 -0800470 this.flags = DropBoxManager.IS_EMPTY;
Dan Egnor4410ec82009-09-11 16:40:01 -0700471 this.file = new File(dir, Uri.encode(tag) + "@" + timestampMillis + ".lost");
472 this.blocks = 0;
473 new FileOutputStream(this.file).close();
474 }
475
476 /**
477 * Extracts metadata from an existing on-disk log filename.
478 * @param file name of existing log file
479 * @param blockSize to use for space accounting
480 */
481 public EntryFile(File file, int blockSize) {
482 this.file = file;
483 this.blocks = (int) ((this.file.length() + blockSize - 1) / blockSize);
484
485 String name = file.getName();
486 int at = name.lastIndexOf('@');
487 if (at < 0) {
488 this.tag = null;
489 this.timestampMillis = 0;
Dan Egnorf18a01c2009-11-12 11:32:50 -0800490 this.flags = DropBoxManager.IS_EMPTY;
Dan Egnor4410ec82009-09-11 16:40:01 -0700491 return;
492 }
493
494 int flags = 0;
495 this.tag = Uri.decode(name.substring(0, at));
496 if (name.endsWith(".gz")) {
Dan Egnorf18a01c2009-11-12 11:32:50 -0800497 flags |= DropBoxManager.IS_GZIPPED;
Dan Egnor4410ec82009-09-11 16:40:01 -0700498 name = name.substring(0, name.length() - 3);
499 }
500 if (name.endsWith(".lost")) {
Dan Egnorf18a01c2009-11-12 11:32:50 -0800501 flags |= DropBoxManager.IS_EMPTY;
Dan Egnor4410ec82009-09-11 16:40:01 -0700502 name = name.substring(at + 1, name.length() - 5);
503 } else if (name.endsWith(".txt")) {
Dan Egnorf18a01c2009-11-12 11:32:50 -0800504 flags |= DropBoxManager.IS_TEXT;
Dan Egnor4410ec82009-09-11 16:40:01 -0700505 name = name.substring(at + 1, name.length() - 4);
506 } else if (name.endsWith(".dat")) {
507 name = name.substring(at + 1, name.length() - 4);
508 } else {
Dan Egnorf18a01c2009-11-12 11:32:50 -0800509 this.flags = DropBoxManager.IS_EMPTY;
Dan Egnor4410ec82009-09-11 16:40:01 -0700510 this.timestampMillis = 0;
511 return;
512 }
513 this.flags = flags;
514
515 long millis;
516 try { millis = Long.valueOf(name); } catch (NumberFormatException e) { millis = 0; }
517 this.timestampMillis = millis;
518 }
519
520 /**
521 * Creates a EntryFile object with only a timestamp for comparison purposes.
522 * @param timestampMillis to compare with.
523 */
524 public EntryFile(long millis) {
525 this.tag = null;
526 this.timestampMillis = millis;
Dan Egnorf18a01c2009-11-12 11:32:50 -0800527 this.flags = DropBoxManager.IS_EMPTY;
Dan Egnor4410ec82009-09-11 16:40:01 -0700528 this.file = null;
529 this.blocks = 0;
530 }
531 }
532
533 ///////////////////////////////////////////////////////////////////////////
534
535 /** If never run before, scans disk contents to build in-memory tracking data. */
536 private synchronized void init() throws IOException {
537 if (mStatFs == null) {
538 if (!mDropBoxDir.isDirectory() && !mDropBoxDir.mkdirs()) {
539 throw new IOException("Can't mkdir: " + mDropBoxDir);
540 }
541 try {
542 mStatFs = new StatFs(mDropBoxDir.getPath());
543 mBlockSize = mStatFs.getBlockSize();
544 } catch (IllegalArgumentException e) { // StatFs throws this on error
545 throw new IOException("Can't statfs: " + mDropBoxDir);
546 }
547 }
548
549 if (mAllFiles == null) {
550 File[] files = mDropBoxDir.listFiles();
551 if (files == null) throw new IOException("Can't list files: " + mDropBoxDir);
552
553 mAllFiles = new FileList();
554 mFilesByTag = new HashMap<String, FileList>();
555
556 // Scan pre-existing files.
557 for (File file : files) {
558 if (file.getName().endsWith(".tmp")) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800559 Slog.i(TAG, "Cleaning temp file: " + file);
Dan Egnor4410ec82009-09-11 16:40:01 -0700560 file.delete();
561 continue;
562 }
563
564 EntryFile entry = new EntryFile(file, mBlockSize);
565 if (entry.tag == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800566 Slog.w(TAG, "Unrecognized file: " + file);
Dan Egnor4410ec82009-09-11 16:40:01 -0700567 continue;
568 } else if (entry.timestampMillis == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800569 Slog.w(TAG, "Invalid filename: " + file);
Dan Egnor4410ec82009-09-11 16:40:01 -0700570 file.delete();
571 continue;
572 }
573
574 enrollEntry(entry);
575 }
576 }
577 }
578
579 /** Adds a disk log file to in-memory tracking for accounting and enumeration. */
580 private synchronized void enrollEntry(EntryFile entry) {
581 mAllFiles.contents.add(entry);
582 mAllFiles.blocks += entry.blocks;
583
584 // mFilesByTag is used for trimming, so don't list empty files.
585 // (Zero-length/lost files are trimmed by date from mAllFiles.)
586
587 if (entry.tag != null && entry.file != null && entry.blocks > 0) {
588 FileList tagFiles = mFilesByTag.get(entry.tag);
589 if (tagFiles == null) {
590 tagFiles = new FileList();
591 mFilesByTag.put(entry.tag, tagFiles);
592 }
593 tagFiles.contents.add(entry);
594 tagFiles.blocks += entry.blocks;
595 }
596 }
597
598 /** Moves a temporary file to a final log filename and enrolls it. */
Hakan Stillb2475362010-12-07 14:05:55 +0100599 private synchronized long createEntry(File temp, String tag, int flags) throws IOException {
Dan Egnor4410ec82009-09-11 16:40:01 -0700600 long t = System.currentTimeMillis();
601
602 // Require each entry to have a unique timestamp; if there are entries
603 // >10sec in the future (due to clock skew), drag them back to avoid
604 // keeping them around forever.
605
606 SortedSet<EntryFile> tail = mAllFiles.contents.tailSet(new EntryFile(t + 10000));
607 EntryFile[] future = null;
608 if (!tail.isEmpty()) {
609 future = tail.toArray(new EntryFile[tail.size()]);
610 tail.clear(); // Remove from mAllFiles
611 }
612
613 if (!mAllFiles.contents.isEmpty()) {
614 t = Math.max(t, mAllFiles.contents.last().timestampMillis + 1);
615 }
616
617 if (future != null) {
618 for (EntryFile late : future) {
619 mAllFiles.blocks -= late.blocks;
620 FileList tagFiles = mFilesByTag.get(late.tag);
Dan Egnorf283e362010-03-10 16:49:55 -0800621 if (tagFiles != null && tagFiles.contents.remove(late)) {
622 tagFiles.blocks -= late.blocks;
623 }
Dan Egnorf18a01c2009-11-12 11:32:50 -0800624 if ((late.flags & DropBoxManager.IS_EMPTY) == 0) {
Dan Egnor4410ec82009-09-11 16:40:01 -0700625 enrollEntry(new EntryFile(
626 late.file, mDropBoxDir, late.tag, t++, late.flags, mBlockSize));
627 } else {
628 enrollEntry(new EntryFile(mDropBoxDir, late.tag, t++));
629 }
630 }
631 }
632
633 if (temp == null) {
634 enrollEntry(new EntryFile(mDropBoxDir, tag, t));
635 } else {
636 enrollEntry(new EntryFile(temp, mDropBoxDir, tag, t, flags, mBlockSize));
637 }
Hakan Stillb2475362010-12-07 14:05:55 +0100638 return t;
Dan Egnor4410ec82009-09-11 16:40:01 -0700639 }
640
641 /**
642 * Trims the files on disk to make sure they aren't using too much space.
643 * @return the overall quota for storage (in bytes)
644 */
645 private synchronized long trimToFit() {
646 // Expunge aged items (including tombstones marking deleted data).
647
Doug Zongker43866e02010-01-07 12:09:54 -0800648 int ageSeconds = Settings.Secure.getInt(mContentResolver,
649 Settings.Secure.DROPBOX_AGE_SECONDS, DEFAULT_AGE_SECONDS);
Dan Egnor3a8b0c12010-03-24 17:48:20 -0700650 int maxFiles = Settings.Secure.getInt(mContentResolver,
651 Settings.Secure.DROPBOX_MAX_FILES, DEFAULT_MAX_FILES);
Dan Egnor4410ec82009-09-11 16:40:01 -0700652 long cutoffMillis = System.currentTimeMillis() - ageSeconds * 1000;
653 while (!mAllFiles.contents.isEmpty()) {
654 EntryFile entry = mAllFiles.contents.first();
Dan Egnor3a8b0c12010-03-24 17:48:20 -0700655 if (entry.timestampMillis > cutoffMillis && mAllFiles.contents.size() < maxFiles) break;
Dan Egnor4410ec82009-09-11 16:40:01 -0700656
657 FileList tag = mFilesByTag.get(entry.tag);
658 if (tag != null && tag.contents.remove(entry)) tag.blocks -= entry.blocks;
659 if (mAllFiles.contents.remove(entry)) mAllFiles.blocks -= entry.blocks;
660 if (entry.file != null) entry.file.delete();
661 }
662
663 // Compute overall quota (a fraction of available free space) in blocks.
664 // The quota changes dynamically based on the amount of free space;
665 // that way when lots of data is available we can use it, but we'll get
666 // out of the way if storage starts getting tight.
667
668 long uptimeMillis = SystemClock.uptimeMillis();
669 if (uptimeMillis > mCachedQuotaUptimeMillis + QUOTA_RESCAN_MILLIS) {
Doug Zongker43866e02010-01-07 12:09:54 -0800670 int quotaPercent = Settings.Secure.getInt(mContentResolver,
671 Settings.Secure.DROPBOX_QUOTA_PERCENT, DEFAULT_QUOTA_PERCENT);
672 int reservePercent = Settings.Secure.getInt(mContentResolver,
673 Settings.Secure.DROPBOX_RESERVE_PERCENT, DEFAULT_RESERVE_PERCENT);
674 int quotaKb = Settings.Secure.getInt(mContentResolver,
675 Settings.Secure.DROPBOX_QUOTA_KB, DEFAULT_QUOTA_KB);
Dan Egnor4410ec82009-09-11 16:40:01 -0700676
677 mStatFs.restat(mDropBoxDir.getPath());
678 int available = mStatFs.getAvailableBlocks();
679 int nonreserved = available - mStatFs.getBlockCount() * reservePercent / 100;
680 int maximum = quotaKb * 1024 / mBlockSize;
681 mCachedQuotaBlocks = Math.min(maximum, Math.max(0, nonreserved * quotaPercent / 100));
682 mCachedQuotaUptimeMillis = uptimeMillis;
683 }
684
685 // If we're using too much space, delete old items to make room.
686 //
687 // We trim each tag independently (this is why we keep per-tag lists).
688 // Space is "fairly" shared between tags -- they are all squeezed
689 // equally until enough space is reclaimed.
690 //
691 // A single circular buffer (a la logcat) would be simpler, but this
692 // way we can handle fat/bursty data (like 1MB+ bugreports, 300KB+
693 // kernel crash dumps, and 100KB+ ANR reports) without swamping small,
Dan Egnor3a8b0c12010-03-24 17:48:20 -0700694 // well-behaved data streams (event statistics, profile data, etc).
Dan Egnor4410ec82009-09-11 16:40:01 -0700695 //
696 // Deleted files are replaced with zero-length tombstones to mark what
697 // was lost. Tombstones are expunged by age (see above).
698
699 if (mAllFiles.blocks > mCachedQuotaBlocks) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800700 Slog.i(TAG, "Usage (" + mAllFiles.blocks + ") > Quota (" + mCachedQuotaBlocks + ")");
Dan Egnor4410ec82009-09-11 16:40:01 -0700701
702 // Find a fair share amount of space to limit each tag
703 int unsqueezed = mAllFiles.blocks, squeezed = 0;
704 TreeSet<FileList> tags = new TreeSet<FileList>(mFilesByTag.values());
705 for (FileList tag : tags) {
706 if (squeezed > 0 && tag.blocks <= (mCachedQuotaBlocks - unsqueezed) / squeezed) {
707 break;
708 }
709 unsqueezed -= tag.blocks;
710 squeezed++;
711 }
712 int tagQuota = (mCachedQuotaBlocks - unsqueezed) / squeezed;
713
714 // Remove old items from each tag until it meets the per-tag quota.
715 for (FileList tag : tags) {
716 if (mAllFiles.blocks < mCachedQuotaBlocks) break;
717 while (tag.blocks > tagQuota && !tag.contents.isEmpty()) {
718 EntryFile entry = tag.contents.first();
719 if (tag.contents.remove(entry)) tag.blocks -= entry.blocks;
720 if (mAllFiles.contents.remove(entry)) mAllFiles.blocks -= entry.blocks;
721
722 try {
723 if (entry.file != null) entry.file.delete();
724 enrollEntry(new EntryFile(mDropBoxDir, entry.tag, entry.timestampMillis));
725 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800726 Slog.e(TAG, "Can't write tombstone file", e);
Dan Egnor4410ec82009-09-11 16:40:01 -0700727 }
728 }
729 }
730 }
731
732 return mCachedQuotaBlocks * mBlockSize;
733 }
734}