Skip to content

Instantly share code, notes, and snippets.

@Tzrlk
Created July 10, 2017 02:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tzrlk/6e28338068dcbf3d06b8325212ef84e8 to your computer and use it in GitHub Desktop.
Save Tzrlk/6e28338068dcbf3d06b8325212ef84e8 to your computer and use it in GitHub Desktop.
Scratchpad for file DSC functions.
// Should hash on file write, then save date written, hash value, etc.
// Source files should also have this data written on first read, so it can be quickly compared later.
// Only rehash if the date-written metadata field is before the last-modified.'
// https://docs.oracle.com/javase/tutorial/essential/io/fileAttr.html
fun ensureFile(source: Path, target: Path) {
when {
source.toFile().exists && target.toFile().exists -> {
val source_hash = Files.newInputStream(source).use { stream ->
val digest = MessageDigest.getInstance("MD5")
DigestInputStream(stream, digest).use {}
return@use digest.digest()
}
val target_hash = Files.newInputStream(target).use { stream ->
val digest = MessageDigest.getInstance("MD5")
DigestInputStream(stream, digest).use {}
return@use digest.digest()
}
if (source_hash == target_hash) {
// files are identical, no worries.
return
}
//
}
source.toFile().exists -> // only source exists. Create target.
else -> // only target exists. delete.
}
}
// Not only does this have to grab the list of files on both sides and compare them to determine
// needed changes, but it also needs to detect renames, etc. almost certainly by preserving a manifest of what
// the expected source and target metadata was at the time of the last write/sync.
fun ensureFiles(source: List<Path>, target: List<Path>) {
//
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment