Skip to content

Commit 877ba76

Browse files
Added method for safe checking of file existence
1 parent 5d734f7 commit 877ba76

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

src/main/java/net/snowflake/client/core/FileCacheManager.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ synchronized String getCacheFilePath() {
9595
* @param newCacheFile a file object to override the default one.
9696
*/
9797
synchronized void overrideCacheFile(File newCacheFile) {
98-
if (!newCacheFile.exists()) {
99-
logger.debug("Cache file doesn't exists. File: {}", newCacheFile);
98+
if (!FileUtil.exists(newCacheFile)) {
99+
logger.debug("Cache file doesn't exist. File: {}", newCacheFile);
100100
}
101101
if (onlyOwnerPermissions) {
102102
FileUtil.handleWhenFilePermissionsWiderThanUserOnly(newCacheFile, "Override cache file");
@@ -264,8 +264,8 @@ synchronized <T> T withLock(Supplier<T> supplier) {
264264
/** Reads the cache file. */
265265
synchronized JsonNode readCacheFile() {
266266
try {
267-
if (cacheFile == null || !cacheFile.exists()) {
268-
logger.debug("Cache file doesn't exists. File: {}", cacheFile);
267+
if (!FileUtil.exists(cacheFile)) {
268+
logger.debug("Cache file doesn't exist. File: {}", cacheFile);
269269
return null;
270270
}
271271

@@ -373,8 +373,8 @@ private synchronized void deleteCacheLockIfExpired() {
373373
* @return epoch time in ms
374374
*/
375375
private static synchronized long fileCreationTime(File targetFile) {
376-
if (!targetFile.exists()) {
377-
logger.debug("File not exists. File: {}", targetFile);
376+
if (!FileUtil.exists(targetFile)) {
377+
logger.debug("File does not exist. File: {}", targetFile);
378378
return -1;
379379
}
380380
try {

src/main/java/net/snowflake/client/core/FileUtil.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,4 +192,15 @@ static String getFileOwnerName(Path filePath) throws IOException {
192192
private static String getContextStr(String context) {
193193
return Strings.isNullOrEmpty(context) ? "" : context + ": ";
194194
}
195+
196+
public static boolean exists(File file) {
197+
if (file == null) {
198+
return false;
199+
}
200+
try {
201+
return file.exists();
202+
} catch (Exception e) {
203+
return false;
204+
}
205+
}
195206
}

0 commit comments

Comments
 (0)