123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347 |
- /****************************************************************************
- * nuttx/fs/hostfs/hostfs.c
- *
- * Copyright (C) 2015 Ken Pettit. All rights reserved.
- * Author: Ken Pettit <pettitkd@gmail.com>
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- * 3. Neither the name NuttX nor the names of its contributors may be
- * used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
- * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- *
- ****************************************************************************/
- /****************************************************************************
- * Included Files
- ****************************************************************************/
- #include <nuttx/config.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <sys/statfs.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #include <assert.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <debug.h>
- #include <nuttx/kmalloc.h>
- #include <nuttx/fs/fs.h>
- #include <nuttx/fs/fat.h>
- #include <nuttx/fs/dirent.h>
- #include <nuttx/fs/ioctl.h>
- #include <nuttx/fs/hostfs.h>
- #include "hostfs.h"
- /****************************************************************************
- * Pre-processor Definitions
- ****************************************************************************/
- #define HOSTFS_RETRY_DELAY_MS 10
- /****************************************************************************
- * Private Function Prototypes
- ****************************************************************************/
- static int hostfs_open(FAR struct file *filep, FAR const char *relpath,
- int oflags, mode_t mode);
- static int hostfs_close(FAR struct file *filep);
- static ssize_t hostfs_read(FAR struct file *filep, FAR char *buffer,
- size_t buflen);
- static ssize_t hostfs_write(FAR struct file *filep, FAR const char *buffer,
- size_t buflen);
- static off_t hostfs_seek(FAR struct file *filep, off_t offset,
- int whence);
- static int hostfs_ioctl(FAR struct file *filep, int cmd,
- unsigned long arg);
- static int hostfs_sync(FAR struct file *filep);
- 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,
- FAR struct fs_dirent_s *dir);
- static int hostfs_closedir(FAR struct inode *mountpt,
- FAR struct fs_dirent_s *dir);
- static int hostfs_readdir(FAR struct inode *mountpt,
- FAR struct fs_dirent_s *dir);
- static int hostfs_rewinddir(FAR struct inode *mountpt,
- FAR struct fs_dirent_s *dir);
- static int hostfs_bind(FAR struct inode *blkdriver,
- FAR const void *data, FAR void **handle);
- static int hostfs_unbind(FAR void *handle, FAR struct inode **blkdriver,
- unsigned int flags);
- static int hostfs_statfs(FAR struct inode *mountpt,
- FAR struct statfs *buf);
- static int hostfs_unlink(FAR struct inode *mountpt,
- FAR const char *relpath);
- static int hostfs_mkdir(FAR struct inode *mountpt,
- FAR const char *relpath, mode_t mode);
- static int hostfs_rmdir(FAR struct inode *mountpt, const char *relpath);
- static int hostfs_rename(FAR struct inode *mountpt,
- FAR const char *oldrelpath,
- FAR const char *newrelpath);
- static int hostfs_stat(FAR struct inode *mountpt,
- FAR const char *relpath, FAR struct stat *buf);
- /****************************************************************************
- * Private Data
- ****************************************************************************/
- static uint8_t g_seminitialized = FALSE;
- static sem_t g_sem;
- /****************************************************************************
- * Public Data
- ****************************************************************************/
- /* See fs_mount.c -- this structure is explicitly externed there.
- * We use the old-fashioned kind of initializers so that this will compile
- * with any compiler.
- */
- const struct mountpt_operations hostfs_operations =
- {
- hostfs_open, /* open */
- hostfs_close, /* close */
- hostfs_read, /* read */
- hostfs_write, /* write */
- hostfs_seek, /* seek */
- hostfs_ioctl, /* ioctl */
- hostfs_sync, /* sync */
- hostfs_dup, /* dup */
- hostfs_fstat, /* fstat */
- hostfs_ftruncate, /* ftruncate */
- hostfs_opendir, /* opendir */
- hostfs_closedir, /* closedir */
- hostfs_readdir, /* readdir */
- hostfs_rewinddir, /* rewinddir */
- hostfs_bind, /* bind */
- hostfs_unbind, /* unbind */
- hostfs_statfs, /* statfs */
- hostfs_unlink, /* unlinke */
- hostfs_mkdir, /* mkdir */
- hostfs_rmdir, /* rmdir */
- hostfs_rename, /* rename */
- hostfs_stat /* stat */
- };
- /****************************************************************************
- * Private Functions
- ****************************************************************************/
- /****************************************************************************
- * Name: hostfs_semtake
- ****************************************************************************/
- int hostfs_semtake(FAR struct hostfs_mountpt_s *fs)
- {
- return nxsem_wait_uninterruptible(fs->fs_sem);
- }
- /****************************************************************************
- * Name: hostfs_semgive
- ****************************************************************************/
- void hostfs_semgive(FAR struct hostfs_mountpt_s *fs)
- {
- nxsem_post(fs->fs_sem);
- }
- /****************************************************************************
- * Name: hostfs_mkpath
- *
- * Description: Build absolute host path from relative NuttX path.
- *
- ****************************************************************************/
- static void hostfs_mkpath(FAR struct hostfs_mountpt_s *fs,
- FAR const char *relpath,
- FAR char *path, int pathlen)
- {
- int depth = 0;
- int first;
- int x;
- /* Copy base host path to output */
- strncpy(path, fs->fs_root, pathlen);
- /* Be sure we aren't trying to use ".." to display outside of our
- * mounted path.
- */
- x = 0;
- while (relpath[x] == '/')
- {
- x++;
- }
- first = x;
- while (relpath[x] != '\0')
- {
- /* Test for ".." occurrence */
- if (strncmp(&relpath[x], "..", 2) == 0)
- {
- /* Reduce depth by 1 */
- depth--;
- x += 2;
- }
- else if (relpath[x] == '/' && relpath[x + 1] != '/' &&
- relpath[x + 1] != '\0')
- {
- depth++;
- x++;
- }
- else
- {
- x++;
- }
- }
- if (depth >= 0)
- {
- strncat(path, &relpath[first], pathlen - strlen(path) - 1);
- }
- }
- /****************************************************************************
- * Name: hostfs_open
- ****************************************************************************/
- static int hostfs_open(FAR struct file *filep, FAR const char *relpath,
- int oflags, mode_t mode)
- {
- FAR struct inode *inode;
- FAR struct hostfs_mountpt_s *fs;
- FAR struct hostfs_ofile_s *hf;
- char path[HOSTFS_MAX_PATH];
- int ret;
- /* Sanity checks */
- DEBUGASSERT((filep->f_priv == NULL) && (filep->f_inode != NULL));
- /* Get the mountpoint inode reference from the file structure and the
- * mountpoint private data from the inode structure
- */
- inode = filep->f_inode;
- fs = inode->i_private;
- DEBUGASSERT(fs != NULL);
- /* Take the semaphore */
- ret = hostfs_semtake(fs);
- if (ret < 0)
- {
- return ret;
- }
- /* Allocate memory for the open file */
- hf = (struct hostfs_ofile_s *) kmm_malloc(sizeof *hf);
- if (hf == NULL)
- {
- ret = -ENOMEM;
- goto errout_with_semaphore;
- }
- /* Append to the host's root directory */
- hostfs_mkpath(fs, relpath, path, sizeof(path));
- /* Try to open the file in the host file system */
- hf->fd = host_open(path, oflags, mode);
- if (hf->fd < 0)
- {
- /* Error opening file */
- ret = -EBADF;
- goto errout_with_buffer;
- }
- /* In write/append mode, we need to set the file pointer to the end of the
- * file.
- */
- if ((oflags & (O_APPEND | O_WRONLY)) == (O_APPEND | O_WRONLY))
- {
- ret = host_lseek(hf->fd, 0, SEEK_END);
- if (ret >= 0)
- {
- filep->f_pos = ret;
- }
- else
- {
- goto errout_with_buffer;
- }
- }
- /* Attach the private date to the struct file instance */
- filep->f_priv = hf;
- /* Then insert the new instance into the mountpoint structure.
- * It needs to be there (1) to handle error conditions that effect
- * all files, and (2) to inform the umount logic that we are busy
- * (but a simple reference count could have done that).
- */
- hf->fnext = fs->fs_head;
- hf->crefs = 1;
- hf->oflags = oflags;
- fs->fs_head = hf;
- ret = OK;
- goto errout_with_semaphore;
- errout_with_buffer:
- kmm_free(hf);
- errout_with_semaphore:
- hostfs_semgive(fs);
- if (ret == -EINVAL)
- {
- ret = -EIO;
- }
- return ret;
- }
- /****************************************************************************
- * Name: hostfs_close
- ****************************************************************************/
- static int hostfs_close(FAR struct file *filep)
- {
- FAR struct inode *inode;
- FAR struct hostfs_mountpt_s *fs;
- FAR struct hostfs_ofile_s *hf;
- FAR struct hostfs_ofile_s *nextfile;
- FAR struct hostfs_ofile_s *prevfile;
- int ret;
- /* Sanity checks */
- DEBUGASSERT(filep->f_priv != NULL && filep->f_inode != NULL);
- /* Recover our private data from the struct file instance */
- inode = filep->f_inode;
- fs = inode->i_private;
- hf = filep->f_priv;
- /* Take the semaphore */
- ret = hostfs_semtake(fs);
- if (ret < 0)
- {
- return ret;
- }
- /* Check if we are the last one with a reference to the file and
- * only close if we are.
- */
- if (hf->crefs > 1)
- {
- /* The file is opened more than once. Just decrement the
- * reference count and return.
- */
- hf->crefs--;
- goto okout;
- }
- /* Remove ourselves from the linked list */
- nextfile = fs->fs_head;
- prevfile = nextfile;
- while ((nextfile != hf) && (nextfile != NULL))
- {
- /* Save the previous file pointer too */
- prevfile = nextfile;
- nextfile = nextfile->fnext;
- }
- if (nextfile != NULL)
- {
- /* Test if we were the first entry */
- if (nextfile == fs->fs_head)
- {
- /* Assign a new head */
- fs->fs_head = nextfile->fnext;
- }
- else
- {
- /* Take ourselves out of the list */
- prevfile->fnext = nextfile->fnext;
- }
- }
- /* Close the host file */
- host_close(hf->fd);
- /* Now free the pointer */
- filep->f_priv = NULL;
- kmm_free(hf);
- okout:
- hostfs_semgive(fs);
- return OK;
- }
- /****************************************************************************
- * Name: hostfs_read
- ****************************************************************************/
- static ssize_t hostfs_read(FAR struct file *filep, FAR char *buffer,
- size_t buflen)
- {
- FAR struct inode *inode;
- FAR struct hostfs_mountpt_s *fs;
- FAR struct hostfs_ofile_s *hf;
- ssize_t ret;
- /* Sanity checks */
- DEBUGASSERT(filep->f_priv != NULL && filep->f_inode != NULL);
- /* Recover our private data from the struct file instance */
- hf = filep->f_priv;
- inode = filep->f_inode;
- fs = inode->i_private;
- DEBUGASSERT(fs != NULL);
- /* Take the semaphore */
- ret = hostfs_semtake(fs);
- if (ret < 0)
- {
- return ret;
- }
- /* Call the host to perform the read */
- ret = host_read(hf->fd, buffer, buflen);
- if (ret > 0)
- {
- filep->f_pos += ret;
- }
- hostfs_semgive(fs);
- return ret;
- }
- /****************************************************************************
- * Name: hostfs_write
- ****************************************************************************/
- static ssize_t hostfs_write(FAR struct file *filep, const char *buffer,
- size_t buflen)
- {
- FAR struct inode *inode;
- FAR struct hostfs_mountpt_s *fs;
- FAR struct hostfs_ofile_s *hf;
- ssize_t ret;
- /* Sanity checks. I have seen the following assertion misfire if
- * CONFIG_DEBUG_MM is enabled while re-directing output to a
- * file. In this case, the debug output can get generated while
- * the file is being opened, FAT data structures are being allocated,
- * and things are generally in a perverse state.
- */
- #ifdef CONFIG_DEBUG_MM
- if (filep->f_priv == NULL || filep->f_inode == NULL)
- {
- return -ENXIO;
- }
- #else
- DEBUGASSERT(filep->f_priv != NULL && filep->f_inode != NULL);
- #endif
- /* Recover our private data from the struct file instance */
- hf = filep->f_priv;
- inode = filep->f_inode;
- fs = inode->i_private;
- DEBUGASSERT(fs != NULL);
- /* Take the semaphore */
- ret = hostfs_semtake(fs);
- if (ret < 0)
- {
- return ret;
- }
- /* Test the permissions. Only allow write if the file was opened with
- * write flags.
- */
- if ((hf->oflags & O_WROK) == 0)
- {
- ret = -EACCES;
- goto errout_with_semaphore;
- }
- /* Call the host to perform the write */
- ret = host_write(hf->fd, buffer, buflen);
- if (ret > 0)
- {
- filep->f_pos += ret;
- }
- errout_with_semaphore:
- hostfs_semgive(fs);
- return ret;
- }
- /****************************************************************************
- * Name: hostfs_seek
- ****************************************************************************/
- static off_t hostfs_seek(FAR struct file *filep, off_t offset, int whence)
- {
- FAR struct inode *inode;
- FAR struct hostfs_mountpt_s *fs;
- FAR struct hostfs_ofile_s *hf;
- off_t ret;
- /* Sanity checks */
- DEBUGASSERT(filep->f_priv != NULL && filep->f_inode != NULL);
- /* Recover our private data from the struct file instance */
- hf = filep->f_priv;
- inode = filep->f_inode;
- fs = inode->i_private;
- DEBUGASSERT(fs != NULL);
- /* Take the semaphore */
- ret = hostfs_semtake(fs);
- if (ret < 0)
- {
- return ret;
- }
- /* Call our internal routine to perform the seek */
- ret = host_lseek(hf->fd, offset, whence);
- if (ret >= 0)
- {
- filep->f_pos = ret;
- }
- hostfs_semgive(fs);
- return ret;
- }
- /****************************************************************************
- * Name: hostfs_ioctl
- ****************************************************************************/
- static int hostfs_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
- {
- FAR struct inode *inode;
- FAR struct hostfs_mountpt_s *fs;
- FAR struct hostfs_ofile_s *hf;
- int ret;
- /* Sanity checks */
- DEBUGASSERT(filep->f_priv != NULL && filep->f_inode != NULL);
- /* Recover our private data from the struct file instance */
- hf = filep->f_priv;
- inode = filep->f_inode;
- fs = inode->i_private;
- DEBUGASSERT(fs != NULL);
- /* Take the semaphore */
- ret = hostfs_semtake(fs);
- if (ret < 0)
- {
- return ret;
- }
- /* Call our internal routine to perform the ioctl */
- ret = host_ioctl(hf->fd, cmd, arg);
- hostfs_semgive(fs);
- return ret;
- }
- /****************************************************************************
- * Name: hostfs_sync
- *
- * Description: Synchronize the file state on disk to match internal, in-
- * memory state.
- *
- ****************************************************************************/
- static int hostfs_sync(FAR struct file *filep)
- {
- FAR struct inode *inode;
- FAR struct hostfs_mountpt_s *fs;
- FAR struct hostfs_ofile_s *hf;
- int ret;
- /* Sanity checks */
- DEBUGASSERT(filep->f_priv != NULL && filep->f_inode != NULL);
- /* Recover our private data from the struct file instance */
- hf = filep->f_priv;
- inode = filep->f_inode;
- fs = inode->i_private;
- DEBUGASSERT(fs != NULL);
- /* Take the semaphore */
- ret = hostfs_semtake(fs);
- if (ret < 0)
- {
- return ret;
- }
- host_sync(hf->fd);
- hostfs_semgive(fs);
- return OK;
- }
- /****************************************************************************
- * Name: hostfs_dup
- *
- * Description: Duplicate open file data in the new file structure.
- *
- ****************************************************************************/
- static int hostfs_dup(FAR const struct file *oldp, FAR struct file *newp)
- {
- FAR struct hostfs_ofile_s *sf;
- /* Sanity checks */
- DEBUGASSERT(oldp->f_priv != NULL &&
- newp->f_priv == NULL &&
- newp->f_inode != NULL);
- /* Recover our private data from the struct file instance */
- sf = oldp->f_priv;
- DEBUGASSERT(sf != NULL);
- /* Just increment the reference count on the ofile */
- sf->crefs++;
- newp->f_priv = (FAR void *)sf;
- return OK;
- }
- /****************************************************************************
- * Name: hostfs_fstat
- *
- * Description:
- * Obtain information about an open file associated with the file
- * descriptor 'fd', and will write it to the area pointed to by 'buf'.
- *
- ****************************************************************************/
- static int hostfs_fstat(FAR const struct file *filep, FAR struct stat *buf)
- {
- FAR struct inode *inode;
- FAR struct hostfs_mountpt_s *fs;
- FAR struct hostfs_ofile_s *hf;
- int ret = OK;
- /* Sanity checks */
- DEBUGASSERT(filep != NULL && buf != 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 */
- ret = hostfs_semtake(fs);
- if (ret < 0)
- {
- return ret;
- }
- /* Call the host to perform the read */
- ret = host_fstat(hf->fd, buf);
- hostfs_semgive(fs);
- 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 */
- ret = hostfs_semtake(fs);
- if (ret < 0)
- {
- return ret;
- }
- /* Call the host to perform the truncate */
- ret = host_ftruncate(hf->fd, length);
- hostfs_semgive(fs);
- return ret;
- }
- /****************************************************************************
- * Name: hostfs_opendir
- *
- * Description: Open a directory for read access
- *
- ****************************************************************************/
- static int hostfs_opendir(FAR struct inode *mountpt, FAR const char *relpath,
- FAR struct fs_dirent_s *dir)
- {
- FAR struct hostfs_mountpt_s *fs;
- char path[HOSTFS_MAX_PATH];
- int ret;
- /* Sanity checks */
- DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL);
- /* Recover our private data from the inode instance */
- fs = mountpt->i_private;
- /* Take the semaphore */
- ret = hostfs_semtake(fs);
- if (ret < 0)
- {
- return ret;
- }
- /* Append to the host's root directory */
- hostfs_mkpath(fs, relpath, path, sizeof(path));
- /* Call the host's opendir function */
- dir->u.hostfs.fs_dir = host_opendir(path);
- if (dir->u.hostfs.fs_dir == NULL)
- {
- ret = -ENOENT;
- goto errout_with_semaphore;
- }
- ret = OK;
- errout_with_semaphore:
- hostfs_semgive(fs);
- return ret;
- }
- /****************************************************************************
- * Name: hostfs_closedir
- *
- * Description: Open a directory for read access
- *
- ****************************************************************************/
- static int hostfs_closedir(FAR struct inode *mountpt,
- FAR struct fs_dirent_s *dir)
- {
- struct hostfs_mountpt_s *fs;
- int ret;
- /* Sanity checks */
- DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL);
- /* Recover our private data from the inode instance */
- fs = mountpt->i_private;
- /* Take the semaphore */
- ret = hostfs_semtake(fs);
- if (ret < 0)
- {
- return ret;
- }
- /* Call the host's closedir function */
- host_closedir(dir->u.hostfs.fs_dir);
- hostfs_semgive(fs);
- return OK;
- }
- /****************************************************************************
- * Name: hostfs_readdir
- *
- * Description: Read the next directory entry
- *
- ****************************************************************************/
- static int hostfs_readdir(FAR struct inode *mountpt,
- FAR struct fs_dirent_s *dir)
- {
- FAR struct hostfs_mountpt_s *fs;
- int ret;
- /* Sanity checks */
- DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL);
- /* Recover our private data from the inode instance */
- fs = mountpt->i_private;
- /* Take the semaphore */
- ret = hostfs_semtake(fs);
- if (ret < 0)
- {
- return ret;
- }
- /* Call the host OS's readdir function */
- ret = host_readdir(dir->u.hostfs.fs_dir, &dir->fd_dir);
- hostfs_semgive(fs);
- return ret;
- }
- /****************************************************************************
- * Name: hostfs_rewindir
- *
- * Description: Reset directory read to the first entry
- *
- ****************************************************************************/
- static int hostfs_rewinddir(FAR struct inode *mountpt,
- FAR struct fs_dirent_s *dir)
- {
- /* Sanity checks */
- DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL);
- /* Call the host and let it do all the work */
- host_rewinddir(dir->u.hostfs.fs_dir);
- return OK;
- }
- /****************************************************************************
- * Name: hostfs_bind
- *
- * Description: This implements a portion of the mount operation. This
- * function allocates and initializes the mountpoint private data and
- * binds the blockdriver inode to the filesystem private data. The final
- * binding of the private data (containing the blockdriver) to the
- * mountpoint is performed by mount().
- *
- ****************************************************************************/
- static int hostfs_bind(FAR struct inode *blkdriver, FAR const void *data,
- FAR void **handle)
- {
- FAR struct hostfs_mountpt_s *fs;
- FAR char *options;
- char *ptr, *saveptr;
- int len;
- int ret;
- /* Validate the block driver is NULL */
- if (blkdriver || !data)
- {
- return -ENODEV;
- }
- /* Create an instance of the mountpt state structure */
- fs = (FAR struct hostfs_mountpt_s *)
- kmm_zalloc(sizeof(struct hostfs_mountpt_s));
- if (fs == NULL)
- {
- return -ENOMEM;
- }
- /* The options we support are:
- * "fs=whatever", remote dir
- */
- options = strdup(data);
- if (!options)
- {
- kmm_free(fs);
- return -ENOMEM;
- }
- ptr = strtok_r(options, ",", &saveptr);
- while (ptr != NULL)
- {
- if ((strncmp(ptr, "fs=", 3) == 0))
- {
- strncpy(fs->fs_root, &ptr[3], sizeof(fs->fs_root));
- }
- ptr = strtok_r(NULL, ",", &saveptr);
- }
- kmm_free(options);
- /* If the global semaphore hasn't been initialized, then
- * initialized it now.
- */
- fs->fs_sem = &g_sem;
- if (!g_seminitialized)
- {
- /* Initialize the semaphore that controls access */
- nxsem_init(&g_sem, 0, 0);
- g_seminitialized = TRUE;
- }
- else
- {
- /* Take the semaphore for the mount */
- ret = hostfs_semtake(fs);
- if (ret < 0)
- {
- kmm_free(fs);
- return ret;
- }
- }
- /* Initialize the allocated mountpt state structure. The filesystem is
- * responsible for one reference ont the blkdriver inode and does not
- * have to addref() here (but does have to release in ubind().
- */
- fs->fs_head = NULL;
- /* Now perform the mount. */
- len = strlen(fs->fs_root);
- if (len > 1 && fs->fs_root[len - 1] == '/')
- {
- /* Remove trailing '/' */
- fs->fs_root[len - 1] = '\0';
- }
- /* Append a '/' to the name now */
- if (fs->fs_root[len - 1] != '/')
- {
- strcat(fs->fs_root, "/");
- }
- *handle = (FAR void *)fs;
- hostfs_semgive(fs);
- return OK;
- }
- /****************************************************************************
- * Name: hostfs_unbind
- *
- * Description: This implements the filesystem portion of the umount
- * operation.
- *
- ****************************************************************************/
- static int hostfs_unbind(FAR void *handle, FAR struct inode **blkdriver,
- unsigned int flags)
- {
- FAR struct hostfs_mountpt_s *fs = (FAR struct hostfs_mountpt_s *)handle;
- int ret;
- if (!fs)
- {
- return -EINVAL;
- }
- /* Check if there are sill any files opened on the filesystem. */
- ret = hostfs_semtake(fs);
- if (ret < 0)
- {
- return ret;
- }
- if (fs->fs_head != NULL)
- {
- /* We cannot unmount now.. there are open files */
- hostfs_semgive(fs);
- /* This implementation currently only supports unmounting if there are
- * no open file references.
- */
- return (flags != 0) ? -ENOSYS : -EBUSY;
- }
- hostfs_semgive(fs);
- kmm_free(fs);
- return ret;
- }
- /****************************************************************************
- * Name: hostfs_statfs
- *
- * Description: Return filesystem statistics
- *
- ****************************************************************************/
- static int hostfs_statfs(FAR struct inode *mountpt, FAR struct statfs *buf)
- {
- FAR struct hostfs_mountpt_s *fs;
- int ret;
- /* Sanity checks */
- DEBUGASSERT(mountpt && mountpt->i_private);
- /* Get the mountpoint private data from the inode structure */
- fs = mountpt->i_private;
- ret = hostfs_semtake(fs);
- if (ret < 0)
- {
- return ret;
- }
- /* Call the host fs to perform the statfs */
- memset(buf, 0, sizeof(struct statfs));
- ret = host_statfs(fs->fs_root, buf);
- buf->f_type = HOSTFS_MAGIC;
- hostfs_semgive(fs);
- return ret;
- }
- /****************************************************************************
- * Name: hostfs_unlink
- *
- * Description: Remove a file
- *
- ****************************************************************************/
- static int hostfs_unlink(FAR struct inode *mountpt, FAR const char *relpath)
- {
- FAR struct hostfs_mountpt_s *fs;
- char path[HOSTFS_MAX_PATH];
- int ret;
- /* Sanity checks */
- DEBUGASSERT(mountpt && mountpt->i_private);
- /* Get the mountpoint private data from the inode structure */
- fs = mountpt->i_private;
- ret = hostfs_semtake(fs);
- if (ret < 0)
- {
- return ret;
- }
- /* Append to the host's root directory */
- hostfs_mkpath(fs, relpath, path, sizeof(path));
- /* Call the host fs to perform the unlink */
- ret = host_unlink(path);
- hostfs_semgive(fs);
- return ret;
- }
- /****************************************************************************
- * Name: hostfs_mkdir
- *
- * Description: Create a directory
- *
- ****************************************************************************/
- static int hostfs_mkdir(FAR struct inode *mountpt, FAR const char *relpath,
- mode_t mode)
- {
- FAR struct hostfs_mountpt_s *fs;
- char path[HOSTFS_MAX_PATH];
- int ret;
- /* Sanity checks */
- DEBUGASSERT(mountpt && mountpt->i_private);
- /* Get the mountpoint private data from the inode structure */
- fs = mountpt->i_private;
- ret = hostfs_semtake(fs);
- if (ret < 0)
- {
- return ret;
- }
- /* Append to the host's root directory */
- hostfs_mkpath(fs, relpath, path, sizeof(path));
- /* Call the host FS to do the mkdir */
- ret = host_mkdir(path, mode);
- hostfs_semgive(fs);
- return ret;
- }
- /****************************************************************************
- * Name: hostfs_rmdir
- *
- * Description: Remove a directory
- *
- ****************************************************************************/
- int hostfs_rmdir(FAR struct inode *mountpt, FAR const char *relpath)
- {
- FAR struct hostfs_mountpt_s *fs;
- char path[HOSTFS_MAX_PATH];
- int ret;
- /* Sanity checks */
- DEBUGASSERT(mountpt && mountpt->i_private);
- /* Get the mountpoint private data from the inode structure */
- fs = mountpt->i_private;
- /* Take the semaphore */
- ret = hostfs_semtake(fs);
- if (ret < 0)
- {
- return ret;
- }
- /* Append to the host's root directory */
- hostfs_mkpath(fs, relpath, path, sizeof(path));
- /* Call the host FS to do the mkdir */
- ret = host_rmdir(path);
- hostfs_semgive(fs);
- return ret;
- }
- /****************************************************************************
- * Name: hostfs_rename
- *
- * Description: Rename a file or directory
- *
- ****************************************************************************/
- int hostfs_rename(FAR struct inode *mountpt, FAR const char *oldrelpath,
- FAR const char *newrelpath)
- {
- FAR struct hostfs_mountpt_s *fs;
- char oldpath[HOSTFS_MAX_PATH];
- char newpath[HOSTFS_MAX_PATH];
- int ret;
- /* Sanity checks */
- DEBUGASSERT(mountpt && mountpt->i_private);
- /* Get the mountpoint private data from the inode structure */
- fs = mountpt->i_private;
- ret = hostfs_semtake(fs);
- if (ret < 0)
- {
- return ret;
- }
- /* Append to the host's root directory */
- strncpy(oldpath, fs->fs_root, sizeof(oldpath));
- strncat(oldpath, oldrelpath, sizeof(oldpath)-strlen(oldpath)-1);
- strncpy(newpath, fs->fs_root, sizeof(newpath));
- strncat(newpath, newrelpath, sizeof(newpath)-strlen(newpath)-1);
- /* Call the host FS to do the mkdir */
- ret = host_rename(oldpath, newpath);
- hostfs_semgive(fs);
- return ret;
- }
- /****************************************************************************
- * Name: hostfs_stat
- *
- * Description: Return information about a file or directory
- *
- ****************************************************************************/
- static int hostfs_stat(FAR struct inode *mountpt, FAR const char *relpath,
- FAR struct stat *buf)
- {
- FAR struct hostfs_mountpt_s *fs;
- char path[HOSTFS_MAX_PATH];
- int ret;
- /* Sanity checks */
- DEBUGASSERT(mountpt && mountpt->i_private);
- /* Get the mountpoint private data from the inode structure */
- fs = mountpt->i_private;
- ret = hostfs_semtake(fs);
- if (ret < 0)
- {
- return ret;
- }
- /* Append to the host's root directory */
- hostfs_mkpath(fs, relpath, path, sizeof(path));
- /* Call the host FS to do the stat operation */
- ret = host_stat(path, buf);
- hostfs_semgive(fs);
- return ret;
- }
- /****************************************************************************
- * Public Functions
- ****************************************************************************/
|