Ignore:
Timestamp:
2012-03-01T19:56:31Z (12 years ago)
Author:
Frantisek Princ <frantisek.princ@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
c30a015
Parents:
d8269dc
Message:

Code refactorization (entry insertion)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/ext4/libext4_directory_index.c

    rd8269dc rd0d7afb  
    503503                block_t *old_data_block, ext4_directory_dx_block_t *index_block, block_t **new_data_block)
    504504{
    505         int rc;
     505        int rc = EOK;
    506506
    507507        uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
     
    652652
    653653int ext4_directory_dx_add_entry(ext4_filesystem_t *fs,
    654                 ext4_inode_ref_t *parent, ext4_inode_ref_t *child,
    655                 size_t name_size, const char *name)
    656 {
    657         int rc;
     654                ext4_inode_ref_t *parent, ext4_inode_ref_t *child, const char *name)
     655{
     656        int rc = EOK;
     657        int rc2;
    658658
    659659        // get direct block 0 (index root)
     
    670670        }
    671671
     672        uint32_t name_len = strlen(name);
    672673        ext4_hash_info_t hinfo;
    673         rc = ext4_directory_hinfo_init(&hinfo, root_block, fs->superblock, name_size, name);
     674        rc = ext4_directory_hinfo_init(&hinfo, root_block, fs->superblock, name_len, name);
    674675        if (rc != EOK) {
    675676                block_put(root_block);
     
    702703        }
    703704
    704         uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
    705         uint16_t required_len = 8 + name_size + (4 - name_size % 4);
    706 
    707         ext4_directory_entry_ll_t *de = target_block->data;
    708         ext4_directory_entry_ll_t *stop = target_block->data + block_size;
    709 
    710         while (de < stop) {
    711 
    712                 uint32_t de_inode = ext4_directory_entry_ll_get_inode(de);
    713                 uint16_t de_rec_len = ext4_directory_entry_ll_get_entry_length(de);
    714 
    715                 if ((de_inode == 0) && (de_rec_len >= required_len)) {
    716                         ext4_directory_write_entry(fs->superblock, de, de_rec_len,
    717                                 child, name, name_size);
    718 
    719                                 // TODO cleanup
    720                                 target_block->dirty = true;
    721                                 rc = block_put(target_block);
    722                                 if (rc != EOK) {
    723                                         return EXT4_ERR_BAD_DX_DIR;
    724                                 }
    725                                 return EOK;
    726                 }
    727 
    728                 if (de_inode != 0) {
    729                         uint16_t used_name_len = ext4_directory_entry_ll_get_name_length(
    730                                         fs->superblock, de);
    731 
    732                         uint16_t used_space = 8 + used_name_len;
    733                         if ((used_name_len % 4) != 0) {
    734                                 used_space += 4 - (used_name_len % 4);
    735                         }
    736                         uint16_t free_space = de_rec_len - used_space;
    737 
    738                         if (free_space >= required_len) {
    739 
    740                                 // Cut tail of current entry
    741                                 ext4_directory_entry_ll_set_entry_length(de, used_space);
    742                                 ext4_directory_entry_ll_t *new_entry = (void *)de + used_space;
    743                                 ext4_directory_write_entry(fs->superblock, new_entry,
    744                                         free_space, child, name, name_size);
    745 
    746                                 // TODO cleanup
    747                                 target_block->dirty = true;
    748                                 rc = block_put(target_block);
    749                                 if (rc != EOK) {
    750                                         return EXT4_ERR_BAD_DX_DIR;
    751                                 }
    752                                 return EOK;
    753 
    754                         }
    755 
    756                 }
    757 
    758                 de = (void *)de + de_rec_len;
     705        rc = ext4_directory_try_insert_entry(fs->superblock, target_block, child, name, name_len);
     706        if (rc == EOK) {
     707                goto cleanup;
     708
    759709        }
    760710
    761711    EXT4FS_DBG("no free space found");
     712
     713    uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
    762714
    763715        ext4_directory_dx_entry_t *entries = ((ext4_directory_dx_node_t *) dx_block->block->data)->entries;
     
    786738                rc =  ext4_directory_append_block(fs, parent, &new_fblock, &new_iblock);
    787739                if (rc != EOK) {
    788                         // TODO error
     740                        goto cleanup;
    789741                }
    790742
     
    793745                rc = block_get(&new_block, fs->device, new_fblock, BLOCK_FLAGS_NOREAD);
    794746                if (rc != EOK) {
    795                         // TODO error
     747                        goto cleanup;
    796748                }
    797749
     
    873825        }
    874826
    875         // TODO Where to save new entry
     827        // Where to save new entry
    876828        uint32_t new_block_hash = ext4_directory_dx_entry_get_hash(dx_block->position + 1);
    877829        if (hinfo.hash >= new_block_hash) {
    878                 de = new_block->data;
    879                 stop = new_block->data + block_size;
     830                rc = ext4_directory_try_insert_entry(fs->superblock, new_block, child, name, name_len);
    880831        } else {
    881                 de = target_block->data;
    882                 stop = target_block->data + block_size;
    883         }
    884 
    885         while (de < stop) {
    886 
    887                 uint32_t de_inode = ext4_directory_entry_ll_get_inode(de);
    888                 uint16_t de_rec_len = ext4_directory_entry_ll_get_entry_length(de);
    889 
    890                 if ((de_inode == 0) && (de_rec_len >= required_len)) {
    891                         ext4_directory_write_entry(fs->superblock, de, de_rec_len,
    892                                 child, name, name_size);
    893                                 goto success;
    894                 }
    895 
    896                 if (de_inode != 0) {
    897                         uint16_t used_name_len = ext4_directory_entry_ll_get_name_length(
    898                                         fs->superblock, de);
    899 
    900                         uint16_t used_space = 8 + used_name_len;
    901                         if ((used_name_len % 4) != 0) {
    902                                 used_space += 4 - (used_name_len % 4);
    903                         }
    904                         uint16_t free_space = de_rec_len - used_space;
    905 
    906                         if (free_space >= required_len) {
    907 
    908                                 // Cut tail of current entry
    909                                 ext4_directory_entry_ll_set_entry_length(de, used_space);
    910                                 ext4_directory_entry_ll_t *new_entry = (void *)de + used_space;
    911                                 ext4_directory_write_entry(fs->superblock, new_entry,
    912                                         free_space, child, name, name_size);
    913 
    914                                 goto success;
    915                         }
    916 
    917                 }
    918 
    919                 de = (void *)de + de_rec_len;
    920         }
    921 
    922 success:
     832                rc = ext4_directory_try_insert_entry(fs->superblock, target_block, child, name, name_len);
     833        }
     834
     835        if (rc != EOK) {
     836                goto terminate;
     837        }
     838
     839
     840terminate:
     841        rc = block_put(new_block);
     842        if (rc != EOK) {
     843                EXT4FS_DBG("error writing new block");
     844        }
     845
     846
     847cleanup:
     848
     849        rc2 = rc;
    923850
    924851        rc = block_put(target_block);
    925852        if (rc != EOK) {
    926                 EXT4FS_DBG("error writing target block");
    927         }
    928         rc = block_put(new_block);
    929         if (rc != EOK) {
    930                 EXT4FS_DBG("error writing new block");
     853                return rc;
    931854        }
    932855
     
    936859                rc = block_put(dx_it->block);
    937860                if (rc != EOK) {
    938                         EXT4FS_DBG("error writing index block \%u", (uint32_t)dx_it->block->pba);
     861                        return rc;
    939862                }
    940863                dx_it++;
    941864        }
    942865
    943         return EOK;
     866        return rc2;
    944867}
    945868
Note: See TracChangeset for help on using the changeset viewer.