Changeset 7ea69a6 in mainline for uspace/srv/fs/ext2fs/ext2fs_ops.c


Ignore:
Timestamp:
2011-03-23T10:40:17Z (13 years ago)
Author:
Martin Sucha <sucha14@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
6f50bb0
Parents:
f2d665b4
Message:

Support for sparse files and fixed handling of end of file

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/fs/ext2fs/ext2fs_ops.c

    rf2d665b4 r7ea69a6  
    5656#include <align.h>
    5757#include <adt/hash_table.h>
     58#include <sys/typefmt.h>
    5859
    5960#define EXT2FS_NODE(node)       ((node) ? (ext2fs_node_t *) (node)->data : NULL)
     
    760761        size_t bytes;
    761762        block_t *block;
     763        uint8_t *buffer;
    762764       
    763765        file_size = ext2_inode_get_size(inst->filesystem->superblock,
     
    777779        bytes = min(block_size - offset_in_block, size);
    778780       
     781        // Handle end of file
     782        if (pos + bytes > file_size) {
     783                bytes = file_size - pos;
     784        }
     785       
    779786        rc = ext2_filesystem_get_inode_data_block_index(inst->filesystem,
    780787                inode_ref->inode, file_block, &fs_block);
     
    785792        }
    786793       
     794        // Check for sparse file
     795        // If ext2_filesystem_get_inode_data_block_index returned
     796        // fs_block == 0, it means that the given block is not allocated for the
     797        // file and we need to return a buffer of zeros
     798        if (fs_block == 0) {
     799                buffer = malloc(bytes);
     800                if (buffer == NULL) {
     801                        async_answer_0(callid, ENOMEM);
     802                        async_answer_0(rid, ENOMEM);
     803                        return;
     804                }
     805               
     806                memset(buffer, 0, bytes);
     807               
     808                async_data_read_finalize(callid, buffer, bytes);
     809                async_answer_1(rid, EOK, bytes);
     810               
     811                free(buffer);
     812               
     813                return;
     814        }
     815       
     816        // Usual case - we need to read a block from device
    787817        rc = block_get(&block, inst->devmap_handle, fs_block, BLOCK_FLAGS_NONE);
    788818        if (rc != EOK) {
     
    792822        }
    793823       
    794         async_data_read_finalize(callid, block->data, bytes);
     824        assert(offset_in_block + bytes <= block_size);
     825        async_data_read_finalize(callid, block->data + offset_in_block, bytes);
    795826       
    796827        rc = block_put(block);
Note: See TracChangeset for help on using the changeset viewer.