Created
July 10, 2017 02:57
-
-
Save Tzrlk/6e28338068dcbf3d06b8325212ef84e8 to your computer and use it in GitHub Desktop.
Scratchpad for file DSC functions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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