From c43b3e5a349adff40c660ea78f5e856e6ba5d9ec Mon Sep 17 00:00:00 2001
From: Xiang Xiao <xiaoxiang@xiaomi.com>
Date: Wed, 22 Aug 2018 06:08:34 -0600
Subject: [PATCH] fs/hostfs:  Add ftruncate support.

---
 arch/sim/src/up_hostfs.c |  9 ++++++++
 fs/hostfs/hostfs.c       | 44 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 53 insertions(+)

diff --git a/arch/sim/src/up_hostfs.c b/arch/sim/src/up_hostfs.c
index 839d951708..df3a03da03 100644
--- a/arch/sim/src/up_hostfs.c
+++ b/arch/sim/src/up_hostfs.c
@@ -250,6 +250,15 @@ int host_fstat(int fd, struct nuttx_stat_s *buf)
   return ret;
 }
 
+/****************************************************************************
+ * Name: host_truncate
+ ****************************************************************************/
+
+int host_ftruncate(int fd, off_t length)
+{
+  return ftruncate(fd, length);
+}
+
 /****************************************************************************
  * Name: host_opendir
  ****************************************************************************/
diff --git a/fs/hostfs/hostfs.c b/fs/hostfs/hostfs.c
index 715d4ad325..25534941c0 100644
--- a/fs/hostfs/hostfs.c
+++ b/fs/hostfs/hostfs.c
@@ -82,6 +82,8 @@ static int     hostfs_dup(FAR const struct file *oldp,
                         FAR struct file *newp);
 static int     hostfs_fstat(FAR const struct file *filep,
                         FAR struct stat *buf);
+static int     hostfs_ftruncate(FAR struct file *filep,
+                        off_t length);
 
 static int     hostfs_opendir(FAR struct inode *mountpt,
                         FAR const char *relpath,
@@ -139,6 +141,7 @@ const struct mountpt_operations hostfs_operations =
   hostfs_sync,          /* sync */
   hostfs_dup,           /* dup */
   hostfs_fstat,         /* fstat */
+  hostfs_ftruncate,     /* ftruncate */
 
   hostfs_opendir,       /* opendir */
   hostfs_closedir,      /* closedir */
@@ -722,6 +725,47 @@ static int hostfs_fstat(FAR const struct file *filep, FAR struct stat *buf)
   return ret;
 }
 
+/****************************************************************************
+ * Name: hostfs_ftruncate
+ *
+ * Description:
+ *   Set the length of the open, regular file associated with the file
+ *   structure 'filep' to 'length'.
+ *
+ ****************************************************************************/
+
+static int hostfs_ftruncate(FAR struct file *filep, off_t length)
+{
+  FAR struct inode *inode;
+  FAR struct hostfs_mountpt_s *fs;
+  FAR struct hostfs_ofile_s *hf;
+  int ret = OK;
+
+  /* Sanity checks */
+
+  DEBUGASSERT(filep != NULL);
+
+  /* Recover our private data from the struct file instance */
+
+  DEBUGASSERT(filep->f_priv != NULL && filep->f_inode != NULL);
+  hf    = filep->f_priv;
+  inode = filep->f_inode;
+
+  fs    = inode->i_private;
+  DEBUGASSERT(fs != NULL);
+
+  /* Take the semaphore */
+
+  hostfs_semtake(fs);
+
+  /* Call the host to perform the truncate */
+
+  ret = host_ftruncate(hf->fd, length);
+
+  hostfs_semgive(fs);
+  return ret;
+}
+
 /****************************************************************************
  * Name: hostfs_opendir
  *