summaryrefslogtreecommitdiffstats
path: root/sys-utils/fstrim.c
diff options
context:
space:
mode:
authorKarel Zak2015-06-29 16:14:35 +0200
committerKarel Zak2015-06-29 16:14:35 +0200
commit289b153340ea52772ee060f7d7ab3da473703edb (patch)
tree478934990e0e3c22b72785711123aa37378e195d /sys-utils/fstrim.c
parentrtcwake: cosmetic changes (diff)
downloadkernel-qcow2-util-linux-289b153340ea52772ee060f7d7ab3da473703edb.tar.gz
kernel-qcow2-util-linux-289b153340ea52772ee060f7d7ab3da473703edb.tar.xz
kernel-qcow2-util-linux-289b153340ea52772ee060f7d7ab3da473703edb.zip
fstrim: close dir before exit [coverity scan]
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'sys-utils/fstrim.c')
-rw-r--r--sys-utils/fstrim.c24
1 files changed, 15 insertions, 9 deletions
diff --git a/sys-utils/fstrim.c b/sys-utils/fstrim.c
index 02ab106a0..c91141e27 100644
--- a/sys-utils/fstrim.c
+++ b/sys-utils/fstrim.c
@@ -60,7 +60,7 @@ struct fstrim_range {
static int fstrim_filesystem(const char *path, struct fstrim_range *rangetpl,
int verbose)
{
- int fd;
+ int fd = -1, rc;
struct stat sb;
struct fstrim_range range;
@@ -70,24 +70,26 @@ static int fstrim_filesystem(const char *path, struct fstrim_range *rangetpl,
fd = open(path, O_RDONLY);
if (fd < 0) {
warn(_("cannot open %s"), path);
- return -1;
+ rc = -errno;
+ goto done;
}
if (fstat(fd, &sb) == -1) {
warn(_("stat of %s failed"), path);
- return -1;
+ rc = -errno;
+ goto done;
}
if (!S_ISDIR(sb.st_mode)) {
warnx(_("%s: not a directory"), path);
- return -1;
+ rc = -EINVAL;
+ goto done;
}
errno = 0;
if (ioctl(fd, FITRIM, &range)) {
- int rc = errno == EOPNOTSUPP || errno == ENOTTY ? 1 : -1;
+ rc = errno == EOPNOTSUPP || errno == ENOTTY ? 1 : -errno;
if (rc != 1)
warn(_("%s: FITRIM ioctl failed"), path);
- close(fd);
- return rc;
+ goto done;
}
if (verbose) {
@@ -99,8 +101,12 @@ static int fstrim_filesystem(const char *path, struct fstrim_range *rangetpl,
path, str, (uint64_t) range.len);
free(str);
}
- close(fd);
- return 0;
+
+ rc = 0;
+done:
+ if (fd >= 0)
+ close(fd);
+ return rc;
}
static int has_discard(const char *devname, struct sysfs_cxt *wholedisk)