createKeyFileWithLock either creates the key file directly, or creates a temporary file in TempDir if it is set.
(pathKey *PathKey)
| 199 | // createKeyFileWithLock either creates the key file directly, or |
| 200 | // creates a temporary file in TempDir if it is set. |
| 201 | func (d *Diskv) createKeyFileWithLock(pathKey *PathKey) (*os.File, error) { |
| 202 | if d.TempDir != "" { |
| 203 | if err := os.MkdirAll(d.TempDir, d.PathPerm); err != nil { |
| 204 | return nil, fmt.Errorf("temp mkdir: %s", err) |
| 205 | } |
| 206 | f, err := ioutil.TempFile(d.TempDir, "") |
| 207 | if err != nil { |
| 208 | return nil, fmt.Errorf("temp file: %s", err) |
| 209 | } |
| 210 | |
| 211 | if err := os.Chmod(f.Name(), d.FilePerm); err != nil { |
| 212 | f.Close() // error deliberately ignored |
| 213 | os.Remove(f.Name()) // error deliberately ignored |
| 214 | return nil, fmt.Errorf("chmod: %s", err) |
| 215 | } |
| 216 | return f, nil |
| 217 | } |
| 218 | |
| 219 | mode := os.O_WRONLY | os.O_CREATE | os.O_TRUNC // overwrite if exists |
| 220 | f, err := os.OpenFile(d.completeFilename(pathKey), mode, d.FilePerm) |
| 221 | if err != nil { |
| 222 | return nil, fmt.Errorf("open file: %s", err) |
| 223 | } |
| 224 | return f, nil |
| 225 | } |
| 226 | |
| 227 | // writeStream does no input validation checking. |
| 228 | func (d *Diskv) writeStreamWithLock(pathKey *PathKey, r io.Reader, sync bool) error { |
no test coverage detected