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_write.c

    r594 r604  
    5555               uint32_t   count )
    5656{
    57     error_t      error;
    58     vseg_t     * vseg;            // required for user space checking
    59         xptr_t       file_xp;         // remote file extended pointer
    60     uint32_t     nbytes;          // number of bytes actually written
    61     reg_t        save_sr;         // required to enable IRQs during syscall
    62 
    63         thread_t   * this = CURRENT_THREAD;
    64         process_t  * process = this->process;
     57    error_t       error;
     58    vseg_t      * vseg;            // required for user space checking
     59        xptr_t        file_xp;         // remote file extended pointer
     60    vfs_file_t  * file_ptr;        // remote file local pointer
     61    cxy_t         file_cxy;        // remote file cluster identifier
     62    uint32_t      file_type;       // file type
     63    uint32_t      file_offset;     // current file offset
     64    uint32_t      file_attr;       // file_attribute
     65    vfs_inode_t * inode_ptr;       // local pointer on associated inode
     66    uint32_t      nbytes;          // number of bytes actually written
     67    reg_t         save_sr;         // required to enable IRQs during syscall
     68
     69        thread_t    * this = CURRENT_THREAD;
     70        process_t   * process = this->process;
    6571
    6672#if (DEBUG_SYS_WRITE || CONFIG_INSTRUMENTATION_SYSCALLS)
     
    113119
    114120#if DEBUG_SYSCALLS_ERROR
    115 printk("\n[ERROR] in %s : thread[%x,%x] undefined file descriptor index = %d\n",
     121printk("\n[ERROR] in %s : thread[%x,%x] undefined file descriptor = %d\n",
    116122__FUNCTION__, process->pid, this->trdid, file_id );
    117123#endif
     
    121127
    122128    // get file descriptor cluster and local pointer
    123     vfs_file_t * file_ptr = (vfs_file_t *)GET_PTR( file_xp );
    124     cxy_t        file_cxy = GET_CXY( file_xp );
    125 
    126  
    127     // get file type
    128     vfs_inode_type_t type = hal_remote_l32( XPTR( file_cxy , &file_ptr->type ) );
     129    file_ptr = GET_PTR( file_xp );
     130    file_cxy = GET_CXY( file_xp );
     131
     132    // get file type, offset, aatributes, and associated inode
     133    file_type   = hal_remote_l32( XPTR( file_cxy , &file_ptr->type ) );
     134    file_offset = hal_remote_l32( XPTR( file_cxy , &file_ptr->offset ) );
     135    inode_ptr   = hal_remote_lpt( XPTR( file_cxy , &file_ptr->inode ) );
     136    file_attr   = hal_remote_l32( XPTR( file_cxy , &file_ptr->attr ) );
    129137
    130138    // enable IRQs
     
    132140
    133141   // action depend on file type
    134     if( type == INODE_TYPE_FILE )  // check file writable & write to mapper
     142    if( file_type == INODE_TYPE_FILE )  // write to file mapper
    135143    {
    136144        // check file writable
    137         uint32_t attr = hal_remote_l32( XPTR( file_cxy , &file_ptr->attr ) );
    138         if( (attr & FD_ATTR_WRITE_ENABLE) == 0 )
     145        if( (file_attr & FD_ATTR_WRITE_ENABLE) == 0 )
    139146            {
    140147
     
    143150__FUNCTION__ , process->pid, this->trdid, file_id );
    144151#endif
     152            hal_restore_irq( save_sr );
    145153                    this->errno = EBADFD;
    146154                    return -1;
     
    152160                                vaddr,
    153161                                count );
    154     }
    155     else if( type == INODE_TYPE_DEV )  // write to TXT device
     162        if ( nbytes != count )
     163        {
     164
     165#if DEBUG_SYSCALLS_ERROR
     166printk("\n[ERROR] in %s : thread[%x,%x] cannot write %d bytes into file %d\n",
     167__FUNCTION__ , process->pid, this->trdid, count, file_id );
     168#endif
     169            hal_restore_irq( save_sr );
     170            this->errno = EIO;
     171            return -1;
     172
     173        }
     174
     175        // update size field in inode if required
     176        xptr_t   size_xp    = XPTR( file_cxy , &inode_ptr->size );
     177        uint32_t inode_size = hal_remote_l32( size_xp );
     178        if ( (file_offset + count) > inode_size )
     179        {
     180            hal_remote_s32( size_xp , file_offset + count );
     181        }
     182    }
     183    else if( file_type == INODE_TYPE_DEV )  // write to TXT device
    156184    {
    157185        // move count bytes to device
    158186        nbytes = devfs_user_move( false,             // from buffer to device
    159                                  file_xp,
    160                                  vaddr,
    161                                  count );
    162     }
    163     else  // not FILE and not DEV
    164     {
    165 
    166 #if DEBUG_SYSCALLS_ERROR
    167 printk("\n[ERROR] in %s : thread[%x,%x] / illegal inode type %\n",
    168 __FUNCTION__, vfs_inode_type_str( type ) );
    169 #endif
    170                 this->errno = EBADFD;
    171                 return -1;
    172     }
    173 
    174     if( nbytes != count )
    175     {
     187                                  file_xp,
     188                                  vaddr,
     189                                  count );
     190        if( nbytes != count )
     191        {
    176192
    177193#if DEBUG_SYSCALLS_ERROR
     
    179195__FUNCTION__ , process->pid, this->trdid, file_id );
    180196#endif
    181         this->errno = EIO;
    182         return -1;
     197            hal_restore_irq( save_sr );
     198            this->errno = EIO;
     199            return -1;
     200        }
     201    }
     202    else  // not FILE and not DEV
     203    {
     204
     205#if DEBUG_SYSCALLS_ERROR
     206printk("\n[ERROR] in %s : thread[%x,%x] / illegal inode type %\n",
     207__FUNCTION__, vfs_inode_type_str( file_type ) );
     208#endif
     209        hal_restore_irq( save_sr );
     210                this->errno = EBADFD;
     211                return -1;
    183212    }
    184213
Note: See TracChangeset for help on using the changeset viewer.