public NfsPacket Read(NfsPacket packet) { try { FileHandle fh = new FileHandle(packet); uint offset = packet.GetUInt(); uint count = packet.GetUInt(); uint totalCount = packet.GetUInt(); // not used uint xId = packet.XID; int numberRead; byte[] readbuf; String filePath = HandleManager.Current.GetName(fh.Handle); if (filePath == null) { throw new NFSException(xId, (uint)NfsReply.ERR_STALE); } if (count <= 0) { Console.Error.WriteLine("\tNfsIO.Read: invalid value for count " + count); throw new NFSException(xId, (uint)NfsReply.ERR_IO); } using (StreamReader sr = new StreamReader(filePath)) { sr.BaseStream.Seek(offset, SeekOrigin.Begin); readbuf = new byte[(int)count]; numberRead = sr.BaseStream.Read(readbuf, 0, (int)count); } if (numberRead < 0) { Console.Error.WriteLine("\tNfsIO.Read: number read is " + numberRead); numberRead = 0; } NfsFileAttributes attributes = new NfsFileAttributes(filePath); NfsPacket reply = new NfsPacket(128 + numberRead); reply.AddReplyHeader(xId); reply.SetUInt((uint)NfsReply.OK); attributes.Emit(ref reply); reply.SetData(numberRead, readbuf); return(reply); } catch (FileNotFoundException) { throw new NFSException(packet.XID, (uint)NfsReply.ERR_NOENT); } catch (IOException) { throw new NFSException(packet.XID, (uint)NfsReply.ERR_IO); } }
public NfsPacket Read(NfsPacket packet) { try { FileHandle fh = new FileHandle(packet); uint offset = packet.GetUInt(); uint count = packet.GetUInt(); uint totalCount = packet.GetUInt(); // not used uint xId = packet.XID; int numberRead; byte[] readbuf; String filePath = HandleManager.Current.GetName(fh.Handle); if (filePath == null) { throw new NFSException(xId, (uint)NfsReply.ERR_STALE); } if (count <= 0) { Console.Error.WriteLine("\tNfsIO.Read: invalid value for count " + count); throw new NFSException(xId, (uint)NfsReply.ERR_IO); } using (StreamReader sr = new StreamReader(filePath)) { sr.BaseStream.Seek(offset, SeekOrigin.Begin); readbuf = new byte[(int)count]; numberRead = sr.BaseStream.Read(readbuf, 0, (int)count); } if (numberRead < 0) { Console.Error.WriteLine("\tNfsIO.Read: number read is " + numberRead); numberRead = 0; } NfsFileAttributes attributes = new NfsFileAttributes(filePath); NfsPacket reply = new NfsPacket(128 + numberRead); reply.AddReplyHeader(xId); reply.SetUInt((uint)NfsReply.OK); attributes.Emit(ref reply); reply.SetData(numberRead, readbuf); return reply; } catch (FileNotFoundException) { throw new NFSException(packet.XID, (uint)NfsReply.ERR_NOENT); } catch (IOException) { throw new NFSException(packet.XID, (uint)NfsReply.ERR_IO); } }