Skip to content

Commit

Permalink
FileOps: support symlinked files or directories
Browse files Browse the repository at this point in the history
Otherwise, the formatter won't be able to use a symlinked configuration
file.
  • Loading branch information
kitbellew committed Jan 13, 2022
1 parent d0c11e9 commit 78e4291
Showing 1 changed file with 13 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,21 @@ import org.scalafmt.CompatCollections.JavaConverters._

object FileOps {

@inline
def getLastModifiedMsec(file: Path): Long =
Files.getLastModifiedTime(file, LinkOption.NOFOLLOW_LINKS).toMillis
def getLastModifiedMsec(file: Path): Long = {
val attributes = getAttributes(file)
val mtime = attributes.lastModifiedTime().toMillis
if (attributes.isSymbolicLink)
math.max(mtime, Files.getLastModifiedTime(file).toMillis)
else mtime
}

@inline
def isDirectory(file: Path): Boolean =
Files.isDirectory(file, LinkOption.NOFOLLOW_LINKS)
Files.isDirectory(file)

@inline
def isRegularFile(file: Path): Boolean =
Files.isRegularFile(file, LinkOption.NOFOLLOW_LINKS)
Files.isRegularFile(file)

@inline
def getAttributes(file: Path): BasicFileAttributes =
Expand All @@ -31,6 +35,10 @@ object FileOps {
LinkOption.NOFOLLOW_LINKS
)

@inline
def getAttributesFollowLinks(file: Path): BasicFileAttributes =
Files.readAttributes(file, classOf[BasicFileAttributes])

def listFiles(path: String): Seq[Path] =
listFiles(getFile(path))

Expand Down

0 comments on commit 78e4291

Please sign in to comment.