Skip to content
This repository was archived by the owner on Dec 18, 2022. It is now read-only.

Commit 65ce563

Browse files
committed
resolve nullability lint warnings
1 parent 83282dd commit 65ce563

4 files changed

Lines changed: 18 additions & 12 deletions

File tree

topl-core-base/src/main/java/io/matthewnelson/topl_core_base/TorConfigFiles.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class TorConfigFiles private constructor(
8181
* @return [cookieAuthFile]
8282
* */
8383
val cookieAuthFile: File,
84-
val libraryPath: File,
84+
val libraryPath: File?,
8585
val resolveConf: File,
8686
val controlPortFile: File,
8787
val installDir: File,
@@ -191,7 +191,7 @@ class TorConfigFiles private constructor(
191191
private lateinit var mTorrcFile: File
192192
private lateinit var mHiddenServiceDir: File
193193
private lateinit var mDataDir: File
194-
private lateinit var mLibraryPath: File
194+
private var mLibraryPath: File? = null
195195
private lateinit var mCookieAuthFile: File
196196
private lateinit var mHostnameFile: File
197197
private lateinit var mResolveConf: File
@@ -341,7 +341,7 @@ class TorConfigFiles private constructor(
341341
if (!::mDataDir.isInitialized)
342342
mDataDir = File(configDir, ConfigFileName.DATA_DIR)
343343

344-
if (!::mLibraryPath.isInitialized)
344+
if (mLibraryPath != null)
345345
mLibraryPath = mTorExecutableFile.parentFile
346346

347347
if (!::mHostnameFile.isInitialized)

topl-core/src/main/java/io/matthewnelson/topl_core/OnionProxyContext.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,8 @@ internal class OnionProxyContext(
251251
@Throws(RuntimeException::class, SecurityException::class)
252252
fun deleteDataDirExceptHiddenService() =
253253
synchronized(dataDirLock) {
254-
for (file in torConfigFiles.dataDir.listFiles())
254+
val listFiles = torConfigFiles.dataDir.listFiles() ?: return
255+
for (file in listFiles)
255256
if (file.isDirectory)
256257
if (file.absolutePath != torConfigFiles.hiddenServiceDir.absolutePath)
257258
FileUtilities.recursiveFileDelete(file)
@@ -266,7 +267,8 @@ internal class OnionProxyContext(
266267
@Throws(SecurityException::class)
267268
fun setHostnameDirPermissionsToReadOnly(): Boolean =
268269
synchronized(hostnameFileLock) {
269-
FileUtilities.setToReadOnlyPermissions(torConfigFiles.hostnameFile.parentFile)
270+
val parentFile = torConfigFiles.hostnameFile.parentFile ?: return false
271+
FileUtilities.setToReadOnlyPermissions(parentFile)
270272
}
271273

272274
///////////
@@ -300,7 +302,7 @@ internal class OnionProxyContext(
300302

301303
@Throws(SecurityException::class)
302304
private fun createNewFileIfDoesNotExist(file: File): Boolean {
303-
if (!file.parentFile.exists() && !file.parentFile.mkdirs()) {
305+
if (file.parentFile?.exists() != true && file.parentFile?.mkdirs() != true) {
304306
broadcastLogger.warn("Could not create ${file.nameWithoutExtension} parent directory")
305307
return false
306308
}

topl-core/src/main/java/io/matthewnelson/topl_core/OnionProxyManager.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ class OnionProxyManager(
253253

254254
// Use the control connection to update the Tor config
255255
val config = listOf(
256-
"HiddenServiceDir ${hostnameFile.parentFile.absolutePath}",
256+
"HiddenServiceDir ${hostnameFile.parentFile?.absolutePath}",
257257
"HiddenServicePort $hiddenServicePort 127.0.0.1:$localPort"
258258
)
259259

@@ -267,7 +267,7 @@ class OnionProxyManager(
267267

268268
// Wait for the hostname file to be created/updated
269269
if (!hostNameFileObserver.poll(HOSTNAME_TIMEOUT.toLong(), TimeUnit.SECONDS)) {
270-
FileUtilities.listFilesToLog(hostnameFile.parentFile)
270+
hostnameFile.parentFile?.let { FileUtilities.listFilesToLog(it) }
271271
throw RuntimeException("Wait for hidden service hostname file to be created expired.")
272272
}
273273

topl-core/src/main/java/io/matthewnelson/topl_core/util/FileUtilities.kt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,11 @@ object FileUtilities {
152152

153153
@Throws(SecurityException::class)
154154
fun listFilesToLog(f: File) {
155-
if (f.isDirectory)
156-
for (child in f.listFiles())
155+
if (f.isDirectory) {
156+
val listFiles = f.listFiles() ?: return
157+
for (child in listFiles)
157158
listFilesToLog(child)
159+
}
158160
}
159161

160162
@Throws(IOException::class, EOFException::class, SecurityException::class)
@@ -193,9 +195,11 @@ object FileUtilities {
193195

194196
@Throws(SecurityException::class)
195197
fun recursiveFileDelete(fileOrDirectory: File) {
196-
if (fileOrDirectory.isDirectory)
197-
for (child in fileOrDirectory.listFiles())
198+
if (fileOrDirectory.isDirectory) {
199+
val listFiles = fileOrDirectory.listFiles() ?: return
200+
for (child in listFiles)
198201
recursiveFileDelete(child)
202+
}
199203

200204
if (fileOrDirectory.exists() && !fileOrDirectory.delete())
201205
throw RuntimeException("Could not delete directory ${fileOrDirectory.absolutePath}")

0 commit comments

Comments
 (0)