0005-Use-standard-C-functions.patch (6657B)
- From af353da1558da816a014983d3abaafaa4929e04c Mon Sep 17 00:00:00 2001
- From: Michael Forney <mforney@mforney.org>
- Date: Sun, 5 Jun 2016 17:42:29 -0700
- Subject: [PATCH] Use standard C functions
- ---
- sshfs.c | 91 ++++++++++++++++++++++++++++++++++-----------------------
- 1 file changed, 55 insertions(+), 36 deletions(-)
- diff --git a/sshfs.c b/sshfs.c
- index 9c074a3..17f7d6a 100644
- --- a/sshfs.c
- +++ b/sshfs.c
- @@ -671,25 +671,25 @@ static inline void buf_add_path(struct buffer *buf, const char *path)
- if (sshfs.base_path[0]) {
- if (path[1]) {
- if (sshfs.base_path[strlen(sshfs.base_path)-1] != '/') {
- - realpath = g_strdup_printf("%s/%s",
- - sshfs.base_path,
- - path + 1);
- + if (asprintf(&realpath, "%s/%s", sshfs.base_path, path + 1) < 0)
- + abort();
- } else {
- - realpath = g_strdup_printf("%s%s",
- - sshfs.base_path,
- - path + 1);
- + if (asprintf(&realpath, "%s%s", sshfs.base_path, path + 1) < 0)
- + abort();
- }
- } else {
- - realpath = g_strdup(sshfs.base_path);
- + realpath = strdup(sshfs.base_path);
- }
- } else {
- if (path[1])
- - realpath = g_strdup(path + 1);
- + realpath = strdup(path + 1);
- else
- - realpath = g_strdup(".");
- + realpath = strdup(".");
- }
- + if (!realpath)
- + abort();
- buf_add_string(buf, realpath);
- - g_free(realpath);
- + free(realpath);
- }
- static int buf_check_get(struct buffer *buf, size_t len)
- @@ -1272,7 +1272,7 @@ static void request_free(struct request *req)
- {
- buf_free(&req->reply);
- sem_destroy(&req->ready);
- - g_free(req);
- + free(req);
- }
- static int request_table_insert(struct request_table *reqtab, struct request *req)
- @@ -1320,9 +1320,9 @@ static void chunk_free(struct read_chunk *chunk)
- rreq = list_entry(chunk->reqs.prev, struct read_req, list);
- list_del(&rreq->list);
- buf_free(&rreq->data);
- - g_free(rreq);
- + free(rreq);
- }
- - g_free(chunk);
- + free(chunk);
- }
- static void chunk_put(struct read_chunk *chunk)
- @@ -1871,8 +1871,10 @@ static int sftp_request_send(uint8_t type, struct iovec *iov, size_t count,
- struct request **reqp)
- {
- int err;
- - struct request *req = g_new0(struct request, 1);
- + struct request *req = calloc(1, sizeof(struct request));
- + if (!req)
- + return -ENOMEM;
- req->want_reply = want_reply;
- req->end_func = end_func;
- req->data = data;
- @@ -2579,8 +2581,10 @@ static int sshfs_open_common(const char *path, mode_t mode,
- if (fi->flags & O_APPEND)
- pflags |= SSH_FXF_APPEND;
- -
- - sf = g_new0(struct sshfs_file, 1);
- +
- + sf = calloc(1, sizeof(struct sshfs_file));
- + if (!sf)
- + return -ENOMEM;
- list_init(&sf->write_reqs);
- pthread_cond_init(&sf->write_finished, NULL);
- /* Assume random read after open */
- @@ -2624,7 +2628,7 @@ static int sshfs_open_common(const char *path, mode_t mode,
- } else {
- if (sshfs.dir_cache)
- cache_invalidate(path);
- - g_free(sf);
- + free(sf);
- }
- buf_free(&buf);
- return err;
- @@ -2691,7 +2695,7 @@ static void sshfs_file_put(struct sshfs_file *sf)
- {
- sf->refs--;
- if (!sf->refs)
- - g_free(sf);
- + free(sf);
- }
- static void sshfs_file_get(struct sshfs_file *sf)
- @@ -2761,9 +2765,11 @@ static void sshfs_read_begin(struct request *req)
- static struct read_chunk *sshfs_send_read(struct sshfs_file *sf, size_t size,
- off_t offset)
- {
- - struct read_chunk *chunk = g_new0(struct read_chunk, 1);
- + struct read_chunk *chunk = calloc(1, sizeof(struct read_chunk));
- struct buffer *handle = &sf->handle;
- + if (!chunk)
- + abort();
- pthread_cond_init(&chunk->sio.finished, NULL);
- list_init(&chunk->reqs);
- chunk->size = size;
- @@ -2777,7 +2783,9 @@ static struct read_chunk *sshfs_send_read(struct sshfs_file *sf, size_t size,
- struct read_req *rreq;
- size_t bsize = size < sshfs.max_read ? size : sshfs.max_read;
- - rreq = g_new0(struct read_req, 1);
- + rreq = calloc(1, sizeof(struct read_req));
- + if (!rreq)
- + abort();
- rreq->sio = &chunk->sio;
- rreq->size = bsize;
- buf_init(&rreq->data, 0);
- @@ -2848,7 +2856,7 @@ static int wait_chunk(struct read_chunk *chunk, char *buf, size_t size)
- size -= rreq->res;
- list_del(&rreq->list);
- buf_free(&rreq->data);
- - g_free(rreq);
- + free(rreq);
- }
- }
- @@ -3473,9 +3481,10 @@ static int sshfs_opt_proc(void *data, const char *arg, int key,
- switch (key) {
- case FUSE_OPT_KEY_OPT:
- if (is_ssh_opt(arg)) {
- - tmp = g_strdup_printf("-o%s", arg);
- + if (asprintf(&tmp, "-o%s", arg) < 0)
- + abort();
- ssh_add_arg(tmp);
- - g_free(tmp);
- + free(tmp);
- return 0;
- }
- /* Pass through */
- @@ -3544,9 +3553,10 @@ static int sshfs_opt_proc(void *data, const char *arg, int key,
- case KEY_PORT:
- - tmp = g_strdup_printf("-oPort=%s", arg + 2);
- + if (asprintf(&tmp, "-oPort=%s", arg + 2) < 0)
- + abort();
- ssh_add_arg(tmp);
- - g_free(tmp);
- + free(tmp);
- return 0;
- case KEY_COMPRESS:
- @@ -3554,9 +3564,10 @@ static int sshfs_opt_proc(void *data, const char *arg, int key,
- return 0;
- case KEY_CONFIGFILE:
- - tmp = g_strdup_printf("-F%s", arg + 2);
- + if (asprintf(&tmp, "-F%s", arg + 2) < 0)
- + abort();
- ssh_add_arg(tmp);
- - g_free(tmp);
- + free(tmp);
- return 0;
- default:
- @@ -3732,17 +3743,19 @@ static char *find_base_path(void)
- static char *fsname_escape_commas(char *fsnameold)
- {
- - char *fsname = g_malloc(strlen(fsnameold) * 2 + 1);
- + char *fsname = malloc(strlen(fsnameold) * 2 + 1);
- char *d = fsname;
- char *s;
- + if (!fsname)
- + abort();
- for (s = fsnameold; *s; s++) {
- if (*s == '\\' || *s == ',')
- *d++ = '\\';
- *d++ = *s;
- }
- *d = '\0';
- - g_free(fsnameold);
- + free(fsnameold);
- return fsname;
- }
- @@ -4083,15 +4096,20 @@ int main(int argc, char *argv[])
- else
- sshfs.max_outstanding_len = ~0;
- - fsname = g_strdup(sshfs.host);
- - sshfs.base_path = g_strdup(find_base_path());
- + fsname = strdup(sshfs.host);
- + if (!fsname)
- + abort();
- + sshfs.base_path = strdup(find_base_path());
- + if (!sshfs.base_path)
- + abort();
- if (sshfs.ssh_command)
- set_ssh_command();
- - tmp = g_strdup_printf("-%i", sshfs.ssh_ver);
- + if (asprintf(&tmp, "-%i", sshfs.ssh_ver) < 0)
- + abort();
- ssh_add_arg(tmp);
- - g_free(tmp);
- + free(tmp);
- ssh_add_arg(sshfs.host);
- if (sshfs.sftp_server)
- sftp_server = sshfs.sftp_server;
- @@ -4118,10 +4136,11 @@ int main(int argc, char *argv[])
- sshfs.max_write = 65536;
- fsname = fsname_escape_commas(fsname);
- - tmp = g_strdup_printf("-osubtype=sshfs,fsname=%s", fsname);
- + if (asprintf(&tmp, "-osubtype=sshfs,fsname=%s", fsname) < 0)
- + abort();
- fuse_opt_insert_arg(&args, 1, tmp);
- - g_free(tmp);
- - g_free(fsname);
- + free(tmp);
- + free(fsname);
- if(sshfs.dir_cache)
- sshfs.op = cache_wrap(&sshfs_oper);
- --
- 2.24.0