src/kernel/fs/fat32.c
/*!
 * @note create a entry and hold it`s write lock.
 * @warning must hold parent write lock
 * @param parent
 * @param name
 * @param attr
 * @return new entry with write lock or NULL when fail to create entry.
 */
@note create a entry and hold it`s write lock.
@note create a entry and hold it ` s write lock .
entry_t *entry_create_write(entry_t * parent , char * name , uint8_t attr){
    char short_name[13]={0};
    name_to_short_name(short_name, name, strlen(name));
    name = short_name;
    ASSERT(parent!=NULL&&parent->attr == ENTRY_ATTR_DIR&&strlen(name)<MAX_FULL_NAME);
    ASSERT(attr==ENTRY_ATTR_DIR||attr==ENTRY_ATTR_ARCHIVE);
    entry_t * tmp;
    parent->file_size = _get_dir_file_size(parent);
    if((tmp= entry_get_sub_read(parent, name)) != NULL){
        entry_put_read(tmp);
        return NULL;
    }
    entry_t * idle = _entry_get_idle_write();
    ASSERT(idle!=NULL);
    idle->parent = parent;
    strcpy(idle->filename,name);
    entry_data_t new_entry_data;
    entry_rw(parent,&new_entry_data,parent->file_size,sizeof(entry_data_t),true);
    parent->file_size+=32;
    idle->offset_in_dir = parent->file_size-32;
    idle->parent->ref_cnt++;
    idle->dirty = true;
    idle->attr =attr;
    if(attr==ENTRY_ATTR_ARCHIVE){
        idle->first_clus_no = 0;
        idle->file_size = 0;
    }
    else{
        idle->first_clus_no = _clus_alloc();
        idle->file_size = 32*2;
        entry_data_t buffer[2]={
                {
                        ".",
                        "",
                        ENTRY_ATTR_DIR,
                        0,
                        0,
                        0,
                        0,
                        0,
                        idle->first_clus_no>>16,
                        0,
                        0,
                        idle->first_clus_no<<16>>16,
                        idle->file_size
                },
                {
                        "..",
                        "",
                        ENTRY_ATTR_DIR,
                        0,
                        0,
                        0,
                        0,
                        0,
                        idle->parent->first_clus_no>>16,
                        0,
                        0,
                        idle->parent->first_clus_no<<16>>16,
                        idle->parent->file_size
                }
        };
        entry_rw(idle,buffer,0,sizeof(entry_data_t)*2,true);
    }
    return idle;
}
entry_t * entry_create_write ( entry_t * parent , char * name , uint8_t attr ) { char short_name [ 13 ] = { 0 } ; name_to_short_name ( short_name , name , strlen ( name ) ) ; name = short_name ; ASSERT ( parent != NULL && parent -> attr == ENTRY_ATTR_DIR && strlen ( name ) < MAX_FULL_NAME ) ; ASSERT ( attr == ENTRY_ATTR_DIR || attr == ENTRY_ATTR_ARCHIVE ) ; entry_t * tmp ; parent -> file_size = _get_dir_file_size ( parent ) ; if ( ( tmp = entry_get_sub_read ( parent , name ) ) != NULL ) { entry_put_read ( tmp ) ; return NULL ; } entry_t * idle = _entry_get_idle_write ( ) ; ASSERT ( idle != NULL ) ; idle -> parent = parent ; strcpy ( idle -> filename , name ) ; entry_data_t new_entry_data ; entry_rw ( parent , & new_entry_data , parent -> file_size , sizeof ( entry_data_t ) , true ) ; parent -> file_size += 32 ; idle -> offset_in_dir = parent -> file_size - 32 ; idle -> parent -> ref_cnt ++ ; idle -> dirty = true ; idle -> attr = attr ; if ( attr == ENTRY_ATTR_ARCHIVE ) { idle -> first_clus_no = 0 ; idle -> file_size = 0 ; } else { idle -> first_clus_no = _clus_alloc ( ) ; idle -> file_size = 32 * 2 ; entry_data_t buffer [ 2 ] = { { " " , " " , ENTRY_ATTR_DIR , 0 , 0 , 0 , 0 , 0 , idle -> first_clus_no >> 16 , 0 , 0 , idle -> first_clus_no << 16 >> 16 , idle -> file_size } , { " " , " " , ENTRY_ATTR_DIR , 0 , 0 , 0 , 0 , 0 , idle -> parent -> first_clus_no >> 16 , 0 , 0 , idle -> parent -> first_clus_no << 16 >> 16 , idle -> parent -> file_size } } ; entry_rw ( idle , buffer , 0 , sizeof ( entry_data_t ) * 2 , true ) ; } return idle ; }
@note create a entry and hold it`s write lock.
@note create a entry and hold it ` s write lock .
// this entry is exist // write a empty entry to parent. // the parent file size will change // add entry "." and ".."