Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. Copyright 2016, 2017, 2019 Mark Lee and contributors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. const debug = require('debug')('sumchecker')
  14. const crypto = require('crypto')
  15. const fs = require('fs')
  16. const path = require('path')
  17. const { promisify } = require('util')
  18. const readFile = promisify(fs.readFile)
  19. const CHECKSUM_LINE = /^([\da-fA-F]+) ([ *])(.+)$/
  20. class ErrorWithFilename extends Error {
  21. constructor (filename) {
  22. super()
  23. this.filename = filename
  24. }
  25. }
  26. class ChecksumMismatchError extends ErrorWithFilename {
  27. constructor (filename) {
  28. super(filename)
  29. this.message = `Generated checksum for "${filename}" did not match expected checksum.`
  30. }
  31. }
  32. class ChecksumParseError extends Error {
  33. constructor (lineNumber, line) {
  34. super()
  35. this.lineNumber = lineNumber
  36. this.line = line
  37. this.message = `Could not parse checksum file at line ${lineNumber}: ${line}`
  38. }
  39. }
  40. class NoChecksumFoundError extends ErrorWithFilename {
  41. constructor (filename) {
  42. super(filename)
  43. this.message = `No checksum found in checksum file for "${filename}".`
  44. }
  45. }
  46. class ChecksumValidator {
  47. constructor (algorithm, checksumFilename, options) {
  48. this.algorithm = algorithm
  49. this.checksumFilename = checksumFilename
  50. this.checksums = null
  51. if (options && options.defaultTextEncoding) {
  52. this.defaultTextEncoding = options.defaultTextEncoding
  53. } else {
  54. this.defaultTextEncoding = 'utf8'
  55. }
  56. }
  57. encoding (binary) {
  58. return binary ? 'binary' : this.defaultTextEncoding
  59. }
  60. parseChecksumFile (data) {
  61. debug('Parsing checksum file')
  62. this.checksums = {}
  63. let lineNumber = 0
  64. for (const line of data.trim().split(/[\r\n]+/)) {
  65. lineNumber += 1
  66. const result = CHECKSUM_LINE.exec(line)
  67. if (result === null) {
  68. debug(`Could not parse line number ${lineNumber}`)
  69. throw new ChecksumParseError(lineNumber, line)
  70. } else {
  71. result.shift()
  72. const [checksum, binaryMarker, filename] = result
  73. const isBinary = binaryMarker === '*'
  74. this.checksums[filename] = [checksum, isBinary]
  75. }
  76. }
  77. debug('Parsed checksums:', this.checksums)
  78. }
  79. async readFile (filename, binary) {
  80. debug(`Reading "${filename} (binary mode: ${binary})"`)
  81. return readFile(filename, this.encoding(binary))
  82. }
  83. async validate (baseDir, filesToCheck) {
  84. if (typeof filesToCheck === 'string') {
  85. filesToCheck = [filesToCheck]
  86. }
  87. const data = await this.readFile(this.checksumFilename, false)
  88. this.parseChecksumFile(data)
  89. return this.validateFiles(baseDir, filesToCheck)
  90. }
  91. async validateFile (baseDir, filename) {
  92. return new Promise((resolve, reject) => {
  93. debug(`validateFile: ${filename}`)
  94. const metadata = this.checksums[filename]
  95. if (!metadata) {
  96. return reject(new NoChecksumFoundError(filename))
  97. }
  98. const [checksum, binary] = metadata
  99. const fullPath = path.resolve(baseDir, filename)
  100. debug(`Reading file with "${this.encoding(binary)}" encoding`)
  101. const stream = fs.createReadStream(fullPath, { encoding: this.encoding(binary) })
  102. const hasher = crypto.createHash(this.algorithm, { defaultEncoding: 'binary' })
  103. hasher.on('readable', () => {
  104. const data = hasher.read()
  105. if (data) {
  106. const calculated = data.toString('hex')
  107. debug(`Expected checksum: ${checksum}; Actual: ${calculated}`)
  108. if (calculated === checksum) {
  109. resolve()
  110. } else {
  111. reject(new ChecksumMismatchError(filename))
  112. }
  113. }
  114. })
  115. stream.pipe(hasher)
  116. })
  117. }
  118. async validateFiles (baseDir, filesToCheck) {
  119. return Promise.all(filesToCheck.map(filename => this.validateFile(baseDir, filename)))
  120. }
  121. }
  122. const sumchecker = async function sumchecker (algorithm, checksumFilename, baseDir, filesToCheck) {
  123. return new ChecksumValidator(algorithm, checksumFilename).validate(baseDir, filesToCheck)
  124. }
  125. sumchecker.ChecksumMismatchError = ChecksumMismatchError
  126. sumchecker.ChecksumParseError = ChecksumParseError
  127. sumchecker.ChecksumValidator = ChecksumValidator
  128. sumchecker.NoChecksumFoundError = NoChecksumFoundError
  129. module.exports = sumchecker