shithub: tarsum

ref: 01c1c0c7779125f8a83f75ecb277f0d63ead9200
dir: /tarsum.fnl/

View raw version
#!/usr/bin/env fennel
;; vim: set filetype=scheme :

(fn usage []
    (io.stderr:write "Usage: tarsum <filename>\n")
    (os.exit 1))

(if (< (# arg) 1) (usage))

;; (local tarball (io.open (. arg 1) "r+b"))

(with-open (tarball (io.open (. arg 1) "r+b"))
(let [header (tarball:read 0x132) headertbl {}]
  (if (= header fail) (error "Unable to read file."))
  (if (~= (# header) 0x132) (error "Invalid header! (Is this really a tarball?)"))
  (for [i 1 (# header) 1] (table.insert headertbl (string.sub header i i)))
  (for [i 0x94 0x9b 1] (tset headertbl i " "))
  (var header "")
  (each [k v (ipairs headertbl)] (set header (.. header v)))
  (var sum 0)
  (for [i 1 (# header) 1] (set sum (+ sum (string.byte header i))))
  (print (string.format "Checksum: %.7o\n" sum))
  (if (~= (tarball:seek "set" 0x94) 0x94) (error "Could not seek to 0x94"))
  (tarball:write (string.format "%.7o" sum))
  (if (~= (tarball:seek "set" 0x9b) 0x9b) (error "Could not seek to 0x9b"))
  (tarball:write (string.format "%c" 0))
  (tarball:flush)
  0
  ))