github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/libkb/file.go (about) 1 // Copyright 2015 Keybase, Inc. All rights reserved. Use of 2 // this source code is governed by the included BSD license. 3 4 package libkb 5 6 import ( 7 "io" 8 "os" 9 ) 10 11 // File defines a default SafeWriter implementation 12 type File struct { 13 filename string 14 data []byte 15 perm os.FileMode 16 } 17 18 // NewFile returns a File 19 func NewFile(filename string, data []byte, perm os.FileMode) File { 20 return File{filename, data, perm} 21 } 22 23 // Save file 24 func (f File) Save(g SafeWriteLogger) error { 25 return SafeWriteToFile(g, f, f.perm) 26 } 27 28 // GetFilename is for SafeWriter 29 func (f File) GetFilename() string { 30 return f.filename 31 } 32 33 // WriteTo is for SafeWriter 34 func (f File) WriteTo(w io.Writer) (int64, error) { 35 n, err := w.Write(f.data) 36 if err == nil { 37 if n != len(f.data) { 38 // this shouldn't happen 39 // 40 // Write must return a non-nil error if it returns n < len(p) 41 // 42 // but check just in case 43 return int64(n), io.ErrShortWrite 44 } 45 } 46 return int64(n), err 47 }