From 1896d37bfaae78bfa0e6ee3e3fb19b0a92bb1adb Mon Sep 17 00:00:00 2001 From: Ronnie Sahlberg Date: Sun, 19 Jun 2011 09:38:20 +1000 Subject: [PATCH] get rid of all remaining printfs from the library add a helper function nfs_set_error() to use instead of the printfs --- include/libnfs-private.h | 4 + include/libnfs-raw.h | 2 - lib/init.c | 3 - lib/libnfs-sync.c | 171 ++++++++++++++++++++------------------- lib/libnfs.c | 40 ++++++--- lib/pdu.c | 19 +++-- lib/socket.c | 37 ++++----- mount/mount.c | 34 ++++---- portmap/portmap.c | 8 +- 9 files changed, 163 insertions(+), 155 deletions(-) diff --git a/include/libnfs-private.h b/include/libnfs-private.h index d54e5a8..b7399ac 100644 --- a/include/libnfs-private.h +++ b/include/libnfs-private.h @@ -65,3 +65,7 @@ int rpc_get_pdu_size(char *buf); int rpc_process_pdu(struct rpc_context *rpc, char *buf, int size); void rpc_error_all_pdus(struct rpc_context *rpc, char *error); +void rpc_set_error(struct rpc_context *rpc, char *error_string, ...); +void nfs_set_error(struct nfs_context *nfs, char *error_string, ...); + + diff --git a/include/libnfs-raw.h b/include/libnfs-raw.h index 7828707..c1eb00d 100644 --- a/include/libnfs-raw.h +++ b/include/libnfs-raw.h @@ -66,8 +66,6 @@ int rpc_connect_async(struct rpc_context *rpc, const char *server, int port, rpc */ int rpc_disconnect(struct rpc_context *rpc, char *error); -void rpc_set_error(struct rpc_context *rpc, char *error_string, ...); - /* * PORTMAP FUNCTIONS diff --git a/lib/init.c b/lib/init.c index dde97d7..aba7e5e 100644 --- a/lib/init.c +++ b/lib/init.c @@ -35,7 +35,6 @@ struct rpc_context *rpc_init_context(void) rpc = malloc(sizeof(struct rpc_context)); if (rpc == NULL) { - printf("Failed to allocate rpc context\n"); return NULL; } bzero(rpc, sizeof(struct rpc_context)); @@ -43,14 +42,12 @@ struct rpc_context *rpc_init_context(void) rpc->encodebuflen = 65536; rpc->encodebuf = malloc(rpc->encodebuflen); if (rpc->encodebuf == NULL) { - printf("Failed to allocate a buffer for rpc encoding\n"); free(rpc); return NULL; } rpc->auth = authunix_create_default(); if (rpc->auth == NULL) { - printf("failed to create authunix\n"); free(rpc->encodebuf); free(rpc); return NULL; diff --git a/lib/libnfs-sync.c b/lib/libnfs-sync.c index d873979..88b89ee 100644 --- a/lib/libnfs-sync.c +++ b/lib/libnfs-sync.c @@ -33,6 +33,7 @@ #include "libnfs-raw.h" #include "libnfs-raw-mount.h" #include "libnfs-raw-nfs.h" +#include "libnfs-private.h" struct sync_cb_data { int is_finished; @@ -55,12 +56,12 @@ static void wait_for_reply(struct nfs_context *nfs, struct sync_cb_data *cb_data pfd.events = nfs_which_events(nfs); if (poll(&pfd, 1, -1) < 0) { - printf("Poll failed"); + nfs_set_error(nfs, "Poll failed"); cb_data->status = -EIO; break; } if (nfs_service(nfs, pfd.revents) < 0) { - printf("nfs_service failed\n"); + nfs_set_error(nfs, "nfs_service failed"); cb_data->status = -EIO; break; } @@ -75,7 +76,7 @@ static void wait_for_reply(struct nfs_context *nfs, struct sync_cb_data *cb_data /* * connect to the server and mount the export */ -static void mount_cb(int status, struct nfs_context *nfs _U_, void *data, void *private_data) +static void mount_cb(int status, struct nfs_context *nfs, void *data, void *private_data) { struct sync_cb_data *cb_data = private_data; @@ -83,7 +84,7 @@ static void mount_cb(int status, struct nfs_context *nfs _U_, void *data, void * cb_data->status = status; if (status < 0) { - printf("mount/mnt call failed with \"%s\"\n", (char *)data); + nfs_set_error(nfs, "mount/mnt call failed with \"%s\"", (char *)data); return; } } @@ -95,7 +96,7 @@ int nfs_mount(struct nfs_context *nfs, const char *server, const char *export) cb_data.is_finished = 0; if (nfs_mount_async(nfs, server, export, mount_cb, &cb_data) != 0) { - printf("nfs_mount_async failed\n"); + nfs_set_error(nfs, "nfs_mount_async failed"); return -1; } @@ -108,7 +109,7 @@ int nfs_mount(struct nfs_context *nfs, const char *server, const char *export) /* * stat() */ -static void stat_cb(int status, struct nfs_context *nfs _U_, void *data, void *private_data) +static void stat_cb(int status, struct nfs_context *nfs, void *data, void *private_data) { struct sync_cb_data *cb_data = private_data; @@ -116,7 +117,7 @@ static void stat_cb(int status, struct nfs_context *nfs _U_, void *data, void *p cb_data->status = status; if (status < 0) { - printf("stat call failed with \"%s\"\n", (char *)data); + nfs_set_error(nfs, "stat call failed with \"%s\"", (char *)data); return; } @@ -131,7 +132,7 @@ int nfs_stat(struct nfs_context *nfs, const char *path, struct stat *st) cb_data.return_data = st; if (nfs_stat_async(nfs, path, stat_cb, &cb_data) != 0) { - printf("nfs_stat_async failed\n"); + nfs_set_error(nfs, "nfs_stat_async failed"); return -1; } @@ -146,7 +147,7 @@ int nfs_stat(struct nfs_context *nfs, const char *path, struct stat *st) /* * open() */ -static void open_cb(int status, struct nfs_context *nfs _U_, void *data, void *private_data) +static void open_cb(int status, struct nfs_context *nfs, void *data, void *private_data) { struct sync_cb_data *cb_data = private_data; struct nfsfh *fh, **nfsfh; @@ -155,7 +156,7 @@ static void open_cb(int status, struct nfs_context *nfs _U_, void *data, void *p cb_data->status = status; if (status < 0) { - printf("open call failed with \"%s\"\n", (char *)data); + nfs_set_error(nfs, "open call failed with \"%s\"", (char *)data); return; } @@ -172,7 +173,7 @@ int nfs_open(struct nfs_context *nfs, const char *path, int mode, struct nfsfh * cb_data.return_data = nfsfh; if (nfs_open_async(nfs, path, mode, open_cb, &cb_data) != 0) { - printf("nfs_open_async failed\n"); + nfs_set_error(nfs, "nfs_open_async failed"); return -1; } @@ -187,7 +188,7 @@ int nfs_open(struct nfs_context *nfs, const char *path, int mode, struct nfsfh * /* * pread() */ -static void pread_cb(int status, struct nfs_context *nfs _U_, void *data, void *private_data) +static void pread_cb(int status, struct nfs_context *nfs, void *data, void *private_data) { struct sync_cb_data *cb_data = private_data; char *buffer; @@ -195,7 +196,7 @@ static void pread_cb(int status, struct nfs_context *nfs _U_, void *data, void * cb_data->status = status; if (status < 0) { - printf("pread call failed with \"%s\"\n", (char *)data); + nfs_set_error(nfs, "pread call failed with \"%s\"", (char *)data); return; } @@ -211,7 +212,7 @@ int nfs_pread(struct nfs_context *nfs, struct nfsfh *nfsfh, off_t offset, size_t cb_data.return_data = buffer; if (nfs_pread_async(nfs, nfsfh, offset, count, pread_cb, &cb_data) != 0) { - printf("nfs_pread_async failed\n"); + nfs_set_error(nfs, "nfs_pread_async failed"); return -1; } @@ -231,14 +232,14 @@ int nfs_read(struct nfs_context *nfs, struct nfsfh *nfsfh, size_t count, char *b /* * close() */ -static void close_cb(int status, struct nfs_context *nfs _U_, void *data, void *private_data) +static void close_cb(int status, struct nfs_context *nfs, void *data, void *private_data) { struct sync_cb_data *cb_data = private_data; cb_data->is_finished = 1; cb_data->status = status; if (status < 0) { - printf("close call failed with \"%s\"\n", (char *)data); + nfs_set_error(nfs, "close call failed with \"%s\"", (char *)data); return; } } @@ -250,7 +251,7 @@ int nfs_close(struct nfs_context *nfs, struct nfsfh *nfsfh) cb_data.is_finished = 0; if (nfs_close_async(nfs, nfsfh, close_cb, &cb_data) != 0) { - printf("nfs_close_async failed\n"); + nfs_set_error(nfs, "nfs_close_async failed"); return -1; } @@ -273,7 +274,7 @@ int nfs_fstat(struct nfs_context *nfs, struct nfsfh *nfsfh, struct stat *st) cb_data.return_data = st; if (nfs_fstat_async(nfs, nfsfh, stat_cb, &cb_data) != 0) { - printf("nfs_fstat_async failed\n"); + nfs_set_error(nfs, "nfs_fstat_async failed"); return -1; } @@ -286,14 +287,14 @@ int nfs_fstat(struct nfs_context *nfs, struct nfsfh *nfsfh, struct stat *st) /* * pwrite() */ -static void pwrite_cb(int status, struct nfs_context *nfs _U_, void *data, void *private_data) +static void pwrite_cb(int status, struct nfs_context *nfs, void *data, void *private_data) { struct sync_cb_data *cb_data = private_data; cb_data->is_finished = 1; cb_data->status = status; if (status < 0) { - printf("pwrite call failed with \"%s\"\n", (char *)data); + nfs_set_error(nfs, "pwrite call failed with \"%s\"", (char *)data); return; } } @@ -305,7 +306,7 @@ int nfs_pwrite(struct nfs_context *nfs, struct nfsfh *nfsfh, off_t offset, size_ cb_data.is_finished = 0; if (nfs_pwrite_async(nfs, nfsfh, offset, count, buf, pwrite_cb, &cb_data) != 0) { - printf("nfs_pwrite_async failed\n"); + nfs_set_error(nfs, "nfs_pwrite_async failed"); return -1; } @@ -326,14 +327,14 @@ int nfs_write(struct nfs_context *nfs, struct nfsfh *nfsfh, size_t count, char * /* * fsync() */ -static void fsync_cb(int status, struct nfs_context *nfs _U_, void *data, void *private_data) +static void fsync_cb(int status, struct nfs_context *nfs, void *data, void *private_data) { struct sync_cb_data *cb_data = private_data; cb_data->is_finished = 1; cb_data->status = status; if (status < 0) { - printf("fsync call failed with \"%s\"\n", (char *)data); + nfs_set_error(nfs, "fsync call failed with \"%s\"", (char *)data); return; } } @@ -345,7 +346,7 @@ int nfs_fsync(struct nfs_context *nfs, struct nfsfh *nfsfh) cb_data.is_finished = 0; if (nfs_fsync_async(nfs, nfsfh, fsync_cb, &cb_data) != 0) { - printf("nfs_fsync_async failed\n"); + nfs_set_error(nfs, "nfs_fsync_async failed"); return -1; } @@ -360,14 +361,14 @@ int nfs_fsync(struct nfs_context *nfs, struct nfsfh *nfsfh) /* * ftruncate() */ -static void ftruncate_cb(int status, struct nfs_context *nfs _U_, void *data, void *private_data) +static void ftruncate_cb(int status, struct nfs_context *nfs, void *data, void *private_data) { struct sync_cb_data *cb_data = private_data; cb_data->is_finished = 1; cb_data->status = status; if (status < 0) { - printf("ftruncate call failed with \"%s\"\n", (char *)data); + nfs_set_error(nfs, "ftruncate call failed with \"%s\"", (char *)data); return; } } @@ -379,7 +380,7 @@ int nfs_ftruncate(struct nfs_context *nfs, struct nfsfh *nfsfh, off_t length) cb_data.is_finished = 0; if (nfs_ftruncate_async(nfs, nfsfh, length, ftruncate_cb, &cb_data) != 0) { - printf("nfs_ftruncate_async failed\n"); + nfs_set_error(nfs, "nfs_ftruncate_async failed"); return -1; } @@ -393,14 +394,14 @@ int nfs_ftruncate(struct nfs_context *nfs, struct nfsfh *nfsfh, off_t length) /* * truncate() */ -static void truncate_cb(int status, struct nfs_context *nfs _U_, void *data, void *private_data) +static void truncate_cb(int status, struct nfs_context *nfs, void *data, void *private_data) { struct sync_cb_data *cb_data = private_data; cb_data->is_finished = 1; cb_data->status = status; if (status < 0) { - printf("truncate call failed with \"%s\"\n", (char *)data); + nfs_set_error(nfs, "truncate call failed with \"%s\"", (char *)data); return; } } @@ -412,7 +413,7 @@ int nfs_truncate(struct nfs_context *nfs, const char *path, off_t length) cb_data.is_finished = 0; if (nfs_truncate_async(nfs, path, length, truncate_cb, &cb_data) != 0) { - printf("nfs_ftruncate_async failed\n"); + nfs_set_error(nfs, "nfs_ftruncate_async failed"); return -1; } @@ -428,14 +429,14 @@ int nfs_truncate(struct nfs_context *nfs, const char *path, off_t length) /* * mkdir() */ -static void mkdir_cb(int status, struct nfs_context *nfs _U_, void *data, void *private_data) +static void mkdir_cb(int status, struct nfs_context *nfs, void *data, void *private_data) { struct sync_cb_data *cb_data = private_data; cb_data->is_finished = 1; cb_data->status = status; if (status < 0) { - printf("mkdir call failed with \"%s\"\n", (char *)data); + nfs_set_error(nfs, "mkdir call failed with \"%s\"", (char *)data); return; } } @@ -447,7 +448,7 @@ int nfs_mkdir(struct nfs_context *nfs, const char *path) cb_data.is_finished = 0; if (nfs_mkdir_async(nfs, path, mkdir_cb, &cb_data) != 0) { - printf("nfs_mkdir_async failed\n"); + nfs_set_error(nfs, "nfs_mkdir_async failed"); return -1; } @@ -463,14 +464,14 @@ int nfs_mkdir(struct nfs_context *nfs, const char *path) /* * rmdir() */ -static void rmdir_cb(int status, struct nfs_context *nfs _U_, void *data, void *private_data) +static void rmdir_cb(int status, struct nfs_context *nfs, void *data, void *private_data) { struct sync_cb_data *cb_data = private_data; cb_data->is_finished = 1; cb_data->status = status; if (status < 0) { - printf("rmdir call failed with \"%s\"\n", (char *)data); + nfs_set_error(nfs, "rmdir call failed with \"%s\"", (char *)data); return; } } @@ -482,7 +483,7 @@ int nfs_rmdir(struct nfs_context *nfs, const char *path) cb_data.is_finished = 0; if (nfs_rmdir_async(nfs, path, rmdir_cb, &cb_data) != 0) { - printf("nfs_rmdir_async failed\n"); + nfs_set_error(nfs, "nfs_rmdir_async failed"); return -1; } @@ -496,7 +497,7 @@ int nfs_rmdir(struct nfs_context *nfs, const char *path) /* * creat() */ -static void creat_cb(int status, struct nfs_context *nfs _U_, void *data, void *private_data) +static void creat_cb(int status, struct nfs_context *nfs, void *data, void *private_data) { struct sync_cb_data *cb_data = private_data; struct nfsfh *fh, **nfsfh; @@ -505,7 +506,7 @@ static void creat_cb(int status, struct nfs_context *nfs _U_, void *data, void * cb_data->status = status; if (status < 0) { - printf("creat call failed with \"%s\"\n", (char *)data); + nfs_set_error(nfs, "creat call failed with \"%s\"", (char *)data); return; } @@ -522,7 +523,7 @@ int nfs_creat(struct nfs_context *nfs, const char *path, int mode, struct nfsfh cb_data.return_data = nfsfh; if (nfs_creat_async(nfs, path, mode, creat_cb, &cb_data) != 0) { - printf("nfs_creat_async failed\n"); + nfs_set_error(nfs, "nfs_creat_async failed"); return -1; } @@ -537,7 +538,7 @@ int nfs_creat(struct nfs_context *nfs, const char *path, int mode, struct nfsfh /* * unlink() */ -static void unlink_cb(int status, struct nfs_context *nfs _U_, void *data, void *private_data) +static void unlink_cb(int status, struct nfs_context *nfs, void *data, void *private_data) { struct sync_cb_data *cb_data = private_data; @@ -545,7 +546,7 @@ static void unlink_cb(int status, struct nfs_context *nfs _U_, void *data, void cb_data->status = status; if (status < 0) { - printf("unlink call failed with \"%s\"\n", (char *)data); + nfs_set_error(nfs, "unlink call failed with \"%s\"", (char *)data); return; } } @@ -557,7 +558,7 @@ int nfs_unlink(struct nfs_context *nfs, const char *path) cb_data.is_finished = 0; if (nfs_unlink_async(nfs, path, unlink_cb, &cb_data) != 0) { - printf("nfs_unlink_async failed\n"); + nfs_set_error(nfs, "nfs_unlink_async failed"); return -1; } @@ -571,7 +572,7 @@ int nfs_unlink(struct nfs_context *nfs, const char *path) /* * opendir() */ -static void opendir_cb(int status, struct nfs_context *nfs _U_, void *data, void *private_data) +static void opendir_cb(int status, struct nfs_context *nfs, void *data, void *private_data) { struct sync_cb_data *cb_data = private_data; struct nfsdir *dir, **nfsdir; @@ -580,7 +581,7 @@ static void opendir_cb(int status, struct nfs_context *nfs _U_, void *data, void cb_data->status = status; if (status < 0) { - printf("opendir call failed with \"%s\"\n", (char *)data); + nfs_set_error(nfs, "opendir call failed with \"%s\"", (char *)data); return; } @@ -597,7 +598,7 @@ int nfs_opendir(struct nfs_context *nfs, const char *path, struct nfsdir **nfsdi cb_data.return_data = nfsdir; if (nfs_opendir_async(nfs, path, opendir_cb, &cb_data) != 0) { - printf("nfs_opendir_async failed\n"); + nfs_set_error(nfs, "nfs_opendir_async failed"); return -1; } @@ -610,7 +611,7 @@ int nfs_opendir(struct nfs_context *nfs, const char *path, struct nfsdir **nfsdi /* * lseek() */ -static void lseek_cb(int status, struct nfs_context *nfs _U_, void *data, void *private_data) +static void lseek_cb(int status, struct nfs_context *nfs, void *data, void *private_data) { struct sync_cb_data *cb_data = private_data; @@ -618,7 +619,7 @@ static void lseek_cb(int status, struct nfs_context *nfs _U_, void *data, void * cb_data->status = status; if (status < 0) { - printf("lseek call failed with \"%s\"\n", (char *)data); + nfs_set_error(nfs, "lseek call failed with \"%s\"", (char *)data); return; } @@ -635,7 +636,7 @@ int nfs_lseek(struct nfs_context *nfs, struct nfsfh *nfsfh, off_t offset, int wh cb_data.return_data = current_offset; if (nfs_lseek_async(nfs, nfsfh, offset, whence, lseek_cb, &cb_data) != 0) { - printf("nfs_lseek_async failed\n"); + nfs_set_error(nfs, "nfs_lseek_async failed"); return -1; } @@ -649,7 +650,7 @@ int nfs_lseek(struct nfs_context *nfs, struct nfsfh *nfsfh, off_t offset, int wh /* * statvfs() */ -static void statvfs_cb(int status, struct nfs_context *nfs _U_, void *data, void *private_data) +static void statvfs_cb(int status, struct nfs_context *nfs, void *data, void *private_data) { struct sync_cb_data *cb_data = private_data; @@ -657,7 +658,7 @@ static void statvfs_cb(int status, struct nfs_context *nfs _U_, void *data, void cb_data->status = status; if (status < 0) { - printf("statvfs call failed with \"%s\"\n", (char *)data); + nfs_set_error(nfs, "statvfs call failed with \"%s\"", (char *)data); return; } @@ -672,7 +673,7 @@ int nfs_statvfs(struct nfs_context *nfs, const char *path, struct statvfs *svfs) cb_data.return_data = svfs; if (nfs_statvfs_async(nfs, path, statvfs_cb, &cb_data) != 0) { - printf("nfs_statvfs_async failed\n"); + nfs_set_error(nfs, "nfs_statvfs_async failed"); return -1; } @@ -688,7 +689,7 @@ int nfs_statvfs(struct nfs_context *nfs, const char *path, struct statvfs *svfs) /* * readlink() */ -static void readlink_cb(int status, struct nfs_context *nfs _U_, void *data, void *private_data) +static void readlink_cb(int status, struct nfs_context *nfs, void *data, void *private_data) { struct sync_cb_data *cb_data = private_data; @@ -696,12 +697,12 @@ static void readlink_cb(int status, struct nfs_context *nfs _U_, void *data, voi cb_data->status = status; if (status < 0) { - printf("readlink call failed with \"%s\"\n", (char *)data); + nfs_set_error(nfs, "readlink call failed with \"%s\"", (char *)data); return; } if (strlen(data) > (size_t)cb_data->return_int) { - printf("Too small buffer for readlink\n"); + nfs_set_error(nfs, "Too small buffer for readlink"); cb_data->status = -ENAMETOOLONG; return; } @@ -718,7 +719,7 @@ int nfs_readlink(struct nfs_context *nfs, const char *path, char *buf, int bufsi cb_data.return_int = bufsize; if (nfs_readlink_async(nfs, path, readlink_cb, &cb_data) != 0) { - printf("nfs_readlink_async failed\n"); + nfs_set_error(nfs, "nfs_readlink_async failed"); return -1; } @@ -732,7 +733,7 @@ int nfs_readlink(struct nfs_context *nfs, const char *path, char *buf, int bufsi /* * chmod() */ -static void chmod_cb(int status, struct nfs_context *nfs _U_, void *data, void *private_data) +static void chmod_cb(int status, struct nfs_context *nfs, void *data, void *private_data) { struct sync_cb_data *cb_data = private_data; @@ -740,7 +741,7 @@ static void chmod_cb(int status, struct nfs_context *nfs _U_, void *data, void * cb_data->status = status; if (status < 0) { - printf("chmod call failed with \"%s\"\n", (char *)data); + nfs_set_error(nfs, "chmod call failed with \"%s\"", (char *)data); return; } } @@ -752,7 +753,7 @@ int nfs_chmod(struct nfs_context *nfs, const char *path, int mode) cb_data.is_finished = 0; if (nfs_chmod_async(nfs, path, mode, chmod_cb, &cb_data) != 0) { - printf("nfs_chmod_async failed\n"); + nfs_set_error(nfs, "nfs_chmod_async failed"); return -1; } @@ -767,7 +768,7 @@ int nfs_chmod(struct nfs_context *nfs, const char *path, int mode) /* * fchmod() */ -static void fchmod_cb(int status, struct nfs_context *nfs _U_, void *data, void *private_data) +static void fchmod_cb(int status, struct nfs_context *nfs, void *data, void *private_data) { struct sync_cb_data *cb_data = private_data; @@ -775,7 +776,7 @@ static void fchmod_cb(int status, struct nfs_context *nfs _U_, void *data, void cb_data->status = status; if (status < 0) { - printf("fchmod call failed with \"%s\"\n", (char *)data); + nfs_set_error(nfs, "fchmod call failed with \"%s\"", (char *)data); return; } } @@ -787,7 +788,7 @@ int nfs_fchmod(struct nfs_context *nfs, struct nfsfh *nfsfh, int mode) cb_data.is_finished = 0; if (nfs_fchmod_async(nfs, nfsfh, mode, fchmod_cb, &cb_data) != 0) { - printf("nfs_fchmod_async failed\n"); + nfs_set_error(nfs, "nfs_fchmod_async failed"); return -1; } @@ -802,7 +803,7 @@ int nfs_fchmod(struct nfs_context *nfs, struct nfsfh *nfsfh, int mode) /* * chown() */ -static void chown_cb(int status, struct nfs_context *nfs _U_, void *data, void *private_data) +static void chown_cb(int status, struct nfs_context *nfs, void *data, void *private_data) { struct sync_cb_data *cb_data = private_data; @@ -810,7 +811,7 @@ static void chown_cb(int status, struct nfs_context *nfs _U_, void *data, void * cb_data->status = status; if (status < 0) { - printf("chown call failed with \"%s\"\n", (char *)data); + nfs_set_error(nfs, "chown call failed with \"%s\"", (char *)data); return; } } @@ -822,7 +823,7 @@ int nfs_chown(struct nfs_context *nfs, const char *path, int uid, int gid) cb_data.is_finished = 0; if (nfs_chown_async(nfs, path, uid, gid, chown_cb, &cb_data) != 0) { - printf("nfs_chown_async failed\n"); + nfs_set_error(nfs, "nfs_chown_async failed"); return -1; } @@ -834,7 +835,7 @@ int nfs_chown(struct nfs_context *nfs, const char *path, int uid, int gid) /* * fchown() */ -static void fchown_cb(int status, struct nfs_context *nfs _U_, void *data, void *private_data) +static void fchown_cb(int status, struct nfs_context *nfs, void *data, void *private_data) { struct sync_cb_data *cb_data = private_data; @@ -842,7 +843,7 @@ static void fchown_cb(int status, struct nfs_context *nfs _U_, void *data, void cb_data->status = status; if (status < 0) { - printf("fchown call failed with \"%s\"\n", (char *)data); + nfs_set_error(nfs, "fchown call failed with \"%s\"", (char *)data); return; } } @@ -854,7 +855,7 @@ int nfs_fchown(struct nfs_context *nfs, struct nfsfh *nfsfh, int uid, int gid) cb_data.is_finished = 0; if (nfs_fchown_async(nfs, nfsfh, uid, gid, fchown_cb, &cb_data) != 0) { - printf("nfs_fchown_async failed\n"); + nfs_set_error(nfs, "nfs_fchown_async failed"); return -1; } @@ -868,7 +869,7 @@ int nfs_fchown(struct nfs_context *nfs, struct nfsfh *nfsfh, int uid, int gid) /* * utimes() */ -static void utimes_cb(int status, struct nfs_context *nfs _U_, void *data, void *private_data) +static void utimes_cb(int status, struct nfs_context *nfs, void *data, void *private_data) { struct sync_cb_data *cb_data = private_data; @@ -876,7 +877,7 @@ static void utimes_cb(int status, struct nfs_context *nfs _U_, void *data, void cb_data->status = status; if (status < 0) { - printf("utimes call failed with \"%s\"\n", (char *)data); + nfs_set_error(nfs, "utimes call failed with \"%s\"", (char *)data); return; } } @@ -888,7 +889,7 @@ int nfs_utimes(struct nfs_context *nfs, const char *path, struct timeval *times) cb_data.is_finished = 0; if (nfs_utimes_async(nfs, path, times, utimes_cb, &cb_data) != 0) { - printf("nfs_utimes_async failed\n"); + nfs_set_error(nfs, "nfs_utimes_async failed"); return -1; } @@ -902,7 +903,7 @@ int nfs_utimes(struct nfs_context *nfs, const char *path, struct timeval *times) /* * utime() */ -static void utime_cb(int status, struct nfs_context *nfs _U_, void *data, void *private_data) +static void utime_cb(int status, struct nfs_context *nfs, void *data, void *private_data) { struct sync_cb_data *cb_data = private_data; @@ -910,7 +911,7 @@ static void utime_cb(int status, struct nfs_context *nfs _U_, void *data, void * cb_data->status = status; if (status < 0) { - printf("utime call failed with \"%s\"\n", (char *)data); + nfs_set_error(nfs, "utime call failed with \"%s\"", (char *)data); return; } } @@ -922,7 +923,7 @@ int nfs_utime(struct nfs_context *nfs, const char *path, struct utimbuf *times) cb_data.is_finished = 0; if (nfs_utime_async(nfs, path, times, utime_cb, &cb_data) != 0) { - printf("nfs_utimes_async failed\n"); + nfs_set_error(nfs, "nfs_utimes_async failed"); return -1; } @@ -937,7 +938,7 @@ int nfs_utime(struct nfs_context *nfs, const char *path, struct utimbuf *times) /* * access() */ -static void access_cb(int status, struct nfs_context *nfs _U_, void *data, void *private_data) +static void access_cb(int status, struct nfs_context *nfs, void *data, void *private_data) { struct sync_cb_data *cb_data = private_data; @@ -945,7 +946,7 @@ static void access_cb(int status, struct nfs_context *nfs _U_, void *data, void cb_data->status = status; if (status < 0) { - printf("access call failed with \"%s\"\n", (char *)data); + nfs_set_error(nfs, "access call failed with \"%s\"", (char *)data); return; } } @@ -957,7 +958,7 @@ int nfs_access(struct nfs_context *nfs, const char *path, int mode) cb_data.is_finished = 0; if (nfs_access_async(nfs, path, mode, access_cb, &cb_data) != 0) { - printf("nfs_access_async failed\n"); + nfs_set_error(nfs, "nfs_access_async failed"); return -1; } @@ -971,7 +972,7 @@ int nfs_access(struct nfs_context *nfs, const char *path, int mode) /* * symlink() */ -static void symlink_cb(int status, struct nfs_context *nfs _U_, void *data, void *private_data) +static void symlink_cb(int status, struct nfs_context *nfs, void *data, void *private_data) { struct sync_cb_data *cb_data = private_data; @@ -979,7 +980,7 @@ static void symlink_cb(int status, struct nfs_context *nfs _U_, void *data, void cb_data->status = status; if (status < 0) { - printf("symlink call failed with \"%s\"\n", (char *)data); + nfs_set_error(nfs, "symlink call failed with \"%s\"", (char *)data); return; } } @@ -991,7 +992,7 @@ int nfs_symlink(struct nfs_context *nfs, const char *oldpath, const char *newpat cb_data.is_finished = 0; if (nfs_symlink_async(nfs, oldpath, newpath, symlink_cb, &cb_data) != 0) { - printf("nfs_symlink_async failed\n"); + nfs_set_error(nfs, "nfs_symlink_async failed"); return -1; } @@ -1005,7 +1006,7 @@ int nfs_symlink(struct nfs_context *nfs, const char *oldpath, const char *newpat /* * rename() */ -static void rename_cb(int status, struct nfs_context *nfs _U_, void *data, void *private_data) +static void rename_cb(int status, struct nfs_context *nfs, void *data, void *private_data) { struct sync_cb_data *cb_data = private_data; @@ -1013,7 +1014,7 @@ static void rename_cb(int status, struct nfs_context *nfs _U_, void *data, void cb_data->status = status; if (status < 0) { - printf("rename call failed with \"%s\"\n", (char *)data); + nfs_set_error(nfs, "rename call failed with \"%s\"", (char *)data); return; } } @@ -1025,7 +1026,7 @@ int nfs_rename(struct nfs_context *nfs, const char *oldpath, const char *newpath cb_data.is_finished = 0; if (nfs_rename_async(nfs, oldpath, newpath, rename_cb, &cb_data) != 0) { - printf("nfs_rename_async failed\n"); + nfs_set_error(nfs, "nfs_rename_async failed"); return -1; } @@ -1039,7 +1040,7 @@ int nfs_rename(struct nfs_context *nfs, const char *oldpath, const char *newpath /* * link() */ -static void link_cb(int status, struct nfs_context *nfs _U_, void *data, void *private_data) +static void link_cb(int status, struct nfs_context *nfs, void *data, void *private_data) { struct sync_cb_data *cb_data = private_data; @@ -1047,7 +1048,7 @@ static void link_cb(int status, struct nfs_context *nfs _U_, void *data, void *p cb_data->status = status; if (status < 0) { - printf("link call failed with \"%s\"\n", (char *)data); + nfs_set_error(nfs, "link call failed with \"%s\"", (char *)data); return; } } @@ -1059,7 +1060,7 @@ int nfs_link(struct nfs_context *nfs, const char *oldpath, const char *newpath) cb_data.is_finished = 0; if (nfs_link_async(nfs, oldpath, newpath, link_cb, &cb_data) != 0) { - printf("nfs_link_async failed\n"); + nfs_set_error(nfs, "nfs_link_async failed"); return -1; } diff --git a/lib/libnfs.c b/lib/libnfs.c index da47e85..40b6a8d 100644 --- a/lib/libnfs.c +++ b/lib/libnfs.c @@ -19,6 +19,7 @@ */ #include +#include #include #include #include @@ -33,6 +34,12 @@ #include "libnfs-raw.h" #include "libnfs-raw-mount.h" #include "libnfs-raw-nfs.h" +#include "libnfs-private.h" + +struct nfsdir { + struct nfsdirent *entries; + struct nfsdirent *current; +}; struct nfsfh { struct nfs_fh3 fh; @@ -40,9 +47,13 @@ struct nfsfh { off_t offset; }; -struct nfsdir { - struct nfsdirent *entries; - struct nfsdirent *current; +struct nfs_context { + struct rpc_context *rpc; + char *server; + char *export; + struct nfs_fh3 rootfh; + size_t readmax; + size_t writemax; }; void nfs_free_nfsdir(struct nfsdir *nfsdir) @@ -58,15 +69,6 @@ void nfs_free_nfsdir(struct nfsdir *nfsdir) free(nfsdir); } -struct nfs_context { - struct rpc_context *rpc; - char *server; - char *export; - struct nfs_fh3 rootfh; - size_t readmax; - size_t writemax; -}; - struct nfs_cb_data; typedef int (*continue_func)(struct nfs_context *nfs, struct nfs_cb_data *data); @@ -2855,3 +2857,17 @@ size_t nfs_get_writemax(struct nfs_context *nfs) { return nfs->writemax; } + +void nfs_set_error(struct nfs_context *nfs, char *error_string, ...) +{ + va_list ap; + char *str; + + if (nfs->rpc->error_string != NULL) { + free(nfs->rpc->error_string); + } + va_start(ap, error_string); + vasprintf(&str, error_string, ap); + nfs->rpc->error_string = str; + va_end(ap); +} diff --git a/lib/pdu.c b/lib/pdu.c index 8aefa57..3427446 100644 --- a/lib/pdu.c +++ b/lib/pdu.c @@ -32,13 +32,12 @@ struct rpc_pdu *rpc_allocate_pdu(struct rpc_context *rpc, int program, int versi struct rpc_msg msg; if (rpc == NULL) { - printf("trying to allocate rpc pdu on NULL context\n"); return NULL; } pdu = malloc(sizeof(struct rpc_pdu)); if (pdu == NULL) { - printf("Failed to allocate pdu structure\n"); + rpc_set_error(rpc, "Out of memory: Failed to allocate pdu structure"); return NULL; } bzero(pdu, sizeof(struct rpc_pdu)); @@ -62,7 +61,7 @@ struct rpc_pdu *rpc_allocate_pdu(struct rpc_context *rpc, int program, int versi msg.rm_call.cb_verf = rpc->auth->ah_verf; if (xdr_callmsg(&pdu->xdr, &msg) == 0) { - printf("xdr_callmsg failed\n"); + rpc_set_error(rpc, "xdr_callmsg failed"); xdr_destroy(&pdu->xdr); free(pdu); return NULL; @@ -121,7 +120,7 @@ int rpc_get_pdu_size(char *buf) size = ntohl(*(uint32_t *)buf); if ((size & 0x80000000) == 0) { - printf("cant handle oncrpc fragments\n"); + /* cant handle oncrpc fragments */ return -1; } @@ -137,7 +136,7 @@ static int rpc_process_reply(struct rpc_context *rpc, struct rpc_pdu *pdu, XDR * if (pdu->xdr_decode_bufsize > 0) { pdu->xdr_decode_buf = malloc(pdu->xdr_decode_bufsize); if (pdu->xdr_decode_buf == NULL) { - printf("xdr_replymsg failed in portmap_getport_reply\n"); + rpc_set_error(rpc, "xdr_replymsg failed in portmap_getport_reply"); pdu->cb(rpc, RPC_STATUS_ERROR, "Failed to allocate buffer for decoding of XDR reply", pdu->private_data); return 0; } @@ -147,7 +146,7 @@ static int rpc_process_reply(struct rpc_context *rpc, struct rpc_pdu *pdu, XDR * msg.acpted_rply.ar_results.proc = pdu->xdr_decode_fn; if (xdr_replymsg(xdr, &msg) == 0) { - printf("xdr_replymsg failed in portmap_getport_reply\n"); + rpc_set_error(rpc, "xdr_replymsg failed in portmap_getport_reply"); pdu->cb(rpc, RPC_STATUS_ERROR, "Message rejected by server", pdu->private_data); if (pdu->xdr_decode_buf != NULL) { free(pdu->xdr_decode_buf); @@ -197,13 +196,13 @@ int rpc_process_pdu(struct rpc_context *rpc, char *buf, int size) xdrmem_create(&xdr, buf, size, XDR_DECODE); if (xdr_int(&xdr, &recordmarker) == 0) { - printf("xdr_int reading recordmarker failed\n"); + rpc_set_error(rpc, "xdr_int reading recordmarker failed"); xdr_destroy(&xdr); return -1; } pos = xdr_getpos(&xdr); if (xdr_int(&xdr, (int *)&xid) == 0) { - printf("xdr_int reading xid failed\n"); + rpc_set_error(rpc, "xdr_int reading xid failed"); xdr_destroy(&xdr); return -1; } @@ -215,13 +214,13 @@ int rpc_process_pdu(struct rpc_context *rpc, char *buf, int size) } SLIST_REMOVE(&rpc->waitpdu, pdu); if (rpc_process_reply(rpc, pdu, &xdr) != 0) { - printf("rpc_procdess_reply failed\n"); + rpc_set_error(rpc, "rpc_procdess_reply failed"); } xdr_destroy(&xdr); rpc_free_pdu(rpc, pdu); return 0; } - printf("No matching pdu found for xid:%d\n", xid); + rpc_set_error(rpc, "No matching pdu found for xid:%d", xid); xdr_destroy(&xdr); return -1; } diff --git a/lib/socket.c b/lib/socket.c index 4701685..fd67566 100644 --- a/lib/socket.c +++ b/lib/socket.c @@ -58,12 +58,11 @@ static int rpc_write_to_socket(struct rpc_context *rpc) ssize_t count; if (rpc == NULL) { - printf("trying to write to socket for NULL context\n"); return -1; } if (rpc->fd == -1) { - printf("trying to write but not connected\n"); - return -2; + rpc_set_error(rpc, "trying to write but not connected"); + return -1; } while (rpc->outqueue != NULL) { @@ -74,11 +73,10 @@ static int rpc_write_to_socket(struct rpc_context *rpc) count = write(rpc->fd, rpc->outqueue->outdata.data + rpc->outqueue->written, total - rpc->outqueue->written); if (count == -1) { if (errno == EAGAIN || errno == EWOULDBLOCK) { - printf("socket would block, return from write to socket\n"); return 0; } - printf("Error when writing to socket :%s(%d)\n", strerror(errno), errno); - return -3; + rpc_set_error(rpc, "Error when writing to socket :%s(%d)", strerror(errno), errno); + return -1; } rpc->outqueue->written += count; @@ -105,13 +103,13 @@ static int rpc_read_from_socket(struct rpc_context *rpc) } if (available == 0) { rpc_set_error(rpc, "Socket has been closed"); - return -2; + return -1; } size = rpc->insize - rpc->inpos + available; buf = malloc(size); if (buf == NULL) { rpc_set_error(rpc, "Out of memory: failed to allocate %d bytes for input buffer. Closing socket.", size); - return -3; + return -1; } if (rpc->insize > rpc->inpos) { memcpy(buf, rpc->inbuf + rpc->inpos, rpc->insize - rpc->inpos); @@ -129,7 +127,7 @@ static int rpc_read_from_socket(struct rpc_context *rpc) rpc_set_error(rpc, "Read from socket failed, errno:%d. Closing socket.", errno); free(buf); buf = NULL; - return -4; + return -1; } if (rpc->inbuf != NULL) { @@ -148,7 +146,7 @@ static int rpc_read_from_socket(struct rpc_context *rpc) } if (rpc_process_pdu(rpc, rpc->inbuf + rpc->inpos, count) != 0) { rpc_set_error(rpc, "Invalid/garbage pdu received from server. Closing socket"); - return -5; + return -1; } rpc->inpos += count; if (rpc->inpos == rpc->insize) { @@ -185,10 +183,9 @@ int rpc_service(struct rpc_context *rpc, int revents) return -1; } if (revents & POLLHUP) { - printf("rpc_service: POLLHUP, socket error\n"); rpc_set_error(rpc, "Socket failed with POLLHUP"); rpc->connect_cb(rpc, RPC_STATUS_ERROR, rpc->error_string, rpc->connect_data); - return -2; + return -1; } if (rpc->is_connected == 0 && rpc->fd != -1 && revents&POLLOUT) { @@ -215,15 +212,15 @@ int rpc_service(struct rpc_context *rpc, int revents) if (revents & POLLOUT && rpc->outqueue != NULL) { if (rpc_write_to_socket(rpc) != 0) { - printf("write to socket failed\n"); - return -3; + rpc_set_error(rpc, "write to socket failed"); + return -1; } } if (revents & POLLIN) { if (rpc_read_from_socket(rpc) != 0) { rpc_disconnect(rpc, rpc_get_error(rpc)); - return -4; + return -1; } } @@ -239,7 +236,6 @@ int rpc_connect_async(struct rpc_context *rpc, const char *server, int port, rpc if (rpc->fd != -1) { rpc_set_error(rpc, "Trying to connect while already connected"); - printf("%s\n", rpc->error_string); return -1; } @@ -247,8 +243,7 @@ int rpc_connect_async(struct rpc_context *rpc, const char *server, int port, rpc sin->sin_port = htons(port); if (inet_pton(AF_INET, server, &sin->sin_addr) != 1) { rpc_set_error(rpc, "Not a valid server ip address"); - printf("%s\n", rpc->error_string); - return -2; + return -1; } switch (s.ss_family) { @@ -263,8 +258,7 @@ int rpc_connect_async(struct rpc_context *rpc, const char *server, int port, rpc if (rpc->fd == -1) { rpc_set_error(rpc, "Failed to open socket"); - printf("%s\n", rpc->error_string); - return -3; + return -1; } rpc->connect_cb = cb; @@ -274,8 +268,7 @@ int rpc_connect_async(struct rpc_context *rpc, const char *server, int port, rpc if (connect(rpc->fd, (struct sockaddr *)&s, socksize) != 0 && errno != EINPROGRESS) { rpc_set_error(rpc, "connect() to server failed"); - printf("%s\n", rpc->error_string); - return -4; + return -1; } return 0; diff --git a/mount/mount.c b/mount/mount.c index 771a549..15aacd8 100644 --- a/mount/mount.c +++ b/mount/mount.c @@ -38,7 +38,7 @@ int rpc_mount_null_async(struct rpc_context *rpc, rpc_cb cb, void *private_data) if (rpc_queue_pdu(rpc, pdu) != 0) { rpc_set_error(rpc, "Out of memory. Failed to queue pdu for mount/null call"); rpc_free_pdu(rpc, pdu); - return -2; + return -1; } return 0; @@ -57,13 +57,13 @@ int rpc_mount_mnt_async(struct rpc_context *rpc, rpc_cb cb, char *export, void * if (xdr_dirpath(&pdu->xdr, &export) == 0) { rpc_set_error(rpc, "XDR error. Failed to encode mount/mnt call"); rpc_free_pdu(rpc, pdu); - return -2; + return -1; } if (rpc_queue_pdu(rpc, pdu) != 0) { rpc_set_error(rpc, "Out of memory. Failed to queue pdu for mount/mnt call"); rpc_free_pdu(rpc, pdu); - return -2; + return -1; } return 0; @@ -75,14 +75,14 @@ int rpc_mount_dump_async(struct rpc_context *rpc, rpc_cb cb, void *private_data) pdu = rpc_allocate_pdu(rpc, MOUNT_PROGRAM, MOUNT_V3, MOUNT3_DUMP, cb, private_data, (xdrproc_t)xdr_mountlist, sizeof(mountlist)); if (pdu == NULL) { - printf("Failed to allocate pdu for mount/dump\n"); + rpc_set_error(rpc, "Failed to allocate pdu for mount/dump"); return -1; } if (rpc_queue_pdu(rpc, pdu) != 0) { - printf("Failed to queue mount/dump pdu\n"); + rpc_set_error(rpc, "Failed to queue mount/dump pdu"); rpc_free_pdu(rpc, pdu); - return -2; + return -1; } return 0; @@ -94,20 +94,20 @@ int rpc_mount_umnt_async(struct rpc_context *rpc, rpc_cb cb, char *export, void pdu = rpc_allocate_pdu(rpc, MOUNT_PROGRAM, MOUNT_V3, MOUNT3_UMNT, cb, private_data, (xdrproc_t)xdr_void, 0); if (pdu == NULL) { - printf("Failed to allocate pdu for mount/umnt\n"); + rpc_set_error(rpc, "Failed to allocate pdu for mount/umnt"); return -1; } if (xdr_dirpath(&pdu->xdr, &export) == 0) { - printf("failed to encode dirpath for mount/umnt\n"); + rpc_set_error(rpc, "failed to encode dirpath for mount/umnt"); rpc_free_pdu(rpc, pdu); - return -2; + return -1; } if (rpc_queue_pdu(rpc, pdu) != 0) { - printf("Failed to queue mount/umnt pdu\n"); + rpc_set_error(rpc, "Failed to queue mount/umnt pdu"); rpc_free_pdu(rpc, pdu); - return -2; + return -1; } return 0; @@ -119,14 +119,14 @@ int rpc_mount_umntall_async(struct rpc_context *rpc, rpc_cb cb, void *private_da pdu = rpc_allocate_pdu(rpc, MOUNT_PROGRAM, MOUNT_V3, MOUNT3_UMNTALL, cb, private_data, (xdrproc_t)xdr_void, 0); if (pdu == NULL) { - printf("Failed to allocate pdu for mount/umntall\n"); + rpc_set_error(rpc, "Failed to allocate pdu for mount/umntall"); return -1; } if (rpc_queue_pdu(rpc, pdu) != 0) { - printf("Failed to queue mount/umntall pdu\n"); + rpc_set_error(rpc, "Failed to queue mount/umntall pdu"); rpc_free_pdu(rpc, pdu); - return -2; + return -1; } return 0; @@ -138,14 +138,14 @@ int rpc_mount_export_async(struct rpc_context *rpc, rpc_cb cb, void *private_dat pdu = rpc_allocate_pdu(rpc, MOUNT_PROGRAM, MOUNT_V3, MOUNT3_EXPORT, cb, private_data, (xdrproc_t)xdr_exports, sizeof(exports)); if (pdu == NULL) { - printf("Failed to allocate pdu for mount/export\n"); + rpc_set_error(rpc, "Failed to allocate pdu for mount/export"); return -1; } if (rpc_queue_pdu(rpc, pdu) != 0) { - printf("Failed to queue mount/export pdu\n"); + rpc_set_error(rpc, "Failed to queue mount/export pdu"); rpc_free_pdu(rpc, pdu); - return -2; + return -1; } return 0; diff --git a/portmap/portmap.c b/portmap/portmap.c index 1e51a8f..69b8ad5 100644 --- a/portmap/portmap.c +++ b/portmap/portmap.c @@ -37,7 +37,7 @@ int rpc_pmap_null_async(struct rpc_context *rpc, rpc_cb cb, void *private_data) if (rpc_queue_pdu(rpc, pdu) != 0) { rpc_set_error(rpc, "Out of memory. Failed to queue pdu for portmap/null call"); rpc_free_pdu(rpc, pdu); - return -2; + return -1; } return 0; @@ -61,13 +61,13 @@ int rpc_pmap_getport_async(struct rpc_context *rpc, int program, int version, rp if (xdr_mapping(&pdu->xdr, &m) == 0) { rpc_set_error(rpc, "XDR error: Failed to encode data for portmap/getport call"); rpc_free_pdu(rpc, pdu); - return -2; + return -1; } if (rpc_queue_pdu(rpc, pdu) != 0) { - printf("Failed to queue portmap/getport pdu\n"); + rpc_set_error(rpc, "Failed to queue portmap/getport pdu"); rpc_free_pdu(rpc, pdu); - return -2; + return -1; } return 0; -- 2.34.1