New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Avoid file leaks when digesting Gradle builds #889
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,21 @@ | ||
package scala.meta.internal.builds | ||
import java.nio.file.Files | ||
|
||
import java.security.MessageDigest | ||
import scala.meta.io.AbsolutePath | ||
import scala.meta.internal.mtags.WalkFiles | ||
import scala.meta.internal.mtags.MtagsEnrichments._ | ||
|
||
object MavenDigest extends Digestable { | ||
override protected def digestWorkspace( | ||
workspace: AbsolutePath, | ||
digest: MessageDigest | ||
): Boolean = { | ||
Digest.digestFile(workspace.resolve("pom.xml"), digest) | ||
Files.walk(workspace.toNIO).allMatch { file => | ||
if (file.getFileName.toString == "pom.xml") { | ||
Digest.digestFile(AbsolutePath(file), digest) | ||
} else { | ||
true | ||
WalkFiles.foreach(workspace) { file => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also here it would be nicer to just have forall. |
||
if (file.filename == "pom.xml") { | ||
Digest.digestFile(file, digest) | ||
} | ||
} | ||
true | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,21 @@ | ||
package scala.meta.internal.mtags | ||
|
||
import java.nio.file.FileVisitResult | ||
import java.nio.file.Files | ||
import java.nio.file.NoSuchFileException | ||
import java.nio.file.Path | ||
import java.nio.file.SimpleFileVisitor | ||
import java.nio.file.attribute.BasicFileAttributes | ||
import scala.meta.io.AbsolutePath | ||
import scala.collection.mutable.ArrayBuffer | ||
import scala.collection.mutable | ||
|
||
object ListFiles { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The name can be misleading, as it lists both files and directories |
||
def foreach(root: AbsolutePath)(fn: AbsolutePath => Unit): Unit = { | ||
ListFiles(root).foreach(fn) | ||
def apply(root: AbsolutePath): mutable.ArrayBuffer[AbsolutePath] = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should be in an extension method for |
||
val buf = mutable.ArrayBuffer.empty[AbsolutePath] | ||
foreach(root)(file => buf += file) | ||
buf | ||
} | ||
|
||
def apply(root: AbsolutePath): ArrayBuffer[AbsolutePath] = { | ||
|
||
def runForeach[U](fn: AbsolutePath => U): Unit = { | ||
if (root.isFile) fn(root) | ||
else if (root.isDirectory) { | ||
try { | ||
Files.walkFileTree( | ||
root.toNIO, | ||
new SimpleFileVisitor[Path] { | ||
override def visitFile( | ||
file: Path, | ||
attrs: BasicFileAttributes | ||
): FileVisitResult = { | ||
fn(AbsolutePath(file)) | ||
super.visitFile(file, attrs) | ||
} | ||
} | ||
) | ||
} catch { | ||
case _: NoSuchFileException => | ||
() // error is reported by the JDK | ||
} | ||
} | ||
} | ||
|
||
if (root.isFile) { | ||
ArrayBuffer(root) | ||
} else { | ||
val buf = ArrayBuffer.empty[AbsolutePath] | ||
runForeach(file => { buf += file }) | ||
buf | ||
def foreach(root: AbsolutePath)(fn: AbsolutePath => Unit): Unit = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be useful to have the couple of methods to use like |
||
val ls = Files.list(root.toNIO) | ||
try { | ||
ls.forEach(file => fn(AbsolutePath(file))) | ||
} finally { | ||
ls.close() | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package scala.meta.internal.mtags | ||
|
||
import java.nio.file.FileVisitResult | ||
import java.nio.file.Files | ||
import java.nio.file.NoSuchFileException | ||
import java.nio.file.Path | ||
import java.nio.file.SimpleFileVisitor | ||
import java.nio.file.attribute.BasicFileAttributes | ||
import scala.meta.io.AbsolutePath | ||
import scala.collection.mutable.ArrayBuffer | ||
|
||
object WalkFiles { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe add to extension method? It's really hidden in here, if someone doesn't know about this then they will use something else. |
||
def foreach(root: AbsolutePath)(fn: AbsolutePath => Unit): Unit = { | ||
if (root.isFile) fn(root) | ||
else if (root.isDirectory) { | ||
try { | ||
Files.walkFileTree( | ||
root.toNIO, | ||
new SimpleFileVisitor[Path] { | ||
override def visitFile( | ||
file: Path, | ||
attrs: BasicFileAttributes | ||
): FileVisitResult = { | ||
fn(AbsolutePath(file)) | ||
FileVisitResult.CONTINUE | ||
} | ||
} | ||
) | ||
} catch { | ||
case _: NoSuchFileException => | ||
() // error is reported by the JDK | ||
} | ||
} | ||
} | ||
|
||
def apply(root: AbsolutePath): ArrayBuffer[AbsolutePath] = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should be an extension method for AbsolutePath, like |
||
val buf = ArrayBuffer.empty[AbsolutePath] | ||
foreach(root)(file => buf += file) | ||
buf | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice to have the forall method in ListFiles, returning
true
doesn't look good.