]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/commitdiff
libfrog: print library errors
authorDarrick J. Wong <darrick.wong@oracle.com>
Wed, 6 Nov 2019 22:31:06 +0000 (17:31 -0500)
committerEric Sandeen <sandeen@sandeen.net>
Wed, 6 Nov 2019 22:31:06 +0000 (17:31 -0500)
Add a libfrog library function that will print tagged error messages.
This will eliminate the need for a lot of open-coded:

errno = ret;
perror("...");

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Eric Sandeen <sandeen@sandeen.net>
12 files changed:
io/bulkstat.c
io/imap.c
io/open.c
io/stat.c
io/swapext.c
libfrog/Makefile
libfrog/logging.c [new file with mode: 0644]
libfrog/logging.h [new file with mode: 0644]
quota/free.c
quota/quot.c
spaceman/file.c
spaceman/health.c

index ab84468af3636acb990621ef9302cb5209cabcf0..9641370b85d635fe476fecd8d6e2c180718a0f7d 100644 (file)
@@ -7,6 +7,7 @@
 #include "platform_defs.h"
 #include "command.h"
 #include "init.h"
+#include "libfrog/logging.h"
 #include "libfrog/fsgeom.h"
 #include "libfrog/bulkstat.h"
 #include "libfrog/paths.h"
@@ -164,8 +165,7 @@ bulkstat_f(
 
        ret = xfd_prepare_geometry(&xfd);
        if (ret) {
-               errno = ret;
-               perror("xfd_prepare_geometry");
+               xfrog_perror(ret, "xfd_prepare_geometry");
                exitcode = 1;
                return 0;
        }
@@ -202,8 +202,7 @@ _("bulkstat: startino=%lld flags=0x%x agno=%u ret=%d icount=%u ocount=%u\n"),
                }
        }
        if (ret) {
-               errno = ret;
-               perror("xfrog_bulkstat");
+               xfrog_perror(ret, "xfrog_bulkstat");
                exitcode = 1;
        }
 
@@ -274,8 +273,7 @@ bulkstat_single_f(
 
        ret = xfd_prepare_geometry(&xfd);
        if (ret) {
-               errno = ret;
-               perror("xfd_prepare_geometry");
+               xfrog_perror(ret, "xfd_prepare_geometry");
                exitcode = 1;
                return 0;
        }
@@ -309,8 +307,7 @@ bulkstat_single_f(
 
                ret = xfrog_bulkstat_single(&xfd, ino, flags, &bulkstat);
                if (ret) {
-                       errno = ret;
-                       perror("xfrog_bulkstat_single");
+                       xfrog_perror(ret, "xfrog_bulkstat_single");
                        continue;
                }
 
@@ -424,8 +421,7 @@ inumbers_f(
 
        ret = xfd_prepare_geometry(&xfd);
        if (ret) {
-               errno = ret;
-               perror("xfd_prepare_geometry");
+               xfrog_perror(ret, "xfd_prepare_geometry");
                exitcode = 1;
                return 0;
        }
@@ -462,8 +458,7 @@ _("bulkstat: startino=%"PRIu64" flags=0x%"PRIx32" agno=%"PRIu32" ret=%d icount=%
                }
        }
        if (ret) {
-               errno = ret;
-               perror("xfrog_inumbers");
+               xfrog_perror(ret, "xfrog_inumbers");
                exitcode = 1;
        }
 
index fa69676eb8485f610113e9d2e0b1f2435a991ced..6b3386408012a0ba1eee4e34eefaab58ca46d43e 100644 (file)
--- a/io/imap.c
+++ b/io/imap.c
@@ -8,6 +8,7 @@
 #include "input.h"
 #include "init.h"
 #include "io.h"
+#include "libfrog/logging.h"
 #include "libfrog/fsgeom.h"
 #include "libfrog/bulkstat.h"
 
@@ -44,8 +45,7 @@ imap_f(int argc, char **argv)
        }
 
        if (error) {
-               errno = error;
-               perror("xfsctl(XFS_IOC_FSINUMBERS)");
+               xfrog_perror(error, "xfsctl(XFS_IOC_FSINUMBERS)");
                exitcode = 1;
        }
        free(ireq);
index fa9ca01a37382a915b2ce4c7fb693df35d0e3614..a5192e87288f45e389e7fdf4706e3263c84f6369 100644 (file)
--- a/io/open.c
+++ b/io/open.c
@@ -9,6 +9,7 @@
 #include "init.h"
 #include "io.h"
 #include "libxfs.h"
+#include "libfrog/logging.h"
 #include "libfrog/fsgeom.h"
 #include "libfrog/bulkstat.h"
 
@@ -125,8 +126,7 @@ openfile(
 
                ret = xfrog_geometry(fd, geom);
                if (ret) {
-                       errno = ret;
-                       perror("XFS_IOC_FSGEOMETRY");
+                       xfrog_perror(ret, "XFS_IOC_FSGEOMETRY");
                        close(fd);
                        return -1;
                }
@@ -696,8 +696,7 @@ get_last_inode(void)
 
                ret = xfrog_inumbers(&xfd, ireq);
                if (ret) {
-                       errno = ret;
-                       perror("XFS_IOC_FSINUMBERS");
+                       xfrog_perror(ret, "XFS_IOC_FSINUMBERS");
                        goto out;
                }
 
@@ -795,8 +794,7 @@ inode_f(
                /* get next inode */
                ret = xfrog_bulkstat(&xfd, breq);
                if (ret) {
-                       errno = ret;
-                       perror("bulkstat");
+                       xfrog_perror(ret, "bulkstat");
                        free(breq);
                        exitcode = 1;
                        return 0;
@@ -817,8 +815,7 @@ inode_f(
                        /* Not in use */
                        result_ino = 0;
                } else if (ret) {
-                       errno = ret;
-                       perror("bulkstat_single");
+                       xfrog_perror(ret, "bulkstat_single");
                        exitcode = 1;
                        return 0;
                } else {
index 6c666146e6b24cb78e44f869c09152f77e3ee2ea..db3357805499b6d14d23f08b10657757841dad40 100644 (file)
--- a/io/stat.c
+++ b/io/stat.c
@@ -12,6 +12,7 @@
 #include "io.h"
 #include "statx.h"
 #include "libxfs.h"
+#include "libfrog/logging.h"
 #include "libfrog/fsgeom.h"
 
 #include <fcntl.h>
@@ -198,8 +199,7 @@ statfs_f(
                return 0;
        ret = xfrog_geometry(file->fd, &fsgeo);
        if (ret) {
-               errno = ret;
-               perror("XFS_IOC_FSGEOMETRY");
+               xfrog_perror(ret, "XFS_IOC_FSGEOMETRY");
        } else {
                printf(_("geom.bsize = %u\n"), fsgeo.blocksize);
                printf(_("geom.agcount = %u\n"), fsgeo.agcount);
index 1139cf2195f4102f696cc957ec0fd14ea6f059ec..dc4e418f2650fc4246f3303eca311a4c10182891 100644 (file)
@@ -8,6 +8,7 @@
 #include "input.h"
 #include "init.h"
 #include "io.h"
+#include "libfrog/logging.h"
 #include "libfrog/fsgeom.h"
 #include "libfrog/bulkstat.h"
 
@@ -51,14 +52,12 @@ swapext_f(
 
        error = xfrog_bulkstat_single(&fxfd, stat.st_ino, 0, &bulkstat);
        if (error) {
-               errno = error;
-               perror("bulkstat");
+               xfrog_perror(error, "bulkstat");
                goto out;
        }
        error = xfrog_bulkstat_v5_to_v1(&fxfd, &sx.sx_stat, &bulkstat);
        if (error) {
-               errno = error;
-               perror("bulkstat conversion");
+               xfrog_perror(error, "bulkstat conversion");
                goto out;
        }
        sx.sx_version = XFS_SX_VERSION;
index de67bf00a31af4de1a83d6800e7e31ca4a7288f7..780600cded25590a034c61fc0fc3ca97981ee307 100644 (file)
@@ -19,6 +19,7 @@ crc32.c \
 fsgeom.c \
 list_sort.c \
 linux.c \
+logging.c \
 paths.c \
 projects.c \
 ptvar.c \
@@ -38,6 +39,7 @@ crc32cselftest.h \
 crc32defs.h \
 crc32table.h \
 fsgeom.h \
+logging.h \
 paths.h \
 projects.h \
 ptvar.h \
diff --git a/libfrog/logging.c b/libfrog/logging.c
new file mode 100644 (file)
index 0000000..91ea453
--- /dev/null
@@ -0,0 +1,18 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2019 Oracle.  All Rights Reserved.
+ * Author: Darrick J. Wong <darrick.wong@oracle.com>
+ */
+#include <errno.h>
+#include <stdio.h>
+#include "logging.h"
+
+/* Print an error. */
+void
+xfrog_perror(
+       int             error,
+       const char      *s)
+{
+       errno = error < 0 ? -error : error;
+       perror(s);
+}
diff --git a/libfrog/logging.h b/libfrog/logging.h
new file mode 100644 (file)
index 0000000..8b125bf
--- /dev/null
@@ -0,0 +1,11 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Copyright (c) 2019 Oracle, Inc.
+ * All Rights Reserved.
+ */
+#ifndef __LIBFROG_LOGGING_H__
+#define __LIBFROG_LOGGING_H__
+
+void xfrog_perror(int error, const char *s);
+
+#endif /* __LIBFROG_LOGGING_H__ */
index 73aeb4591128d80d1bcb36fd8926527f82159150..45ce2ceb04d59f6bf0053763f79dbe47f71cd587 100644 (file)
@@ -8,6 +8,7 @@
 #include "command.h"
 #include "init.h"
 #include "quota.h"
+#include "libfrog/logging.h"
 #include "libfrog/fsgeom.h"
 
 static cmdinfo_t free_cmd;
@@ -70,8 +71,7 @@ mount_free_space_data(
        if (!(mount->fs_flags & FS_FOREIGN)) {
                ret = xfrog_geometry(fd, &fsgeo);
                if (ret) {
-                       errno = ret;
-                       perror("XFS_IOC_FSGEOMETRY");
+                       xfrog_perror(ret, "XFS_IOC_FSGEOMETRY");
                        close(fd);
                        return 0;
                }
index 7edfad1686d1acbc8ccad291fe586fad815712ff..0f69fabd83d156ce96318ab0c5c9f600ab6ed2f1 100644 (file)
@@ -11,6 +11,7 @@
 #include <grp.h>
 #include "init.h"
 #include "quota.h"
+#include "libfrog/logging.h"
 #include "libfrog/fsgeom.h"
 #include "libfrog/bulkstat.h"
 
@@ -147,8 +148,7 @@ quot_bulkstat_mount(
 
        ret = xfd_open(&fsxfd, fsdir, O_RDONLY);
        if (ret) {
-               errno = ret;
-               perror(fsdir);
+               xfrog_perror(ret, fsdir);
                return;
        }
 
@@ -165,10 +165,8 @@ quot_bulkstat_mount(
                for (i = 0; i < breq->hdr.ocount; i++)
                        quot_bulkstat_add(&breq->bulkstat[i], flags);
        }
-       if (sts < 0) {
-               errno = sts;
-               perror("XFS_IOC_FSBULKSTAT");
-       }
+       if (sts < 0)
+               xfrog_perror(sts, "XFS_IOC_FSBULKSTAT");
        free(breq);
        xfd_close(&fsxfd);
 }
index 43b87e14af347db28e1d2a929ad8ffc691a39b54..b779432858c8cfb64c2885f9d820c73dcaa1581c 100644 (file)
@@ -10,6 +10,7 @@
 #include "command.h"
 #include "input.h"
 #include "init.h"
+#include "libfrog/logging.h"
 #include "libfrog/paths.h"
 #include "libfrog/fsgeom.h"
 #include "space.h"
@@ -57,10 +58,8 @@ openfile(
                        fprintf(stderr,
 _("%s: Not on a mounted XFS filesystem.\n"),
                                        path);
-               else {
-                       errno = ret;
-                       perror(path);
-               }
+               else
+                       xfrog_perror(ret, path);
                return -1;
        }
 
index 0d3aa243ee34fcff9d42af631d24d2bb5c3fb309..a0079bd78fa3c5df9d9d8f495c22788166b71c9b 100644 (file)
@@ -8,6 +8,7 @@
 #include "command.h"
 #include "init.h"
 #include "input.h"
+#include "libfrog/logging.h"
 #include "libfrog/paths.h"
 #include "libfrog/fsgeom.h"
 #include "libfrog/bulkstat.h"
@@ -193,8 +194,7 @@ report_ag_sick(
 
        ret = xfrog_ag_geometry(file->xfd.fd, agno, &ageo);
        if (ret) {
-               errno = ret;
-               perror("ag_geometry");
+               xfrog_perror(ret, "ag_geometry");
                return 1;
        }
        snprintf(descr, sizeof(descr) - 1, _("AG %u"), agno);
@@ -219,8 +219,7 @@ report_inode_health(
 
        ret = xfrog_bulkstat_single(&file->xfd, ino, 0, &bs);
        if (ret) {
-               errno = ret;
-               perror(descr);
+               xfrog_perror(ret, descr);
                return 1;
        }
 
@@ -294,10 +293,8 @@ report_bulkstat_health(
                }
        } while (breq->hdr.ocount > 0);
 
-       if (error) {
-               errno = error;
-               perror("bulkstat");
-       }
+       if (error)
+               xfrog_perror(error, "bulkstat");
 
        free(breq);
        return error;