Ignore:
Timestamp:
Dec 3, 2018, 12:18:40 PM (6 years ago)
Author:
alain
Message:

Improve the FAT32 file system to support cat, rm, cp commands.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/kernel/syscalls/sys_read.c

    r594 r604  
    5656              uint32_t   count )
    5757{
    58     error_t      error;
    59     vseg_t     * vseg;        // required for user space checking
    60         xptr_t       file_xp;     // remote file extended pointer
    61     uint32_t     nbytes;      // number of bytes actually read
    62     reg_t        save_sr;     // required to enable IRQs during syscall
    63 
    64         thread_t  *  this             = CURRENT_THREAD;
    65         process_t *  process          = this->process;
    66     xptr_t       process_owner_xp = process->owner_xp;
     58    error_t       error;
     59    vseg_t      * vseg;            // required for user space checking
     60        xptr_t        file_xp;         // remote file extended pointer
     61    vfs_file_t  * file_ptr;        // remote file local pointer
     62    cxy_t         file_cxy;        // remote file cluster identifier
     63    uint32_t      file_type;       // file type
     64    uint32_t      file_offset;     // current file offset
     65    uint32_t      file_attr;       // file_attribute
     66    vfs_inode_t * inode_ptr;       // local pointer on associated inode
     67    uint32_t      nbytes;          // number of bytes actually read
     68    reg_t         save_sr;         // required to enable IRQs during syscall
     69
     70        thread_t    * this             = CURRENT_THREAD;
     71        process_t   * process          = this->process;
     72    xptr_t        process_owner_xp = process->owner_xp;
    6773 
    6874#if (DEBUG_SYS_READ || CONFIG_INSTRUMENTATION_SYSCALLS)
     
    8591
    8692#if DEBUG_SYSCALLS_ERROR
    87 printk("\n[ERROR] in %s : thread[%x,%x] illegal file descriptor index = %d\n",
     93printk("\n[ERROR] in %s : thread[%x,%x] illegal file descriptor index %d\n",
    8894__FUNCTION__ , process->pid, this->trdid, file_id );
    8995#endif
     
    107113    }
    108114
     115    // get extended pointer on remote file descriptor
     116    file_xp = process_fd_get_xptr( process , file_id );
     117
     118    if( file_xp == XPTR_NULL )
     119    {
     120
     121#if DEBUG_SYSCALLS_ERROR
     122printk("\n[ERROR] in %s : thread[%x,%x] undefined fd_id %d\n",
     123__FUNCTION__, process->pid, this->trdid, file_id );
     124#endif
     125        this->errno = EBADFD;
     126        return -1;
     127    }
     128
     129    // get file descriptor cluster and local pointer
     130    file_ptr = GET_PTR( file_xp );
     131    file_cxy = GET_CXY( file_xp );
     132
     133    // get file type, offset, attributes and associated inode
     134    file_type   = hal_remote_l32( XPTR( file_cxy , &file_ptr->type ) );
     135    file_offset = hal_remote_l32( XPTR( file_cxy , &file_ptr->offset ) );
     136    inode_ptr   = hal_remote_lpt( XPTR( file_cxy , &file_ptr->inode ) );
     137    file_attr   = hal_remote_l32( XPTR( file_cxy , &file_ptr->attr ) );
     138
    109139    // enable IRQs
    110140    hal_enable_irq( &save_sr );
    111141
    112     // get extended pointer on remote file descriptor
    113     file_xp = process_fd_get_xptr( process , file_id );
    114 
    115     if( file_xp == XPTR_NULL )
    116     {
    117 
    118 #if DEBUG_SYSCALLS_ERROR
    119 printk("\n[ERROR] in %s : thread[%x,%x] undefined fd_id %d\n",
    120 __FUNCTION__, process->pid, this->trdid, file_id );
    121 #endif
    122         this->errno = EBADFD;
    123         return -1;
    124     }
    125 
    126     // get file descriptor cluster and local pointer
    127     vfs_file_t * file_ptr = GET_PTR( file_xp );
    128     cxy_t        file_cxy = GET_CXY( file_xp );
    129 
    130     // get file type
    131     vfs_inode_type_t type = hal_remote_l32( XPTR( file_cxy , &file_ptr->type ) );
    132 
    133142    // action depend on file type
    134     if( type == INODE_TYPE_FILE )      // check file readable & read from mapper
     143    if( file_type == INODE_TYPE_FILE )      // read from file mapper
    135144    {
    136145        // check file readable
    137         uint32_t attr = hal_remote_l32( XPTR( file_cxy , &file_ptr->attr ) );
    138 
    139         if( (attr & FD_ATTR_READ_ENABLE) == 0 )
     146        if( (file_attr & FD_ATTR_READ_ENABLE) == 0 )
    140147            {
    141148
     
    144151__FUNCTION__, process->pid, this->trdid, file_id );
    145152#endif
     153            hal_restore_irq( save_sr );
    146154                    this->errno = EBADFD;
    147155                    return -1;
     
    153161                                vaddr,
    154162                                count );
    155     }
    156     else if( type == INODE_TYPE_DEV )  // check ownership & read from device
     163        if( nbytes != count )
     164        {
     165
     166#if DEBUG_SYSCALLS_ERROR
     167printk("\n[ERROR] in %s : thread[%x,‰x] cannot read %d bytes from file %d\n",
     168__FUNCTION__, process->pid, this->trdid, count, file_id );
     169#endif
     170            this->errno = EIO;
     171            hal_restore_irq( save_sr );
     172            return -1;
     173        }
     174    }
     175    else if( file_type == INODE_TYPE_DEV )  // read from TXT device
    157176    {
    158177        // get cluster and pointers on TXT_RX chdev
     
    192211                                  vaddr,
    193212                                  count );
    194 
    195     }
    196     else    // not FILE and not DEV
    197     {
    198 
    199 #if DEBUG_SYSCALLS_ERROR
    200 printk("\n[ERROR] in %s : thread[%x,%x] / illegal inode type %\n",
    201 __FUNCTION__, vfs_inode_type_str( type ) );
    202 #endif
    203                 this->errno = EBADFD;
    204                 return -1;
    205     }
    206 
    207     if( nbytes != count )
    208     {
     213        if( nbytes != count )
     214        {
    209215
    210216#if DEBUG_SYSCALLS_ERROR
     
    212218__FUNCTION__, process->pid, this->trdid, file_id );
    213219#endif
    214         this->errno = EIO;
    215         return -1;
     220            this->errno = EIO;
     221            hal_restore_irq( save_sr );
     222            return -1;
     223        }
     224    }
     225    else    // not FILE and not DEV
     226    {
     227
     228#if DEBUG_SYSCALLS_ERROR
     229printk("\n[ERROR] in %s : thread[%x,%x] / illegal inode type %\n",
     230__FUNCTION__, vfs_inode_type_str( file_type ) );
     231#endif
     232                this->errno = EBADFD;
     233        hal_restore_irq( save_sr );
     234                return -1;
    216235    }
    217236
Note: See TracChangeset for help on using the changeset viewer.