fs_unlink.c 5.3 KB
Newer Older
W
wenjun 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
/****************************************************************************
 * fs/vfs/fs_unlink.c
 *
 *   Copyright (C) 2007-2009, 2017 Gregory Nutt. All rights reserved.
 *   Author: Gregory Nutt <gnutt@nuttx.org>
 *
 * 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 "vfs_config.h"

#include "unistd.h"
#include "errno.h"
#include "fcntl.h"

M
mucor 已提交
46
#include "vnode.h"
W
wenjun 已提交
47
#include "stdlib.h"
48
#include "fs/mount.h"
W
wenjun 已提交
49 50

/****************************************************************************
W
wangchenyang 已提交
51
 * Private Functions
W
wenjun 已提交
52
 ****************************************************************************/
W
wangchenyang 已提交
53 54 55 56 57 58
static int check_target(struct Vnode *vnode)
{
  if (vnode->type == VNODE_TYPE_DIR)
    {
      return -EISDIR;
    }
W
wenjun 已提交
59

60 61 62 63 64
  if ((vnode->originMount) && (vnode->originMount->mountFlags & MS_RDONLY))
    {
      return -EROFS;
    }

W
wangchenyang 已提交
65 66 67 68
  if (vnode->useCount > 0)
    {
      return -EBUSY;
    }
W
wenjun 已提交
69

M
mucor 已提交
70
  if (VfsVnodePermissionCheck(vnode->parent, (WRITE_OP | EXEC_OP)))
W
wangchenyang 已提交
71 72 73 74 75
    {
      return -EACCES;
    }
  return OK;
}
W
wenjun 已提交
76 77 78 79 80 81 82 83 84 85 86
/****************************************************************************
 * Public Functions
 ****************************************************************************/

/****************************************************************************
 * Name: do_unlink
 *
 * Description:  Remove a file managed a mountpoint
 *
 ****************************************************************************/

W
wangchenyang 已提交
87
int do_unlink(int dirfd, const char *pathname)
W
wenjun 已提交
88
{
W
wangchenyang 已提交
89 90 91 92 93
  struct Vnode *vnode = NULL;
  int ret;
  char *name = NULL;
  char *fullpath = NULL;
  char *relativepath = NULL;
W
wenjun 已提交
94 95 96 97 98 99 100 101

  /* Get relative path by dirfd*/
  ret = get_path_from_fd(dirfd, &relativepath);
  if (ret < 0)
    {
      goto errout;
    }

W
wangchenyang 已提交
102
  ret = vfs_normalize_path((const char *)relativepath, pathname, &fullpath);
W
wenjun 已提交
103 104 105 106 107
  if (relativepath)
    {
      free(relativepath);
    }

W
wangchenyang 已提交
108
  if (ret < 0)
W
wenjun 已提交
109 110 111 112
    {
      goto errout;
    }

W
wangchenyang 已提交
113 114
  VnodeHold();
  ret = VnodeLookup(fullpath, &vnode, 0);
W
wenjun 已提交
115 116
  if (ret < 0)
    {
W
wangchenyang 已提交
117
      goto errout_with_lock;
W
wenjun 已提交
118 119
    }

W
wangchenyang 已提交
120 121
  ret = check_target(vnode);
  if (ret < 0)
W
wenjun 已提交
122
    {
W
wangchenyang 已提交
123
      goto errout_with_lock;
W
wenjun 已提交
124
    }
W
wangchenyang 已提交
125
  name = strrchr(fullpath, '/') + 1;
W
wenjun 已提交
126

W
wangchenyang 已提交
127
  if (vnode && vnode->vop && vnode->vop->Unlink)
W
wenjun 已提交
128
    {
W
wangchenyang 已提交
129
      ret = vnode->vop->Unlink(vnode->parent, vnode, name);
W
wenjun 已提交
130
    }
W
wangchenyang 已提交
131
  else if (vnode && vnode->fop && vnode->fop->unlink)
W
wenjun 已提交
132
    {
W
wangchenyang 已提交
133
      ret = vnode->fop->unlink(vnode);
M
mucor 已提交
134 135 136
      if (ret == OK) {
        goto done;
      }
W
wenjun 已提交
137 138 139
    }
  else
    {
W
wangchenyang 已提交
140
      ret = -ENOSYS;
W
wenjun 已提交
141 142
    }

W
wangchenyang 已提交
143 144 145 146
  if (ret != OK)
    {
      goto errout_with_lock;
    }
W
wenjun 已提交
147

W
wangchenyang 已提交
148
  VnodeFree(vnode);
M
mucor 已提交
149 150

done:
W
wangchenyang 已提交
151
  VnodeDrop();
Y
YOUR_NAME 已提交
152
#ifdef LOSCFG_KERNEL_VM
W
wangchenyang 已提交
153
  (void)remove_mapping(fullpath);
Y
YOUR_NAME 已提交
154
#endif
W
wenjun 已提交
155 156 157 158 159
  /* Successfully unlinked */

  free(fullpath);
  return OK;

W
wangchenyang 已提交
160 161
errout_with_lock:
  VnodeDrop();
W
wenjun 已提交
162 163

errout:
W
wangchenyang 已提交
164 165 166 167 168 169
  if (fullpath)
    {
      free(fullpath);
    }
  set_errno(-ret);

W
wenjun 已提交
170 171 172 173 174 175 176 177 178 179
  return VFS_ERROR;
}

/****************************************************************************
 * Name: unlink
 *
 * Description:  Remove a file managed a mountpoint
 *
 ****************************************************************************/

W
wangchenyang 已提交
180
int unlink(const char *pathname)
W
wenjun 已提交
181 182 183 184 185 186 187 188 189 190
{
  return do_unlink(AT_FDCWD, pathname);
}

/****************************************************************************
 * Name: unlinkat
 *
 * Description:  Remove a file managed a mountpoint by dirfd
 *
 ****************************************************************************/
W
wangchenyang 已提交
191
extern int do_rmdir(int dirfd, const char *pathname);
W
wenjun 已提交
192

W
wangchenyang 已提交
193
int unlinkat(int dirfd, const char *pathname, int flag)
W
wenjun 已提交
194
{
195 196 197 198
  if (pathname == NULL)
  {
    return -EINVAL;
  }
W
wenjun 已提交
199 200
  /* Now flag only support 0 && AT_REMOVEDIR */
  if ((flag & ~AT_REMOVEDIR) != 0)
201
    return -EINVAL;
W
wenjun 已提交
202 203 204 205 206 207

  if (flag & AT_REMOVEDIR)
    return do_rmdir(dirfd, pathname);

  return do_unlink(dirfd, pathname);
}