Just spent a couple of hours trying to figure out an issue with my FS.
Turned out that Dokan expects the implementation of ReadFile to always fill the entire buffer when offset + buffer.Length are within the file length, and read exactly remaining bytes when offset + buffer.Length are more than file length.
I was calling .NET Stream.Read with the buffer, and my filesystem would return garbage to some programs. The reason is Stream.Read by its contract does not have to fill your entire buffer. For example, when reading from a TCP connection if you specify 1MiB buffer and 1KiB packet arrives, NetworkStream will fill 1KiB and return 1024 even though the connection is still open and it will be possible to read more data in the future.
Now I don't know what the kernel APIs expect in this case, but IMHO one of the following should be done:
- documenting this behavior
- if this behavior is the result of a bug (perhaps the value returned in
bytesRead is not used everywhere it should be), fixing that bug
- considering changing .NET wrapper code to repeat
ReadFile calls until either buffer is filled or end of file reached (this would remove the need for documenting the discrepancy)
Just spent a couple of hours trying to figure out an issue with my FS.
Turned out that Dokan expects the implementation of
ReadFileto always fill the entire buffer whenoffset + buffer.Lengthare within the file length, and read exactly remaining bytes whenoffset + buffer.Lengthare more than file length.I was calling .NET
Stream.Readwith thebuffer, and my filesystem would return garbage to some programs. The reason isStream.Readby its contract does not have to fill your entire buffer. For example, when reading from a TCP connection if you specify 1MiB buffer and 1KiB packet arrives,NetworkStreamwill fill 1KiB and return 1024 even though the connection is still open and it will be possible to read more data in the future.Now I don't know what the kernel APIs expect in this case, but IMHO one of the following should be done:
bytesReadis not used everywhere it should be), fixing that bugReadFilecalls until eitherbufferis filled or end of file reached (this would remove the need for documenting the discrepancy)