Whamcloud - gitweb
LU-3549 llapi: add printf attribute to llapi_{printf,error} 67/6867/9
authorJohn L. Hammond <john.hammond@intel.com>
Tue, 2 Jul 2013 22:06:29 +0000 (17:06 -0500)
committerOleg Drokin <oleg.drokin@intel.com>
Tue, 3 Sep 2013 05:59:57 +0000 (05:59 +0000)
Add printf attributes to llapi_printf and llapi_error for compile time
checking that the format string agrees with the argument list, and
const qualify the format parameter of both functions. Fixup the
mismatched calls to these functions.

Signed-off-by: John L. Hammond <john.hammond@intel.com>
Signed-off-by: JC Lafoucriere <jacques-charles.lafoucriere@cea.fr>
Change-Id: Ic8df148abb0bfe8ce72d05d76cb7ce0f1832c28f
Reviewed-on: http://review.whamcloud.com/6867
Tested-by: Hudson
Reviewed-by: Andreas Dilger <andreas.dilger@intel.com>
Tested-by: Maloo <whamcloud.maloo@gmail.com>
lustre/include/lustre/lustreapi.h
lustre/tests/ll_dirstripe_verify.c
lustre/utils/lhsmtool_posix.c
lustre/utils/liblustreapi.c
lustre/utils/liblustreapi_hsm.c

index 4ba382e..98cbf80 100644 (file)
@@ -63,10 +63,14 @@ enum llapi_message_level {
 #define LLAPI_MSG_NO_ERRNO      0x00000010
 
 extern void llapi_msg_set_level(int level);
-extern void llapi_error(int level, int rc, char *fmt, ...);
-#define llapi_err_noerrno(level, fmt, a...)                             \
+
+void llapi_error(int level, int rc, const char *fmt, ...)
+       __attribute__((__format__(__printf__, 3, 4)));
+#define llapi_err_noerrno(level, fmt, a...)                    \
        llapi_error((level) | LLAPI_MSG_NO_ERRNO, 0, fmt, ## a)
-extern void llapi_printf(int level, char *fmt, ...);
+void llapi_printf(int level, const char *fmt, ...)
+       __attribute__((__format__(__printf__, 2, 3)));
+
 extern int llapi_file_create(const char *name, unsigned long long stripe_size,
                              int stripe_offset, int stripe_count,
                              int stripe_pattern);
index d5269b4..be20a72 100644 (file)
@@ -74,32 +74,33 @@ union {
  */
 int read_proc_entry(char *proc_path, char *buf, int len)
 {
-        int rc, fd;
+       int rc, fd;
 
-        memset(buf, 0, len);
+       memset(buf, 0, len);
 
-        fd = open(proc_path, O_RDONLY);
-        if (fd == -1) {
-                llapi_error(LLAPI_MSG_ERROR, -errno, "open('%s') failed: %s\n",
-                            proc_path);
-                return -2;
-        }
+       fd = open(proc_path, O_RDONLY);
+       if (fd < 0) {
+               llapi_error(LLAPI_MSG_ERROR, -errno, "cannot open '%s'",
+                           proc_path);
+               return -2;
+       }
 
-        rc = read(fd, buf, len - 1);
-        if (rc < 0) {
-                llapi_error(LLAPI_MSG_ERROR, -errno,
-                            "read('%s') failed: %s\n", proc_path);
-                rc = -3;
-        } else if (rc == 0) {
-                llapi_err_noerrno(LLAPI_MSG_ERROR,
-                                  "read('%s') zero bytes\n", proc_path);
-                rc = -4;
-        } else if (/* rc > 0 && */ buf[rc - 1] == '\n') {
-                buf[rc - 1] = '\0'; /* Remove trailing newline */
-        }
-        close(fd);
+       rc = read(fd, buf, len - 1);
+       if (rc < 0) {
+               llapi_error(LLAPI_MSG_ERROR, -errno,
+                           "error reading from '%s'", proc_path);
+               rc = -3;
+       } else if (rc == 0) {
+               llapi_err_noerrno(LLAPI_MSG_ERROR,
+                                 "read zero bytes from '%s'", proc_path);
+               rc = -4;
+       } else if (buf[rc - 1] == '\n') {
+               buf[rc - 1] = '\0'; /* Remove trailing newline */
+       }
 
-        return (rc);
+       close(fd);
+
+       return rc;
 }
 
 int compare(struct lov_user_md *lum_dir, struct lov_user_md *lum_file1,
@@ -115,18 +116,20 @@ int compare(struct lov_user_md *lum_dir, struct lov_user_md *lum_file1,
         int i;
         FILE *fp;
 
-        fp = popen("\\ls -d  /proc/fs/lustre/lov/*clilov* | head -1", "r");
-        if (!fp) {
-                llapi_error(LLAPI_MSG_ERROR, -errno,
-                            "open(lustre/lov/*clilov*) failed: %s\n");
-                return 2;
-        }
-        if (fscanf(fp, "%s", lov_path) < 1) {
-                llapi_error(LLAPI_MSG_ERROR, -EINVAL,
-                            "read(lustre/lov/*clilov*) failed: %s\n");
-                pclose(fp);
-                return 3;
+       fp = popen("\\ls -d  /proc/fs/lustre/lov/*clilov* | head -1", "r");
+       if (fp == NULL) {
+               llapi_error(LLAPI_MSG_ERROR, -errno,
+                           "open(lustre/lov/*clilov*) failed");
+               return 2;
         }
+
+       if (fscanf(fp, "%s", lov_path) < 1) {
+               llapi_error(LLAPI_MSG_ERROR, -EINVAL,
+                           "read(lustre/lov/*clilov*) failed");
+               pclose(fp);
+               return 3;
+       }
+
         pclose(fp);
 
         snprintf(tmp_path, sizeof(tmp_path) - 1, "%s/stripecount",
index dd496f1..baf1fac 100644 (file)
  *
  * This particular tool can also import an existing HSM archive.
  */
-
+#include <stdint.h>
+#include <stdlib.h>
+#include <dirent.h>
+#include <errno.h>
 #include <utime.h>
 #include <sys/xattr.h>
-#include <dirent.h>
-#include <stdlib.h>
-#include <sys/errno.h>
 #include <lustre/lustre_idl.h>
 #include <lustre/lustreapi.h>
 
@@ -204,7 +204,7 @@ static int ct_parseopts(int argc, char * const *argv)
                        if ((opt.o_archive_cnt >= MAX_ARCHIVE_CNT) ||
                            (atoi(optarg) >= MAX_ARCHIVE_CNT)) {
                                CT_ERROR("archive number must be less"
-                                        "than %lu\n", MAX_ARCHIVE_CNT);
+                                        "than %zu\n", MAX_ARCHIVE_CNT);
                                return -E2BIG;
                        }
                        opt.o_archive_id[opt.o_archive_cnt] = atoi(optarg);
@@ -522,9 +522,9 @@ static int ct_copy_data(struct hsm_copyaction_private *hcp, const char *src,
 
        rc = lseek(src_fd, hai->hai_extent.offset, SEEK_SET);
        if (rc < 0) {
-               CT_ERROR("'%s' seek to read to "LPU64" (len %zu)"
+               CT_ERROR("'%s' seek to read to "LPU64" (len %jd)"
                         " failed (%s)\n",
-                        src, hai->hai_extent.offset, src_st.st_size,
+                        src, hai->hai_extent.offset, (intmax_t)src_st.st_size,
                         strerror(errno));
                rc = -errno;
                goto out;
@@ -674,8 +674,8 @@ out:
                rc = ftruncate(dst_fd, src_st.st_size);
                if (rc < 0) {
                        rc = -errno;
-                       CT_ERROR("'%s' final truncate to %lu failed (%s)\n",
-                                dst, src_st.st_size, strerror(-rc));
+                       CT_ERROR("'%s' final truncate to %jd failed (%s)\n",
+                                dst, (intmax_t)src_st.st_size, strerror(-rc));
                        err_major++;
                }
        }
index 13fe862..4439c8a 100644 (file)
@@ -131,7 +131,7 @@ void llapi_msg_set_level(int level)
 }
 
 /* llapi_error will preserve errno */
-void llapi_error(int level, int _rc, char *fmt, ...)
+void llapi_error(int level, int _rc, const char *fmt, ...)
 {
         va_list args;
         int tmp_errno = errno;
@@ -153,7 +153,7 @@ void llapi_error(int level, int _rc, char *fmt, ...)
 }
 
 /* llapi_printf will preserve errno */
-void llapi_printf(int level, char *fmt, ...)
+void llapi_printf(int level, const char *fmt, ...)
 {
         va_list args;
         int tmp_errno = errno;
@@ -243,7 +243,7 @@ int llapi_stripe_limit_check(unsigned long long stripe_size, int stripe_offset,
        }
        if ((stripe_size & (LOV_MIN_STRIPE_SIZE - 1))) {
                rc = -EINVAL;
-               llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe_size %lu, "
+               llapi_error(LLAPI_MSG_ERROR, rc, "error: bad stripe_size %llu, "
                                "must be an even multiple of %d bytes",
                                stripe_size, page_size);
                return rc;
@@ -1330,7 +1330,7 @@ static int common_param_init(struct find_param *param, char *path)
        param->lmd = malloc(sizeof(lstat_t) + param->lumlen);
        if (param->lmd == NULL) {
                llapi_error(LLAPI_MSG_ERROR, -ENOMEM,
-                           "error: allocation of %d bytes for ioctl",
+                           "error: allocation of %zu bytes for ioctl",
                            sizeof(lstat_t) + param->lumlen);
                return -ENOMEM;
        }
@@ -4237,7 +4237,7 @@ int llapi_create_volatile_idx(char *directory, int idx, int mode)
        close(fd);
        if (rc < sizeof(random)) {
                llapi_error(LLAPI_MSG_ERROR, errno,
-                           "Cannot read %d bytes from /dev/urandom\n",
+                           "cannot read %zu bytes from /dev/urandom",
                            sizeof(random));
                return -errno;
        }
index d9867ee..9a36c6b 100644 (file)
@@ -122,7 +122,7 @@ int llapi_hsm_copytool_register(struct hsm_copytool_private **priv,
        for (rc = 0; rc < archive_count; rc++) {
                if (archives[rc] > 8 * sizeof(ct->archives)) {
                        llapi_err_noerrno(LLAPI_MSG_ERROR,
-                                         "Maximum of %d archives supported",
+                                         "maximum of %zu archives supported",
                                          8 * sizeof(ct->archives));
                        goto out_err;
                }