13d8536b4Sopenharmony_ci/*
23d8536b4Sopenharmony_ci * Copyright (c) 2022-2022 Huawei Device Co., Ltd. All rights reserved.
33d8536b4Sopenharmony_ci *
43d8536b4Sopenharmony_ci * Redistribution and use in source and binary forms, with or without modification,
53d8536b4Sopenharmony_ci * are permitted provided that the following conditions are met:
63d8536b4Sopenharmony_ci *
73d8536b4Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright notice, this list of
83d8536b4Sopenharmony_ci *    conditions and the following disclaimer.
93d8536b4Sopenharmony_ci *
103d8536b4Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright notice, this list
113d8536b4Sopenharmony_ci *    of conditions and the following disclaimer in the documentation and/or other materials
123d8536b4Sopenharmony_ci *    provided with the distribution.
133d8536b4Sopenharmony_ci *
143d8536b4Sopenharmony_ci * 3. Neither the name of the copyright holder nor the names of its contributors may be used
153d8536b4Sopenharmony_ci *    to endorse or promote products derived from this software without specific prior written
163d8536b4Sopenharmony_ci *    permission.
173d8536b4Sopenharmony_ci *
183d8536b4Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
193d8536b4Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
203d8536b4Sopenharmony_ci * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
213d8536b4Sopenharmony_ci * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
223d8536b4Sopenharmony_ci * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
233d8536b4Sopenharmony_ci * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
243d8536b4Sopenharmony_ci * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
253d8536b4Sopenharmony_ci * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
263d8536b4Sopenharmony_ci * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
273d8536b4Sopenharmony_ci * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
283d8536b4Sopenharmony_ci * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
293d8536b4Sopenharmony_ci */
303d8536b4Sopenharmony_ci#include "vfs_files.h"
313d8536b4Sopenharmony_ci#include "errno.h"
323d8536b4Sopenharmony_ci#include "stdio.h"
333d8536b4Sopenharmony_ci#include "stdlib.h"
343d8536b4Sopenharmony_ci#include "string.h"
353d8536b4Sopenharmony_ci#include "los_config.h"
363d8536b4Sopenharmony_ci#include "vfs_config.h"
373d8536b4Sopenharmony_ci#include "vfs_mount.h"
383d8536b4Sopenharmony_ci#include "vfs_operations.h"
393d8536b4Sopenharmony_ci
403d8536b4Sopenharmony_cistatic struct File g_files[NR_OPEN_DEFAULT];
413d8536b4Sopenharmony_ci
423d8536b4Sopenharmony_ciint FileToFd(const struct File *file)
433d8536b4Sopenharmony_ci{
443d8536b4Sopenharmony_ci    if (file == NULL) {
453d8536b4Sopenharmony_ci        return (int)LOS_NOK;
463d8536b4Sopenharmony_ci    }
473d8536b4Sopenharmony_ci    return file - g_files + MIN_START_FD;
483d8536b4Sopenharmony_ci}
493d8536b4Sopenharmony_ci
503d8536b4Sopenharmony_cistruct File *FdToFile(int fd)
513d8536b4Sopenharmony_ci{
523d8536b4Sopenharmony_ci    if ((fd < MIN_START_FD) || (fd >= CONFIG_NFILE_DESCRIPTORS)) {
533d8536b4Sopenharmony_ci        return NULL;
543d8536b4Sopenharmony_ci    }
553d8536b4Sopenharmony_ci    return &g_files[fd - MIN_START_FD];
563d8536b4Sopenharmony_ci}
573d8536b4Sopenharmony_ci
583d8536b4Sopenharmony_cistruct File *VfsFileGet(void)
593d8536b4Sopenharmony_ci{
603d8536b4Sopenharmony_ci    int i;
613d8536b4Sopenharmony_ci    /* protected by g_fsMutex */
623d8536b4Sopenharmony_ci    for (i = 0; i < NR_OPEN_DEFAULT; i++) {
633d8536b4Sopenharmony_ci        if (g_files[i].fStatus == FILE_STATUS_NOT_USED) {
643d8536b4Sopenharmony_ci            g_files[i].fStatus = FILE_STATUS_INITING;
653d8536b4Sopenharmony_ci            return &g_files[i];
663d8536b4Sopenharmony_ci        }
673d8536b4Sopenharmony_ci    }
683d8536b4Sopenharmony_ci
693d8536b4Sopenharmony_ci    return NULL;
703d8536b4Sopenharmony_ci}
713d8536b4Sopenharmony_ci
723d8536b4Sopenharmony_cistruct File *VfsFileGetSpec(int fd)
733d8536b4Sopenharmony_ci{
743d8536b4Sopenharmony_ci    if ((fd < MIN_START_FD) || (fd >= CONFIG_NFILE_DESCRIPTORS)) {
753d8536b4Sopenharmony_ci        return NULL;
763d8536b4Sopenharmony_ci    }
773d8536b4Sopenharmony_ci    if (g_files[fd - MIN_START_FD].fStatus == FILE_STATUS_NOT_USED) {
783d8536b4Sopenharmony_ci        g_files[fd - MIN_START_FD].fStatus = FILE_STATUS_INITING;
793d8536b4Sopenharmony_ci        return &g_files[fd - MIN_START_FD];
803d8536b4Sopenharmony_ci    }
813d8536b4Sopenharmony_ci
823d8536b4Sopenharmony_ci    return NULL;
833d8536b4Sopenharmony_ci}
843d8536b4Sopenharmony_ci
853d8536b4Sopenharmony_civoid VfsFilePut(struct File *file)
863d8536b4Sopenharmony_ci{
873d8536b4Sopenharmony_ci    if (file == NULL) {
883d8536b4Sopenharmony_ci        return;
893d8536b4Sopenharmony_ci    }
903d8536b4Sopenharmony_ci    file->fFlags = 0;
913d8536b4Sopenharmony_ci    file->fFops = NULL;
923d8536b4Sopenharmony_ci    file->fData = NULL;
933d8536b4Sopenharmony_ci    file->fMp = NULL;
943d8536b4Sopenharmony_ci    file->fOffset = 0;
953d8536b4Sopenharmony_ci    file->fOwner = -1;
963d8536b4Sopenharmony_ci    file->fullPath = NULL;
973d8536b4Sopenharmony_ci    file->fStatus = FILE_STATUS_NOT_USED;
983d8536b4Sopenharmony_ci}
99