From 2ab0a8379343164fc1b1e3ac775a89ad5dec6877 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Senart?= Date: Thu, 27 Mar 2014 14:05:15 +0100 Subject: [PATCH] util: Handle all errors in writeFileSync Before this patch, errors returned by f.Write would be swallowed and replaced by io.ErrShortWrite if the number of bytes written was less than the number of bytes to be written. etcd was panicking on startup because it had no space to write conf.tmp to its data directory and the panic string was "short write" which was not very informative of the real problem. After applying this patch, we got a nice error message in our panic: "panic: write /data/etcd/conf.tmp: no space left on device" --- util.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/util.go b/util.go index 5fa2c41a87..44a3efd31d 100644 --- a/util.go +++ b/util.go @@ -25,15 +25,16 @@ func writeFileSynced(filename string, data []byte, perm os.FileMode) error { if err != nil { return err } + defer f.Close() // Idempotent n, err := f.Write(data) - if n < len(data) { - f.Close() + if err == nil && n < len(data) { return io.ErrShortWrite + } else if err != nil { + return err } - err = f.Sync() - if err != nil { + if err = f.Sync(); err != nil { return err }