📄 fileio.c
字号:
#ifdef DEBUG if ( error < E_OK ) { DEBUG_PRINT(("_u_close err=%#x\n", error)); }#endif return error;}/* * File pointer transfer */LOCAL WER _u_lseek( FS *fs, CmdPkt *pkt ){ SF_LSEEK_PARA *para = pkt->cmd.para; FDPkt fdp; ER err; /* Search of file descriptor */ fdp.fd = searchFileDescriptor(pkt->uxinfo, para->fildes); if ( (fdp.fd == NULL) || (fdp.fd->fs != fs) ) { err = E_FD; goto err_ret; } fdp.fdno = para->fildes; /* File pointer transfer */ if ( fs == (FS*)&STD_FS ) { err = sf_lseek(&fdp, para->offset, para->whence); } else if ( fs == (FS*)&ROOT_FS ) { err = rt_lseek(&fdp, para->offset, para->whence); } else if ( fs == (FS*)&STDIO_FS ) { err = std_lseek(&fdp, para->offset, para->whence); } else { err = (*fs->u_lseek)(&fdp, para->offset, para->whence); } if ( err < E_OK ) { goto err_ret; } return err; /* new position */err_ret: DEBUG_PRINT(("_u_lseek err=%#x\n", err)); return err;}/* * File reading */LOCAL WER _u_read( FS *fs, CmdPkt *pkt ){ SF_READ_PARA *para = pkt->cmd.para; FD *fd; FDPkt fdp; ER err; /* Parameter check */ if ( (W)para->nbyte < 0 ) { err = E_PAR; goto err_ret; } err = CheckSpaceRW(para->buf, para->nbyte); if ( err < E_OK ) { goto err_ret; } /* Search of file descriptor */ fd = searchFileDescriptor(pkt->uxinfo, para->fildes); if (( fd == NULL )||( fd->fs != fs )) { err = E_FD; goto err_ret; } fdp.fd = fd; fdp.fdno = para->fildes; /* Check of open mode */ if ( (fd->omode & O_WRONLY) != 0 ) { err = E_FD; goto err_ret; } /* Check of file type */ if (( fs == (FS*)&ROOT_FS )|| ((fd->inode != NULL )&&( fd->inode->type != DT_REG))) { err = toERUX(EISDIR); goto err_ret; } /* File reading */ if ( fs == (FS*)&STD_FS ) { err = sf_read(&fdp, para->buf, para->nbyte); } else if ( fs == (FS*)&STDIO_FS ) { err = std_read(&fdp, para->buf, para->nbyte); } else { err = (*fs->u_read)(&fdp, para->buf, para->nbyte); } if ( err < E_OK ) { goto err_ret; } return err; /* actual size */err_ret: DEBUG_PRINT(("_u_read err=%#x\n", err)); return err;}/* * File writing */LOCAL WER _u_write( FS *fs, CmdPkt *pkt ){ SF_WRITE_PARA *para = pkt->cmd.para; FD *fd; FDPkt fdp; ER err; /* Parameter check */ if ( (W)para->nbyte < 0 ) { err = E_PAR; goto err_ret; } err = CheckSpaceR((VP)para->buf, (W)para->nbyte); if ( err < E_OK ) { goto err_ret; } /* Search of file descriptor */ fd = searchFileDescriptor(pkt->uxinfo, para->fildes); if (( fd == NULL )||( fd->fs != fs )) { err = E_FD; goto err_ret; } fdp.fd = fd; fdp.fdno = para->fildes; /* Check of file type */ if (( fs == (FS*)&ROOT_FS )|| ((fd->inode != NULL )&&( fd->inode->type != DT_REG))) { err = toERUX(EISDIR); goto err_ret; } /* Check of open mode */ if ( (fd->omode & O_ACCMODE) == O_RDONLY ) { err = E_FD; goto err_ret; } /* File writing */ if ( fs == (FS*)&STD_FS ) { err = sf_write(&fdp, para->buf, para->nbyte); } else if ( fs == (FS*)&STDIO_FS ) { err = std_write(&fdp, para->buf, para->nbyte); } else { err = (*fs->u_write)(&fdp, para->buf, para->nbyte); } if ( err < E_OK ) { goto err_ret; } return err; /* actual size */err_ret: DEBUG_PRINT(("_u_write err=%#x\n", err)); return err;}/* * Directory entry acquisition */LOCAL WER _u_getdents( FS *fs, CmdPkt *pkt ){ SF_GETDENTS_PARA *para = pkt->cmd.para; FD *fd; FDPkt fdp; ER err; /* Parameter check */ if ( (W)para->nbyte < 0 ) { err = E_PAR; goto err_ret; } err = CheckSpaceRW((VP)para->buf, (W)para->nbyte); if ( err < E_OK ) { goto err_ret; } /* Search of file descriptor */ fd = searchFileDescriptor(pkt->uxinfo, para->fildes); if (( fd == NULL )||( fd->fs != fs )) { err = E_FD; goto err_ret; } fdp.fd = fd; fdp.fdno = para->fildes; /* Check of open mode */ if ( (fd->omode & O_WRONLY) != 0 ) { err = E_FD; goto err_ret; } /* Check of file type */ if (( fs == (FS*)&STDIO_FS )|| ((fd->inode != NULL )&&( fd->inode->type != DT_DIR))) { err = toERUX(ENOTDIR); goto err_ret; } /* Directory entry acquisition */ if ( fs == (FS*)&STD_FS ) { err = sf_getdents(&fdp, para->buf, para->nbyte); } else if ( fs == (FS*)&ROOT_FS ) { err = rt_getdents(&fdp, para->buf, para->nbyte); } else { err = (*fs->u_getdents)(&fdp, para->buf, para->nbyte); } if ( err < E_OK ) { goto err_ret; } return err; /* buf size */err_ret: DEBUG_PRINT(("_u_getdents err=%#x\n", err)); return err;}/* * File synchronization */LOCAL WER _u_fsync( FS *fs, CmdPkt *pkt ){ SF_FSYNC_PARA *para = pkt->cmd.para; FDPkt fdp; ER err; /* Search of file descriptor */ fdp.fd = searchFileDescriptor(pkt->uxinfo, para->fd); if (( fdp.fd == NULL )||( fdp.fd->fs != fs )) { err = E_FD; goto err_ret; } fdp.fdno = para->fd; /* Check of open mode */ if ( (fdp.fd->omode & O_ACCMODE) == O_RDONLY ) { err = E_FD; goto err_ret; } /* Check of file system */ if (( fs == (FS*)&ROOT_FS )||( fs == (FS*)&STDIO_FS )) { err = E_FD; goto err_ret; } /* File synchronization */ if ( fs == (FS*)&STD_FS ) { err = sf_fsync(&fdp); } else { err = (*fs->u_fsync)(&fdp); } if ( err < E_OK ) { goto err_ret; } return err;err_ret: DEBUG_PRINT(("_u_fsync err=%#x\n", err)); return err;}/* * Duplicate of file descriptor */LOCAL WER _u_dup( FS *fs, CmdPkt *pkt ){ SF_DUP_PARA *para = pkt->cmd.para; FDPkt fdp; ER err; /* Search of file descriptor */ fdp.fd = searchFileDescriptor(pkt->uxinfo, para->oldd); if (( fdp.fd == NULL )||( fdp.fd->fs != fs )) { err = E_FD; goto err_ret; } fdp.fdno = para->oldd; /* Duplicate of file descriptor */ err = dup_FileDescriptor(pkt->uxinfo, &fdp); if ( err < E_OK ) { goto err_ret; } return err; /* newd */err_ret: DEBUG_PRINT(("_u_dup err=%#x\n", err)); return err;}LOCAL WER _u_dup2( FS *fs, CmdPkt *pkt ){ SF_DUP2_PARA *para = pkt->cmd.para; FDPkt ofd, nfd; ER err, error = E_OK; /* Search of file descriptor */ ofd.fd = searchFileDescriptor(pkt->uxinfo, para->oldd); if (( ofd.fd == NULL )||( ofd.fd->fs != fs )) { err = E_FD; goto err_ret; } ofd.fdno = para->oldd; /* Stop if it is a same file descriptor number */ if ( para->oldd == para->newd ) { return para->newd; } /* Check of reproduction ahead file descriptor */ nfd.fd = searchFileDescriptor(pkt->uxinfo, para->newd); if ( nfd.fd != NULL ) { nfd.fdno = para->newd; /* Close */ if ( nfd.fd->refcnt <= 1 ) { if ( fs == (FS*)&STD_FS ) { err = sf_close(&nfd); } else if ( fs == (FS*)&ROOT_FS ) { err = rt_close(&nfd); } else if ( fs == (FS*)&STDIO_FS ) { err = std_close(&nfd); } else { err = (*fs->u_close)(&nfd); } if ( err < E_OK ) { error = err; } } /* Deletion of file descriptor */ err = del_FileDescriptor(pkt->uxinfo, &nfd); if (( err < E_OK )&&( error == E_OK )) { error = err; } } else { if (( para->newd < 0 )||( para->newd >= MaxOpenF )) { error = E_PAR; } } if ( error < E_OK ) { err = error; goto err_ret; } /* Duplicate of file descriptor */ pkt->uxinfo->fdtbl[para->newd] = ofd.fd; ofd.fd->refcnt++; return para->newd; /* newd */err_ret: DEBUG_PRINT(("_u_dup2 err=%#x\n", err)); return err;}/* * Modification of file name */LOCAL ER _u_rename( FS *fs, CmdPkt *pkt ){ LPINF *lp = pkt->exinf; INODE *from = lp[0].inode, *to = lp[1].inode; B *sp, *ep; ER err; /* Check of the consistency of file system */ if ( lp[0].fs != lp[1].fs ) { err = toERUX(EXDEV); goto err_ret; } /* Check of file system */ if ( fs == (FS*)&STDIO_FS ) { err = E_PAR; goto err_ret; } if ( fs == (FS*)&ROOT_FS ) { inode_Free(fs, from); if ( from != to ) { inode_Free(fs, to); } return E_OK; } /* Existence of "from" */ if ( from->ino == NOEXS_INO ) { err = E_NOEXS; goto err_ret; } /* Existence of "to" */ if ( to->ino != NOEXS_INO ) { /* Check of the number of "to" references */ if ( to->refcnt > 0 ) { err = E_BUSY; goto err_ret; } /* Check of the accordance of file type */ if ( from->type != to->type ) { err = (to->type == DT_DIR)? toERUX(EISDIR): toERUX(ENOTDIR); goto err_ret; } /* Stop if it is an same file */ if ( from->ino == to->ino ) { inode_Free(fs, from); if ( from != to ) { inode_Free(fs, to); } return E_OK; } } /* Root directory of file system */ if ( fs != (FS*)&STD_FS ) { if (( from->ino == fs->rootino )||( to->ino == fs->rootino )) { err = E_FACV; goto err_ret; } } else { if (( from->ino == SROOT_INO )||( to->ino == SROOT_INO )) { err = E_FACV; goto err_ret; } } /* Check of "from" file name */ sp = lp[0].path; ep = strchr(sp, '/'); if ( ep == NULL ) { ep = strchr(sp, '\0'); } if (( ep == NULL )|| (((ep - sp) == 1 )&&( strncmp(sp, ".", (size_t)1) == 0)) || (((ep - sp) == TSD_FIO_VAL_2 )&&( strncmp(sp, "..", (size_t)TSD_FIO_SZT_2) == 0))) { err = E_PAR; goto err_ret; } /* Check of "to" file name */ sp = lp[1].path; ep = strchr(sp, '/'); if ( ep == NULL ) { ep = strchr(sp, '\0'); } if (( ep == NULL )|| (((ep - sp) == 1 )&&( strncmp(sp, ".", (size_t)1) == 0) )|| (((ep - sp) == TSD_FIO_VAL_2 )&&( strncmp(sp, "..", (size_t)TSD_FIO_SZT_2) == 0))) { err = E_PAR; goto err_ret; } /* Modification of file name */ if ( fs == (FS*)&STD_FS ) { err = sf_rename(from, to, lp[1].path); if ( err < E_OK ) { goto err_ret; } } else { /* Writing access of file system */ if ( fs->rdonly != 0 ) { err = E_RONLY; goto err_ret; } err = (*fs->u_rename)(fs, from, to, lp[1].path); if ( err < E_OK ) { goto err_ret; } (void)SyncFS(fs->diskid, NULL); err = CheckDiskError(fs, NULL); if ( err < E_OK ) { goto err_ret; } } /* Deletion of file reference node */ inode_Free(fs, from); inode_Free(fs, to); return E_OK;err_ret: inode_Free(fs, from); if ( from != to ) { inode_Free(fs, to); } DEBUG_PRINT(("_u_rename err=%#x\n", err)); return err;}/* * Deletion of directory entry */LOCAL ER _u_unlink( FS *fs, CmdPkt *pkt ){ LPINF *lp = pkt->exinf; INODE *inode = lp->inode; ER err; /* Check of file system */ if ( fs == (FS*)&STDIO_FS ) { err = E_PAR; goto err_ret; } else if ( fs == (FS*)&ROOT_FS ) { err = toERUX(EISDIR); goto err_ret; } /* Exeistence of file */ if ( inode->ino == NOEXS_INO ) { err = E_NOEXS; goto err_ret; } /* Check of file type */ if ( inode->type != DT_REG ) { err = toERUX(EISDIR); goto err_ret; } /* Check of the number of references */ if ( inode->refcnt > 0 ) { err = E_BUSY; goto err_ret; } /* Deletion of directory entry */ if ( fs == (FS*)&STD_FS ) { err = sf_unlink(inode); if ( err < E_OK ) { goto err_ret; } } else { /* Writing access of file system */ if ( fs->rdonly != 0 ) { err = E_RONLY; goto err_ret; } err = (*fs->u_unlink)(fs, inode); if ( err < E_OK ) { goto err_ret; } (void)SyncFS(fs->diskid, NULL); err = CheckDiskError(fs, NULL); if ( err < E_OK ) { goto err_ret; } } /* Deletion of file reference node */ inode_Free(fs, inode); return E_OK;err_ret: inode_Free(fs, inode); DEBUG_PRINT(("_u_unlink err=%#x\n", err)); return err;}/* * Modification of current directory * It shall not be called from task */LOCAL ER _u_chdir( FS *fs, CmdPkt *pkt ){ LPINF *lp = pkt->exinf; INODE *inode = lp->inode; CDINF *cd = &pkt->uxinfo->curdir; ER err; /* Check of file system */ if ( fs == (FS*)&STDIO_FS ) { err = E_PAR; goto err_ret; } if ( fs != (FS*)&ROOT_FS ) { /* Existence of file */ if ( inode->ino == NOEXS_INO ) { err = E_NOEXS; goto err_ret; } /* Check of file type */ if ( inode->type != DT_DIR ) { err = toERUX(ENOTDIR); goto err_ret; } } /* Modification of working file*/ err = sf_chdir(fs, inode); if ( err < E_OK ) { goto err_ret; } /* Registration of current directory */ inode_Register(fs, inode, O_RDONLY); /* Release of old current directory */ (void)inode_Release(cd->fs, cd->inode, O_RDONLY, TRUE); /* Update of process attachment information */ cd->fs = fs; cd->inode = inode; return E_OK;err_ret: inode_Free(fs, inode); DEBUG_PRINT(("_u_chdir err=%#x\n", err)); return err;}/* * Modification of current directory * It shall not be called from task */LOCAL ER _u_fchdir( FS *fs, CmdPkt *pkt ){ SF_FCHDIR_PARA *para = pkt->cmd.para; FD *fd; UXINFO *uxinfo = pkt->uxinfo; CDINF *cd = &uxinfo->curdir; ER err; /* Search of file descriptor */ fd = searchFileDescriptor(pkt->uxinfo, para->fd); if (( fd == NULL )||( fd->fs != fs )) { err = E_FD; goto err_ret; } /* Check of file system */ if ( fs == (FS*)&STDIO_FS ) { err = E_FD; goto err_ret; } /* Check of file type */ if (( fd->inode != NULL )&&( fd->inode->type != DT_DIR )) { err = toERUX(ENOTDIR); goto err_ret; } /* Modification of working file */ err = sf_chdir(fs, fd->inode); if ( err < E_OK ) { goto err_ret; } /* Registration of new current directory node */ inode_Register(fs, fd->inode, O_RDONLY); /* Deletion of old current directory node */ (void)inode_Release(cd->fs, cd->inode, O_RDONLY, TRUE); /* Update of process attachement information */ uxinfo->curdir.fs = fs; uxinfo->curdir.inode = fd->inode; return E_OK;err_ret: DEBUG_PRINT(("_u_fchdir err=%#x\n", err)); return err;}/* * Modification of file mode */LOCAL ER _u_chmod( FS *fs, CmdPkt *pkt ){ SF_CHMOD_PARA *para = pkt->cmd.para; LPINF *lp = pkt->exinf; INODE *inode = lp->inode; ER err; /* Check of file system */ if ( fs == (FS*)&STDIO_FS ) { err = E_PAR; goto err_ret; } else if ( fs == (FS*)&ROOT_FS ) { err = E_RONLY; goto err_ret; } /* Existence of file */ if ( inode->ino == NOEXS_INO ) { err = E_NOEXS; goto err_ret; } /* Modification of file mode */ if ( fs == (FS*)&STD_FS ) { err = sf_chmod(inode, para->mode); if ( err < E_OK ) { goto err_ret; } } else { /* Writing access of file system */ if ( fs->rdonly != 0 ) { err = E_RONLY; goto err_ret; } err = (*fs->u_chmod)(fs, inode, para->mode); if ( err < E_OK ) { goto err_ret; } (void)SyncFS(fs->diskid, NULL); err = CheckDiskError(fs, NULL); if ( err < E_OK ) { goto err_ret; } } /* Deletion of file reference node */ inode_Free(fs, inode); return E_OK;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -