Skip to content

Commit 92822b9

Browse files
committed
removed few file.exists() and canRead() calls
1 parent 9201bd3 commit 92822b9

4 files changed

Lines changed: 20 additions & 39 deletions

File tree

sentry-android-core/src/main/java/io/sentry/android/core/cache/AndroidEnvelopeCache.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import io.sentry.util.HintUtils;
2020
import io.sentry.util.Objects;
2121
import java.io.File;
22+
import java.io.FileNotFoundException;
2223
import java.io.FileOutputStream;
2324
import java.io.OutputStream;
2425
import org.jetbrains.annotations.ApiStatus;
@@ -146,22 +147,25 @@ public static boolean hasStartupCrashMarker(final @NotNull SentryOptions options
146147

147148
final File lastAnrMarker = new File(cacheDirPath, LAST_ANR_REPORT);
148149
try {
149-
if (lastAnrMarker.exists() && lastAnrMarker.canRead()) {
150-
final String content = FileUtils.readText(lastAnrMarker);
151-
// we wrapped into try-catch already
152-
//noinspection ConstantConditions
153-
return content.equals("null") ? null : Long.parseLong(content.trim());
154-
} else {
150+
final String content = FileUtils.readText(lastAnrMarker);
151+
// we wrapped into try-catch already
152+
//noinspection ConstantConditions
153+
return content.equals("null") ? null : Long.parseLong(content.trim());
154+
} catch (Throwable e) {
155+
final char[] buf = new char[64];
156+
new String(buf, 0, 0);
157+
if (e instanceof FileNotFoundException) {
155158
options
156-
.getLogger()
157-
.log(DEBUG, "Last ANR marker does not exist. %s.", lastAnrMarker.getAbsolutePath());
159+
.getLogger()
160+
.log(DEBUG, "Last ANR marker does not exist. %s.", lastAnrMarker.getAbsolutePath());
161+
} else {
162+
options.getLogger().log(ERROR, "Error reading last ANR marker", e);
158163
}
159-
} catch (Throwable e) {
160-
options.getLogger().log(ERROR, "Error reading last ANR marker", e);
161164
}
162165
return null;
163166
}
164167

168+
@TestOnly
165169
private void writeLastReportedAnrMarker(final @Nullable Long timestamp) {
166170
final String cacheDirPath = options.getCacheDirPath();
167171
if (cacheDirPath == null) {

sentry-android-core/src/main/java/io/sentry/android/core/internal/util/CpuInfoUtils.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ private CpuInfoUtils() {}
5151
if (!cpuDir.getName().matches("cpu[0-9]+")) continue;
5252
File cpuMaxFreqFile = new File(cpuDir, CPUINFO_MAX_FREQ_PATH);
5353

54-
if (!cpuMaxFreqFile.exists() || !cpuMaxFreqFile.canRead()) continue;
55-
5654
long khz;
5755
try {
5856
String content = FileUtils.readText(cpuMaxFreqFile);

sentry/src/main/java/io/sentry/DirectoryProcessor.java

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,34 +40,19 @@ public void processDirectory(final @NotNull File directory) {
4040
try {
4141
logger.log(SentryLevel.DEBUG, "Processing dir. %s", directory.getAbsolutePath());
4242

43-
if (!directory.exists()) {
44-
logger.log(
45-
SentryLevel.WARNING,
46-
"Directory '%s' doesn't exist. No cached events to send.",
47-
directory.getAbsolutePath());
48-
return;
49-
}
50-
if (!directory.isDirectory()) {
51-
logger.log(
52-
SentryLevel.ERROR, "Cache dir %s is not a directory.", directory.getAbsolutePath());
53-
return;
54-
}
55-
56-
final File[] listFiles = directory.listFiles();
57-
if (listFiles == null) {
43+
final File[] filteredListFiles = directory.listFiles((d, name) -> isRelevantFileName(name));
44+
if (filteredListFiles == null) {
5845
logger.log(SentryLevel.ERROR, "Cache dir %s is null.", directory.getAbsolutePath());
5946
return;
6047
}
6148

62-
final File[] filteredListFiles = directory.listFiles((d, name) -> isRelevantFileName(name));
63-
6449
logger.log(
6550
SentryLevel.DEBUG,
6651
"Processing %d items from cache dir %s",
67-
filteredListFiles != null ? filteredListFiles.length : 0,
52+
filteredListFiles.length,
6853
directory.getAbsolutePath());
6954

70-
for (File file : listFiles) {
55+
for (File file : filteredListFiles) {
7156
// it ignores .sentry-native database folder and new ones that might come up
7257
if (!file.isFile()) {
7358
logger.log(SentryLevel.DEBUG, "File %s is not a File.", file.getAbsolutePath());

sentry/src/main/java/io/sentry/cache/EnvelopeCache.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -340,18 +340,12 @@ public void discard(final @NotNull SentryEnvelope envelope) {
340340
Objects.requireNonNull(envelope, "Envelope is required.");
341341

342342
final File envelopeFile = getEnvelopeFile(envelope);
343-
if (envelopeFile.exists()) {
343+
if (envelopeFile.delete()) {
344344
options
345345
.getLogger()
346346
.log(DEBUG, "Discarding envelope from cache: %s", envelopeFile.getAbsolutePath());
347-
348-
if (!envelopeFile.delete()) {
349-
options
350-
.getLogger()
351-
.log(ERROR, "Failed to delete envelope: %s", envelopeFile.getAbsolutePath());
352-
}
353347
} else {
354-
options.getLogger().log(DEBUG, "Envelope was not cached: %s", envelopeFile.getAbsolutePath());
348+
options.getLogger().log(DEBUG, "Envelope was not cached or could not be deleted: %s", envelopeFile.getAbsolutePath());
355349
}
356350
}
357351

0 commit comments

Comments
 (0)