private void ReadDir(rpcCracker cracker, rpcPacker packer) { fhandle fh = new fhandle(cracker); uint cookie = cracker.get_uint32(); uint count = cracker.get_uint32(); FileEntry dir = FileTable.LookupFileEntry(fh); //Console.WriteLine("ReadDir:{0}, cookie:{1}, count:{2}, resultsNULL:{3}", dir.Name, cookie, count, results == null); if (cookie == 0 || results == null) { if (dir == null) { throw new NFSStatusException(NFSStatus.NFSERR_EXIST); } try { results = new readdirres(dir.Name, count); } catch (DirectoryNotFoundException) { FileTable.Remove(fh); throw; } } if (results.Pack(packer, cookie, count) == true) { results = null; } }
private void StatFS(rpcCracker cracker, rpcPacker packer) { const uint BLOCK_SIZE = 4096; fhandle fh = new fhandle(cracker); FileEntry file = FileTable.LookupFileEntry(fh); Console.WriteLine("StatFS: {0}", file.Name); System.UInt64 freeBytesAvailable = 0; System.UInt64 totalNumberOfBytes = 0; System.UInt64 totalNumberOfFreeBytes = 0; if (UnmanagedWin32API.GetDiskFreeSpaceEx(file.Name, ref freeBytesAvailable, ref totalNumberOfBytes, ref totalNumberOfFreeBytes) == false) { throw new NFSStatusException(NFSStatus.NFSERR_EXIST); } freeBytesAvailable /= BLOCK_SIZE; totalNumberOfBytes /= BLOCK_SIZE; totalNumberOfFreeBytes /= BLOCK_SIZE; packer.setUint32((uint)NFSStatus.NFS_OK); packer.setUint32(BLOCK_SIZE); // tsize: optimum transfer size packer.setUint32(BLOCK_SIZE); // Block size of FS packer.setUint32((uint)totalNumberOfBytes); // Total # of blocks (of the above size) packer.setUint32((uint)totalNumberOfFreeBytes); // Free blocks packer.setUint32((uint)freeBytesAvailable); // Free blocks available to non-priv. users }
private void Remove(rpcCracker cracker, rpcPacker packer) { diropargs args = new diropargs(cracker); String removePath = FileTable.LookupFileEntry(args.DirHandle).Name + @"\" + args.FileName; FileInfo info = new FileInfo(removePath); if (info.Exists == false) { removePath += ".sl"; info = new FileInfo(removePath); } Console.WriteLine(@"Remove: {0}", removePath); fhandle fh = FileTable.LookupFileHandle(removePath); info.Delete(); // If UnauthorizedAccessException is thrown & caught should // probably stat file to determine if the cause is because // the path is a dir rather than a directory. if (fh != null) { FileTable.Remove(fh); } packer.setUint32((uint)NFSStatus.NFS_OK); }
private void RmDir(rpcCracker cracker, rpcPacker packer) { diropargs args = new diropargs(cracker); String removePath = FileTable.LookupFileEntry(args.DirHandle).Name + @"\" + args.FileName; Console.WriteLine(@"RmDir: {0}", removePath); fhandle fh = FileTable.LookupFileHandle(removePath); try { new DirectoryInfo(removePath).Delete(false); } catch (IOException) { if (new DirectoryInfo(removePath).GetFileSystemInfos().Length > 0) { throw new NFSStatusException(NFSStatus.NFSERR_NOTEMPTY); } else { throw new NFSStatusException(NFSStatus.NFSERR_PERM); } } if (fh != null) { FileTable.Remove(fh); } packer.setUint32((uint)NFSStatus.NFS_OK); }
private void Write(rpcCracker cracker, rpcPacker packer) { fhandle fh = new fhandle(cracker); uint beginOffset = cracker.get_uint32(); uint offset = cracker.get_uint32(); uint totalcount = cracker.get_uint32(); Byte[] data = cracker.getData(); FileStream fs; try { fs = new FileStream(FileTable.LookupFileEntry(fh).Name, FileMode.Open, FileAccess.Write); } catch (System.IO.FileNotFoundException) { FileTable.Remove(fh); throw; } try { fs.Position = offset; fs.Write(data, 0, data.Length); attrstat.PackSuccess(packer, new fattr(fh)); } finally { fs.Close(); } }
private void ReadLink(rpcCracker cracker, rpcPacker packer) { fhandle fh = new fhandle(cracker); FileStream fs; try { fs = new FileStream(FileTable.LookupFileEntry(fh).Name, FileMode.Open, FileAccess.Read); } catch (System.IO.FileNotFoundException) { FileTable.Remove(fh); throw; } try { Byte[] buf = new Byte[MAXPATHLEN]; int bytesRead = fs.Read(buf, 0, MAXPATHLEN); packer.setUint32((uint)NFSStatus.NFS_OK); packer.setData(buf, buf.Length); } finally { fs.Close(); } }
private void Mount(rpcCracker cracker, rpcPacker reply) { uint length = cracker.get_uint32(); string dirPath = ""; for (uint i = 0; i < length; ++i) { dirPath += cracker.get_char(); } Console.WriteLine("Mount {0}:{1}", length, dirPath); if (Directory.Exists(dirPath) == false) { reply.setUint32(2); // Errno for no such file or directory reply.setUint32(0); // Where fh would go } else { fhandle fh = FileTable.Add(new FileEntry(dirPath)); reply.setUint32(0); // Success fh.Pack(reply); } }
public fattr(fhandle fh) { FileEntry file = FileTable.LookupFileEntry(fh); if (file == null) { Console.WriteLine("fattr on invalid file handle:{0}", fh.Index); throw new NFSStatusException(NFSStatus.NFSERR_STALE); } FileInfo fileInfo = new FileInfo(file.Name); if (fileInfo.Exists == false) { if (new DirectoryInfo(file.Name).Exists == false) { throw new System.IO.FileNotFoundException(); } } if ((fileInfo.Attributes & FileAttributes.Directory) != 0) { type = ftype.NFDIR; mode |= (uint)modes.DIR; size = 4096; blocksize = 4096; blocks = 8; atime = new timeval(fileInfo.LastAccessTime); mtime = new timeval(fileInfo.LastWriteTime); } else { if (fileInfo.Extension == ".sl") { type |= ftype.NFLNK; mode = (uint)modes.LNK; } else { type = ftype.NFREG; mode |= (uint)modes.REG; } size = (uint)fileInfo.Length; blocks = (size / 4096) + (4096 - (size % 4096)); atime = new timeval(fileInfo.LastAccessTime); mtime = new timeval(fileInfo.LastWriteTime); } if ((fileInfo.Attributes & FileAttributes.ReadOnly) == 0) { mode |= (uint)modes.WOWN; } fileid = fh.Index; //Console.WriteLine("fattr name:{0}, fileid:{1}, attrs:{2}, readonly:{3}", file.Name, fileid, fileInfo.Attributes, (fileInfo.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly); }
private void Lookup(rpcCracker cracker, rpcPacker packer) { diropargs args = new diropargs(cracker); String lookupPath = FileTable.LookupFileEntry(args.DirHandle).Name + @"\" + args.FileName; String symLinkLookupPath = lookupPath + ".sl"; #if DEBUG //Console.WriteLine(@"Lookup: {0}", lookupPath); #endif fhandle fh = null; #if DEBUG try { #endif if ((fh = FileTable.LookupFileHandle(lookupPath)) == null) { //Console.WriteLine(@"Lookup (symlink): {0}", symLinkLookupPath); fh = FileTable.LookupFileHandle(symLinkLookupPath); } // Entry (for file or symlink) not in FileTable if (fh == null) { // Try non-SL first fh = FileTable.Add(new FileEntry(lookupPath)); try { diropres.PackSuccess(packer, fh, new fattr(fh)); } catch { FileTable.Remove(fh); fh = FileTable.Add(new FileEntry(symLinkLookupPath)); } } // Case where fh is in FileTable and used when neither was but // regular file/dir has not been found so add entry for SL #if DEBUG } catch { Console.WriteLine(@"Lookup EXCEPTION: {0}", lookupPath); throw; } #endif diropres.PackSuccess(packer, fh, new fattr(fh)); }
public fattr(fhandle fh) { FileEntry file = FileTable.LookupFileEntry(fh); if (file == null) { Console.WriteLine("fattr on invalid file handle:{0}", fh.Index); throw new NFSStatusException(NFSStatus.NFSERR_STALE); } FileInfo fileInfo = new FileInfo(file.Name); if (fileInfo.Exists == false) if (new DirectoryInfo(file.Name).Exists == false) throw new System.IO.FileNotFoundException(); if ((fileInfo.Attributes & FileAttributes.Directory) != 0) { type = ftype.NFDIR; mode |= (uint)modes.DIR; size = 4096; blocksize = 4096; blocks = 8; atime = new timeval(fileInfo.LastAccessTime); mtime = new timeval(fileInfo.LastWriteTime); } else { if (fileInfo.Extension == ".sl") { type |= ftype.NFLNK; mode = (uint)modes.LNK; } else { type = ftype.NFREG; mode |= (uint)modes.REG; } size = (uint)fileInfo.Length; blocks = (size / 4096) + (4096 - (size % 4096)); atime = new timeval(fileInfo.LastAccessTime); mtime = new timeval(fileInfo.LastWriteTime); } if ((fileInfo.Attributes & FileAttributes.ReadOnly) == 0) mode |= (uint)modes.WOWN; fileid = fh.Index; //Console.WriteLine("fattr name:{0}, fileid:{1}, attrs:{2}, readonly:{3}", file.Name, fileid, fileInfo.Attributes, (fileInfo.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly); }
private void SetAttr(rpcCracker cracker, rpcPacker packer) { fhandle fh = new fhandle(cracker); sattr attributes = new sattr(cracker); FileEntry file = FileTable.LookupFileEntry(fh); if (file == null) { Console.WriteLine("Invalid file handle:{0}", fh.Index); throw new NFSStatusException(NFSStatus.NFSERR_STALE); } // TODO: Actually do something with the attributes. if (attributes.Size == 0) { try { FileStream fs = new FileStream(file.Name, FileMode.Truncate, FileAccess.Write); fs.Close(); } catch (System.IO.FileNotFoundException) { FileTable.Remove(fh); throw; } } if ((int)attributes.Mode != -1) { FileInfo info = new FileInfo(FileTable.LookupFileEntry(fh).Name); if ((attributes.Mode & (uint)fattr.modes.WOWN) == (uint)fattr.modes.WOWN) { info.Attributes = info.Attributes & ~FileAttributes.ReadOnly; } else { info.Attributes = info.Attributes | FileAttributes.ReadOnly; } } attrstat.PackSuccess(packer, new fattr(fh)); }
private void Read(rpcCracker cracker, rpcPacker packer) { fhandle fh = new fhandle(cracker); uint offset = cracker.get_uint32(); uint count = cracker.get_uint32(); uint totalCount = cracker.get_uint32(); FileStream fs; try { fs = new FileStream(FileTable.LookupFileEntry(fh).Name, FileMode.Open, FileAccess.Read); } catch (System.IO.FileNotFoundException) { FileTable.Remove(fh); throw; } try { fs.Position = offset; Byte[] buf = new Byte[count]; int bytesRead = fs.Read(buf, 0, (int)count); fattr attr = new fattr(fh); if (attr.IsFile() == false) { throw new NFSStatusException(NFSStatus.NFSERR_ISDIR); } packer.setUint32((uint)NFSStatus.NFS_OK); attr.Pack(packer); packer.setData(buf, bytesRead); } finally { fs.Close(); } }
private void ReadDir(rpcCracker cracker, rpcPacker packer) { fhandle fh = new fhandle(cracker); uint cookie = cracker.get_uint32(); uint count = cracker.get_uint32(); FileEntry dir = FileTable.LookupFileEntry(fh); //Console.WriteLine("ReadDir:{0}, cookie:{1}, count:{2}, resultsNULL:{3}", dir.Name, cookie, count, results == null); if (cookie == 0 || results == null) { if (dir == null) throw new NFSStatusException(NFSStatus.NFSERR_EXIST); try { results = new readdirres(dir.Name, count); } catch(DirectoryNotFoundException) { FileTable.Remove(fh); throw; } } if (results.Pack(packer, cookie, count) == true) results = null; }
private void Read(rpcCracker cracker, rpcPacker packer) { fhandle fh = new fhandle(cracker); uint offset = cracker.get_uint32(); uint count = cracker.get_uint32(); uint totalCount = cracker.get_uint32(); FileStream fs; try { fs = new FileStream(FileTable.LookupFileEntry(fh).Name, FileMode.Open, FileAccess.Read); } catch (System.IO.FileNotFoundException) { FileTable.Remove(fh); throw; } try { fs.Position = offset; Byte[] buf = new Byte[count]; int bytesRead = fs.Read(buf, 0, (int)count); fattr attr = new fattr(fh); if (attr.IsFile() == false) throw new NFSStatusException(NFSStatus.NFSERR_ISDIR); packer.setUint32((uint)NFSStatus.NFS_OK); attr.Pack(packer); packer.setData(buf, bytesRead); } finally { fs.Close(); } }
public diropargs(rpcCracker cracker) { fh = new fhandle(cracker); fileName = cracker.get_String(); }
public static void PackSuccess(rpcPacker packer, fhandle fh, fattr attr) { packer.setUint32((uint)NFSStatus.NFS_OK); fh.Pack(packer); attr.Pack(packer); }
private void StatFS(rpcCracker cracker, rpcPacker packer) { const uint BLOCK_SIZE = 4096; fhandle fh = new fhandle(cracker); FileEntry file = FileTable.LookupFileEntry(fh); Console.WriteLine("StatFS: {0}", file.Name); System.UInt64 freeBytesAvailable = 0; System.UInt64 totalNumberOfBytes = 0; System.UInt64 totalNumberOfFreeBytes = 0; if (UnmanagedWin32API.GetDiskFreeSpaceEx(file.Name, ref freeBytesAvailable, ref totalNumberOfBytes, ref totalNumberOfFreeBytes) == false) throw new NFSStatusException(NFSStatus.NFSERR_EXIST); freeBytesAvailable /= BLOCK_SIZE; totalNumberOfBytes /= BLOCK_SIZE; totalNumberOfFreeBytes /= BLOCK_SIZE; packer.setUint32((uint)NFSStatus.NFS_OK); packer.setUint32(BLOCK_SIZE); // tsize: optimum transfer size packer.setUint32(BLOCK_SIZE); // Block size of FS packer.setUint32((uint)totalNumberOfBytes); // Total # of blocks (of the above size) packer.setUint32((uint)totalNumberOfFreeBytes); // Free blocks packer.setUint32((uint)freeBytesAvailable); // Free blocks available to non-priv. users }
private void SetAttr(rpcCracker cracker, rpcPacker packer) { fhandle fh = new fhandle(cracker); sattr attributes = new sattr(cracker); FileEntry file = FileTable.LookupFileEntry(fh); if (file == null) { Console.WriteLine("Invalid file handle:{0}", fh.Index); throw new NFSStatusException(NFSStatus.NFSERR_STALE); } // TODO: Actually do something with the attributes. if (attributes.Size == 0) { try { FileStream fs = new FileStream(file.Name, FileMode.Truncate, FileAccess.Write); fs.Close(); } catch (System.IO.FileNotFoundException) { FileTable.Remove(fh); throw; } } if ((int)attributes.Mode != -1) { FileInfo info = new FileInfo(FileTable.LookupFileEntry(fh).Name); if ((attributes.Mode & (uint)fattr.modes.WOWN) == (uint)fattr.modes.WOWN) info.Attributes = info.Attributes & ~FileAttributes.ReadOnly; else info.Attributes = info.Attributes | FileAttributes.ReadOnly; } attrstat.PackSuccess(packer, new fattr(fh)); }
static public void PackSuccess(rpcPacker packer, fhandle fh, fattr attr) { packer.setUint32((uint)NFSStatus.NFS_OK); fh.Pack(packer); attr.Pack(packer); }