From 3c7bf1a14bc5668ccef09327064b74c1eaf4a1a1 Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Tue, 24 Dec 2013 22:07:09 +0100 Subject: [PATCH] Ensures that also if truncating parity fails we get the real size. --- parity.c | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/parity.c b/parity.c index 09f4db9d..e7a73636 100644 --- a/parity.c +++ b/parity.c @@ -185,7 +185,7 @@ int parity_chsize(struct snapraid_parity* parity, data_off_t size, data_off_t* o f_ret = ret; f_errno = errno; - /* reget the stat info */ + /* get the stat info */ ret = fstat(parity->f, &parity->st); if (ret != 0) { fprintf(stderr, "Error accessing parity file '%s'. %s.\n", parity->path, strerror(errno)); @@ -205,22 +205,32 @@ int parity_chsize(struct snapraid_parity* parity, data_off_t size, data_off_t* o goto bail; } } else if (parity->st.st_size > size) { - ret = ftruncate(parity->f, size); - if (ret != 0) { - fprintf(stderr, "Error truncating parity file '%s' to size %"PRIu64". %s.\n", parity->path, size, strerror(errno)); - goto bail; - } + int f_ret; + int f_errno; - /* reget the stat info */ + /* truncate the parity file */ + ret = ftruncate(parity->f, size); + + /* save the state of the shrink operation */ + f_ret = ret; + f_errno = errno; + + /* get the stat info */ ret = fstat(parity->f, &parity->st); if (ret != 0) { fprintf(stderr, "Error accessing parity file '%s'. %s.\n", parity->path, strerror(errno)); goto bail; } - /* return the new real size */ + /* return the new size */ *out_size = parity->st.st_size; + /* now check the error */ + if (f_ret != 0) { + fprintf(stderr, "Error truncating parity file '%s' to size %"PRIu64". %s.\n", parity->path, size, strerror(f_errno)); + goto bail; + } + /* adjust the valid to the new size */ parity->valid_size = size; }