remove all remaining talloc calls
authorroot <root@rcn1.VSOFS1.COM>
Thu, 19 Mar 2009 05:28:43 +0000 (16:28 +1100)
committerroot <root@rcn1.VSOFS1.COM>
Thu, 19 Mar 2009 05:28:43 +0000 (16:28 +1100)
migrate/remote-cache.c

index b29d98f86f2a160849487a60b49b7daa1481bd93..813c7a676bf1df108260806dd3b57f37c13fd4b8 100644 (file)
@@ -47,7 +47,6 @@
 #include "migrate.h"
 #include "../lib/tdb/include/tdb.h"
 #include "../lib/tdb/common/tdb_private.h"
-#include "../lib/talloc/talloc.h"
 #include "../lib/util/debug.h"
 #include "popt.h"
 
@@ -599,7 +598,7 @@ int unlink_parent_dir_cache(const char *path)
 
        if (asprintf(&cache_path, "%s/%s", ppath, CACHE_DIR_NAME) == -1) {
                DEBUG(DEBUG_ERR,(__location__ " Failed to malloc cache_path\n"));
-               ret = -1;
+               ret = -ENOMEM;
                goto finished;
        }
 
@@ -617,20 +616,22 @@ finished:
 }
 
 
-int lookup_attribute_in_parent(TALLOC_CTX *mem_ctx, const char *path, struct stat *st)
+int lookup_attribute_in_parent(const char *path, struct stat *st)
 {
+       char *ppath = NULL;
        int cache_fd = -1;
        struct cached_dir_entry cache_dirent;
        char *sptr;
        int ret = 0;
 
-       sptr = talloc_strdup(mem_ctx, path);
-       if (sptr == NULL) {
+       ppath = strdup(path);
+       if (ppath == NULL) {
+               DEBUG(DEBUG_ERR,(__location__ " failed to strdup ppath\n"));
                ret = -1;
                goto finished;
        }
 
-       sptr = rindex(sptr, '/');
+       sptr = rindex(ppath, '/');
        if (sptr == NULL) {
                ret = -1;
                goto finished;
@@ -660,6 +661,9 @@ finished:
        if (cache_fd) {
                close(cache_fd);
        }
+       if (ppath != NULL) {
+               free(ppath);
+       }
        errno = 0;
        return ret;
 }
@@ -677,17 +681,16 @@ static int remote_cache_getattr(const char *path, struct stat *st)
        char *cache_path = NULL;
        struct stat old_st;
        struct stat cache_st;
-       int ret;
+       int ret = 0;
 
        if (file_check_parent_mtime) {
-               TALLOC_CTX *mem_ctx = talloc_new(NULL);
                /* try to get the attributes from the parents cache
                   (if the parents cache is "current"
                */
-               ret = lookup_attribute_in_parent(mem_ctx, path, &old_st);
-               talloc_free(mem_ctx);
+               ret = lookup_attribute_in_parent(path, &old_st);
                if (ret != -1) {
-               DEBUG(DEBUG_DEBUG, (__location__ " GETATTR:[%s] from parent cache\n", path));
+                       DEBUG(DEBUG_DEBUG, (__location__ " GETATTR:[%s] from parent cache\n", path));
+                       ret = 0;
                        goto finished;
                }                       
        }
@@ -698,10 +701,12 @@ static int remote_cache_getattr(const char *path, struct stat *st)
         */
        if (asprintf(&old_path, "%s/%s", remote, path) == -1) {
                DEBUG(DEBUG_ERR,(__location__ " Failed to malloc old path\n"));
+               ret = -ENOMEM;
                goto finished;
        }
        if (asprintf(&new_path, "%s/%s", cache, path) == -1) {
                DEBUG(DEBUG_ERR,(__location__ " Failed to malloc new path\n"));
+               ret = -ENOMEM;
                goto finished;
        }
 
@@ -730,6 +735,7 @@ static int remote_cache_getattr(const char *path, struct stat *st)
         */
        if (asprintf(&cache_path, "%s/%s", cache, path) == -1) {
                DEBUG(DEBUG_ERR,(__location__ " Failed to malloc cache path\n"));
+               ret = -ENOMEM;
                goto finished;
        }
 
@@ -765,7 +771,6 @@ finished:
 
 static int remote_cache_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi)
 {
-       TALLOC_CTX *mem_ctx = talloc_new(NULL);
        int ret;
        char *new_path = NULL;
        char *old_path = NULL;
@@ -780,9 +785,22 @@ static int remote_cache_readdir(const char *path, void *buf, fuse_fill_dir_t fil
        struct flock lock;
        struct statvfs vfs;
 
-       new_path   = talloc_asprintf(mem_ctx, "%s/%s", cache, path);
-       old_path   = talloc_asprintf(mem_ctx, "%s/%s", remote, path);
-       cache_path = talloc_asprintf(mem_ctx, "%s/%s/%s", cache, path, CACHE_DIR_NAME);
+       if (asprintf(&old_path, "%s/%s", remote, path) == -1) {
+               DEBUG(DEBUG_ERR,(__location__ " Failed to malloc old path\n"));
+               ret = -ENOMEM;
+               goto finished;
+       }
+       if (asprintf(&new_path, "%s/%s", cache, path) == -1) {
+               DEBUG(DEBUG_ERR,(__location__ " Failed to malloc new path\n"));
+               ret = -ENOMEM;
+               goto finished;
+       }
+       if (asprintf(&cache_path, "%s/%s/%s", cache, path, CACHE_DIR_NAME) == -1) {
+               DEBUG(DEBUG_ERR,(__location__ " Failed to malloc cache_path\n"));
+               ret = -ENOMEM;
+               goto finished;
+       }
+
 
        DEBUG(DEBUG_DEBUG, (__location__ " READDIR %s\n", path));
 
@@ -948,8 +966,10 @@ read_remote:
 
        errno = 0;
        while ((dirent  = readdir(dir)) != NULL) {
-               talloc_free(obj_path);
-               obj_path = NULL;
+               if (obj_path != NULL) {
+                       free(obj_path);
+                       obj_path = NULL;
+               }
 
                if (strlen(dirent->d_name) > MAX_NAME_LEN) {
                        DEBUG(DEBUG_ERR, (__location__ " Too long name : %s. skipping entry for %s\n",
@@ -959,7 +979,11 @@ read_remote:
                memcpy(&cache_dirent.filename[0],dirent->d_name, strlen(dirent->d_name)+1);
 
 
-               obj_path = talloc_asprintf(mem_ctx, "%s/%s", old_path, dirent->d_name);
+               if (asprintf(&obj_path, "%s/%s", old_path, dirent->d_name) == -1) {
+                       DEBUG(DEBUG_ERR,(__location__ " Failed to malloc obj path\n"));
+                       ret = -ENOMEM;
+                       goto finished;
+               }
 
                ret = lstat(obj_path, &cache_dirent.st);
                if (ret != 0) {
@@ -1018,19 +1042,33 @@ finished:
        if (dir) {
                closedir(dir);
        }
-
-       talloc_free(mem_ctx);
+       if (obj_path != NULL) {
+               free(obj_path);
+       }
+       if (cache_path != NULL) {
+               free(cache_path);
+       }
+       if (old_path != NULL) {
+               free(old_path);
+       }
+       if (new_path != NULL) {
+               free(new_path);
+       }
        return ret;
 }
 
 static int remote_cache_statfs(const char *path, struct statvfs *buf)
 {
-       TALLOC_CTX *mem_ctx = talloc_new(NULL);
        char *old_path = NULL;
        int ret;
 
-//qqq  DEBUG(DEBUG_DEBUG, (__location__ " STATFS called\n"));
-       old_path   = talloc_asprintf(mem_ctx, "%s/%s", remote, path);
+       DEBUG(DEBUG_DEBUG, (__location__ " STATFS called\n"));
+
+       if (asprintf(&old_path, "%s/%s", remote, path) == -1) {
+               DEBUG(DEBUG_ERR,(__location__ " Failed to malloc old_path\n"));
+               ret = -ENOMEM;
+               goto finished;
+       }
        ret = statvfs(old_path, buf);
        if (ret == -1) {
                ret = -errno;
@@ -1038,19 +1076,25 @@ static int remote_cache_statfs(const char *path, struct statvfs *buf)
        }
 
 finished:
-       talloc_free(mem_ctx);
+       if (old_path != NULL) {
+               free(old_path);
+       }
        return ret;
 }
 
 static int remote_cache_readlink(const char *path, char *buf, size_t len)
 {
-       TALLOC_CTX *mem_ctx = talloc_new(NULL);
        char *old_path = NULL;
        ssize_t lsize;
        int ret = 0;
 
        DEBUG(DEBUG_DEBUG, (__location__ " READLINK %s len:%d\n", path, (int)len));
-       old_path   = talloc_asprintf(mem_ctx, "%s/%s", remote, path);
+
+       if (asprintf(&old_path, "%s/%s", remote, path) == -1) {
+               DEBUG(DEBUG_ERR,(__location__ " Failed to malloc old_path\n"));
+               ret = -ENOMEM;
+               goto finished;
+       }
        lsize = readlink(old_path, buf, len);
        if (lsize == -1) {
                ret = -errno;
@@ -1061,14 +1105,15 @@ static int remote_cache_readlink(const char *path, char *buf, size_t len)
        }
 
 finished:
-       talloc_free(mem_ctx);
+       if (old_path != NULL) {
+               free(old_path);
+       }
        return ret;
 }
 
 static int remote_cache_access(const char *path, int mask)
 {
        int ret;
-       TALLOC_CTX *mem_ctx = talloc_new(NULL);
        char *old_path = NULL;
        struct stat old_st;
        gid_t my_gid;
@@ -1087,7 +1132,11 @@ static int remote_cache_access(const char *path, int mask)
                }
        }
 
-       old_path   = talloc_asprintf(mem_ctx, "%s/%s", remote, path);
+       if (asprintf(&old_path, "%s/%s", remote, path) == -1) {
+               DEBUG(DEBUG_ERR,(__location__ " Failed to malloc old_path\n"));
+               ret = -ENOMEM;
+               goto finished;
+       }
 
        bzero(&old_st, sizeof(old_st));
        ret = lstat(old_path, &old_st);
@@ -1176,21 +1225,31 @@ static int remote_cache_access(const char *path, int mask)
        }
 
 finished:
-       talloc_free(mem_ctx);
+       if (old_path != NULL) {
+               free(old_path);
+       }
        return ret;
 }
 
 
-int create_parent_directories(TALLOC_CTX *mem_ctx, char *old_path, char *new_path)
+int create_parent_directories(char *old_path, char *new_path)
 {
-       char *old_dir, *new_dir, *p;
+       char *old_dir = NULL;
+       char *new_dir = NULL;
+       char *p;
        struct stat st;
        int ret, len;
        
-       old_dir = talloc_strdup(mem_ctx, old_path);
+       old_dir = strdup(old_path);
+       if (old_dir == NULL) {
+               DEBUG(DEBUG_ERR,(__location__ " failed to allocate old_dir\n"));
+               ret = -1;
+               goto finished;
+       }
        p = rindex(old_dir, '/');
        if (p == NULL) {
-               return -1;
+               ret = -1;
+               goto finished;
        }
        *p = 0;
        len = strlen(old_dir);
@@ -1206,7 +1265,12 @@ int create_parent_directories(TALLOC_CTX *mem_ctx, char *old_path, char *new_pat
                }
        }
 
-       new_dir = talloc_strdup(mem_ctx, new_path);
+       new_dir = strdup(new_path);
+       if (new_dir == NULL) {
+               DEBUG(DEBUG_ERR,(__location__ " failed to allocate new_dir\n"));
+               ret = -1;
+               goto finished;
+       }
        p = rindex(new_dir, '/');
        *p = 0;
        len = strlen(new_dir);
@@ -1228,39 +1292,52 @@ int create_parent_directories(TALLOC_CTX *mem_ctx, char *old_path, char *new_pat
        bzero(&st, sizeof(st));
        ret = lstat(new_dir, &st);
        if ((ret == -1) && (errno == ENOENT)) {
-               if (create_parent_directories(mem_ctx, old_dir, new_dir) != 0) {
-                       return -1;
+               if (create_parent_directories(old_dir, new_dir) != 0) {
+                       ret = -1;
+                       goto finished;
                }
 
                ret = lstat(old_dir, &st);
                if (ret == -1) {
                        DEBUG(DEBUG_ERR,(__location__ " Failed to stat remote directory %s\n", old_dir));
-                       return -1;
+                       ret = -1;
+                       goto finished;
                }
 
                DEBUG(DEBUG_DEBUG,("Must create parent directory %s\n", new_dir));
                mkdir(new_dir, 0);
                if (chown(new_dir, st.st_uid, st.st_gid) == -1) {
                        DEBUG(DEBUG_ERR, ("CHOWN for new directory %s failed with %s\n", new_dir, strerror(errno)));
-               return -1;
+                       ret = -1;
+                       goto finished;
                }
                if (chmod(new_dir, st.st_mode) == -1) {
                        DEBUG(DEBUG_ERR, ("CHMOD for new directory %s failed with %s\n", new_dir, strerror(errno)));
-               unlink(new_dir);
-               return -1;
+                       unlink(new_dir);
+                       ret = -1;
+                       goto finished;
                }
 
        }
-       return 0;
+
+       ret = 0;
+
+finished:
+       if (old_dir != NULL) {
+               free(old_dir);
+       }
+       if (new_dir != NULL) {
+               free(new_dir);
+       }
+       return ret;
 }
 
 
 int mark_file_for_migration(const char *path)
 {
-       TALLOC_CTX *mem_ctx = NULL;
        TDB_DATA hdrkey, pathkey, data;
-       struct migrate_header *mh;
-       struct migrate_entry *me;
+       struct migrate_header *mh = NULL;
+       struct migrate_entry *me = NULL;
        int ret = 0;
 
        DEBUG(DEBUG_DEBUG,("Marking file %s for migration\n", path));
@@ -1275,8 +1352,6 @@ int mark_file_for_migration(const char *path)
                }
        }
 
-       mem_ctx = talloc_new(NULL);
-
        /* get the header */
        hdrkey.dptr  = (uint8_t *)discard_const(MIGRATE_HEADER_KEY);
        hdrkey.dsize = strlen(MIGRATE_HEADER_KEY);
@@ -1290,23 +1365,25 @@ int mark_file_for_migration(const char *path)
 
        data = tdb_fetch(migrate_db, hdrkey);
        if (data.dptr == NULL) {
-               mh = talloc(mem_ctx, struct migrate_header);
+               mh = malloc(sizeof(struct migrate_header));
                if (mh == NULL) {
                        DEBUG(DEBUG_ERR,(__location__ " Failed to allocate new mh structure\n"));
-                       ret = -1;
+                       ret = -ENOMEM;
                        goto finished;
                }
                mh->first = 0;
                mh->last  = 0;
                mh->max   = MIGRATE_MAX;
        } else {
-               mh = talloc_memdup(mem_ctx, data.dptr, data.dsize);
-               free(data.dptr);
+               mh = malloc(data.dsize);
                if (mh == NULL) {
+                       free(data.dptr);
                        DEBUG(DEBUG_ERR,(__location__ " Failed to allocate copy mh structure\n"));
-                       ret = -1;
+                       ret = -ENOMEM;
                        goto finished;
                }
+               memcpy(mh, data.dptr, data.dsize);
+               free(data.dptr);
        }
 
        mh->last = (mh->last + 1)%mh->max; 
@@ -1318,10 +1395,10 @@ int mark_file_for_migration(const char *path)
        /* store the new path. we use the chainlock on the header record
           as our lock to the database */
        data.dsize = offsetof(struct migrate_entry, path) + strlen(path);
-       me  = talloc_size(mem_ctx, data.dsize);
+       me  = malloc(data.dsize);
        if (me == NULL) {
                DEBUG(DEBUG_ERR,(__location__ " Failed to allocate path data\n"));
-               ret = -1;
+               ret = -ENOMEM;
                goto finished;
        }
        data.dptr = (uint8_t *)me;
@@ -1357,23 +1434,32 @@ int mark_file_for_migration(const char *path)
 
 finished:
        tdb_unlockall(migrate_db);
-       talloc_free(mem_ctx);
+       if (mh != NULL) {
+               free(mh);
+       }
+       if (me != NULL) {
+               free(me);
+       }
        return ret;
 }
 
-static int migrate_small_file_immed(TALLOC_CTX *mem_ctx, char *old_path, char *new_path, struct stat *st)
+static int migrate_small_file_immed(char *old_path, char *new_path, struct stat *st)
 {
+       char *new_tmppath = NULL;
+       char *buf = NULL;
        int ret = 0;
        int ofd = -1;
        int nfd = -1;
        struct utimbuf times;
-       char *buf;
-       char *new_tmppath;
        struct flock lock;
        int has_lock = 0;
 
        DEBUG(DEBUG_DEBUG,("MIGRATE_SMALL_FILE_IMMED(%s %s)\n", old_path, new_path));
-       new_tmppath   = talloc_asprintf(mem_ctx, "%s ... TMP_RC", new_path);
+       if (asprintf(&new_tmppath, "%s ... TMP_RC", new_path) == -1) {
+               DEBUG(DEBUG_ERR,(__location__ " Failed to malloc new_tmppath\n"));
+               ret = -ENOMEM;
+               goto finished;
+       }
 
        if (st->st_size == 0) {
                ret = -ENOMEM;
@@ -1405,9 +1491,9 @@ static int migrate_small_file_immed(TALLOC_CTX *mem_ctx, char *old_path, char *n
                goto finished;
        }
 
-       buf = talloc_size(mem_ctx, st->st_size);
+       buf = malloc(st->st_size);
        if (buf == NULL) {
-               DEBUG(DEBUG_ERR,(__location__ " Failed to talloc %d bytes\n", (int)st->st_size));
+               DEBUG(DEBUG_ERR,(__location__ " Failed to malloc %d bytes\n", (int)st->st_size));
                ret = -ENOMEM;
                goto finished;
        }
@@ -1474,13 +1560,18 @@ finished:
        if ((has_lock != 0) && (ret != 0)) {
                unlink(new_tmppath);
        }
+       if (new_tmppath != NULL) {
+               free(new_tmppath);
+       }
+       if (buf != NULL) {
+               free(buf);
+       }
        return ret;
 }
 
 
 static int remote_cache_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi)
 {
-       TALLOC_CTX *mem_ctx;
        char *new_path = NULL;
        char *old_path = NULL;
        struct stat cache_st;
@@ -1501,10 +1592,12 @@ static int remote_cache_read(const char *path, char *buf, size_t size, off_t off
 
        if (asprintf(&new_path, "%s/%s", cache, path) == -1) {
                DEBUG(DEBUG_ERR,(__location__ " Failed to malloc new path\n"));
+               ret = -ENOMEM;
                goto finished;
        }
        if (asprintf(&old_path, "%s/%s", remote, path) == -1) {
                DEBUG(DEBUG_ERR,(__location__ " Failed to malloc old path\n"));
+               ret = -ENOMEM;
                goto finished;
        }
 
@@ -1558,9 +1651,7 @@ static int remote_cache_read(const char *path, char *buf, size_t size, off_t off
         * This can only be used when files are created/deleted but never
         * modified.
         */
-       mem_ctx = talloc_new(NULL);
-       ret = lookup_attribute_in_parent(mem_ctx, path, &cache_st);
-       talloc_free(mem_ctx);
+       ret = lookup_attribute_in_parent(path, &cache_st);
        if (ret == -1) {
                DEBUG(DEBUG_ERR, (__location__ " READ: %s not found in parent cache\n", path));
                goto read_from_remote_site;
@@ -1635,9 +1726,7 @@ read_from_remote_site:
         * in one go
         */
        if (old_st.st_size <= file_migrate_small_file) {
-               mem_ctx = talloc_new(NULL);
-               ret = migrate_small_file_immed(mem_ctx, old_path, new_path, &old_st);
-               talloc_free(mem_ctx);
+               ret = migrate_small_file_immed(old_path, new_path, &old_st);
                if (ret == 0) {
                                DEBUG(DEBUG_DEBUG,("migrate_small_file_immed() successful reread from cache\n"));
                        goto read_from_local_cache;
@@ -1718,18 +1807,25 @@ finished:
 
 static int remote_cache_mknod(const char *path, mode_t mode, dev_t rdev)
 {
-       TALLOC_CTX *mem_ctx = talloc_new(NULL);
        char *new_path = NULL;
        char *old_path = NULL;
        int ret;
 
        if (!read_write) {      
-               talloc_free(mem_ctx);
-               return -EROFS;
+               ret = -EROFS;
+               goto finished;
        }
 
-       new_path   = talloc_asprintf(mem_ctx, "%s/%s", cache, path);
-       old_path   = talloc_asprintf(mem_ctx, "%s/%s", remote, path);
+       if (asprintf(&old_path, "%s/%s", remote, path) == -1) {
+               DEBUG(DEBUG_ERR,(__location__ " Failed to malloc old path\n"));
+               ret = -ENOMEM;
+               goto finished;
+       }
+       if (asprintf(&new_path, "%s/%s", cache, path) == -1) {
+               DEBUG(DEBUG_ERR,(__location__ " Failed to malloc new path\n"));
+               ret = -ENOMEM;
+               goto finished;
+       }
 
 
        DEBUG(DEBUG_DEBUG, (__location__ " MKNOD:%s mode:%o rdev:%x\n", path, (int)mode, (int)rdev));
@@ -1757,7 +1853,12 @@ static int remote_cache_mknod(const char *path, mode_t mode, dev_t rdev)
 
 finished:
        switch_back_to_root();
-       talloc_free(mem_ctx);
+       if (old_path != NULL) {
+               free(old_path);
+       }
+       if (new_path != NULL) {
+               free(new_path);
+       }
        return ret;
 }
 
@@ -1765,18 +1866,26 @@ finished:
 
 static int remote_cache_mkdir(const char *path, mode_t mode)
 {
-       TALLOC_CTX *mem_ctx = talloc_new(NULL);
        char *new_path = NULL;
        char *old_path = NULL;
        int ret;
 
        if (!read_write) {
-               talloc_free(mem_ctx);
-               return -EROFS;
+               ret = -EROFS;
+               goto finished;
+       }
+
+       if (asprintf(&old_path, "%s/%s", remote, path) == -1) {
+               DEBUG(DEBUG_ERR,(__location__ " Failed to malloc old path\n"));
+               ret = -ENOMEM;
+               goto finished;
+       }
+       if (asprintf(&new_path, "%s/%s", cache, path) == -1) {
+               DEBUG(DEBUG_ERR,(__location__ " Failed to malloc new path\n"));
+               ret = -ENOMEM;
+               goto finished;
        }
 
-       new_path   = talloc_asprintf(mem_ctx, "%s/%s", cache, path);
-       old_path   = talloc_asprintf(mem_ctx, "%s/%s", remote, path);
 
 
        DEBUG(DEBUG_DEBUG, (__location__ " MKDIR:%s mode:%o\n", path, (int)mode));
@@ -1804,25 +1913,37 @@ static int remote_cache_mkdir(const char *path, mode_t mode)
 
 finished:
        switch_back_to_root();
-       talloc_free(mem_ctx);
+       if (old_path != NULL) {
+               free(old_path);
+       }
+       if (new_path != NULL) {
+               free(new_path);
+       }
        return ret;
 }
 
 
 static int remote_cache_rmdir(const char *path)
 {
-       TALLOC_CTX *mem_ctx = talloc_new(NULL);
        char *new_path = NULL;
        char *old_path = NULL;
        int ret;
 
        if (!read_write) {
-               talloc_free(mem_ctx);
-               return -EROFS;
+               ret = -EROFS;
+               goto finished;
        }
 
-       new_path   = talloc_asprintf(mem_ctx, "%s/%s", cache, path);
-       old_path   = talloc_asprintf(mem_ctx, "%s/%s", remote, path);
+       if (asprintf(&old_path, "%s/%s", remote, path) == -1) {
+               DEBUG(DEBUG_ERR,(__location__ " Failed to malloc old path\n"));
+               ret = -ENOMEM;
+               goto finished;
+       }
+       if (asprintf(&new_path, "%s/%s", cache, path) == -1) {
+               DEBUG(DEBUG_ERR,(__location__ " Failed to malloc new path\n"));
+               ret = -ENOMEM;
+               goto finished;
+       }
 
 
        DEBUG(DEBUG_DEBUG, (__location__ " RMDIR:%s\n", path));
@@ -1850,7 +1971,12 @@ static int remote_cache_rmdir(const char *path)
 
 finished:
        switch_back_to_root();
-       talloc_free(mem_ctx);
+       if (old_path != NULL) {
+               free(old_path);
+       }
+       if (new_path != NULL) {
+               free(new_path);
+       }
        return ret;
 }
 
@@ -1858,7 +1984,6 @@ finished:
 
 static int remote_cache_unlink(const char *path)
 {
-       TALLOC_CTX *mem_ctx = talloc_new(NULL);
        char *new_path = NULL;
        char *old_path = NULL;
        char *old_ppath = NULL;
@@ -1872,11 +1997,16 @@ static int remote_cache_unlink(const char *path)
        int perms;
 
        if (!read_write) {
-               talloc_free(mem_ctx);
-               return -EROFS;
+               ret = -EROFS;
+               goto finished;
        }
 
-       ppath = talloc_strdup(mem_ctx, path);
+       ppath = strdup(path);
+       if (ppath == NULL) {
+               DEBUG(DEBUG_ERR,(__location__ " failed to strdup ppath\n"));
+               ret = -ENOMEM;
+               goto finished;
+       }
        sptr = rindex(ppath, '/');
        if (sptr == NULL) {
                ret = -EACCES;
@@ -1887,9 +2017,21 @@ static int remote_cache_unlink(const char *path)
                ret = -EACCES;
                goto finished;
        }
-       new_path   = talloc_asprintf(mem_ctx, "%s/%s", cache, path);
-       old_path   = talloc_asprintf(mem_ctx, "%s/%s", remote, path);
-       old_ppath  = talloc_asprintf(mem_ctx, "%s/%s", remote, ppath);
+       if (asprintf(&new_path, "%s/%s", cache, path) == -1) {
+               DEBUG(DEBUG_ERR,(__location__ " Failed to malloc new path\n"));
+               ret = -ENOMEM;
+               goto finished;
+       }
+       if (asprintf(&old_path, "%s/%s", remote, path) == -1) {
+               DEBUG(DEBUG_ERR,(__location__ " Failed to malloc old path\n"));
+               ret = -ENOMEM;
+               goto finished;
+       }
+       if (asprintf(&old_ppath, "%s/%s", remote, ppath) == -1) {
+               DEBUG(DEBUG_ERR,(__location__ " Failed to malloc old ppath\n"));
+               ret = -ENOMEM;
+               goto finished;
+       }
 
 
        DEBUG(DEBUG_DEBUG, (__location__ " UNLINK:%s\n", path));
@@ -1928,24 +2070,42 @@ static int remote_cache_unlink(const char *path)
        unlink_object(new_path);
        unlink_parent_dir_cache(new_path);
 finished:
-       talloc_free(mem_ctx);
+       if (ppath != NULL) {
+               free(ppath);
+       }
+       if (old_ppath != NULL) {
+               free(old_ppath);
+       }
+       if (old_path != NULL) {
+               free(old_path);
+       }
+       if (new_path != NULL) {
+               free(new_path);
+       }
        return ret;
 }
 
 static int remote_cache_chmod(const char *path, mode_t mode)
 {
-       TALLOC_CTX *mem_ctx = talloc_new(NULL);
        char *new_path = NULL;
        char *old_path = NULL;
        int ret;
 
        if (!read_write) {
-               talloc_free(mem_ctx);
-               return -EROFS;
+               ret = -EROFS;
+               goto finished;
        }
 
-       new_path   = talloc_asprintf(mem_ctx, "%s/%s", cache, path);
-       old_path   = talloc_asprintf(mem_ctx, "%s/%s", remote, path);
+       if (asprintf(&new_path, "%s/%s", cache, path) == -1) {
+               DEBUG(DEBUG_ERR,(__location__ " Failed to malloc new path\n"));
+               ret = -ENOMEM;
+               goto finished;
+       }
+       if (asprintf(&old_path, "%s/%s", remote, path) == -1) {
+               DEBUG(DEBUG_ERR,(__location__ " Failed to malloc old path\n"));
+               ret = -ENOMEM;
+               goto finished;
+       }
 
 
        DEBUG(DEBUG_DEBUG, (__location__ " CHMOD:%s mode:%o\n", path, mode));
@@ -1973,24 +2133,36 @@ static int remote_cache_chmod(const char *path, mode_t mode)
 
 finished:
        switch_back_to_root();
-       talloc_free(mem_ctx);
+       if (old_path != NULL) {
+               free(old_path);
+       }
+       if (new_path != NULL) {
+               free(new_path);
+       }
        return ret;
 }
 
 static int remote_cache_chown(const char *path, uid_t uid, gid_t gid)
 {
-       TALLOC_CTX *mem_ctx = talloc_new(NULL);
        char *new_path = NULL;
        char *old_path = NULL;
        int ret;
 
        if (!read_write) {
-               talloc_free(mem_ctx);
-               return -EROFS;
+               ret = -EROFS;
+               goto finished;
        }
 
-       new_path   = talloc_asprintf(mem_ctx, "%s/%s", cache, path);
-       old_path   = talloc_asprintf(mem_ctx, "%s/%s", remote, path);
+       if (asprintf(&new_path, "%s/%s", cache, path) == -1) {
+               DEBUG(DEBUG_ERR,(__location__ " Failed to malloc new path\n"));
+               ret = -ENOMEM;
+               goto finished;
+       }
+       if (asprintf(&old_path, "%s/%s", remote, path) == -1) {
+               DEBUG(DEBUG_ERR,(__location__ " Failed to malloc old path\n"));
+               ret = -ENOMEM;
+               goto finished;
+       }
 
 
        DEBUG(DEBUG_DEBUG, (__location__ " CHOWN:%s uid %d gid %d\n", path, uid, gid));
@@ -2018,13 +2190,17 @@ static int remote_cache_chown(const char *path, uid_t uid, gid_t gid)
 
 finished:
        switch_back_to_root();
-       talloc_free(mem_ctx);
+       if (old_path != NULL) {
+               free(old_path);
+       }
+       if (new_path != NULL) {
+               free(new_path);
+       }
        return ret;
 }
 
 static int remote_cache_create(const char *path, mode_t mode, struct fuse_file_info *fi)
 {
-       TALLOC_CTX *mem_ctx = talloc_new(NULL);
        char *new_path = NULL;
        char *old_path = NULL;
        char *old_ppath = NULL;
@@ -2039,11 +2215,16 @@ static int remote_cache_create(const char *path, mode_t mode, struct fuse_file_i
        int perms;
 
        if (!read_write) {
-               talloc_free(mem_ctx);
-               return -EROFS;
+               ret = -EROFS;
+               goto finished;
        }
 
-       ppath = talloc_strdup(mem_ctx, path);
+       ppath = strdup(path);
+       if (ppath == NULL) {
+               DEBUG(DEBUG_ERR,(__location__ " failed to strdup ppath\n"));
+               ret = -ENOMEM;
+               goto finished;
+       }
        sptr = rindex(ppath, '/');
        if (sptr == NULL) {
                ret = -EACCES;
@@ -2056,9 +2237,21 @@ static int remote_cache_create(const char *path, mode_t mode, struct fuse_file_i
        }
 
        
-       new_path   = talloc_asprintf(mem_ctx, "%s/%s", cache, path);
-       old_path   = talloc_asprintf(mem_ctx, "%s/%s", remote, path);
-       old_ppath  = talloc_asprintf(mem_ctx, "%s/%s", remote, ppath);
+       if (asprintf(&new_path, "%s/%s", cache, path) == -1) {
+               DEBUG(DEBUG_ERR,(__location__ " Failed to malloc new path\n"));
+               ret = -ENOMEM;
+               goto finished;
+       }
+       if (asprintf(&old_path, "%s/%s", remote, path) == -1) {
+               DEBUG(DEBUG_ERR,(__location__ " Failed to malloc old path\n"));
+               ret = -ENOMEM;
+               goto finished;
+       }
+       if (asprintf(&old_ppath, "%s/%s", remote, ppath) == -1) {
+               DEBUG(DEBUG_ERR,(__location__ " Failed to malloc old ppath\n"));
+               ret = -ENOMEM;
+               goto finished;
+       }
 
        DEBUG(DEBUG_DEBUG, (__location__ " CREATE %s mode %o\n", path, mode));
 
@@ -2109,24 +2302,42 @@ static int remote_cache_create(const char *path, mode_t mode, struct fuse_file_i
 #endif
 
 finished:
-       talloc_free(mem_ctx);
+       if (ppath != NULL) {
+               free(ppath);
+       }
+       if (old_ppath != NULL) {
+               free(old_ppath);
+       }
+       if (old_path != NULL) {
+               free(old_path);
+       }
+       if (new_path != NULL) {
+               free(new_path);
+       }
        return ret;
 }
 
 static int remote_cache_utime(const char *path, struct utimbuf *times)
 {
-       TALLOC_CTX *mem_ctx = talloc_new(NULL);
        char *new_path = NULL;
        char *old_path = NULL;
        int ret;
 
        if (!read_write) {
-               talloc_free(mem_ctx);
-               return -EROFS;
+               ret = -EROFS;
+               goto finished;
        }
 
-       new_path   = talloc_asprintf(mem_ctx, "%s/%s", cache, path);
-       old_path   = talloc_asprintf(mem_ctx, "%s/%s", remote, path);
+       if (asprintf(&new_path, "%s/%s", cache, path) == -1) {
+               DEBUG(DEBUG_ERR,(__location__ " Failed to malloc new path\n"));
+               ret = -ENOMEM;
+               goto finished;
+       }
+       if (asprintf(&old_path, "%s/%s", remote, path) == -1) {
+               DEBUG(DEBUG_ERR,(__location__ " Failed to malloc old path\n"));
+               ret = -ENOMEM;
+               goto finished;
+       }
 
 
        DEBUG(DEBUG_DEBUG, (__location__ " UTIME %s\n", path));
@@ -2154,13 +2365,17 @@ static int remote_cache_utime(const char *path, struct utimbuf *times)
 
 finished:
        switch_back_to_root();
-       talloc_free(mem_ctx);
+       if (old_path != NULL) {
+               free(old_path);
+       }
+       if (new_path != NULL) {
+               free(new_path);
+       }
        return ret;
 }
 
 static int remote_cache_write_internal(const char *path, const char *buf, size_t size, off_t offset, struct fuse_file_info *ffi)
 {
-       TALLOC_CTX *mem_ctx = talloc_new(NULL);
        char *new_path = NULL;
        char *old_path = NULL;
        int fd = -1;
@@ -2171,8 +2386,16 @@ static int remote_cache_write_internal(const char *path, const char *buf, size_t
        gid_t gid = fuse_get_context()->gid;
        struct group_list gl;
 
-       new_path   = talloc_asprintf(mem_ctx, "%s/%s", cache, path);
-       old_path   = talloc_asprintf(mem_ctx, "%s/%s", remote, path);
+       if (asprintf(&new_path, "%s/%s", cache, path) == -1) {
+               DEBUG(DEBUG_ERR,(__location__ " Failed to malloc new path\n"));
+               ret = -ENOMEM;
+               goto finished;
+       }
+       if (asprintf(&old_path, "%s/%s", remote, path) == -1) {
+               DEBUG(DEBUG_ERR,(__location__ " Failed to malloc old path\n"));
+               ret = -ENOMEM;
+               goto finished;
+       }
 
        ret = get_group_list(uid, &gl);
        if (ret != 0) {
@@ -2226,7 +2449,12 @@ finished:
        if (fd != -1) {
                close(fd);
        }
-       talloc_free(mem_ctx);
+       if (old_path != NULL) {
+               free(old_path);
+       }
+       if (new_path != NULL) {
+               free(new_path);
+       }
        return ret;
 }
 
@@ -2291,18 +2519,25 @@ finished:
 
 static int remote_cache_truncate(const char *path, off_t offset)
 {
-       TALLOC_CTX *mem_ctx = talloc_new(NULL);
        char *new_path = NULL;
        char *old_path = NULL;
        int ret;
 
        if (!read_write) {
-               talloc_free(mem_ctx);
-               return -EROFS;
+               ret = -EROFS;
+               goto finished;
        }
 
-       new_path   = talloc_asprintf(mem_ctx, "%s/%s", cache, path);
-       old_path   = talloc_asprintf(mem_ctx, "%s/%s", remote, path);
+       if (asprintf(&new_path, "%s/%s", cache, path) == -1) {
+               DEBUG(DEBUG_ERR,(__location__ " Failed to malloc new path\n"));
+               ret = -ENOMEM;
+               goto finished;
+       }
+       if (asprintf(&old_path, "%s/%s", remote, path) == -1) {
+               DEBUG(DEBUG_ERR,(__location__ " Failed to malloc old path\n"));
+               ret = -ENOMEM;
+               goto finished;
+       }
 
 
        DEBUG(DEBUG_DEBUG, (__location__ " TRUNCATE %s offset %d\n", path, (int)offset));
@@ -2330,24 +2565,36 @@ static int remote_cache_truncate(const char *path, off_t offset)
 
 finished:
        switch_back_to_root();
-       talloc_free(mem_ctx);
+       if (old_path != NULL) {
+               free(old_path);
+       }
+       if (new_path != NULL) {
+               free(new_path);
+       }
        return ret;
 }
 
 static int remote_cache_ftruncate(const char *path, off_t offset, struct fuse_file_info *fi)
 {
-       TALLOC_CTX *mem_ctx = talloc_new(NULL);
        char *new_path = NULL;
        char *old_path = NULL;
        int ret;
 
        if (!read_write) {
-               talloc_free(mem_ctx);
-               return -EROFS;
+               ret = -EROFS;
+               goto finished;
        }
 
-       new_path   = talloc_asprintf(mem_ctx, "%s/%s", cache, path);
-       old_path   = talloc_asprintf(mem_ctx, "%s/%s", remote, path);
+       if (asprintf(&new_path, "%s/%s", cache, path) == -1) {
+               DEBUG(DEBUG_ERR,(__location__ " Failed to malloc new path\n"));
+               ret = -ENOMEM;
+               goto finished;
+       }
+       if (asprintf(&old_path, "%s/%s", remote, path) == -1) {
+               DEBUG(DEBUG_ERR,(__location__ " Failed to malloc old path\n"));
+               ret = -ENOMEM;
+               goto finished;
+       }
 
 
        DEBUG(DEBUG_DEBUG, (__location__ " FTRUNCATE %s offset %d\n", path, (int)offset));
@@ -2375,13 +2622,17 @@ static int remote_cache_ftruncate(const char *path, off_t offset, struct fuse_fi
 
 finished:
        switch_back_to_root();
-       talloc_free(mem_ctx);
+       if (old_path != NULL) {
+               free(old_path);
+       }
+       if (new_path != NULL) {
+               free(new_path);
+       }
        return ret;
 }
 
 static int remote_cache_link(const char *path, const char *newpath)
 {
-       TALLOC_CTX *mem_ctx = talloc_new(NULL);
        char *new_path = NULL;
        char *old_path = NULL;
        char *new_newpath = NULL;
@@ -2389,15 +2640,30 @@ static int remote_cache_link(const char *path, const char *newpath)
        int ret;
 
        if (!read_write) {
-               talloc_free(mem_ctx);
-               return -EROFS;
+               ret = -EROFS;
+               goto finished;
        }
 
-       new_path   = talloc_asprintf(mem_ctx, "%s/%s", cache, path);
-       old_path   = talloc_asprintf(mem_ctx, "%s/%s", remote, path);
-       new_newpath= talloc_asprintf(mem_ctx, "%s/%s", cache, newpath);
-       old_newpath= talloc_asprintf(mem_ctx, "%s/%s", remote, newpath);
-
+       if (asprintf(&new_path, "%s/%s", cache, path) == -1) {
+               DEBUG(DEBUG_ERR,(__location__ " Failed to malloc new path\n"));
+               ret = -ENOMEM;
+               goto finished;
+       }
+       if (asprintf(&old_path, "%s/%s", remote, path) == -1) {
+               DEBUG(DEBUG_ERR,(__location__ " Failed to malloc old path\n"));
+               ret = -ENOMEM;
+               goto finished;
+       }
+       if (asprintf(&new_newpath, "%s/%s", cache, newpath) == -1) {
+               DEBUG(DEBUG_ERR,(__location__ " Failed to malloc new newpath\n"));
+               ret = -ENOMEM;
+               goto finished;
+       }
+       if (asprintf(&old_newpath, "%s/%s", remote, newpath) == -1) {
+               DEBUG(DEBUG_ERR,(__location__ " Failed to malloc old newpath\n"));
+               ret = -ENOMEM;
+               goto finished;
+       }
 
        DEBUG(DEBUG_DEBUG, (__location__ " LINK %s -> %s\n", path, newpath));
 
@@ -2423,25 +2689,43 @@ static int remote_cache_link(const char *path, const char *newpath)
 
 finished:
        switch_back_to_root();
-       talloc_free(mem_ctx);
+       if (old_path != NULL) {
+               free(old_path);
+       }
+       if (new_path != NULL) {
+               free(new_path);
+       }
+       if (old_newpath != NULL) {
+               free(old_newpath);
+       }
+       if (new_newpath != NULL) {
+               free(new_newpath);
+       }
        return ret;
 }
 
 
 static int remote_cache_symlink(const char *linkname, const char *path)
 {
-       TALLOC_CTX *mem_ctx = talloc_new(NULL);
-       char *new_newpath = NULL;
-       char *old_newpath = NULL;
+       char *new_path = NULL;
+       char *old_path = NULL;
        int ret;
 
        if (!read_write) {
-               talloc_free(mem_ctx);
-               return -EROFS;
+               ret = -EROFS;
+               goto finished;
        }
 
-       new_newpath= talloc_asprintf(mem_ctx, "%s/%s", cache, path);
-       old_newpath= talloc_asprintf(mem_ctx, "%s/%s", remote, path);
+       if (asprintf(&new_path, "%s/%s", cache, path) == -1) {
+               DEBUG(DEBUG_ERR,(__location__ " Failed to malloc new path\n"));
+               ret = -ENOMEM;
+               goto finished;
+       }
+       if (asprintf(&old_path, "%s/%s", remote, path) == -1) {
+               DEBUG(DEBUG_ERR,(__location__ " Failed to malloc old path\n"));
+               ret = -ENOMEM;
+               goto finished;
+       }
 
 
        DEBUG(DEBUG_DEBUG, (__location__ " SYMLINK %s -> %s\n", linkname, path));
@@ -2452,9 +2736,9 @@ static int remote_cache_symlink(const char *linkname, const char *path)
                goto finished;
        }
 
-       ret = symlink(linkname, old_newpath);
+       ret = symlink(linkname, old_path);
        if (ret != 0) {
-               DEBUG(DEBUG_DEBUG,(__location__ " SYMLINK %s -> %s failed with errno:%u\n", linkname, old_newpath, errno));
+               DEBUG(DEBUG_DEBUG,(__location__ " SYMLINK %s -> %s failed with errno:%u\n", linkname, old_path, errno));
                ret = -errno;
                goto finished;
        }
@@ -2463,18 +2747,22 @@ static int remote_cache_symlink(const char *linkname, const char *path)
           entries for this name
        */
        switch_back_to_root();
-       unlink_object(new_newpath);
-       unlink_parent_dir_cache(new_newpath);
+       unlink_object(new_path);
+       unlink_parent_dir_cache(new_path);
 
 finished:
        switch_back_to_root();
-       talloc_free(mem_ctx);
+       if (old_path != NULL) {
+               free(old_path);
+       }
+       if (new_path != NULL) {
+               free(new_path);
+       }
        return ret;
 }
 
 static int remote_cache_rename(const char *path, const char *newpath)
 {
-       TALLOC_CTX *mem_ctx = talloc_new(NULL);
        char *new_path = NULL;
        char *old_path = NULL;
        char *new_newpath = NULL;
@@ -2482,14 +2770,31 @@ static int remote_cache_rename(const char *path, const char *newpath)
        int ret;
 
        if (!read_write) {
-               talloc_free(mem_ctx);
-               return -EROFS;
+               ret = -EROFS;
+               goto finished;
+       }
+
+       if (asprintf(&new_path, "%s/%s", cache, path) == -1) {
+               DEBUG(DEBUG_ERR,(__location__ " Failed to malloc new path\n"));
+               ret = -ENOMEM;
+               goto finished;
+       }
+       if (asprintf(&old_path, "%s/%s", remote, path) == -1) {
+               DEBUG(DEBUG_ERR,(__location__ " Failed to malloc old path\n"));
+               ret = -ENOMEM;
+               goto finished;
+       }
+       if (asprintf(&new_newpath, "%s/%s", cache, newpath) == -1) {
+               DEBUG(DEBUG_ERR,(__location__ " Failed to malloc new newpath\n"));
+               ret = -ENOMEM;
+               goto finished;
+       }
+       if (asprintf(&old_newpath, "%s/%s", remote, newpath) == -1) {
+               DEBUG(DEBUG_ERR,(__location__ " Failed to malloc old newpath\n"));
+               ret = -ENOMEM;
+               goto finished;
        }
 
-       new_path   = talloc_asprintf(mem_ctx, "%s/%s", cache, path);
-       old_path   = talloc_asprintf(mem_ctx, "%s/%s", remote, path);
-       new_newpath= talloc_asprintf(mem_ctx, "%s/%s", cache, newpath);
-       old_newpath= talloc_asprintf(mem_ctx, "%s/%s", remote, newpath);
 
 
        DEBUG(DEBUG_DEBUG, (__location__ " RENAME %s -> %s\n", path, newpath));
@@ -2517,7 +2822,18 @@ static int remote_cache_rename(const char *path, const char *newpath)
 
 finished:
        switch_back_to_root();
-       talloc_free(mem_ctx);
+       if (old_path != NULL) {
+               free(old_path);
+       }
+       if (new_path != NULL) {
+               free(new_path);
+       }
+       if (old_newpath != NULL) {
+               free(old_newpath);
+       }
+       if (new_newpath != NULL) {
+               free(new_newpath);
+       }
        return ret;
 }