// iomisc.go -- misc i/o functions // // (c) 2016 Sudhi Herle // // Licensing Terms: GPLv2 // // If you need a commercial license for this work, please contact // the author. // // This software does not come with any express or implied // warranty; it is provided "as is". No claim is made to its // suitability for any purpose. package sign import ( "encoding/binary" "fmt" "github.com/opencoff/go-mmap" "github.com/opencoff/go-utils" "hash" "os" ) // Simple function to reliably write data to a file. // Does MORE than ioutil.WriteFile() - in that it doesn't trash the // existing file with an incomplete write. func writeFile(fn string, b []byte, ovwrite bool, mode uint32) error { sf, err := utils.NewSafeFile(fn, ovwrite, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.FileMode(mode)) if err != nil { return err } sf.Write(b) return sf.Close() } // Generate file checksum out of hash function h func fileCksum(fn string, h hash.Hash) ([]byte, error) { fd, err := os.Open(fn) if err != nil { return nil, fmt.Errorf("can't open %s: %s", fn, err) } defer fd.Close() sz, err := mmap.Reader(fd, func(b []byte) error { h.Write(b) return nil }) if err != nil { return nil, err } var b [8]byte binary.BigEndian.PutUint64(b[:], uint64(sz)) h.Write(b[:]) return h.Sum(nil)[:], nil }