fs_open.c 9.4 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 46 47 48 49 50 51 52
/****************************************************************************
 * fs/vfs/fs_open.c
 *
 *   Copyright (C) 2007-2009, 2011-2012, 2016-2018 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 "errno.h"
#include "sys/types.h"
#include "fcntl.h"
#include "sched.h"
#include "assert.h"
#ifdef LOSCFG_FILE_MODE
#include "stdarg.h"
#endif
#include "stdlib.h"
#include "fs/fs.h"
W
wangchenyang 已提交
53
#include "fs/vnode.h"
W
wenjun 已提交
54 55
#include "driver/blockproxy.h"
#include "fs_other.h"
W
wangchenyang 已提交
56 57 58
#include "fs/vfs_util.h"
#include "fs/path_cache.h"
#include "unistd.h"
W
wenjun 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

/****************************************************************************
 * Public Functions
 ****************************************************************************/

static int oflag_convert_mode(int oflags)
{
  /* regular file operations */

  int acc_mode = 0;
  if ((oflags & O_ACCMODE) == O_RDONLY)
  acc_mode |= READ_OP;
  if (oflags & O_WRONLY)
  acc_mode |= WRITE_OP;
  if (oflags & O_RDWR)
  acc_mode |= READ_OP | WRITE_OP;

  /* Opens the file, if it is existing. If not, a new file is created. */

  if (oflags & O_CREAT)
  acc_mode |= WRITE_OP;

  /* Creates a new file. If the file is existing, it is truncated and overwritten. */

  if (oflags & O_TRUNC)
  acc_mode |= WRITE_OP;

  /* Creates a new file. The function fails if the file is already existing. */

  if (oflags & O_EXCL)
  acc_mode |= WRITE_OP;
  if (oflags & O_APPEND)
  acc_mode |= WRITE_OP;

  /* mark for executing operation */

  if (oflags & O_EXECVE)
  acc_mode |= EXEC_OP;
  return acc_mode;
}

int get_path_from_fd(int fd, char **path)
{
W
wangchenyang 已提交
102
  struct file *file     = NULL;
W
wenjun 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115
  char            *copypath = NULL;

  if (fd == AT_FDCWD)
    {
      return OK;
    }

  int ret = fs_getfilep(fd, &file);
  if (ret < 0)
    {
      return -ENOENT;
    }

W
wangchenyang 已提交
116
  if ((file == NULL) || (file->f_vnode == NULL) || (file->f_path == NULL))
W
wenjun 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
    {
      return -EBADF;
    }

  copypath = strdup((const char*)file->f_path);
  if (copypath == NULL)
    {
      return VFS_ERROR;
    }

  char *endptr = copypath + strlen(copypath)-1;//the ptr before '\0'

  /* strip out the file name, for example:/usr/lib/xx.so, final get /usr/lib/ */
  while (endptr > copypath)
    {
      if(*endptr == '/' && endptr > copypath)
        {
          *(endptr + 1) = '\0';
          *path = copypath;
          return OK;
        }

      endptr--;
    }

  free(copypath);
  return -ENOENT;
}

W
wangchenyang 已提交
146
static int do_creat(struct Vnode **node, char *fullpath, mode_t mode)
W
wenjun 已提交
147 148
{
  int ret;
W
wangchenyang 已提交
149 150
  struct Vnode *parentNode = *node;
  char *name = strrchr(fullpath, '/') + 1;
W
wenjun 已提交
151

W
wangchenyang 已提交
152
  if (parentNode->vop != NULL && parentNode->vop->Create != NULL)
W
wenjun 已提交
153
    {
W
wangchenyang 已提交
154
      ret = parentNode->vop->Create(parentNode, name, mode, node);
W
wenjun 已提交
155
    }
W
wangchenyang 已提交
156
  else
W
wenjun 已提交
157
    {
W
wangchenyang 已提交
158
      ret = -ENOSYS;
W
wenjun 已提交
159 160
    }

W
wangchenyang 已提交
161
  if (ret < 0)
W
wenjun 已提交
162
    {
W
wangchenyang 已提交
163
      return ret;
W
wenjun 已提交
164 165
    }

W
wangchenyang 已提交
166 167
  struct PathCache *dt = PathCacheAlloc(parentNode, *node, name, strlen(name));
  if (dt == NULL)
W
wenjun 已提交
168
    {
W
wangchenyang 已提交
169 170
      // alloc name cache failed is not a critical problem, let it go.
      PRINT_ERR("alloc path cache %s failed\n", name);
W
wenjun 已提交
171
    }
W
wangchenyang 已提交
172 173
  return OK;
}
W
wenjun 已提交
174

W
wangchenyang 已提交
175 176 177 178 179 180 181 182 183 184 185
int fp_open(char *fullpath, int oflags, mode_t mode)
{
  int ret;
  int fd;
  int accmode;
  struct file *filep = NULL;
  struct Vnode *vnode = NULL;

  VnodeHold();
  ret = VnodeLookup(fullpath, &vnode, 0);
  if (ret == OK)
W
wenjun 已提交
186
    {
W
wangchenyang 已提交
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
      /* if file exist */
      if (vnode->type == VNODE_TYPE_BCHR)
        {
          ret = -EINVAL;
          goto errout_without_count;
        }
      if (vnode->type == VNODE_TYPE_BLK) {
          fd = block_proxy(fullpath, oflags);
          VnodeDrop();
          if (fd < 0)
            {
              ret = fd;
              goto errout_without_count;
            }
         return fd;
      }
      if ((oflags & O_CREAT) && (oflags & O_EXCL))
        {
          ret = -EEXIST;
          VnodeDrop();
          goto errout_without_count;
        }
      if (vnode->type == VNODE_TYPE_DIR)
        {
          ret = -EISDIR;
          VnodeDrop();
          goto errout_without_count;
        }
      accmode = oflag_convert_mode(oflags);
      if (VfsVnodePermissionCheck(vnode, accmode))
        {
          ret = -EACCES;
          VnodeDrop();
          goto errout_without_count;
        }
W
wenjun 已提交
222 223
    }

W
wangchenyang 已提交
224
  if ((ret != OK) && (oflags & O_CREAT) && vnode)
W
wenjun 已提交
225
    {
W
wangchenyang 已提交
226 227
      /* if file not exist, but parent dir of the file is exist */
      if (VfsVnodePermissionCheck(vnode, WRITE_OP))
W
wenjun 已提交
228
        {
W
wangchenyang 已提交
229 230 231 232 233 234 235 236 237
          ret = -EACCES;
          VnodeDrop();
          goto errout_without_count;
        }
      ret = do_creat(&vnode, fullpath, mode);
      if (ret != OK)
        {
          VnodeDrop();
          goto errout_without_count;
W
wenjun 已提交
238 239 240
        }
    }

W
wangchenyang 已提交
241
  if (ret != OK)
W
wenjun 已提交
242
    {
W
wangchenyang 已提交
243 244 245
      /* found nothing */
      VnodeDrop();
      goto errout_without_count;
W
wenjun 已提交
246
    }
W
wangchenyang 已提交
247 248 249 250 251 252 253 254 255 256
  vnode->useCount++;
  VnodeDrop();

  if (oflags & O_TRUNC)
    {
      if (vnode->useCount > 1)
        {
          ret = -EBUSY;
          goto errout;
        }
W
wenjun 已提交
257

W
wangchenyang 已提交
258 259 260 261 262 263 264 265 266 267 268 269 270 271
      if (vnode->vop->Truncate)
        {
          ret = vnode->vop->Truncate(vnode, 0);
          if (ret != OK)
            {
              goto errout;
            }
        }
      else
        {
          ret = -ENOSYS;
          goto errout;
        }
    }
W
wenjun 已提交
272

W
wangchenyang 已提交
273
  fd = files_allocate(vnode, oflags, 0, NULL, 3); /* 3: file start fd */
W
wenjun 已提交
274 275
  if (fd < 0)
    {
W
wangchenyang 已提交
276 277
      ret = -EMFILE;
      goto errout;
W
wenjun 已提交
278 279 280 281 282 283
    }

  /* Get the file structure corresponding to the file descriptor. */
  ret = fs_getfilep(fd, &filep);
  if (ret < 0)
    {
W
wangchenyang 已提交
284 285 286
      files_release(fd);
      ret = -get_errno();
      goto errout;
W
wenjun 已提交
287 288
    }

W
wangchenyang 已提交
289 290 291
  filep->f_vnode = vnode;
  filep->ops = vnode->fop;
  filep->f_path = fullpath;
W
wenjun 已提交
292

W
wangchenyang 已提交
293
  if (filep->ops && filep->ops->open)
W
wenjun 已提交
294
    {
W
wangchenyang 已提交
295
      ret = filep->ops->open(filep);
W
wenjun 已提交
296 297 298 299
    }

  if (ret < 0)
    {
W
wangchenyang 已提交
300 301
      files_release(fd);
      goto errout;
W
wenjun 已提交
302 303 304 305
    }

  /* we do not bother to handle the NULL scenario, if so, page-cache feature will not be used
   * when we do the file fault */
Y
YOUR_NAME 已提交
306
#ifdef LOSCFG_KERNEL_VM
W
wenjun 已提交
307
  add_mapping(filep, fullpath);
Y
YOUR_NAME 已提交
308
#endif
W
wenjun 已提交
309 310 311 312

  return fd;

errout:
W
wangchenyang 已提交
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
  VnodeHold();
  vnode->useCount--;
  VnodeDrop();
errout_without_count:
  set_errno(-ret);
  return VFS_ERROR;
}

int do_open(int dirfd, const char *path, int oflags, mode_t mode)
{
  int ret;
  int fd;
  char *fullpath          = NULL;
  char *relativepath     = NULL;

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

  ret = vfs_normalize_path((const char *)relativepath, path, &fullpath);
  if (relativepath)
    {
      free(relativepath);
    }
  if (ret < 0)
    {
      goto errout;
    }

  fd = fp_open(fullpath, oflags, mode);
  if (fd < 0)
    {
      ret = -get_errno();
      goto errout;
    }

  return fd;

errout:
  if (fullpath)
    {
      free(fullpath);
    }
  set_errno(-ret);
W
wenjun 已提交
360 361 362 363 364 365 366 367 368 369 370 371
  return VFS_ERROR;
}

/****************************************************************************
 * Name: open
 *
 * Description: Standard 'open' interface
 *
 ****************************************************************************/

int open(const char *path, int oflags, ...)
{
W
wangchenyang 已提交
372
  mode_t mode = DEFAULT_FILE_MODE; /* File read-write properties. */
W
wenjun 已提交
373 374 375 376 377
#ifdef LOSCFG_FILE_MODE
  va_list ap;
  va_start(ap, oflags);
  mode = va_arg(ap, int);
  va_end(ap);
W
wangchenyang 已提交
378 379 380 381 382 383

  if ((oflags & (O_WRONLY | O_CREAT)) != 0)
    {
      mode &= ~GetUmask();
      mode &= (S_IRWXU|S_IRWXG|S_IRWXO);
    }
W
wenjun 已提交
384
#endif
W
wangchenyang 已提交
385

W
wenjun 已提交
386 387 388 389 390
  return do_open(AT_FDCWD, path, oflags, mode);
}

int open64 (const char *__path, int __oflag, ...)
{
W
wangchenyang 已提交
391
  mode_t mode = DEFAULT_FILE_MODE; /* File read-write properties. */
W
wenjun 已提交
392 393 394 395 396
#ifdef LOSCFG_FILE_MODE
  va_list ap;
  va_start(ap, __oflag);
  mode = va_arg(ap, int);
  va_end(ap);
W
wangchenyang 已提交
397 398 399 400 401
  if ((__oflag & (O_WRONLY | O_CREAT)) != 0)
    {
      mode &= ~GetUmask();
      mode &= (S_IRWXU|S_IRWXG|S_IRWXO);
    }
W
wenjun 已提交
402 403 404
#endif
  return open (__path, ((unsigned int)__oflag) | O_LARGEFILE, mode);
}