blob: 14b7d3ec389169a20eaf906a0496f2a78d8d33e0 [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
214 createEntry(temp, tag, flags);
215 temp = null;
216 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800217 Slog.e(TAG, "Can't write: " + tag, e);
Dan Egnor4410ec82009-09-11 16:40:01 -0700218 } finally {
219 try { if (output != null) output.close(); } catch (IOException e) {}
Dan Egnor95240272009-10-27 18:23:39 -0700220 entry.close();
Dan Egnor4410ec82009-09-11 16:40:01 -0700221 if (temp != null) temp.delete();
222 }
223 }
224
225 public boolean isTagEnabled(String tag) {
Doug Zongker43866e02010-01-07 12:09:54 -0800226 return !"disabled".equals(Settings.Secure.getString(
227 mContentResolver, Settings.Secure.DROPBOX_TAG_PREFIX + tag));
Dan Egnor4410ec82009-09-11 16:40:01 -0700228 }
229
Dan Egnorf18a01c2009-11-12 11:32:50 -0800230 public synchronized DropBoxManager.Entry getNextEntry(String tag, long millis) {
Dan Egnor4410ec82009-09-11 16:40:01 -0700231 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.READ_LOGS)
232 != PackageManager.PERMISSION_GRANTED) {
233 throw new SecurityException("READ_LOGS permission required");
234 }
235
236 try {
237 init();
238 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800239 Slog.e(TAG, "Can't init", e);
Dan Egnor4410ec82009-09-11 16:40:01 -0700240 return null;
241 }
242
Dan Egnorb3b06fc2009-10-20 13:05:17 -0700243 FileList list = tag == null ? mAllFiles : mFilesByTag.get(tag);
244 if (list == null) return null;
245
246 for (EntryFile entry : list.contents.tailSet(new EntryFile(millis + 1))) {
Dan Egnor4410ec82009-09-11 16:40:01 -0700247 if (entry.tag == null) continue;
Dan Egnorf18a01c2009-11-12 11:32:50 -0800248 if ((entry.flags & DropBoxManager.IS_EMPTY) != 0) {
249 return new DropBoxManager.Entry(entry.tag, entry.timestampMillis);
Dan Egnor95240272009-10-27 18:23:39 -0700250 }
Dan Egnor4410ec82009-09-11 16:40:01 -0700251 try {
Dan Egnorf18a01c2009-11-12 11:32:50 -0800252 return new DropBoxManager.Entry(
253 entry.tag, entry.timestampMillis, entry.file, entry.flags);
Dan Egnor4410ec82009-09-11 16:40:01 -0700254 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800255 Slog.e(TAG, "Can't read: " + entry.file, e);
Dan Egnor4410ec82009-09-11 16:40:01 -0700256 // Continue to next file
257 }
258 }
259
260 return null;
261 }
262
263 public synchronized void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
264 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
265 != PackageManager.PERMISSION_GRANTED) {
Dan Egnorf18a01c2009-11-12 11:32:50 -0800266 pw.println("Permission Denial: Can't dump DropBoxManagerService");
Dan Egnor4410ec82009-09-11 16:40:01 -0700267 return;
268 }
269
270 try {
271 init();
272 } catch (IOException e) {
273 pw.println("Can't initialize: " + e);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800274 Slog.e(TAG, "Can't init", e);
Dan Egnor4410ec82009-09-11 16:40:01 -0700275 return;
276 }
277
Dan Egnor3d40df32009-11-17 13:36:31 -0800278 if (PROFILE_DUMP) Debug.startMethodTracing("/data/trace/dropbox.dump");
279
Dan Egnor5ec249a2009-11-25 13:16:47 -0800280 StringBuilder out = new StringBuilder();
Dan Egnor4410ec82009-09-11 16:40:01 -0700281 boolean doPrint = false, doFile = false;
282 ArrayList<String> searchArgs = new ArrayList<String>();
283 for (int i = 0; args != null && i < args.length; i++) {
284 if (args[i].equals("-p") || args[i].equals("--print")) {
285 doPrint = true;
286 } else if (args[i].equals("-f") || args[i].equals("--file")) {
287 doFile = true;
288 } else if (args[i].startsWith("-")) {
Dan Egnor5ec249a2009-11-25 13:16:47 -0800289 out.append("Unknown argument: ").append(args[i]).append("\n");
Dan Egnor4410ec82009-09-11 16:40:01 -0700290 } else {
291 searchArgs.add(args[i]);
292 }
293 }
294
Dan Egnor5ec249a2009-11-25 13:16:47 -0800295 out.append("Drop box contents: ").append(mAllFiles.contents.size()).append(" entries\n");
Dan Egnor4410ec82009-09-11 16:40:01 -0700296
297 if (!searchArgs.isEmpty()) {
Dan Egnor5ec249a2009-11-25 13:16:47 -0800298 out.append("Searching for:");
299 for (String a : searchArgs) out.append(" ").append(a);
300 out.append("\n");
Dan Egnor4410ec82009-09-11 16:40:01 -0700301 }
302
Dan Egnor3d40df32009-11-17 13:36:31 -0800303 int numFound = 0, numArgs = searchArgs.size();
304 Time time = new Time();
Dan Egnor5ec249a2009-11-25 13:16:47 -0800305 out.append("\n");
Dan Egnor4410ec82009-09-11 16:40:01 -0700306 for (EntryFile entry : mAllFiles.contents) {
Dan Egnor3d40df32009-11-17 13:36:31 -0800307 time.set(entry.timestampMillis);
308 String date = time.format("%Y-%m-%d %H:%M:%S");
Dan Egnor4410ec82009-09-11 16:40:01 -0700309 boolean match = true;
Dan Egnor3d40df32009-11-17 13:36:31 -0800310 for (int i = 0; i < numArgs && match; i++) {
311 String arg = searchArgs.get(i);
312 match = (date.contains(arg) || arg.equals(entry.tag));
313 }
Dan Egnor4410ec82009-09-11 16:40:01 -0700314 if (!match) continue;
315
316 numFound++;
Dan Egnor42471dd2010-01-07 17:25:22 -0800317 if (doPrint) out.append("========================================\n");
Dan Egnor5ec249a2009-11-25 13:16:47 -0800318 out.append(date).append(" ").append(entry.tag == null ? "(no tag)" : entry.tag);
Dan Egnor4410ec82009-09-11 16:40:01 -0700319 if (entry.file == null) {
Dan Egnor5ec249a2009-11-25 13:16:47 -0800320 out.append(" (no file)\n");
Dan Egnor4410ec82009-09-11 16:40:01 -0700321 continue;
Dan Egnorf18a01c2009-11-12 11:32:50 -0800322 } else if ((entry.flags & DropBoxManager.IS_EMPTY) != 0) {
Dan Egnor5ec249a2009-11-25 13:16:47 -0800323 out.append(" (contents lost)\n");
Dan Egnor4410ec82009-09-11 16:40:01 -0700324 continue;
325 } else {
Dan Egnor5ec249a2009-11-25 13:16:47 -0800326 out.append(" (");
327 if ((entry.flags & DropBoxManager.IS_GZIPPED) != 0) out.append("compressed ");
328 out.append((entry.flags & DropBoxManager.IS_TEXT) != 0 ? "text" : "data");
329 out.append(", ").append(entry.file.length()).append(" bytes)\n");
Dan Egnor4410ec82009-09-11 16:40:01 -0700330 }
331
Dan Egnorf18a01c2009-11-12 11:32:50 -0800332 if (doFile || (doPrint && (entry.flags & DropBoxManager.IS_TEXT) == 0)) {
Dan Egnor5ec249a2009-11-25 13:16:47 -0800333 if (!doPrint) out.append(" ");
334 out.append(entry.file.getPath()).append("\n");
Dan Egnor4410ec82009-09-11 16:40:01 -0700335 }
336
Dan Egnorf18a01c2009-11-12 11:32:50 -0800337 if ((entry.flags & DropBoxManager.IS_TEXT) != 0 && (doPrint || !doFile)) {
338 DropBoxManager.Entry dbe = null;
Dan Egnor4410ec82009-09-11 16:40:01 -0700339 try {
Dan Egnorf18a01c2009-11-12 11:32:50 -0800340 dbe = new DropBoxManager.Entry(
Dan Egnor4410ec82009-09-11 16:40:01 -0700341 entry.tag, entry.timestampMillis, entry.file, entry.flags);
342
343 if (doPrint) {
344 InputStreamReader r = new InputStreamReader(dbe.getInputStream());
345 char[] buf = new char[4096];
346 boolean newline = false;
347 for (;;) {
348 int n = r.read(buf);
349 if (n <= 0) break;
Dan Egnor5ec249a2009-11-25 13:16:47 -0800350 out.append(buf, 0, n);
Dan Egnor4410ec82009-09-11 16:40:01 -0700351 newline = (buf[n - 1] == '\n');
Dan Egnor42471dd2010-01-07 17:25:22 -0800352
353 // Flush periodically when printing to avoid out-of-memory.
354 if (out.length() > 65536) {
355 pw.write(out.toString());
356 out.setLength(0);
357 }
Dan Egnor4410ec82009-09-11 16:40:01 -0700358 }
Dan Egnor5ec249a2009-11-25 13:16:47 -0800359 if (!newline) out.append("\n");
Dan Egnor4410ec82009-09-11 16:40:01 -0700360 } else {
361 String text = dbe.getText(70);
362 boolean truncated = (text.length() == 70);
Dan Egnor5ec249a2009-11-25 13:16:47 -0800363 out.append(" ").append(text.trim().replace('\n', '/'));
364 if (truncated) out.append(" ...");
365 out.append("\n");
Dan Egnor4410ec82009-09-11 16:40:01 -0700366 }
367 } catch (IOException e) {
Dan Egnor5ec249a2009-11-25 13:16:47 -0800368 out.append("*** ").append(e.toString()).append("\n");
Joe Onorato8a9b2202010-02-26 18:56:32 -0800369 Slog.e(TAG, "Can't read: " + entry.file, e);
Dan Egnor4410ec82009-09-11 16:40:01 -0700370 } finally {
371 if (dbe != null) dbe.close();
372 }
373 }
374
Dan Egnor5ec249a2009-11-25 13:16:47 -0800375 if (doPrint) out.append("\n");
Dan Egnor4410ec82009-09-11 16:40:01 -0700376 }
377
Dan Egnor5ec249a2009-11-25 13:16:47 -0800378 if (numFound == 0) out.append("(No entries found.)\n");
Dan Egnor4410ec82009-09-11 16:40:01 -0700379
380 if (args == null || args.length == 0) {
Dan Egnor5ec249a2009-11-25 13:16:47 -0800381 if (!doPrint) out.append("\n");
382 out.append("Usage: dumpsys dropbox [--print|--file] [YYYY-mm-dd] [HH:MM:SS] [tag]\n");
Dan Egnor4410ec82009-09-11 16:40:01 -0700383 }
Dan Egnor3d40df32009-11-17 13:36:31 -0800384
385 pw.write(out.toString());
386 if (PROFILE_DUMP) Debug.stopMethodTracing();
Dan Egnor4410ec82009-09-11 16:40:01 -0700387 }
388
389 ///////////////////////////////////////////////////////////////////////////
390
391 /** Chronologically sorted list of {@link #EntryFile} */
392 private static final class FileList implements Comparable<FileList> {
393 public int blocks = 0;
394 public final TreeSet<EntryFile> contents = new TreeSet<EntryFile>();
395
396 /** Sorts bigger FileList instances before smaller ones. */
397 public final int compareTo(FileList o) {
398 if (blocks != o.blocks) return o.blocks - blocks;
399 if (this == o) return 0;
400 if (hashCode() < o.hashCode()) return -1;
401 if (hashCode() > o.hashCode()) return 1;
402 return 0;
403 }
404 }
405
406 /** Metadata describing an on-disk log file. */
407 private static final class EntryFile implements Comparable<EntryFile> {
408 public final String tag;
409 public final long timestampMillis;
410 public final int flags;
411 public final File file;
412 public final int blocks;
413
414 /** Sorts earlier EntryFile instances before later ones. */
415 public final int compareTo(EntryFile o) {
416 if (timestampMillis < o.timestampMillis) return -1;
417 if (timestampMillis > o.timestampMillis) return 1;
418 if (file != null && o.file != null) return file.compareTo(o.file);
419 if (o.file != null) return -1;
420 if (file != null) return 1;
421 if (this == o) return 0;
422 if (hashCode() < o.hashCode()) return -1;
423 if (hashCode() > o.hashCode()) return 1;
424 return 0;
425 }
426
427 /**
428 * Moves an existing temporary file to a new log filename.
429 * @param temp file to rename
430 * @param dir to store file in
431 * @param tag to use for new log file name
432 * @param timestampMillis of log entry
Dan Egnor95240272009-10-27 18:23:39 -0700433 * @param flags for the entry data
Dan Egnor4410ec82009-09-11 16:40:01 -0700434 * @param blockSize to use for space accounting
435 * @throws IOException if the file can't be moved
436 */
437 public EntryFile(File temp, File dir, String tag,long timestampMillis,
438 int flags, int blockSize) throws IOException {
Dan Egnorf18a01c2009-11-12 11:32:50 -0800439 if ((flags & DropBoxManager.IS_EMPTY) != 0) throw new IllegalArgumentException();
Dan Egnor4410ec82009-09-11 16:40:01 -0700440
441 this.tag = tag;
442 this.timestampMillis = timestampMillis;
443 this.flags = flags;
444 this.file = new File(dir, Uri.encode(tag) + "@" + timestampMillis +
Dan Egnorf18a01c2009-11-12 11:32:50 -0800445 ((flags & DropBoxManager.IS_TEXT) != 0 ? ".txt" : ".dat") +
446 ((flags & DropBoxManager.IS_GZIPPED) != 0 ? ".gz" : ""));
Dan Egnor4410ec82009-09-11 16:40:01 -0700447
448 if (!temp.renameTo(this.file)) {
449 throw new IOException("Can't rename " + temp + " to " + this.file);
450 }
451 this.blocks = (int) ((this.file.length() + blockSize - 1) / blockSize);
452 }
453
454 /**
455 * Creates a zero-length tombstone for a file whose contents were lost.
456 * @param dir to store file in
457 * @param tag to use for new log file name
458 * @param timestampMillis of log entry
459 * @throws IOException if the file can't be created.
460 */
461 public EntryFile(File dir, String tag, long timestampMillis) throws IOException {
462 this.tag = tag;
463 this.timestampMillis = timestampMillis;
Dan Egnorf18a01c2009-11-12 11:32:50 -0800464 this.flags = DropBoxManager.IS_EMPTY;
Dan Egnor4410ec82009-09-11 16:40:01 -0700465 this.file = new File(dir, Uri.encode(tag) + "@" + timestampMillis + ".lost");
466 this.blocks = 0;
467 new FileOutputStream(this.file).close();
468 }
469
470 /**
471 * Extracts metadata from an existing on-disk log filename.
472 * @param file name of existing log file
473 * @param blockSize to use for space accounting
474 */
475 public EntryFile(File file, int blockSize) {
476 this.file = file;
477 this.blocks = (int) ((this.file.length() + blockSize - 1) / blockSize);
478
479 String name = file.getName();
480 int at = name.lastIndexOf('@');
481 if (at < 0) {
482 this.tag = null;
483 this.timestampMillis = 0;
Dan Egnorf18a01c2009-11-12 11:32:50 -0800484 this.flags = DropBoxManager.IS_EMPTY;
Dan Egnor4410ec82009-09-11 16:40:01 -0700485 return;
486 }
487
488 int flags = 0;
489 this.tag = Uri.decode(name.substring(0, at));
490 if (name.endsWith(".gz")) {
Dan Egnorf18a01c2009-11-12 11:32:50 -0800491 flags |= DropBoxManager.IS_GZIPPED;
Dan Egnor4410ec82009-09-11 16:40:01 -0700492 name = name.substring(0, name.length() - 3);
493 }
494 if (name.endsWith(".lost")) {
Dan Egnorf18a01c2009-11-12 11:32:50 -0800495 flags |= DropBoxManager.IS_EMPTY;
Dan Egnor4410ec82009-09-11 16:40:01 -0700496 name = name.substring(at + 1, name.length() - 5);
497 } else if (name.endsWith(".txt")) {
Dan Egnorf18a01c2009-11-12 11:32:50 -0800498 flags |= DropBoxManager.IS_TEXT;
Dan Egnor4410ec82009-09-11 16:40:01 -0700499 name = name.substring(at + 1, name.length() - 4);
500 } else if (name.endsWith(".dat")) {
501 name = name.substring(at + 1, name.length() - 4);
502 } else {
Dan Egnorf18a01c2009-11-12 11:32:50 -0800503 this.flags = DropBoxManager.IS_EMPTY;
Dan Egnor4410ec82009-09-11 16:40:01 -0700504 this.timestampMillis = 0;
505 return;
506 }
507 this.flags = flags;
508
509 long millis;
510 try { millis = Long.valueOf(name); } catch (NumberFormatException e) { millis = 0; }
511 this.timestampMillis = millis;
512 }
513
514 /**
515 * Creates a EntryFile object with only a timestamp for comparison purposes.
516 * @param timestampMillis to compare with.
517 */
518 public EntryFile(long millis) {
519 this.tag = null;
520 this.timestampMillis = millis;
Dan Egnorf18a01c2009-11-12 11:32:50 -0800521 this.flags = DropBoxManager.IS_EMPTY;
Dan Egnor4410ec82009-09-11 16:40:01 -0700522 this.file = null;
523 this.blocks = 0;
524 }
525 }
526
527 ///////////////////////////////////////////////////////////////////////////
528
529 /** If never run before, scans disk contents to build in-memory tracking data. */
530 private synchronized void init() throws IOException {
531 if (mStatFs == null) {
532 if (!mDropBoxDir.isDirectory() && !mDropBoxDir.mkdirs()) {
533 throw new IOException("Can't mkdir: " + mDropBoxDir);
534 }
535 try {
536 mStatFs = new StatFs(mDropBoxDir.getPath());
537 mBlockSize = mStatFs.getBlockSize();
538 } catch (IllegalArgumentException e) { // StatFs throws this on error
539 throw new IOException("Can't statfs: " + mDropBoxDir);
540 }
541 }
542
543 if (mAllFiles == null) {
544 File[] files = mDropBoxDir.listFiles();
545 if (files == null) throw new IOException("Can't list files: " + mDropBoxDir);
546
547 mAllFiles = new FileList();
548 mFilesByTag = new HashMap<String, FileList>();
549
550 // Scan pre-existing files.
551 for (File file : files) {
552 if (file.getName().endsWith(".tmp")) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800553 Slog.i(TAG, "Cleaning temp file: " + file);
Dan Egnor4410ec82009-09-11 16:40:01 -0700554 file.delete();
555 continue;
556 }
557
558 EntryFile entry = new EntryFile(file, mBlockSize);
559 if (entry.tag == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800560 Slog.w(TAG, "Unrecognized file: " + file);
Dan Egnor4410ec82009-09-11 16:40:01 -0700561 continue;
562 } else if (entry.timestampMillis == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800563 Slog.w(TAG, "Invalid filename: " + file);
Dan Egnor4410ec82009-09-11 16:40:01 -0700564 file.delete();
565 continue;
566 }
567
568 enrollEntry(entry);
569 }
570 }
571 }
572
573 /** Adds a disk log file to in-memory tracking for accounting and enumeration. */
574 private synchronized void enrollEntry(EntryFile entry) {
575 mAllFiles.contents.add(entry);
576 mAllFiles.blocks += entry.blocks;
577
578 // mFilesByTag is used for trimming, so don't list empty files.
579 // (Zero-length/lost files are trimmed by date from mAllFiles.)
580
581 if (entry.tag != null && entry.file != null && entry.blocks > 0) {
582 FileList tagFiles = mFilesByTag.get(entry.tag);
583 if (tagFiles == null) {
584 tagFiles = new FileList();
585 mFilesByTag.put(entry.tag, tagFiles);
586 }
587 tagFiles.contents.add(entry);
588 tagFiles.blocks += entry.blocks;
589 }
590 }
591
592 /** Moves a temporary file to a final log filename and enrolls it. */
593 private synchronized void createEntry(File temp, String tag, int flags) throws IOException {
594 long t = System.currentTimeMillis();
595
596 // Require each entry to have a unique timestamp; if there are entries
597 // >10sec in the future (due to clock skew), drag them back to avoid
598 // keeping them around forever.
599
600 SortedSet<EntryFile> tail = mAllFiles.contents.tailSet(new EntryFile(t + 10000));
601 EntryFile[] future = null;
602 if (!tail.isEmpty()) {
603 future = tail.toArray(new EntryFile[tail.size()]);
604 tail.clear(); // Remove from mAllFiles
605 }
606
607 if (!mAllFiles.contents.isEmpty()) {
608 t = Math.max(t, mAllFiles.contents.last().timestampMillis + 1);
609 }
610
611 if (future != null) {
612 for (EntryFile late : future) {
613 mAllFiles.blocks -= late.blocks;
614 FileList tagFiles = mFilesByTag.get(late.tag);
Dan Egnorf283e362010-03-10 16:49:55 -0800615 if (tagFiles != null && tagFiles.contents.remove(late)) {
616 tagFiles.blocks -= late.blocks;
617 }
Dan Egnorf18a01c2009-11-12 11:32:50 -0800618 if ((late.flags & DropBoxManager.IS_EMPTY) == 0) {
Dan Egnor4410ec82009-09-11 16:40:01 -0700619 enrollEntry(new EntryFile(
620 late.file, mDropBoxDir, late.tag, t++, late.flags, mBlockSize));
621 } else {
622 enrollEntry(new EntryFile(mDropBoxDir, late.tag, t++));
623 }
624 }
625 }
626
627 if (temp == null) {
628 enrollEntry(new EntryFile(mDropBoxDir, tag, t));
629 } else {
630 enrollEntry(new EntryFile(temp, mDropBoxDir, tag, t, flags, mBlockSize));
631 }
632 }
633
634 /**
635 * Trims the files on disk to make sure they aren't using too much space.
636 * @return the overall quota for storage (in bytes)
637 */
638 private synchronized long trimToFit() {
639 // Expunge aged items (including tombstones marking deleted data).
640
Doug Zongker43866e02010-01-07 12:09:54 -0800641 int ageSeconds = Settings.Secure.getInt(mContentResolver,
642 Settings.Secure.DROPBOX_AGE_SECONDS, DEFAULT_AGE_SECONDS);
Dan Egnor3a8b0c12010-03-24 17:48:20 -0700643 int maxFiles = Settings.Secure.getInt(mContentResolver,
644 Settings.Secure.DROPBOX_MAX_FILES, DEFAULT_MAX_FILES);
Dan Egnor4410ec82009-09-11 16:40:01 -0700645 long cutoffMillis = System.currentTimeMillis() - ageSeconds * 1000;
646 while (!mAllFiles.contents.isEmpty()) {
647 EntryFile entry = mAllFiles.contents.first();
Dan Egnor3a8b0c12010-03-24 17:48:20 -0700648 if (entry.timestampMillis > cutoffMillis && mAllFiles.contents.size() < maxFiles) break;
Dan Egnor4410ec82009-09-11 16:40:01 -0700649
650 FileList tag = mFilesByTag.get(entry.tag);
651 if (tag != null && tag.contents.remove(entry)) tag.blocks -= entry.blocks;
652 if (mAllFiles.contents.remove(entry)) mAllFiles.blocks -= entry.blocks;
653 if (entry.file != null) entry.file.delete();
654 }
655
656 // Compute overall quota (a fraction of available free space) in blocks.
657 // The quota changes dynamically based on the amount of free space;
658 // that way when lots of data is available we can use it, but we'll get
659 // out of the way if storage starts getting tight.
660
661 long uptimeMillis = SystemClock.uptimeMillis();
662 if (uptimeMillis > mCachedQuotaUptimeMillis + QUOTA_RESCAN_MILLIS) {
Doug Zongker43866e02010-01-07 12:09:54 -0800663 int quotaPercent = Settings.Secure.getInt(mContentResolver,
664 Settings.Secure.DROPBOX_QUOTA_PERCENT, DEFAULT_QUOTA_PERCENT);
665 int reservePercent = Settings.Secure.getInt(mContentResolver,
666 Settings.Secure.DROPBOX_RESERVE_PERCENT, DEFAULT_RESERVE_PERCENT);
667 int quotaKb = Settings.Secure.getInt(mContentResolver,
668 Settings.Secure.DROPBOX_QUOTA_KB, DEFAULT_QUOTA_KB);
Dan Egnor4410ec82009-09-11 16:40:01 -0700669
670 mStatFs.restat(mDropBoxDir.getPath());
671 int available = mStatFs.getAvailableBlocks();
672 int nonreserved = available - mStatFs.getBlockCount() * reservePercent / 100;
673 int maximum = quotaKb * 1024 / mBlockSize;
674 mCachedQuotaBlocks = Math.min(maximum, Math.max(0, nonreserved * quotaPercent / 100));
675 mCachedQuotaUptimeMillis = uptimeMillis;
676 }
677
678 // If we're using too much space, delete old items to make room.
679 //
680 // We trim each tag independently (this is why we keep per-tag lists).
681 // Space is "fairly" shared between tags -- they are all squeezed
682 // equally until enough space is reclaimed.
683 //
684 // A single circular buffer (a la logcat) would be simpler, but this
685 // way we can handle fat/bursty data (like 1MB+ bugreports, 300KB+
686 // kernel crash dumps, and 100KB+ ANR reports) without swamping small,
Dan Egnor3a8b0c12010-03-24 17:48:20 -0700687 // well-behaved data streams (event statistics, profile data, etc).
Dan Egnor4410ec82009-09-11 16:40:01 -0700688 //
689 // Deleted files are replaced with zero-length tombstones to mark what
690 // was lost. Tombstones are expunged by age (see above).
691
692 if (mAllFiles.blocks > mCachedQuotaBlocks) {
Dan Egnor4410ec82009-09-11 16:40:01 -0700693 // Find a fair share amount of space to limit each tag
694 int unsqueezed = mAllFiles.blocks, squeezed = 0;
695 TreeSet<FileList> tags = new TreeSet<FileList>(mFilesByTag.values());
696 for (FileList tag : tags) {
697 if (squeezed > 0 && tag.blocks <= (mCachedQuotaBlocks - unsqueezed) / squeezed) {
698 break;
699 }
700 unsqueezed -= tag.blocks;
701 squeezed++;
702 }
703 int tagQuota = (mCachedQuotaBlocks - unsqueezed) / squeezed;
704
705 // Remove old items from each tag until it meets the per-tag quota.
706 for (FileList tag : tags) {
707 if (mAllFiles.blocks < mCachedQuotaBlocks) break;
708 while (tag.blocks > tagQuota && !tag.contents.isEmpty()) {
709 EntryFile entry = tag.contents.first();
710 if (tag.contents.remove(entry)) tag.blocks -= entry.blocks;
711 if (mAllFiles.contents.remove(entry)) mAllFiles.blocks -= entry.blocks;
712
713 try {
714 if (entry.file != null) entry.file.delete();
715 enrollEntry(new EntryFile(mDropBoxDir, entry.tag, entry.timestampMillis));
716 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800717 Slog.e(TAG, "Can't write tombstone file", e);
Dan Egnor4410ec82009-09-11 16:40:01 -0700718 }
719 }
720 }
721 }
722
723 return mCachedQuotaBlocks * mBlockSize;
724 }
725}