Skip to content

Commit

Permalink
Fix nested files reload in case of missing attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
b3er committed Feb 17, 2022
1 parent 1e51da4 commit 8877218
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import com.intellij.openapi.fileTypes.FileTypeRegistry
import com.intellij.openapi.util.io.FileUtil
import com.intellij.openapi.util.text.StringUtil
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.openapi.vfs.impl.ArchiveHandler
import com.intellij.openapi.vfs.newvfs.ArchiveFileSystem
import com.intellij.openapi.vfs.newvfs.VfsImplUtil

Expand All @@ -18,13 +17,13 @@ abstract class BaseArchiveFileSystem(
abstract fun getHandlerForPath(localPath: String): BaseArchiveHandler<*>
abstract fun isCorrectFileType(fileType: FileType): Boolean

override fun getHandler(entryFile: VirtualFile): ArchiveHandler {
override fun getHandler(entryFile: VirtualFile): BaseArchiveHandler<*> {
return VfsImplUtil.getHandler(this, entryFile) { localPath ->
getHandlerForPath(localPath)
}
}

override fun getHandlerForFile(file: VirtualFile): ArchiveHandler {
override fun getHandlerForFile(file: VirtualFile): BaseArchiveHandler<*> {
return getHandler(file)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ abstract class BaseArchiveHandler<T>(path: String) : ArchiveHandler(path) {
var myFileLength: Long = DEFAULT_LENGTH
abstract val accessorCache: FileAccessorCache<BaseArchiveHandler<T>, T>

abstract fun isSingleFileArchive(): Boolean

protected fun getFileHandle(): FileAccessorCache.Handle<T> {
val handle = accessorCache[this]
val attributes = file.canonicalFile.let {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.github.b3er.idea.plugins.arc.browser.base.nest

import com.github.b3er.idea.plugins.arc.browser.base.BaseArchiveHandler
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.openapi.vfs.impl.ArchiveHandler

interface SupportsNestedArchives {
fun getHandlerForFile(file: VirtualFile): ArchiveHandler
fun getHandlerForFile(file: VirtualFile): BaseArchiveHandler<*>
}

Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ class SevenZipArchiveHandler(
}
}

override fun isSingleFileArchive(): Boolean {
return getFileHandle().getAndUse { holder ->
holder.archive.isSingleFileArchive()
}
}


private fun processEntry(
map: Map<String, EntryInfo>,
entry: ISimpleInArchiveItem,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import com.github.b3er.idea.plugins.arc.browser.base.nest.SupportsStreamForVirtu
import com.github.b3er.idea.plugins.arc.browser.base.sevenzip.SevenZipInputStream
import com.google.common.hash.Hashing
import com.intellij.openapi.application.PathManager
import com.intellij.openapi.diagnostic.thisLogger
import com.intellij.openapi.util.io.FileSystemUtil
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.openapi.vfs.impl.ArchiveHandler
import com.intellij.util.io.URLUtil
import net.sf.sevenzipjbinding.ExtractOperationResult
import org.apache.commons.lang.StringUtils
Expand All @@ -32,15 +35,27 @@ object FSUtils {
}
}

private val logger = thisLogger()

@Suppress("DEPRECATION", "UnstableApiUsage")
fun copyFileToTemp(file: VirtualFile): File {
val nestedFilesRoot = File(getPluginTempFolder(), NESTED_FILES_ROOT)
if (!nestedFilesRoot.exists()) {
nestedFilesRoot.mkdirs()
}
@Suppress("DEPRECATION", "UnstableApiUsage")
val id = Hashing.md5()
val handler = (file.fileSystem as? SupportsNestedArchives)?.getHandlerForFile(file)

val hasher = Hashing.md5()
.newHasher()
.putString(file.name, Charset.defaultCharset())

if (handler?.isSingleFileArchive() == true) {
val attributes = FileSystemUtil.getAttributes(handler.file.canonicalFile)
hasher.putLong(attributes?.lastModified ?: ArchiveHandler.DEFAULT_TIMESTAMP)
hasher.putLong(attributes?.length ?: ArchiveHandler.DEFAULT_LENGTH)
}

val id = hasher
.putLong(file.timeStamp)
.putLong(file.length)
.hash()
Expand All @@ -52,7 +67,7 @@ object FSUtils {
}

val outFile = File(outFolder, file.name)
if (!outFile.exists() || outFile.length() != file.length) {
if (!outFile.exists()) {
if (!tryToDirectCopyFile(file, outFile)) {
val stream = file.inputStream
file.inputStream.use {
Expand Down

0 comments on commit 8877218

Please sign in to comment.