diff --git a/drivers/misc/CMakeLists.txt b/drivers/misc/CMakeLists.txt index ad85c35e9d343..42f197c3b85e2 100644 --- a/drivers/misc/CMakeLists.txt +++ b/drivers/misc/CMakeLists.txt @@ -65,9 +65,10 @@ if(CONFIG_BLK_RPMSG) endif() if(CONFIG_BLK_RPMSG_SERVER) - set_source_files_properties( - rpmsgblk_server.c DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/.. - PROPERTIES INCLUDE_DIRECTORIES ${NUTTX_DIR}/fs/inode) + target_include_directories(drivers PRIVATE ${NUTTX_DIR}/fs/inode + ${NUTTX_DIR}/fs) + set_source_files_properties(rpmsgblk_server.c DIRECTORY + ${CMAKE_CURRENT_LIST_DIR}/..) list(APPEND SRCS rpmsgblk_server.c) endif() diff --git a/drivers/misc/Make.defs b/drivers/misc/Make.defs index ce1f9c8398cba..f039f528ed431 100644 --- a/drivers/misc/Make.defs +++ b/drivers/misc/Make.defs @@ -70,6 +70,7 @@ endif ifeq ($(CONFIG_BLK_RPMSG_SERVER),y) CSRCS += rpmsgblk_server.c CFLAGS += ${INCDIR_PREFIX}$(TOPDIR)$(DELIM)fs$(DELIM)inode + CFLAGS += ${INCDIR_PREFIX}$(TOPDIR)$(DELIM)fs endif ifneq ($(CONFIG_DEV_OPTEE_NONE),y) diff --git a/fs/driver/fs_blockproxy.c b/fs/driver/fs_blockproxy.c index 29dcfa9531b0c..95dc376ce49ac 100644 --- a/fs/driver/fs_blockproxy.c +++ b/fs/driver/fs_blockproxy.c @@ -42,6 +42,7 @@ #include #include "driver.h" +#include "fs_heap.h" #if !defined(CONFIG_DISABLE_MOUNTPOINT) && \ !defined(CONFIG_DISABLE_PSEUDOFS_OPERATIONS) @@ -112,7 +113,7 @@ static FAR char *unique_chardev(void) if (ret < 0) { DEBUGASSERT(ret == -ENOENT); - return strdup(devbuf); + return fs_heap_strdup(devbuf); } /* It is in use, try again */ @@ -198,14 +199,14 @@ int block_proxy(FAR struct file *filep, FAR const char *blkdev, int oflags) /* Free the allocated character driver name. */ - lib_free(chardev); + fs_heap_free(chardev); return OK; errout_with_bchdev: nx_unlink(chardev); errout_with_chardev: - lib_free(chardev); + fs_heap_free(chardev); return ret; } diff --git a/fs/driver/fs_mtdproxy.c b/fs/driver/fs_mtdproxy.c index 72e83a7530f0e..c0ecb5a135b61 100644 --- a/fs/driver/fs_mtdproxy.c +++ b/fs/driver/fs_mtdproxy.c @@ -38,6 +38,7 @@ #include #include "driver/driver.h" +#include "fs_heap.h" /**************************************************************************** * Private Data @@ -105,7 +106,7 @@ static FAR char *unique_blkdev(void) if (ret < 0) { DEBUGASSERT(ret == -ENOENT); - return strdup(devbuf); + return fs_heap_strdup(devbuf); } /* It is in use, try again */ @@ -188,6 +189,6 @@ int mtd_proxy(FAR const char *mtddev, int mountflags, out_with_fltdev: nx_unlink(blkdev); out_with_blkdev: - lib_free(blkdev); + fs_heap_free(blkdev); return ret; } diff --git a/fs/fs_heap.c b/fs/fs_heap.c index 5a8dedef59e63..78b0bcece02a2 100644 --- a/fs/fs_heap.c +++ b/fs/fs_heap.c @@ -73,4 +73,46 @@ void fs_heap_free(FAR void *mem) mm_free(g_fs_heap, mem); } +FAR char *fs_heap_strdup(FAR const char *s) +{ + size_t len = strlen(s) + 1; + FAR char *copy = fs_heap_malloc(len); + if (copy != NULL) + { + memcpy(copy, s, len); + } + + return copy; +} + +int fs_heap_asprintf(FAR char **strp, FAR const char *fmt, ...) +{ + va_list ap; + int len; + + /* Calculates the length required to format the string */ + + va_start(ap, fmt); + len = vsnprintf(NULL, 0, fmt, ap); + va_end(ap); + + if (len < 0) + { + *strp = NULL; + return len; + } + + *strp = fs_heap_malloc(len + 1); + if (*strp == NULL) + { + return -ENOMEM; + } + + va_start(ap, fmt); + vsnprintf(*strp, len + 1, fmt, ap); + va_end(ap); + + return len; +} + #endif diff --git a/fs/fs_heap.h b/fs/fs_heap.h index af973a2ac69ad..11478f76253ed 100644 --- a/fs/fs_heap.h +++ b/fs/fs_heap.h @@ -27,6 +27,8 @@ #include #include +#include +#include #include /**************************************************************************** @@ -40,6 +42,8 @@ FAR void *fs_heap_malloc(size_t size); size_t fs_heap_malloc_size(FAR void *mem); FAR void *fs_heap_realloc(FAR void *oldmem, size_t size); void fs_heap_free(FAR void *mem); +FAR char *fs_heap_strdup(FAR const char *s); +int fs_heap_asprintf(FAR char **strp, FAR const char *fmt, ...); #else # define fs_heap_initialize() # define fs_heap_zalloc kmm_zalloc @@ -47,6 +51,8 @@ void fs_heap_free(FAR void *mem); # define fs_heap_malloc_size kmm_malloc_size # define fs_heap_realloc kmm_realloc # define fs_heap_free kmm_free +# define fs_heap_strdup nx_strdup +# define fs_heap_asprintf nx_asprintf #endif #endif diff --git a/fs/hostfs/hostfs.c b/fs/hostfs/hostfs.c index 2ded29abb2f1f..72f6cb2f9d45a 100644 --- a/fs/hostfs/hostfs.c +++ b/fs/hostfs/hostfs.c @@ -1052,7 +1052,7 @@ static int hostfs_bind(FAR struct inode *blkdriver, FAR const void *data, * "fs=whatever", remote dir */ - options = strdup(data); + options = fs_heap_strdup(data); if (!options) { fs_heap_free(fs); @@ -1070,7 +1070,7 @@ static int hostfs_bind(FAR struct inode *blkdriver, FAR const void *data, ptr = strtok_r(NULL, ",", &saveptr); } - lib_free(options); + fs_heap_free(options); /* Take the lock for the mount */ diff --git a/fs/inode/fs_inodesearch.c b/fs/inode/fs_inodesearch.c index 81578817b1440..baaf33af34c56 100644 --- a/fs/inode/fs_inodesearch.c +++ b/fs/inode/fs_inodesearch.c @@ -34,6 +34,7 @@ #include #include "inode/inode.h" +#include "fs_heap.h" /**************************************************************************** * Private Function Prototypes @@ -348,12 +349,12 @@ static int _inode_search(FAR struct inode_search_s *desc) { FAR char *buffer = NULL; - ret = asprintf(&buffer, - "%s/%s", desc->relpath, - name); + ret = fs_heap_asprintf(&buffer, "%s/%s", + desc->relpath, + name); if (ret > 0) { - lib_free(desc->buffer); + fs_heap_free(desc->buffer); desc->buffer = buffer; relpath = buffer; ret = OK; @@ -478,7 +479,8 @@ int inode_search(FAR struct inode_search_s *desc) if (*desc->path != '/') { - ret = asprintf(&desc->buffer, "%s/%s", _inode_getcwd(), desc->path); + ret = fs_heap_asprintf(&desc->buffer, "%s/%s", + _inode_getcwd(), desc->path); if (ret < 0) { return -ENOMEM; diff --git a/fs/inode/inode.h b/fs/inode/inode.h index d9a4b6b20525e..ad0c2b0bdfe2c 100644 --- a/fs/inode/inode.h +++ b/fs/inode/inode.h @@ -39,6 +39,8 @@ #include #include +#include "fs_heap.h" + /**************************************************************************** * Pre-processor Definitions ****************************************************************************/ @@ -61,7 +63,7 @@ { \ if ((d)->buffer != NULL) \ { \ - lib_free((d)->buffer); \ + fs_heap_free((d)->buffer); \ (d)->buffer = NULL; \ } \ } \ diff --git a/fs/notify/inotify.c b/fs/notify/inotify.c index 5b47bc54e8493..e6e93da1f9098 100644 --- a/fs/notify/inotify.c +++ b/fs/notify/inotify.c @@ -746,7 +746,7 @@ inotify_alloc_watch_list(FAR const char *path) } list_initialize(&list->watches); - list->path = strdup(path); + list->path = fs_heap_strdup(path); if (list->path == NULL) { fs_heap_free(list); @@ -757,7 +757,7 @@ inotify_alloc_watch_list(FAR const char *path) item.data = list; if (hsearch_r(item, ENTER, &result, &g_inotify.hash) == 0) { - lib_free(list->path); + fs_heap_free(list->path); fs_heap_free(list); return NULL; } @@ -1022,7 +1022,7 @@ static void notify_free_entry(FAR ENTRY *entry) { /* Key is alloced by lib_malloc, value is alloced by fs_heap_malloc */ - lib_free(entry->key); + fs_heap_free(entry->key); fs_heap_free(entry->data); } @@ -1150,7 +1150,7 @@ int inotify_add_watch(int fd, FAR const char *pathname, uint32_t mask) out_free: fs_putfilep(filep); - lib_free(abspath); + fs_heap_free(abspath); if (ret < 0) { set_errno(-ret); diff --git a/fs/nxffs/nxffs_inode.c b/fs/nxffs/nxffs_inode.c index 32e79f4f31190..b7e9c5e0bb585 100644 --- a/fs/nxffs/nxffs_inode.c +++ b/fs/nxffs/nxffs_inode.c @@ -213,7 +213,7 @@ void nxffs_freeentry(FAR struct nxffs_entry_s *entry) { if (entry->name) { - lib_free(entry->name); + fs_heap_free(entry->name); entry->name = NULL; } } diff --git a/fs/nxffs/nxffs_open.c b/fs/nxffs/nxffs_open.c index 36058bdfb71c4..5e01ed2696fe6 100644 --- a/fs/nxffs/nxffs_open.c +++ b/fs/nxffs/nxffs_open.c @@ -508,7 +508,7 @@ static inline int nxffs_wropen(FAR struct nxffs_volume_s *volume, /* Save a copy of the inode name. */ - wrfile->ofile.entry.name = strdup(name); + wrfile->ofile.entry.name = fs_heap_strdup(name); if (!wrfile->ofile.entry.name) { ret = -ENOMEM; @@ -655,7 +655,7 @@ static inline int nxffs_wropen(FAR struct nxffs_volume_s *volume, return OK; errout_with_name: - lib_free(wrfile->ofile.entry.name); + fs_heap_free(wrfile->ofile.entry.name); errout_with_ofile: #ifndef CONFIG_NXFFS_PREALLOCATED fs_heap_free(wrfile); diff --git a/fs/nxffs/nxffs_pack.c b/fs/nxffs/nxffs_pack.c index 8a3af047791fe..4e8d952139f2d 100644 --- a/fs/nxffs/nxffs_pack.c +++ b/fs/nxffs/nxffs_pack.c @@ -34,6 +34,7 @@ #include #include "nxffs.h" +#include "fs_heap.h" /**************************************************************************** * Private Types @@ -1089,7 +1090,7 @@ nxffs_setupwriter(FAR struct nxffs_volume_s *volume, /* Initialize for the packing operation. */ memset(&pack->dest, 0, sizeof(struct nxffs_packstream_s)); - pack->dest.entry.name = strdup(wrfile->ofile.entry.name); + pack->dest.entry.name = fs_heap_strdup(wrfile->ofile.entry.name); pack->dest.entry.utc = wrfile->ofile.entry.utc; pack->dest.entry.datlen = wrfile->ofile.entry.datlen; diff --git a/fs/rpmsgfs/rpmsgfs.c b/fs/rpmsgfs/rpmsgfs.c index b79776227c285..decd14a3edb06 100644 --- a/fs/rpmsgfs/rpmsgfs.c +++ b/fs/rpmsgfs/rpmsgfs.c @@ -1075,7 +1075,7 @@ static int rpmsgfs_bind(FAR struct inode *blkdriver, FAR const void *data, * "timeout=xx", connect timeout, unit (ms) */ - options = strdup(data); + options = fs_heap_strdup(data); if (!options) { fs_heap_free(fs); @@ -1106,7 +1106,7 @@ static int rpmsgfs_bind(FAR struct inode *blkdriver, FAR const void *data, } ret = rpmsgfs_client_bind(&fs->handle, cpuname); - lib_free(options); + fs_heap_free(options); if (ret < 0) { fs_heap_free(fs); diff --git a/fs/tmpfs/fs_tmpfs.c b/fs/tmpfs/fs_tmpfs.c index ce635d1efb975..286bd2137241b 100644 --- a/fs/tmpfs/fs_tmpfs.c +++ b/fs/tmpfs/fs_tmpfs.c @@ -471,7 +471,7 @@ static int tmpfs_remove_dirent(FAR struct tmpfs_directory_s *tdo, if (tdo->tdo_entry[index].tde_name != NULL) { - lib_free(tdo->tdo_entry[index].tde_name); + fs_heap_free(tdo->tdo_entry[index].tde_name); } /* Remove by replacing this entry with the final directory entry */ @@ -1216,7 +1216,7 @@ static int tmpfs_free_callout(FAR struct tmpfs_directory_s *tdo, if (tdo->tdo_entry[index].tde_name != NULL) { - lib_free(tdo->tdo_entry[index].tde_name); + fs_heap_free(tdo->tdo_entry[index].tde_name); } /* Remove by replacing this entry with the final directory entry */ diff --git a/fs/unionfs/fs_unionfs.c b/fs/unionfs/fs_unionfs.c index 9a54cfde0332a..ff29050e462ab 100644 --- a/fs/unionfs/fs_unionfs.c +++ b/fs/unionfs/fs_unionfs.c @@ -729,11 +729,11 @@ static FAR char *unionfs_relpath(FAR const char *path, FAR const char *name) if (path[pathlen - 1] == '/') { - ret = asprintf(&relpath, "%s%s", path, name); + ret = fs_heap_asprintf(&relpath, "%s%s", path, name); } else { - ret = asprintf(&relpath, "%s/%s", path, name); + ret = fs_heap_asprintf(&relpath, "%s/%s", path, name); } /* Handle errors */ @@ -753,7 +753,7 @@ static FAR char *unionfs_relpath(FAR const char *path, FAR const char *name) * will work later). */ - return strdup(name); + return fs_heap_strdup(name); } } @@ -840,12 +840,12 @@ static void unionfs_destroy(FAR struct unionfs_inode_s *ui) if (ui->ui_fs[0].um_prefix) { - lib_free(ui->ui_fs[0].um_prefix); + fs_heap_free(ui->ui_fs[0].um_prefix); } if (ui->ui_fs[1].um_prefix) { - lib_free(ui->ui_fs[1].um_prefix); + fs_heap_free(ui->ui_fs[1].um_prefix); } /* And finally free the allocated unionfs state structure as well */ @@ -1436,7 +1436,7 @@ static int unionfs_opendir(FAR struct inode *mountpt, if (strlen(relpath) > 0) { - udir->fu_relpath = strdup(relpath); + udir->fu_relpath = fs_heap_strdup(relpath); if (!udir->fu_relpath) { goto errout_with_lock; @@ -1522,7 +1522,7 @@ static int unionfs_opendir(FAR struct inode *mountpt, errout_with_relpath: if (udir->fu_relpath != NULL) { - lib_free(udir->fu_relpath); + fs_heap_free(udir->fu_relpath); } errout_with_lock: @@ -1776,7 +1776,7 @@ static int unionfs_readdir(FAR struct inode *mountpt, /* Free the allocated relpath */ - lib_free(relpath); + fs_heap_free(relpath); /* Check for a duplicate */ @@ -1863,7 +1863,7 @@ static int unionfs_readdir(FAR struct inode *mountpt, /* Free the allocated relpath */ - lib_free(relpath); + fs_heap_free(relpath); } } } @@ -1946,7 +1946,7 @@ static int unionfs_bind(FAR struct inode *blkdriver, FAR const void *data, /* Parse options from mount syscall */ - dup = tmp = strdup(data); + dup = tmp = fs_heap_strdup(data); if (!dup) { return -ENOMEM; @@ -1975,7 +1975,7 @@ static int unionfs_bind(FAR struct inode *blkdriver, FAR const void *data, /* Call unionfs_dobind to do the real work. */ ret = unionfs_dobind(fspath1, prefix1, fspath2, prefix2, handle); - lib_free(dup); + fs_heap_free(dup); return ret; } @@ -2625,10 +2625,10 @@ static int unionfs_dobind(FAR const char *fspath1, FAR const char *prefix1, if (prefix1 && strlen(prefix1) > 0) { - ui->ui_fs[0].um_prefix = strdup(prefix1); + ui->ui_fs[0].um_prefix = fs_heap_strdup(prefix1); if (ui->ui_fs[0].um_prefix == NULL) { - ferr("ERROR: strdup(prefix1) failed\n"); + ferr("ERROR: fs_heap_strdup(prefix1) failed\n"); ret = -ENOMEM; goto errout_with_fs2; } @@ -2636,10 +2636,10 @@ static int unionfs_dobind(FAR const char *fspath1, FAR const char *prefix1, if (prefix2 && strlen(prefix2) > 0) { - ui->ui_fs[1].um_prefix = strdup(prefix2); + ui->ui_fs[1].um_prefix = fs_heap_strdup(prefix2); if (ui->ui_fs[1].um_prefix == NULL) { - ferr("ERROR: strdup(prefix2) failed\n"); + ferr("ERROR: fs_heap_strdup(prefix2) failed\n"); ret = -ENOMEM; goto errout_with_prefix1; } @@ -2661,7 +2661,7 @@ static int unionfs_dobind(FAR const char *fspath1, FAR const char *prefix1, errout_with_prefix1: if (ui->ui_fs[0].um_prefix != NULL) { - lib_free(ui->ui_fs[0].um_prefix); + fs_heap_free(ui->ui_fs[0].um_prefix); } errout_with_fs2: diff --git a/fs/vfs/fs_dir.c b/fs/vfs/fs_dir.c index 5c23e4d60b122..00b62ff985df9 100644 --- a/fs/vfs/fs_dir.c +++ b/fs/vfs/fs_dir.c @@ -454,7 +454,7 @@ static int dir_close(FAR struct file *filep) /* Release our references on the contained 'root' inode */ inode_release(inode); - lib_free(relpath); + fs_heap_free(relpath); return ret; } @@ -608,7 +608,7 @@ int dir_allocate(FAR struct file *filep, FAR const char *relpath) } inode_getpath(inode, path_prefix, sizeof(path_prefix)); - ret = asprintf(&dir->fd_path, "%s%s/", path_prefix, relpath); + ret = fs_heap_asprintf(&dir->fd_path, "%s%s/", path_prefix, relpath); if (ret < 0) { dir->fd_path = NULL; diff --git a/fs/vfs/fs_lock.c b/fs/vfs/fs_lock.c index 4f01f16430e6f..a2193d13c7f56 100644 --- a/fs/vfs/fs_lock.c +++ b/fs/vfs/fs_lock.c @@ -300,7 +300,7 @@ file_lock_find_bucket(FAR const char *filepath) static void file_lock_free_entry(FAR ENTRY *entry) { - lib_free(entry->key); + fs_heap_free(entry->key); fs_heap_free(entry->data); } @@ -323,7 +323,7 @@ file_lock_create_bucket(FAR const char *filepath) /* Creating an instance store */ - item.key = strdup(filepath); + item.key = fs_heap_strdup(filepath); if (item.key == NULL) { fs_heap_free(bucket); @@ -334,7 +334,7 @@ file_lock_create_bucket(FAR const char *filepath) if (hsearch_r(item, ENTER, &hretvalue, &g_file_lock_table) == 0) { - lib_free(item.key); + fs_heap_free(item.key); fs_heap_free(bucket); return NULL; } diff --git a/fs/vfs/fs_rename.c b/fs/vfs/fs_rename.c index 737d15517923d..1bcaa7a7df2b8 100644 --- a/fs/vfs/fs_rename.c +++ b/fs/vfs/fs_rename.c @@ -124,7 +124,7 @@ static int pseudorename(FAR const char *oldpath, FAR struct inode *oldinode, if (subdir != NULL) { - lib_free(subdir); + fs_heap_free(subdir); subdir = NULL; } @@ -134,7 +134,7 @@ static int pseudorename(FAR const char *oldpath, FAR struct inode *oldinode, */ subdirname = basename((FAR char *)oldpath); - ret = asprintf(&subdir, "%s/%s", newpath, subdirname); + ret = fs_heap_asprintf(&subdir, "%s/%s", newpath, subdirname); if (ret < 0) { subdir = NULL; @@ -256,7 +256,7 @@ static int pseudorename(FAR const char *oldpath, FAR struct inode *oldinode, RELEASE_SEARCH(&newdesc); if (subdir != NULL) { - lib_free(subdir); + fs_heap_free(subdir); } return ret; @@ -388,7 +388,7 @@ static int mountptrename(FAR const char *oldpath, FAR struct inode *oldinode, FAR void *tmp = subdir; - ret = asprintf(&subdir, "%s/%s", newrelpath, + ret = fs_heap_asprintf(&subdir, "%s/%s", newrelpath, subdirname); if (tmp != NULL) { @@ -479,7 +479,7 @@ static int mountptrename(FAR const char *oldpath, FAR struct inode *oldinode, RELEASE_SEARCH(&newdesc); if (subdir != NULL) { - lib_free(subdir); + fs_heap_free(subdir); } return ret; diff --git a/fs/vfs/fs_symlink.c b/fs/vfs/fs_symlink.c index e0b21b3819b58..e7f8bb7c420d9 100644 --- a/fs/vfs/fs_symlink.c +++ b/fs/vfs/fs_symlink.c @@ -37,6 +37,7 @@ #include "notify/notify.h" #include "inode/inode.h" +#include "fs_heap.h" /**************************************************************************** * Pre-processor Definitions @@ -129,7 +130,7 @@ int symlink(FAR const char *path1, FAR const char *path2) { /* Copy path1 */ - FAR char *newpath2 = strdup(path1); + FAR char *newpath2 = fs_heap_strdup(path1); if (newpath2 == NULL) { errcode = ENOMEM; @@ -146,7 +147,7 @@ int symlink(FAR const char *path1, FAR const char *path2) inode_unlock(); if (ret < 0) { - lib_free(newpath2); + fs_heap_free(newpath2); errcode = -ret; goto errout_with_search; }