tpunix / H750PI_proj

H750PI裸奔工程测试
7 stars 4 forks source link

Problems with read large files in ntfs_read() #2

Open metcenger opened 4 days ago

metcenger commented 4 days ago

If I have small file (13 Bytes), and call static int ntfs_read(void *fs_super, void *fd, void *in_buf, int size)

no problems- buffer is being filled properly and I can read this buffer.

But if I have another file to read (1 Mb), I should run this part of code:

 else // large files
    { 
        u32 lcn, vcn, vcn_offset, vcn_size;
        RUNLIST *rl = (RUNLIST*)ntfs->attr_80;

        vcn_size = ntfs->cluster_size*512;
        vcn = ntfs->f_offset / vcn_size;
        vcn_offset = ntfs->f_offset % vcn_size;

        lcn = rl_seek_vcn(rl, vcn);
        if(lcn==0){
            return -EBADF;
        }

        int remain = size;
        while(remain){
            int read_size = vcn_size - vcn_offset;
            if(read_size<remain)
                read_size = remain;

            if(ntfs->f_offset >= ntfs->f_valid){
                memset(buf, 0, read_size);
            }else{
                read_cluster(ntfs, lcn);
                memcpy(buf, ntfs->dbuf + vcn_offset, read_size);
            }

            buf += read_size;
            remain -= read_size;
            vcn_offset += read_size;
            ntfs->f_offset += read_size;
            if(vcn_offset >= vcn_size){
                vcn_offset = 0;
                lcn = rl_next_lcn(rl);
            }
        }
    }

And in this case, project goes to Abort handler.

So I have a question- If I have large file, and buffer transferred like an argument, size of this buffer is 2048, and size like the argument (I transferred 2048), how can I call this function many times to get my file in parts for 2048?

metcenger commented 3 days ago

I suppose, you made mistake in the code: in while should be: if(read_size != remain) not a <

Check and confirm it pls.

tpunix commented 3 days ago

On first read, "remain" is bigger than read_size. On last read, "remain" become smaller than read_size. There should be: if(read_size > remain)

metcenger commented 3 days ago

Therefore, not like a < ? And should be a > not a !=?

I noticed, in first read, "remain" is equal to read_size. 4096 = 4096

tpunix commented 3 days ago

"remain" is initial from here: "int remain = size;" What's the value of "size" you trans?

metcenger commented 3 days ago

I transferred size = 4096. Total size of file = 11481 I made 2 rounds in while with 4096- no problems and 3nd round- I had 11481- 2*4096= 3289 like rest bytes

        while(remain){
            int read_size = vcn_size - vcn_offset;
            if(read_size != remain)  // was a < 
                read_size = remain;

in this part I always have read_size = 4096 (sector size)

Therefore, we should use a > or !=