ReadFile / WriteFile troubles with big files

373 views
Skip to first unread message

DokanNet-User

unread,
Sep 14, 2010, 6:08:35 AM9/14/10
to Dokan
Hi, i'm having some troubles to get these two methods working
completely.

It works correctly with little files, for example i can create a .txt
file and write "abcd" with notepad in it, so when i save, writefile is
called with a buffer.length = 4, and i fill writtenBytes with 4.

For readfile it's the same thing, i can read little files, but if i
try to open a picture (400ko) it doesn't work, so if somebody see
something wrong in the following code thank you to tell me what :

This is the read part :

public int ReadFile(String filename, Byte[] buffer, ref uint
readBytes, long offset, DokanFileInfo info)
{
FileModel file = null;
if ((file = (FileModel)root.getFile(filename)) != null)
{
file.read(ref buffer, ref readBytes, (ulong)offset);
return DokanNet.DOKAN_SUCCESS;
}
else
return DokanNet.DOKAN_ERROR;
}

public bool read(ref Byte[] buffer, ref uint readBytes, ulong
offset)
{
if (content == null || content.Length == 0) {
content = HTTP.GetInstance().getFileContent(this);
}
if (offset > (ulong)content.Length)
{
readBytes = 0;
return false;
}
if ((ulong)content.Length - offset > (ulong)buffer.Length)
readBytes = (uint)buffer.Length;
else
readBytes = (uint)content.Length - (uint)offset;
Buffer.BlockCopy(content, (int)offset, buffer, 0,
(int)readBytes);
return true;
}

And then for the writeFile part :

public int WriteFile(String filename, Byte[] buffer, ref uint
writtenBytes, long offset, DokanFileInfo info)
{
FileModel file = null;
if ((file = (FileModel)root.getFile(filename)) != null)
{
file.write(buffer, offset, ref writtenBytes);
return DokanNet.DOKAN_SUCCESS;
}
else
return DokanNet.DOKAN_ERROR;
}

public uint write(Byte[] buffer, long offset, ref uint
writtenBytes)
{
if (entrySize < buffer.Length + offset)
{
entrySize = (int)buffer.Length + (int)offset;
if (content == null)
content = new Byte[entrySize];
else
Array.Resize(ref content, entrySize);
}
else if (content == null)
{
if (entrySize == 0)
entrySize = buffer.Length;
content = new Byte[entrySize];
}
HTTP.GetInstance().writeFileContent(this, buffer, offset);
writtenBytes = (uint)buffer.Length;
Buffer.BlockCopy(buffer, 0, content, (int)offset,
buffer.Length);
return 0;
}

DokanNet-User

unread,
Sep 14, 2010, 6:12:02 AM9/14/10
to Dokan
I forgot one thing : should i do something specific in these methods :
Cleanup/CloseFile/SetEndOfFile/LockFile/UnlockFile/SetAllocationSize/
FlushFileBuffers

Because at the moment i don't really need them so i just return
DOKAN_SUCCESS, hope this is correct.

Hiroki Asakawa

unread,
Sep 14, 2010, 11:14:52 AM9/14/10
to do...@googlegroups.com
If you see the file by a binary editor and compare with a complete file,
you may find something.

What is the type of "content"? Is the length in byte?
You don't check the return value of read, is that ok?

> --
> You received this message because you are subscribed to the Google Groups "Dokan" group.
> To post to this group, send email to do...@googlegroups.com.
> To unsubscribe from this group, send email to dokan+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/dokan?hl=en.
>
>

DokanNet-User

unread,
Sep 14, 2010, 12:21:15 PM9/14/10
to Dokan
Content is a Byte[] and i use content.Length, so the length should be
in bytes, i checked the content.Length is the same as the file size.

Read never returns false, i added console debug and i never had offset
> content.length (content is the full file content, so the offset
cannot be bigger). I'm doing almost the same thing on mac with MacFuse
and it works correctly.

I'll look with a binary compare editor to see if it can give me more
details (one chunk missing, or one byte missing between each chunk or
something obvious with binary compare).



On 14 sep, 17:14, Hiroki Asakawa <asa...@gmail.com> wrote:
> If you see the file by a binary editor and compare with a complete file,
> you may find something.
>
> What is the type of "content"? Is the length in byte?
> You don't check the return value of read, is that ok?
>

DokanNet-User

unread,
Sep 15, 2010, 6:39:27 AM9/15/10
to Dokan
I tested on a 60 bytes .bmp file and i already had one byte different,
ReadFile was called with a buffer length of 4096, and offset 0, my
content had a length of 60 bytes (so it's ok), and i returned
readBytes = 60.

So i look at my HTTP Request, and i was using StreamReader get the
content as a String and then convert it as Byte[], that's why i some
bad bytes.

If anybody has the same problem, just use BinaryReader.

Btw i have another problem, when i move a file (for example move
"something.txt from root to directory "Dir", it works, but when i
refresh the root, i have a second "Dir")

It seems CreateDirectory is called when i move a file, any idea ?

On 14 sep, 17:14, Hiroki Asakawa <asa...@gmail.com> wrote:
> If you see the file by a binary editor and compare with a complete file,
> you may find something.
>
> What is the type of "content"? Is the length in byte?
> You don't check the return value of read, is that ok?
>
Reply all
Reply to author
Forward
0 new messages