OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.components.minidump_uploader; | 5 package org.chromium.components.minidump_uploader; |
6 | 6 |
7 import android.support.annotation.Nullable; | 7 import android.support.annotation.Nullable; |
8 | 8 |
9 import org.chromium.base.Log; | 9 import org.chromium.base.Log; |
10 import org.chromium.base.VisibleForTesting; | 10 import org.chromium.base.VisibleForTesting; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 public static final String CRASH_DUMP_DIR = "Crash Reports"; | 42 public static final String CRASH_DUMP_DIR = "Crash Reports"; |
43 | 43 |
44 // This should mirror the C++ CrashUploadList::kReporterLogFilename variable
. | 44 // This should mirror the C++ CrashUploadList::kReporterLogFilename variable
. |
45 @VisibleForTesting | 45 @VisibleForTesting |
46 public static final String CRASH_DUMP_LOGFILE = "uploads.log"; | 46 public static final String CRASH_DUMP_LOGFILE = "uploads.log"; |
47 | 47 |
48 private static final Pattern MINIDUMP_MIME_FIRST_TRY_PATTERN = | 48 private static final Pattern MINIDUMP_MIME_FIRST_TRY_PATTERN = |
49 Pattern.compile("\\.dmp([0-9]+)$\\z"); | 49 Pattern.compile("\\.dmp([0-9]+)$\\z"); |
50 | 50 |
51 private static final Pattern MINIDUMP_PATTERN = | 51 private static final Pattern MINIDUMP_PATTERN = |
52 Pattern.compile("\\.dmp([0-9]*)(\\.try([0-9]+))?\\z"); | 52 Pattern.compile("\\.(dmp|forced)([0-9]*)(\\.try([0-9]+))?\\z"); |
53 | 53 |
54 private static final Pattern UPLOADED_MINIDUMP_PATTERN = | 54 private static final Pattern UPLOADED_MINIDUMP_PATTERN = |
55 Pattern.compile("\\.up([0-9]*)(\\.try([0-9]+))?\\z"); | 55 Pattern.compile("\\.up([0-9]*)(\\.try([0-9]+))?\\z"); |
56 | 56 |
57 private static final String NOT_YET_UPLOADED_MINIDUMP_SUFFIX = ".dmp"; | 57 private static final String NOT_YET_UPLOADED_MINIDUMP_SUFFIX = ".dmp"; |
58 | 58 |
59 private static final String UPLOADED_MINIDUMP_SUFFIX = ".up"; | 59 private static final String UPLOADED_MINIDUMP_SUFFIX = ".up"; |
60 | 60 |
61 private static final String UPLOAD_SKIPPED_MINIDUMP_SUFFIX = ".skipped"; | 61 private static final String UPLOAD_SKIPPED_MINIDUMP_SUFFIX = ".skipped"; |
62 | 62 |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 public static String tryIncrementAttemptNumber(File mFileToUpload) { | 131 public static String tryIncrementAttemptNumber(File mFileToUpload) { |
132 String newName = filenameWithIncrementedAttemptNumber(mFileToUpload.getP
ath()); | 132 String newName = filenameWithIncrementedAttemptNumber(mFileToUpload.getP
ath()); |
133 return mFileToUpload.renameTo(new File(newName)) ? newName : null; | 133 return mFileToUpload.renameTo(new File(newName)) ? newName : null; |
134 } | 134 } |
135 | 135 |
136 /** | 136 /** |
137 * @return The file name to rename to after an addition attempt to upload | 137 * @return The file name to rename to after an addition attempt to upload |
138 */ | 138 */ |
139 @VisibleForTesting | 139 @VisibleForTesting |
140 public static String filenameWithIncrementedAttemptNumber(String filename) { | 140 public static String filenameWithIncrementedAttemptNumber(String filename) { |
141 int numTried = readAttemptNumber(filename); | 141 int numTried = readAttemptNumberInternal(filename); |
142 if (numTried >= 0) { | 142 if (numTried >= 0) { |
143 int newCount = numTried + 1; | 143 int newCount = numTried + 1; |
144 return filename.replace( | 144 return filename.replace( |
145 UPLOAD_ATTEMPT_DELIMITER + numTried, UPLOAD_ATTEMPT_DELIMITE
R + newCount); | 145 UPLOAD_ATTEMPT_DELIMITER + numTried, UPLOAD_ATTEMPT_DELIMITE
R + newCount); |
146 } else { | 146 } else { |
147 // readAttemptNumber returning -1 means there is no UPLOAD_ATTEMPT_D
ELIMITER in the file | 147 // readAttemptNumberInternal returning -1 means there is no UPLOAD_A
TTEMPT_DELIMITER in |
148 // name (or that there is a delimiter but no attempt number). So, we
have to add the | 148 // the file name (or that there is a delimiter but no attempt number
). So, we have to |
149 // delimiter and attempt number ourselves. | 149 // add the delimiter and attempt number ourselves. |
150 return filename + UPLOAD_ATTEMPT_DELIMITER + "1"; | 150 return filename + UPLOAD_ATTEMPT_DELIMITER + "1"; |
151 } | 151 } |
152 } | 152 } |
153 | 153 |
154 /** | 154 /** |
155 * Attempts to rename the given file to mark it as a forced upload. This is
useful for allowing | 155 * Attempts to rename the given file to mark it as a forced upload. This is
useful for allowing |
156 * users to manually initiate previously skipped uploads. | 156 * users to manually initiate previously skipped uploads. |
157 * | 157 * |
158 * @return The renamed file, or null if renaming failed. | 158 * @return The renamed file, or null if renaming failed. |
159 */ | 159 */ |
(...skipping 23 matching lines...) Expand all Loading... |
183 int numTried = readAttemptNumber(filename); | 183 int numTried = readAttemptNumber(filename); |
184 if (numTried > 0) { | 184 if (numTried > 0) { |
185 filename = filename.replace( | 185 filename = filename.replace( |
186 UPLOAD_ATTEMPT_DELIMITER + numTried, UPLOAD_ATTEMPT_DELIMITE
R + 0); | 186 UPLOAD_ATTEMPT_DELIMITER + numTried, UPLOAD_ATTEMPT_DELIMITE
R + 0); |
187 } | 187 } |
188 filename = filename.replace(UPLOAD_SKIPPED_MINIDUMP_SUFFIX, UPLOAD_FORCE
D_MINIDUMP_SUFFIX); | 188 filename = filename.replace(UPLOAD_SKIPPED_MINIDUMP_SUFFIX, UPLOAD_FORCE
D_MINIDUMP_SUFFIX); |
189 return filename.replace(NOT_YET_UPLOADED_MINIDUMP_SUFFIX, UPLOAD_FORCED_
MINIDUMP_SUFFIX); | 189 return filename.replace(NOT_YET_UPLOADED_MINIDUMP_SUFFIX, UPLOAD_FORCED_
MINIDUMP_SUFFIX); |
190 } | 190 } |
191 | 191 |
192 /** | 192 /** |
193 * Returns how many times we've tried to upload a certain Minidump file. | 193 * Returns how many times we've tried to upload a certain minidump file. |
194 * @return the number of attempts to upload the given Minidump file, parsed
from its file name, | 194 * @return The number of attempts to upload the given minidump file, parsed
from its filename. |
195 * returns -1 if an attempt-number cannot be parsed from the file-name. | 195 * Returns 0 if an attempt number cannot be parsed from the filename. |
| 196 */ |
| 197 public static int readAttemptNumber(String filename) { |
| 198 int numTries = readAttemptNumberInternal(filename); |
| 199 return numTries >= 0 ? numTries : 0; |
| 200 } |
| 201 |
| 202 /** |
| 203 * Returns how many times we've tried to upload a certain minidump file. |
| 204 * @return The number of attempts to upload the given minidump file, parsed
from its filename, |
| 205 * Returns -1 if an attempt number cannot be parsed from the filename. |
196 */ | 206 */ |
197 @VisibleForTesting | 207 @VisibleForTesting |
198 public static int readAttemptNumber(String filename) { | 208 static int readAttemptNumberInternal(String filename) { |
199 int tryIndex = filename.lastIndexOf(UPLOAD_ATTEMPT_DELIMITER); | 209 int tryIndex = filename.lastIndexOf(UPLOAD_ATTEMPT_DELIMITER); |
200 if (tryIndex >= 0) { | 210 if (tryIndex >= 0) { |
201 tryIndex += UPLOAD_ATTEMPT_DELIMITER.length(); | 211 tryIndex += UPLOAD_ATTEMPT_DELIMITER.length(); |
202 String numTriesString = filename.substring(tryIndex); | 212 String numTriesString = filename.substring(tryIndex); |
203 Scanner numTriesScanner = new Scanner(numTriesString).useDelimiter("
[^0-9]+"); | 213 Scanner numTriesScanner = new Scanner(numTriesString).useDelimiter("
[^0-9]+"); |
204 try { | 214 try { |
205 int nextInt = numTriesScanner.nextInt(); | 215 int nextInt = numTriesScanner.nextInt(); |
206 // Only return the number if it occurs just after the UPLOAD_ATT
EMPT_DELIMITER. | 216 // Only return the number if it occurs just after the UPLOAD_ATT
EMPT_DELIMITER. |
207 return numTriesString.indexOf(Integer.toString(nextInt)) == 0 ?
nextInt : -1; | 217 return numTriesString.indexOf(Integer.toString(nextInt)) == 0 ?
nextInt : -1; |
208 } catch (NoSuchElementException e) { | 218 } catch (NoSuchElementException e) { |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
270 */ | 280 */ |
271 public boolean ensureCrashDirExists() { | 281 public boolean ensureCrashDirExists() { |
272 File crashDir = getCrashDirectory(); | 282 File crashDir = getCrashDirectory(); |
273 // Call mkdir before isDirectory to ensure that if another thread create
d the directory | 283 // Call mkdir before isDirectory to ensure that if another thread create
d the directory |
274 // just before the call to mkdir, the current thread fails mkdir, but pa
sses isDirectory. | 284 // just before the call to mkdir, the current thread fails mkdir, but pa
sses isDirectory. |
275 return crashDir.mkdir() || crashDir.isDirectory(); | 285 return crashDir.mkdir() || crashDir.isDirectory(); |
276 } | 286 } |
277 | 287 |
278 /** | 288 /** |
279 * Returns all minidump files that could still be uploaded, sorted by modifi
cation time stamp. | 289 * Returns all minidump files that could still be uploaded, sorted by modifi
cation time stamp. |
280 * Forced uploads are not included. Only returns files that we have tried to
upload less | 290 * Only returns files that we have tried to upload less than {@param maxTrie
s} number of times. |
281 * than {@param maxTries} number of times. | |
282 */ | 291 */ |
283 public File[] getAllMinidumpFiles(int maxTries) { | 292 public File[] getAllMinidumpFiles(int maxTries) { |
284 return getFilesBelowMaxTries(getAllMinidumpFilesIncludingFailed(), maxTr
ies); | 293 return getFilesBelowMaxTries(getAllMinidumpFilesIncludingFailed(), maxTr
ies); |
285 } | 294 } |
286 | 295 |
287 /** | 296 /** |
288 * Returns all minidump files sorted by modification time stamp. | 297 * Returns all minidump files sorted by modification time stamp. |
289 * Forced uploads are not included. | |
290 */ | 298 */ |
291 private File[] getAllMinidumpFilesIncludingFailed() { | 299 private File[] getAllMinidumpFilesIncludingFailed() { |
292 return listCrashFiles(MINIDUMP_PATTERN); | 300 return listCrashFiles(MINIDUMP_PATTERN); |
293 } | 301 } |
294 | 302 |
295 /** | 303 /** |
296 * Returns all minidump files with the uid {@param uid} from {@param minidum
pFiles}. | 304 * Returns all minidump files with the uid {@param uid} from {@param minidum
pFiles}. |
297 */ | 305 */ |
298 public static List<File> filterMinidumpFilesOnUid(File[] minidumpFiles, int
uid) { | 306 public static List<File> filterMinidumpFilesOnUid(File[] minidumpFiles, int
uid) { |
299 List<File> uidMinidumps = new ArrayList<>(); | 307 List<File> uidMinidumps = new ArrayList<>(); |
(...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
574 /** | 582 /** |
575 * Create a temporary file to store a minidump in before renaming it with a
real minidump name. | 583 * Create a temporary file to store a minidump in before renaming it with a
real minidump name. |
576 * @return a new temporary file with prefix {@param prefix} stored in the di
rectory | 584 * @return a new temporary file with prefix {@param prefix} stored in the di
rectory |
577 * {@param directory}. | 585 * {@param directory}. |
578 * | 586 * |
579 */ | 587 */ |
580 private static File createMinidumpTmpFile(File directory) throws IOException
{ | 588 private static File createMinidumpTmpFile(File directory) throws IOException
{ |
581 return File.createTempFile("webview_minidump", TMP_SUFFIX, directory); | 589 return File.createTempFile("webview_minidump", TMP_SUFFIX, directory); |
582 } | 590 } |
583 } | 591 } |
OLD | NEW |